1
0

vanitybot.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import re
  2. import logging
  3. import asyncio
  4. from aiogram.contrib.fsm_storage.memory import MemoryStorage
  5. from aiogram import Bot, Dispatcher, executor, types
  6. from aiogram_dialog import DialogManager, DialogRegistry, StartMode
  7. from config import API_TOKEN
  8. from dialog import dialog, MySG
  9. from progress import bg_dialog
  10. from summarize import get_paper_desc
  11. # Initialize bot and dispatcher
  12. bot = Bot(token=API_TOKEN) # type: ignore
  13. dp = Dispatcher(bot, storage=MemoryStorage())
  14. registry = DialogRegistry(dp)
  15. registry.register(dialog)
  16. registry.register(bg_dialog)
  17. help_message = "Hello!\n\n\
  18. Send me a link paper from arxiv.org and \
  19. I'll send you back snippet of paper and arxiv-vanity.com mobile friendly link!\n\
  20. Or add me to chat and I'll be watching the arxiv link and \
  21. reply to them with fancy arxiv-vanity links."
  22. @dp.message_handler(commands=['start'])
  23. async def process_start_command(message: types.Message):
  24. await message.reply(help_message)
  25. @dp.message_handler(commands=['help'])
  26. async def process_help_command(message: types.Message):
  27. await message.reply(help_message)
  28. @dp.message_handler(commands=['long'])
  29. async def long(message: types.Message):
  30. import random
  31. long = "".join(str(random.randint(1,10)) for _ in range(3700))
  32. await message.reply(long)
  33. @dp.message_handler(regexp=r'arxiv.org\/(?:abs|pdf)\/\d{4}\.\d{5}')
  34. async def vanitify(message: types.Message, dialog_manager: DialogManager):
  35. papers_ids = re.findall(r'arxiv.org\/(?:abs|pdf)\/(\d{4}\.\d{5}[v]?[\d]?)', message.text)
  36. async def start_dialog(manager=dialog_manager.bg(), state=MySG.main, mode=StartMode.NEW_STACK, data={}):
  37. await manager.start(state=state, mode=mode, data=data)
  38. async def get_paper_abs(id_):
  39. reply_message = f"[Here you can read the paper in mobile friendly way](https://www.arxiv-vanity.com/papers/{id_})"
  40. data = {
  41. "id": id_,
  42. "reply_message": reply_message,
  43. "url": None,
  44. "title": None,
  45. "abs": None
  46. }
  47. if paper := await get_paper_desc(id_):
  48. id_, url, title, abstract, authors = paper.values()
  49. reply_message = f'{url}\n\n***{title}***\n\n{abstract}\n\n{reply_message}'
  50. data.update({
  51. "id": id_,
  52. "reply_message": reply_message,
  53. "url": url,
  54. "title": title,
  55. "abs": abstract,
  56. "authors": authors
  57. })
  58. else:
  59. reply_message = f'Something went wrong. Can not reach arxiv.com :('
  60. data["reply_message"] = reply_message
  61. return data
  62. list_data = await asyncio.gather(*[get_paper_abs(id_) for id_ in papers_ids])
  63. asyncio.gather(*[start_dialog(data=data) for data in list_data])
  64. if __name__ == "__main__":
  65. logging.basicConfig(level=logging.INFO)
  66. logging.getLogger("asyncio").setLevel(logging.DEBUG)
  67. logging.getLogger("aiogram_dialog").setLevel(logging.DEBUG)
  68. executor.start_polling(dp, skip_updates=True)