Skip to content

Instantly share code, notes, and snippets.

@Mladia
Last active June 6, 2019 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mladia/251768b4b09f13a9d07cef1f4ba8dc2c to your computer and use it in GitHub Desktop.
Save Mladia/251768b4b09f13a9d07cef1f4ba8dc2c to your computer and use it in GitHub Desktop.
Python script, that prints documents in atis
#!/bin/env python3
from bs4 import BeautifulSoup
import urllib.request
import subprocess
import re
import sys
import random
#try:
# file = str(sys.argv[1])
#except IndexError:
# print("Please provide a file as an argument")
# exit(-1);
atis_website="https://atis.informatik.kit.edu/1194.php"
site_info = urllib.request.urlopen(atis_website).read()
soup = BeautifulSoup(site_info, 'html.parser')
#Getting only black and white printers
printer_full = subprocess.run(['lpstat', '-p', '-d'], stdout=subprocess.PIPE).stdout.decode('utf-8')
printers = []
table=soup.find_all('td')
for line in printer_full.split("\n"):
try:
name = line.split(" ")[1]
except IndexError:
break
if "deaktiviert" in line :
print(name, "is deactivated")
continue
#s/w printers
if "sw" in name :
for i in range(0, len(table)):
printerOnline = table[i].getText()
if printerOnline.find(name) != -1:
#if this one is idle, then 0 jobs
if table[i+1].getText() == "idle\n":
jobs = 0
else:
#getting number of jobs
auftrag= table[i+1].find("a").next_sibling
try:
jobs=re.findall('\d+', auftrag)[0]
jobs.strip
except IndexError:
jobs = 1
# print(name +", jobs:" + str(jobs))
printers.append( tuple( (name, jobs) ) )
break
#Printers and jobs
for (name, jobs) in printers:
print(name + ", jobs: "+ str(jobs))
#finding printer with min number of jobs
min = 5000
the_printer=""
for (name, jobs) in printers:
if int(min) > int(jobs):
the_printer=name
min = jobs
#overwriting printer for handling user input printer
for x in sys.argv:
if "-sw1" in x:
the_printer="pool-sw1"
elif "-sw2" in x:
the_printer="pool-sw2"
elif "-sw3" in x:
the_printer="pool-sw3"
#Looking for color printer?
for x in sys.argv:
if x == "-color" or x == "-c" or x == "-farb" or x =="-f" :
the_printer="pool-farb1"
#Handling input
#Number of copies
copies = "-#1"
for x in sys.argv:
if re.match("-#\d", x):
copies=x
mode = "sides=two-sided-long-edge"
for x in sys.argv:
if x == "--landscape" or x == "-l":
mode = "sides=two-sided-short-edge"
file = list()
for x in sys.argv:
if not re.match("-.", x):
file.append(x)
#pop the name of the file
file.pop(0)
dry=False
#checking for dry input
for x in sys.argv:
if x == "--dry" or x == "-d":
dry=True
for filex in file:
print(filex + " to be send to " + the_printer)
#creating the print command
command=["lpr", "-m", "-o", mode , copies , "-P", the_printer, filex ]
command_human="lpr -m -o " + mode + " " + copies + " -P " + the_printer + " " + filex
if dry:
print(command_human)
continue
yes = {'yes','y', 'ye', ''}
no = {'no','n'}
print("Are you sure?")
directly_yes=False
for x in sys.argv:
if x == "--yes" or x == "-y":
directly_yes=True
if not directly_yes:
choice = input().lower()
else:
choice = "yes"
if choice in yes or directly_yes:
print("Sending print job to " + the_printer + "...")
#lpr -o sides=two-sided-long-edge -#1 -P $file
subprocess.run( command )
elif choice in no:
print(command_human)
else:
sys.stdout.write("Please respond with 'yes' or 'no'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment