This weekend I tried to refresh my python with one single idea in mind.
But now I don’t have time to explain it, so here comes the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
#!/usr/bin/env python # #joanmarcrieraATgmail(dot)com from GmailClass import GmailClass import goslate import datetime, xmlrpclib import time from wordpress_xmlrpc import Client, WordPressPost from wordpress_xmlrpc.methods.posts import NewPost from datetime import datetime gs=goslate.Goslate() correo=GmailClass("jmraccount@gmail.com","Password","Foldera") correo.connect() correo.select_mailbox() no_leidos=correo.get_unreaded_list() wp_url = 'http://www.joanmarcriera.es/blog/xmlrpc.php' wp_username = 'username' wp_password = 'password' for un_correo_no_leido in no_leidos[0].split(): print "\nProcessing one unread..." msg=correo.get_a_mail(un_correo_no_leido) titulo_original=correo.get_the_subject(msg) print "\t " + titulo_original body_original=correo.get_the_body(msg) titulo_es=gs.translate(titulo_original,'es') body_es=gs.translate(body_original,'es') title=titulo_es content=body_es post = WordPressPost() post.title = title post.content = content post.date = datetime(2014, 8, 10, 12, 34, 2, 860000) post.post_status = 'draft' try: wp = Client(wp_url,wp_username,wp_password) wp.call(NewPost(post)) except ServerConnectionError as e: #do something with e #or by now pass print "serverconnection error" pass except ProtocolError as e: #do something or pass print "protocol error" pass else: del post del wp print "\t Done." print "Now wait 10 seconds\n" time.sleep(10) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
#!/usr/bin/env python # #joanmarcrieraATgmail(dot)com import sys import imaplib #not needed #import getpass import email import email.header import datetime #class to manage gmail. class GmailClass(object): #Basic initialize. #Here is were we need to declare which account and folder we would like to process #example a=GmailClass('jmr@gmail.com','superpass','kate') def __init__(self, account, pwsd, folder): self.folder = folder self.email_account = account self.email_password = pwsd #automatic for test def prepare(self): self.connect() self.select_mailbox() #Connect to the mailbox. def connect(self): self.mailbox = imaplib.IMAP4_SSL('imap.gmail.com') try: rv, data = self.mailbox.login(self.email_account, self.email_password) except imaplib.IMAP4.error: print "LOGIN FAILED!!! " sys.exit(1) print rv, data print "You can now select a mailbox" #just if you want to know how many folders do you have def list_mailboxes(self): #needs pretty print rv, mailboxes = self.mailbox.list() if rv == 'OK': print "Mailboxes:" print mailboxes print "Select one of them to be able to process it." #go into the folder def select_mailbox(self): rv, data = self.mailbox.select(self.folder) if rv == 'OK': print "You can now start processing mailbox...\n" else: print "ERROR: Unable to open mailbox ", rv #which mails are still unread def get_unreaded_list(self): rv, data = self.mailbox.search(None, "UnSeen") if rv != 'OK': print "No messages found!" return else: print "Messages found " return data #return a mail object #the list from get_unreaded_list needs to be looped data[i] def get_a_mail(self,num): rv, data = self.mailbox.fetch(num, '(RFC822)') if rv != 'OK': print "ERROR getting message", num return else: msg = email.message_from_string(data[0][1]) return msg #from a mail object get the subject def get_the_subject(self,msg): decode = email.header.decode_header(msg['Subject'])[0] subject = unicode(decode[0]) #print 'Message Subject: %s' % ( subject) return subject #from a mail object the date def get_the_date(self,msg): #print 'Raw Date:', msg['Date'] return msg['Date'] #from a mail object get the body def get_the_body(self,msg): maintype = msg.get_content_maintype() if maintype == 'multipart': for part in msg.get_payload(): if part.get_content_maintype() == 'text': return part.get_payload() elif maintype == 'text': return msg.get_payload() #method to process mailbox def process_mailbox(self): """ Do something with emails messages in the folder. For the sake of this example, print some headers. """ rv, data = self.mailbox.search(None, "ALL") if rv != 'OK': print "No messages found!" return for num in data[0].split(): rv, data = self.mailbox.fetch(num, '(RFC822)') if rv != 'OK': print "ERROR getting message", num return msg = email.message_from_string(data[0][1]) decode = email.header.decode_header(msg['Subject'])[0] subject = unicode(decode[0]) print 'Message %s: %s' % (num, subject) print 'Raw Date:', msg['Date'] # Now convert to local date-time date_tuple = email.utils.parsedate_tz(msg['Date']) if date_tuple: local_date = datetime.datetime.fromtimestamp( email.utils.mktime_tz(date_tuple)) print "Local Date:", \ local_date.strftime("%a, %d %b %Y %H:%M:%S") #Close the sessions at the end def close(self): self.mailbox.close() self.mailbox.logout() |