splitting to separate functions

This commit is contained in:
Aleksey 2022-10-29 15:48:03 +03:00
parent 8d6005eec1
commit b248582193

57
bot.py
View File

@ -40,13 +40,13 @@ def get_sticker_setid(document):
if hasattr(a, "stickerset"):
stickerset = a.stickerset
if stickerset is None:
dllog.info("document %s is not a sticker", document.id)
dllog.debug("document %s is not a sticker", document.id)
return None, None
if isinstance(stickerset, types.InputStickerSetID):
log.info("document %s is a normal sticker", document.id)
log.debug("document %s is a normal sticker", document.id)
return str(stickerset.id), stickerset
if isinstance(stickerset, types.InputStickerSetEmpty):
dllog.info("document %s is an inline sticker", document.id)
dllog.debug("document %s is an inline sticker", document.id)
return "inline", stickerset
def fetch_dialogs(client):
@ -57,9 +57,7 @@ def fetch_dialogs(client):
log.info(dialog.stringify())
yield dialog
def main(client):
stickerset_seen=set()
for sticker_archive in fetch_dialogs(client):
def process_archive(archive, stickerset_seen=set()):
for msg in client.iter_messages(sticker_archive, limit=None):
log.debug(msg)
if not hasattr(msg, "media"):
@ -75,7 +73,7 @@ def main(client):
elif dldir is not None:
download_sticker(client, msg.media.document)
if setid.id not in stickerset_seen:
log.debug("preparing to download whole stickerset %s as %s", setid, dldir)
log.info("preparing to download whole stickerset %s as %s", setid, dldir)
stickerset_seen.add(setid.id)
try:
for doc in client(functions.messages.GetStickerSetRequest(stickerset=setid,hash=0)).documents:
@ -88,45 +86,24 @@ def main(client):
except Exception as e:
log.error("somethin wrong happened during checking message: %s", msg.stringify(), exc_info=e)
# for doc in stickers_inline:
# try:
# download_sticker(client, doc)
# except Exception as e:
# log.critical("ouchie: %s", exc_info=e)
# for stickerset in stickers_sets.values():
# try:
# for doc in client(functions.messages.GetStickerSetRequest(stickerset=stickerset,hash=0)).documents:
# try:
# download_sticker(client, doc)
# except Exception as e:
# log.critical("oops: %s", exc_info=e)
# except errors.rpcerrorlist.StickersetInvalidError:
# log.warning("sadly, stickerset %s no longer exists", stickerset.id)
# except Exception as e:
# log.critical("omg: %s", exc_info=e)
####################
log.debug("opening %s", repr("config/bot.yaml"))
with open("config/bot.yaml") as cfgstream:
if __name__ == "__main__":
log.debug("opening %s", repr("config/bot.yaml"))
with open("config/bot.yaml") as cfgstream:
cfg = yaml.safe_load(cfgstream)
log.debug(cfg)
app = cfg["apps"][0]
log.debug("starting client with id %s and hash %s", app['id'], app['hash'])
client = TelegramClient('env/gentoo_session', app['id'], app['hash'])
client.start()
log.debug(client.get_me().stringify())
stickers_sets=dict()
stickers_favorites=set()
stickers_inline=list()
sticker_archives=list()
try:
main(client)
except KeyboardInterrupt:
log.debug("starting client with id %s and hash %s", app['id'], app['hash'])
client = TelegramClient('env/gentoo_session', app['id'], app['hash'])
client.start()
log.debug(client.get_me().stringify())
try:
for sticker_archive in fetch_dialogs(client):
process_archive(sticker_archive)
except KeyboardInterrupt:
pass
client.disconnect()
client.disconnect()