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"): if hasattr(a, "stickerset"):
stickerset = a.stickerset stickerset = a.stickerset
if stickerset is None: 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 return None, None
if isinstance(stickerset, types.InputStickerSetID): 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 return str(stickerset.id), stickerset
if isinstance(stickerset, types.InputStickerSetEmpty): 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 return "inline", stickerset
def fetch_dialogs(client): def fetch_dialogs(client):
@ -57,9 +57,7 @@ def fetch_dialogs(client):
log.info(dialog.stringify()) log.info(dialog.stringify())
yield dialog yield dialog
def main(client): def process_archive(archive, stickerset_seen=set()):
stickerset_seen=set()
for sticker_archive in fetch_dialogs(client):
for msg in client.iter_messages(sticker_archive, limit=None): for msg in client.iter_messages(sticker_archive, limit=None):
log.debug(msg) log.debug(msg)
if not hasattr(msg, "media"): if not hasattr(msg, "media"):
@ -75,7 +73,7 @@ def main(client):
elif dldir is not None: elif dldir is not None:
download_sticker(client, msg.media.document) download_sticker(client, msg.media.document)
if setid.id not in stickerset_seen: 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) stickerset_seen.add(setid.id)
try: try:
for doc in client(functions.messages.GetStickerSetRequest(stickerset=setid,hash=0)).documents: for doc in client(functions.messages.GetStickerSetRequest(stickerset=setid,hash=0)).documents:
@ -88,45 +86,24 @@ def main(client):
except Exception as e: except Exception as e:
log.error("somethin wrong happened during checking message: %s", msg.stringify(), exc_info=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")) if __name__ == "__main__":
with open("config/bot.yaml") as cfgstream: log.debug("opening %s", repr("config/bot.yaml"))
with open("config/bot.yaml") as cfgstream:
cfg = yaml.safe_load(cfgstream) cfg = yaml.safe_load(cfgstream)
log.debug(cfg) log.debug(cfg)
app = cfg["apps"][0] app = cfg["apps"][0]
log.debug("starting client with id %s and hash %s", app['id'], app['hash']) log.debug("starting client with id %s and hash %s", app['id'], app['hash'])
client = TelegramClient('env/gentoo_session', app['id'], app['hash']) client = TelegramClient('env/gentoo_session', app['id'], app['hash'])
client.start() client.start()
log.debug(client.get_me().stringify()) log.debug(client.get_me().stringify())
stickers_sets=dict() try:
stickers_favorites=set() for sticker_archive in fetch_dialogs(client):
stickers_inline=list() process_archive(sticker_archive)
sticker_archives=list() except KeyboardInterrupt:
try:
main(client)
except KeyboardInterrupt:
pass pass
client.disconnect() client.disconnect()