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"):
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,76 +57,53 @@ def fetch_dialogs(client):
log.info(dialog.stringify())
yield dialog
def main(client):
stickerset_seen=set()
for sticker_archive in fetch_dialogs(client):
for msg in client.iter_messages(sticker_archive, limit=None):
log.debug(msg)
if not hasattr(msg, "media"):
log.debug("message %i has no media", msg.id)
continue
if not hasattr(msg.media, "document"):
log.debug("message %i has no documents", msg.id)
continue
try:
dldir, setid = get_sticker_setid(msg.media.document)
if dldir == "inline":
download_sticker(client, msg.media.document)
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)
stickerset_seen.add(setid.id)
try:
for doc in client(functions.messages.GetStickerSetRequest(stickerset=setid,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", setid.id)
except Exception as e:
log.error("somethin wrong happened during checking message: %s", msg.stringify(), exc_info=e)
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"):
log.debug("message %i has no media", msg.id)
continue
if not hasattr(msg.media, "document"):
log.debug("message %i has no documents", msg.id)
continue
try:
dldir, setid = get_sticker_setid(msg.media.document)
if dldir == "inline":
download_sticker(client, msg.media.document)
elif dldir is not None:
download_sticker(client, msg.media.document)
if setid.id not in stickerset_seen:
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:
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", setid.id)
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:
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:
pass
client.disconnect()
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())
try:
for sticker_archive in fetch_dialogs(client):
process_archive(sticker_archive)
except KeyboardInterrupt:
pass
client.disconnect()