1
0
Fork 0
printer_bot/bot.py

65 lines
2.4 KiB
Python
Raw Normal View History

2023-08-01 21:10:14 +00:00
from telethon import TelegramClient
import logging, asyncio
from telethon.tl.types import MessageMediaDocument, DocumentAttributeSticker, DocumentAttributeAnimated, MessageMediaPhoto
from telethon import events
from telethon.tl.custom import Button
from os.path import isfile, join
from grp import getgrgid
from os import getgroups
2023-08-01 21:10:14 +00:00
from os import system
from os import path
2023-08-01 21:10:14 +00:00
from time import time
from PIL import Image
from config import *
from os import makedirs
import argparse
2023-08-01 21:10:14 +00:00
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='./bot.py',
description='Recieve images through IRC or Telegram and print the content with brother_ql'
)
parser.add_argument('-t', '--telegram', help='Enable Telegram (requires configuration)',
action='store_true') # on/off flag
parser.add_argument('-i', '--irc', help='Enable IRC (generate random channel and username by default, set values in the configuration if you want a static name)',
action='store_true') # on/off flag
parser.add_argument('-p', '--printer', help='Printer devicefile, default is /dev/usb/lp0',
action='store_true') # on/off flag
2023-08-01 21:10:14 +00:00
args = parser.parse_args()
2023-08-01 21:10:14 +00:00
# os.getgroups() seems to return the *effective* groups of the current user, not
# what is in /etc/group https://docs.python.org/3/library/os.html#os.getgrouplist
# This is good thing, otherwise the error would disappear even if the user has not
# logged in again.
if "lp" not in [getgrgid(g).gr_name for g in getgroups()]:
print("""Error: User is not in the lp group. Run the following and sign in again:
sudo usermod -a -G lp $USER""")
2023-08-01 21:10:14 +00:00
# Assumes this hardcoded path, prevent
# continuing if there is not even a printer
if not path.exists("/dev/usb/lp0"):
exit("There seems to be no printer connected.")
2023-08-01 21:10:14 +00:00
# By default the printer turns off after a certain time to save power.
# For the use case of a public usable printer at events this is
# annoying. Currently, there is no function in the brother_ql project to
# disable power saving: https://github.com/pklaus/brother_ql/issues/50
# The workaround shell command is rewritten to Python code. Please
# remove this code if brother_ql implements a working fix themselves
with open("/dev/usb/lp0", "wb") as p:
p.write(b'\x1b\x69\x55\x41\x00\x00')
2023-08-01 21:10:14 +00:00
if args.irc:
import bot_irc
2023-08-01 21:10:14 +00:00
if args.telegram:
import bot_telegram
2023-08-01 21:10:14 +00:00
makedirs(CACHE_DIR, exist_ok=True)
client.run_until_disconnected()