Skip to content

Instantly share code, notes, and snippets.

@nb
Created June 26, 2010 10:01
Show Gist options
  • Save nb/453944 to your computer and use it in GitHub Desktop.
Save nb/453944 to your computer and use it in GitHub Desktop.
# coding: utf-8
import email, imaplib, chardet, pickle
m = imaplib.IMAP4_SSL('imap.gmail.com', 993)
m.login('nb@nikolay.bg', 'тринайсетзелениголиорангутанагонятгоркатапеперудка')
m.select('&BD8ENQRCBDA- &BDcEMAQ0BDAERwQw-') # пета задача
typ, data = m.search(None, 'ALL')
essays = []
for num in data[0].split():
print 'Getting %s' % num
typ, data = m.fetch(num, '(RFC822)')
raw_text = data[0][1]
multipart = email.message_from_string(raw_text)
message = multipart if not multipart.is_multipart() else multipart.get_payload()[0]
text = message.get_payload(decode=True)
if chardet.detect(text)['encoding'] == 'windows-1251':
text = unicode(text, 'windows-1251').encode('utf-8')
essays.append({'from': multipart.get('From'), 'text': text})
print text
pickle.dump(essays, open('essays', 'wb'))
m.close()
m.logout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment