Created
January 9, 2014 21:58
-
-
Save jfinstrom/8342833 to your computer and use it in GitHub Desktop.
wrote this to parse my ingress portal submissions from my gmail box. It is not the prettiest thing in the world but does the task I need it to. This may be useful in implementing your own solution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# ingressmail.py | |
# | |
# Copyright 2014 James Finstrom <jfinstrom at gmail.com> | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
# MA 02110-1301, USA. | |
# | |
####BEGIN USER EDITS #### | |
# I sort my ingress emails under 3 labels this script uses those labels: | |
approvedLabel = 'portalAproved' | |
submitLabel = 'portalSubmitions' | |
rejectLabel = 'portalRejected' | |
#Gmail credentials | |
login = '[email protected]' | |
passw = 'password' | |
#db info, a table called portals will be created. | |
dbhost = 'localhost' | |
dbuser = 'ingressdb' | |
dbpass = 'ingresspass' | |
dbname = 'ingress' | |
####END USER EDITS#### | |
import imaplib | |
import email | |
import quopri | |
import re | |
import MySQLdb as mdb | |
import sys | |
from PIL import Image | |
from StringIO import StringIO | |
import requests | |
from datetime import datetime | |
con = mdb.connect(dbhost, dbuser, dbpass, dbname) | |
with con: | |
cur = con.cursor() | |
cur.execute("DROP TABLE IF EXISTS portals") | |
cur.execute("CREATE TABLE portals(Id VARCHAR(16) PRIMARY KEY, Name VARCHAR(60), coords VARCHAR(25), ddate DATE, adate DATE, status VARCHAR(25))") | |
mail = imaplib.IMAP4_SSL('imap.gmail.com') | |
mail.login(login, passw) | |
def hash_img(i): | |
im = Image.open(i) | |
im = im.resize((8, 8), Image.ANTIALIAS) | |
im = im.convert("L") | |
pixels = list(im.getdata()) | |
avg = sum(pixels) / len(pixels) | |
bits = "".join(map(lambda pixel: '1' if pixel < avg else '0', pixels)) | |
hexval = int(bits, 2).__format__('016x').upper() | |
return hexval | |
def portal_reject(em): | |
email_message = email.message_from_string(em[1]) | |
#con = mdb.connect('localhost', 'ingressdb', '', 'ingress') | |
dd = email.utils.parsedate(email_message['Date']) | |
ddate = '%s-%s-%s' % (dd[0],dd[1],dd[2]) | |
try: | |
msg = email_message.get_payload()[1]._payload | |
except: | |
return 0 | |
msgtxt = quopri.decodestring(msg) | |
images = re.findall('(http[s]?://lh[0-9]\..*)\" alt', msgtxt) | |
if images: | |
r = requests.get(images[0]) | |
ihash = hash_img(StringIO(r.content)) | |
print "marking %s as Rejected" % (ihash) | |
with con: | |
cur = con.cursor() | |
cur.execute('UPDATE portals set adate="%s", status="REJECTED" WHERE Id= "%s"' % (ddate,ihash)) | |
def process_submit(em): | |
email_message = email.message_from_string(em[1]) | |
#con = mdb.connect('localhost', 'ingressdb', '', 'ingress') | |
subj = email_message['Subject'] | |
dd = email.utils.parsedate(email_message['Date']) | |
ddate = '%s-%s-%s' % (dd[0],dd[1],dd[2]) | |
pname = re.findall('Ingress Portal Submitted: (.*)', subj) | |
if pname: | |
pda = con.escape_string(pname[0]) | |
else: | |
pda = NONE | |
try: | |
msg = email_message.get_payload()[1]._payload | |
except: | |
return 0 | |
msgtxt = quopri.decodestring(msg) | |
images = re.findall('(http://lh[0-9]\..*)\" alt', msgtxt) | |
if images: | |
r = requests.get(images[0]) | |
ihash = hash_img(StringIO(r.content)) | |
print "Creating Entry: %s" % (ihash) | |
with con: | |
cur = con.cursor() | |
cur.execute("INSERT INTO portals(Id, Name, ddate, status) VALUES('%s','%s','%s','%s')" % (ihash,pda,ddate,'SUBMITTED')) | |
def portal_success(em): | |
email_message = email.message_from_string(em[1]) | |
#con = mdb.connect('localhost', 'ingressdb', '', 'ingress') | |
dd = email.utils.parsedate(email_message['Date']) | |
ddate = '%s-%s-%s' % (dd[0],dd[1],dd[2]) | |
try: | |
msg = email_message.get_payload()[1]._payload | |
except: | |
return 0 | |
msgtxt = quopri.decodestring(msg) | |
coords = re.findall('.*ll=([0-9\-.]*,[0-9\-.]*).*', msgtxt) | |
if len(coords) > 0: | |
cda = con.escape_string(coords[0]) | |
else: | |
cda = 'null' | |
images = re.findall('(http[s]?://lh[0-9]\..*)\" alt', msgtxt) | |
if images: | |
r = requests.get(images[0]) | |
ihash = hash_img(StringIO(r.content)) | |
print "marking %s as Approved" % (ihash) | |
with con: | |
cur = con.cursor() | |
cur.execute('UPDATE portals set adate="%s", coords="%s", status="APPROVED" WHERE Id= "%s"' % (ddate,cda,ihash)) | |
def main(): | |
mail.select(submitLabel) | |
result, data = mail.uid('search', None, "ALL") | |
for msg in data[0].split(): | |
result, data = mail.uid('fetch', msg, '(RFC822)') | |
if data: | |
process_submit(data[0]) | |
mail.select(rejectLabel) | |
result, data = mail.uid('search', None, "ALL") | |
for msg in data[0].split(): | |
result, data = mail.uid('fetch', msg, '(RFC822)') | |
if data: | |
portal_reject(data[0]) | |
mail.select(approvedLabel) | |
result, data = mail.uid('search', None, "ALL") | |
for msg in data[0].split(): | |
result, data = mail.uid('fetch', msg, '(RFC822)') | |
if data: | |
portal_success(data[0]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment