mail_alert2.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. '''Created Sep 28, 2021 Levi'''
  2. import configparser
  3. import sys
  4. import traceback
  5. import ssl
  6. from time import sleep
  7. from collections import deque
  8. import telegram
  9. import keyring
  10. from bs4 import BeautifulSoup
  11. from telegram.error import BadRequest
  12. from imap_tools import MailBox, AND
  13. #-> for python 3.10, the dh key too small problem
  14. context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
  15. context.set_ciphers('DEFAULT@SECLEVEL=1')
  16. config = configparser.ConfigParser()
  17. config.read('bot')
  18. bot = telegram.Bot(token=config['AUTH']['bank'])
  19. mailbox = MailBox(host=config['email']['host'], ssl_context=context)
  20. def pretty_exc():
  21. exc_type, _exc_obj, exc_tb = sys.exc_info()
  22. # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
  23. for tb in list(traceback.format_exception(exc_type, _exc_obj, exc_tb)):
  24. print(tb)
  25. def mask(msg, to_):
  26. return any([to_ == msg.from_,
  27. to_ in msg.to,
  28. to_ in msg.cc,
  29. to_ in msg.bcc,
  30. to_ in msg.subject])
  31. def mail_alert():
  32. container = deque(maxlen=500)
  33. mailbox.login(config['email']['user'], keyring.get_password('yagmail', 'levente.marton@mzk.ro'), initial_folder='bank_events')
  34. while True:
  35. try:
  36. sleep(15)
  37. msgs = [msgs for msgs in mailbox.fetch(AND(seen=False), mark_seen=True)]
  38. for msg in msgs:
  39. chk = mask(msg, 'account')
  40. # print(chk)
  41. if chk:
  42. if msg.uid not in container:
  43. # print(f'{msg.uid} not in container')
  44. soup = BeautifulSoup(msg.html, 'html.parser')
  45. msg_text = soup.prettify().replace('<br/>', '').replace('<html>', '').replace('<head>', '').replace('</head>', '').replace('<body>', '').replace('<div>', '').replace('</div>', '').replace('</body>', '').replace('</html>', '')
  46. msg_text = msg_text[:msg_text.find('Intraday transactions')]
  47. try:
  48. if len(msg.text) or len(msg.html) != 0:
  49. bot.send_message(chat_id=config['AUTH']['chatid'], text=msg_text)
  50. else:
  51. bot.send_message(chat_id=config['AUTH']['chatid'], text=msg_text)
  52. container.appendleft(msg.uid)
  53. print('message appended', len(container))
  54. except BadRequest:
  55. try:
  56. if len(msg.text) or len(msg.html) != 0:
  57. bot.send_message(chat_id=config['AUTH']['chatid'], text=msg_text // 5)
  58. else:
  59. bot.send_message(chat_id=config['AUTH']['chatid'], text=msg_text // 5)
  60. container.appendleft(msg.uid)
  61. print('message appended', len(container))
  62. except BadRequest:
  63. bot.send_message(chat_id=config['AUTH']['chatid'], text=f'Message is too long')
  64. container.appendleft(msg.uid)
  65. print('message appended', len(container), '!! last message was to long !!')
  66. except Exception:
  67. mailbox.logout()
  68. pretty_exc()
  69. break
  70. except Exception:
  71. # pretty_exc()
  72. mailbox.logout()
  73. break
  74. if __name__ == '__main__':
  75. mail_alert()
  76. # mailbox.login(config['email']['user'], keyring.get_password('yagmail', 'levente.marton@mzk.ro'), initial_folder='bank_events')
  77. # msgs = [msgs for msgs in mailbox.fetch(AND(seen=False), mark_seen=True, reverse=True)]
  78. # for msg in msgs:
  79. # chk = mask(msg, 'account')
  80. # if chk:
  81. # print(msg.uid, msg.subject)
  82. # msg = msgs[0]
  83. # soup = BeautifulSoup(msg.html, 'html.parser')
  84. # print(soup.prettify().replace('<br/>', ''))