vanitybot.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. async def deploy_message():
  23. await bot.send_message(chat_id=1147194, text='The deployment has been performed')
  24. @dp.message_handler(commands=['start'])
  25. async def process_start_command(message: types.Message):
  26. await message.reply(help_message)
  27. @dp.message_handler(commands=['help'])
  28. async def process_help_command(message: types.Message):
  29. await message.reply(help_message)
  30. @dp.message_handler(commands=['long'])
  31. async def long(message: types.Message):
  32. import random
  33. long = "".join(str(random.randint(1,10)) for _ in range(3700))
  34. await message.reply(long)
  35. @dp.message_handler(regexp=r'arxiv.org\/(?:abs|pdf)\/\d{4}\.\d{5}')
  36. async def vanitify(message: types.Message, dialog_manager: DialogManager):
  37. papers_ids = re.findall(r'arxiv.org\/(?:abs|pdf)\/(\d{4}\.\d{5}[v]?[\d]?)', message.text)
  38. async def start_dialog(manager=dialog_manager.bg(), state=MySG.main, mode=StartMode.NEW_STACK, data={}):
  39. await manager.start(state=state, mode=mode, data=data)
  40. async def get_paper_abs(id_):
  41. reply_message = f"[Here you maybe can read the paper in mobile friendly way](https://www.arxiv-vanity.com/papers/{id_})"
  42. data = {
  43. "id": id_,
  44. "reply_message": reply_message,
  45. "url": None,
  46. "title": None,
  47. "abs": None
  48. }
  49. if paper := await get_paper_desc(id_):
  50. id_, url, title, abstract, authors = paper.values()
  51. reply_message = f'{url}\n\n***{title}***\n\n{abstract}\n\n{reply_message}'
  52. data.update({
  53. "id": id_,
  54. "reply_message": reply_message,
  55. "url": url,
  56. "title": title,
  57. "abs": abstract,
  58. "authors": authors
  59. })
  60. else:
  61. reply_message = f'Something went wrong. Can not reach arxiv.com :('
  62. data["reply_message"] = reply_message
  63. return data
  64. list_data = await asyncio.gather(*[get_paper_abs(id_) for id_ in papers_ids])
  65. asyncio.gather(*[start_dialog(data=data) for data in list_data])
  66. if __name__ == "__main__":
  67. logging.basicConfig(level=logging.INFO)
  68. # logging.getLogger("asyncio").setLevel(logging.DEBUG)
  69. # logging.getLogger("aiogram_dialog").setLevel(logging.DEBUG)
  70. asyncio.get_event_loop().run_until_complete(deploy_message())
  71. executor.start_polling(dp, skip_updates=True)