pjmakey2

ImapLib Get Files

Mar 30th, 2021
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. def proccess_bitsc(current,year, month):
  2.     filter_time = False
  3.     dirf = '/home/peter/currens/{}/bin/gps'.format(current)
  4.     year = year
  5.     month = str(month).zfill(2)
  6.     ahora = arrow.get('{}-{}-01'.format(year, month))
  7.     server = 'imap.th31l1_1s10nofthefallen.anywhere'
  8.     user = 'whocares'
  9.     password = 'whocancareless'
  10.     m = imaplib.IMAP4_SSL(server, 993)
  11.     m.login(user, password)
  12.     m.select('inbox')
  13.     imaplib._MAXLINE = 4000000
  14.     ahoraf = ahora.strftime('%d-%b-%Y')
  15.     sv = '(SINCE "{}" Subject "Currency rates")'.format(ahoraf)
  16.     resp, items = m.search(None, sv)
  17.     items = items[0].split()
  18.     for emailid in tqdm(items):
  19.         resp, data = m.fetch(emailid, "(RFC822)")
  20.         email_body = data[0][1]
  21.         mail = email.message_from_string(email_body)
  22.         # if mail.get_content_maintype() != 'multipart': continue
  23.         for part in mail.walk():
  24.             if part.get_filename():
  25.                 ffname = dirf + '/' + part.get_filename()
  26.                 ffname = re.sub('\r\n', '', ffname).replace(' ', '_')
  27.                 open(ffname, 'wb').write(part.get_payload(decode=True))
Advertisement
Add Comment
Please, Sign In to add comment