splitting to separate functions

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

117
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,76 +57,53 @@ 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 msg in client.iter_messages(sticker_archive, limit=None):
for sticker_archive in fetch_dialogs(client): log.debug(msg)
for msg in client.iter_messages(sticker_archive, limit=None): if not hasattr(msg, "media"):
log.debug(msg) log.debug("message %i has no media", msg.id)
if not hasattr(msg, "media"): continue
log.debug("message %i has no media", msg.id) if not hasattr(msg.media, "document"):
continue log.debug("message %i has no documents", msg.id)
if not hasattr(msg.media, "document"): continue
log.debug("message %i has no documents", msg.id) try:
continue dldir, setid = get_sticker_setid(msg.media.document)
try: if dldir == "inline":
dldir, setid = get_sticker_setid(msg.media.document) download_sticker(client, msg.media.document)
if dldir == "inline": elif dldir is not None:
download_sticker(client, msg.media.document) download_sticker(client, msg.media.document)
elif dldir is not None: if setid.id not in stickerset_seen:
download_sticker(client, msg.media.document) log.info("preparing to download whole stickerset %s as %s", setid, dldir)
if setid.id not in stickerset_seen: stickerset_seen.add(setid.id)
log.debug("preparing to download whole stickerset %s as %s", setid, dldir) try:
stickerset_seen.add(setid.id) for doc in client(functions.messages.GetStickerSetRequest(stickerset=setid,hash=0)).documents:
try: try:
for doc in client(functions.messages.GetStickerSetRequest(stickerset=setid,hash=0)).documents: download_sticker(client, doc)
try: except Exception as e:
download_sticker(client, doc) log.critical("oops: %s", exc_info=e)
except Exception as e: except errors.rpcerrorlist.StickersetInvalidError:
log.critical("oops: %s", exc_info=e) log.warning("sadly, stickerset %s no longer exists", setid.id)
except errors.rpcerrorlist.StickersetInvalidError: except Exception as e:
log.warning("sadly, stickerset %s no longer exists", setid.id) log.error("somethin wrong happened during checking message: %s", msg.stringify(), exc_info=e)
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")) if __name__ == "__main__":
with open("config/bot.yaml") as cfgstream: log.debug("opening %s", repr("config/bot.yaml"))
cfg = yaml.safe_load(cfgstream) with open("config/bot.yaml") as cfgstream:
log.debug(cfg) cfg = yaml.safe_load(cfgstream)
app = cfg["apps"][0] log.debug(cfg)
log.debug("starting client with id %s and hash %s", app['id'], app['hash']) app = cfg["apps"][0]
client = TelegramClient('env/gentoo_session', app['id'], app['hash']) log.debug("starting client with id %s and hash %s", app['id'], app['hash'])
client.start() client = TelegramClient('env/gentoo_session', app['id'], app['hash'])
log.debug(client.get_me().stringify()) client.start()
stickers_sets=dict() log.debug(client.get_me().stringify())
stickers_favorites=set() try:
stickers_inline=list() for sticker_archive in fetch_dialogs(client):
sticker_archives=list() process_archive(sticker_archive)
try: except KeyboardInterrupt:
main(client) pass
except KeyboardInterrupt: client.disconnect()
pass
client.disconnect()