Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python <code>email</code> library will help.</p> <pre><code>import email, getpass, imaplib, os, re import matplotlib.pyplot as plt </code></pre> <p>This directory is where you will save attachments</p> <pre><code> detach_dir = "F:\OTHERS\CS\PYTHONPROJECTS" </code></pre> <p>Your script then asks user (or yourself) for account features</p> <pre><code>user = raw_input("Enter your GMail username --&gt; ") pwd = getpass.getpass("Enter your password --&gt; ") </code></pre> <p>Connect then to the gmail imap server and login</p> <pre><code>m = imaplib.IMAP4_SSL("imap.gmail.com") m.login(user, pwd) </code></pre> <p>Select one folder, you could use the whole INBOX instead</p> <pre><code>m.select("BUSINESS/PETROLEUM") </code></pre> <p>One should use <code>m.list()</code> to get all the mailboxes. Search for all emails coming from specified sender and select the mail ids:</p> <pre><code>resp, items = m.search(None, '(FROM "EIA_eLists@eia.gov")') items = items[0].split() my_msg = [] # store relevant msgs here in please msg_cnt = 0 break_ = False </code></pre> <p>I want the last emails, so that I am using <code>items[::-1]</code></p> <pre><code>for emailid in items[::-1]: resp, data = m.fetch(emailid, "(RFC822)") if ( break_ ): break for response_part in data: if isinstance(response_part, tuple): msg = email.message_from_string(response_part[1]) varSubject = msg['subject'] varDate = msg['date'] </code></pre> <p>I want only the ones beginning with <code>$</code> </p> <pre><code> if varSubject[0] == '$': r, d = m.fetch(emailid, "(UID BODY[TEXT])") ymd = email.utils.parsedate(varDate)[0:3] my_msg.append([ email.message_from_string(d[0][1]) , ymd ]) msg_cnt += 1 </code></pre> <p>I want only the N=100 last messages</p> <pre><code> if ( msg_cnt == 100 ): break_ = True l = len(my_msg) US, EastCst, NewEng, CenAtl, LwrAtl, Midwst, GulfCst, RkyMt, WCst, CA = [0]*l, [0]*l, [0]*l, [0]*l, [0]*l, [0]*l, [0]*l, [0]*l, [0]*l, [0]*l absc = [k for k in range(len(my_msg))] dates = [str(msg[1][2])+'-'+str(msg[1][3])+'-'+str(msg[1][0]) for msg in my_msg] cnt = -1 for msg in my_msg: data = str(msg[0]).split("\n") cnt+=1 for c in [k.split("\r")[0] for k in data[2:-2]]: </code></pre> <p>Use regular expressions to fetch relevant information</p> <pre><code> m = re.match( r"(.+)(=3D\$)(.+)" , c ) if( m == None ): continue country, na, price = m.groups() if ( country == "US" or country == "USA" ) : US[cnt] = float(price) elif( country == "NewEng" ) : EastCst[cnt] = float(price) elif( country == "EastCst" ) : NewEng[cnt] = float(price) elif( country == "EastCst" ) : CenAtl[cnt] = float(price) elif( country == "EastCst" ) : LwrAtl[cnt] = float(price) elif( country == "EastCst" ) : Midwst[cnt] = float(price) elif( country == "EastCst" ) : GulfCst[cnt] = float(price) elif( country == "EastCst" ) : RkyMt[cnt] = float(price) elif( country == "EastCst" ) : WCst[cnt] = float(price) elif( country == "EastCst" ) : CA[cnt] = float(price) </code></pre> <p>Plot all these curves with US prices</p> <pre><code>plt.plot( absc, US ) plt.plot( absc, EastCst ) plt.plot( absc, NewEng, '#251BE0' ) plt.plot( absc, EastCst, '#1BE0BF' ) plt.plot( absc, CenAtl, '#E0771B' ) plt.plot( absc, LwrAtl, '#CC1BE0' ) plt.plot( absc, Midwst, '#E01B8B' ) plt.plot( absc, GulfCst, '#E01B3F' ) plt.plot( absc, RkyMt ) plt.plot( absc, WCst ) plt.plot( absc, CA ) plt.legend( ('US', 'EastCst', 'NewEng' , 'EastCst', 'CenAtl', 'LwrAtl', 'Midwst', 'GulfCst', 'RkyMt', 'WCst', 'CA') ) plt.title('Diesel price') locs,labels = plt.xticks(absc, dates) plt.show() </code></pre> <p>Some related interesting topics are here</p> <p><a href="https://stackoverflow.com/questions/13210737/get-only-new-emails-imaplib-and-python?lq=1">Get only new emails</a></p> <p><a href="https://stackoverflow.com/questions/2230037/how-to-fetch-an-email-body-using-imaplib-in-python?lq=1">Fetch mail body</a></p> <p><a href="https://stackoverflow.com/questions/5572589/python-forward-imap-email-with-attachments-imaplib-smtplib?lq=1">Forward emails with attachment</a></p> <p><a href="https://stackoverflow.com/questions/14029768/python-imaplib-fetch-body-emails-gmail">Fetch body emails in gmail</a></p> <p>Results are here for three areas only</p> <p><img src="https://i.stack.imgur.com/DkM3T.png" alt="us prices"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload