156 lines
3.1 KiB
Python
156 lines
3.1 KiB
Python
import subprocess
|
|
|
|
import os, time
|
|
|
|
from os.path import expanduser
|
|
home = expanduser("~")
|
|
|
|
mainCategory = ""
|
|
|
|
subCategory = ""
|
|
|
|
numberOfPages = -1
|
|
|
|
print("")
|
|
|
|
#Try to get the URI of the printer at the file in ~/.local/share/scan-auto/printer_uri
|
|
|
|
try:
|
|
|
|
f = open(home+"/.local/share/scan-auto/printer_uri", "r")
|
|
|
|
print("Printer URI is:")
|
|
|
|
printerURI = f.readline().replace("\n", "")
|
|
|
|
print(printerURI)
|
|
except:
|
|
print("Please create a file at " + home + "/.local/share/scan-auto/printer_uri")
|
|
|
|
print("And populate it with the hp-probe URI of your printer, e.g: escl:https://192.168.X.XX:443")
|
|
|
|
exit()
|
|
|
|
while mainCategory == "":
|
|
try:
|
|
mainCategory = str(input("Main category of document: "))
|
|
|
|
except:
|
|
|
|
mainCategory = ""
|
|
|
|
if mainCategory == "":
|
|
# Loop until a valid input is received.
|
|
os.system("clear")
|
|
print("Invalid input, please try again:")
|
|
|
|
#Keep the terminal clean :)
|
|
|
|
print("")
|
|
|
|
while subCategory == "":
|
|
try:
|
|
subCategory = str(input("Subcategory of document: "))
|
|
|
|
except:
|
|
|
|
subCategory = ""
|
|
|
|
if subCategory == "":
|
|
|
|
os.system("clear")
|
|
print("Invalid input, please try again:")
|
|
|
|
print("")
|
|
|
|
while numberOfPages <= 0:
|
|
try:
|
|
numberOfPages = int(input("Number of pages for document: "))
|
|
|
|
except:
|
|
|
|
numberOfPages = -1
|
|
|
|
if numberOfPages <= 0:
|
|
|
|
os.system("clear")
|
|
print("Invalid input, please try again:")
|
|
|
|
|
|
# Start putitng together the final command.
|
|
|
|
finalCommand = ""
|
|
|
|
documentName = mainCategory + "-" + subCategory
|
|
|
|
def putCommandTogether(pageNumber=int()):
|
|
|
|
#Final command looks like this: hp-scan -d escl:https://192.168.0.XX:443 -o <mainCategory>-<subCategory>-pg<pageNumber>.jpg -mcolor; magick convert <mainCategory>-<subCategory>-<pageNumber>.jpg img.pdf
|
|
|
|
|
|
#More Convienient
|
|
|
|
documentName = "scan"
|
|
|
|
|
|
|
|
|
|
finalCommand = "hp-scan -d " + printerURI + " "
|
|
|
|
|
|
|
|
finalCommand = finalCommand + "-o \"" + documentName + ".jpg\" -mcolor; magick convert \"" + documentName + ".jpg\" \"" + documentName + ".pdf\""
|
|
|
|
documentName = mainCategory + "-" + subCategory
|
|
|
|
finalCommand = finalCommand + ";pdftk \"" + documentName + ".pdf\"" + " scan.pdf cat output \"" + documentName + "-work.pdf\""
|
|
return finalCommand
|
|
|
|
#Make folders needed
|
|
|
|
os.system("mkdir \"" + mainCategory +"\"")
|
|
|
|
os.chdir(mainCategory)
|
|
|
|
os.system("mkdir \"" + subCategory + "\"")
|
|
|
|
os.chdir(subCategory)
|
|
|
|
curPage = 0
|
|
|
|
#Cleanup
|
|
|
|
os.system("rm scan.pdf")
|
|
|
|
while numberOfPages > curPage:
|
|
curPage += 1
|
|
|
|
finalCommand = putCommandTogether(curPage)
|
|
|
|
os.system(finalCommand)
|
|
|
|
|
|
|
|
if curPage != numberOfPages:
|
|
|
|
print("Waiting 7 seconds for next page...")
|
|
|
|
time.sleep(7)
|
|
|
|
#Move PDF if it's the first page
|
|
|
|
if curPage == 1:
|
|
os.system("mv scan.pdf \"" + documentName + ".pdf" + "\"")
|
|
|
|
#Cleanup
|
|
|
|
os.system("rm *.jpg")
|
|
|
|
|
|
|
|
os.system("mv \"" + documentName + "-work.pdf\" " + "\"" + documentName + ".pdf" + "\"")
|
|
|
|
for i in range((numberOfPages -1)):
|
|
# final command looks like this : pdftk 1.pdf 2.pdf cat output out.pdf
|
|
exit()
|