Skip to content

Instantly share code, notes, and snippets.

@mgranberry
Last active February 17, 2016 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgranberry/87fc7f97bd21e08d1587 to your computer and use it in GitHub Desktop.
Save mgranberry/87fc7f97bd21e08d1587 to your computer and use it in GitHub Desktop.
This gist downloads, converts, and tags the time in the images contained emails sent from my daughter's daycare. (Precious Status)
import imaplib
import re
import datetime
from os import system, environ
def process(client):
rv, data = client.search(None, 'ALL')
# print rv, data
emails = data[0].split()
# emails = ['209']
for msgid in emails:
rv, data = client.fetch(msgid, '(RFC822)')
if data is not None:
matches = re.findall(r'<img alt="Photo Update" border="0" src="(https://s3.amazonaws.com/[^"]*.png)\?([0-9]+)"', data[0][1], flags=re.M)
for match in matches:
print msgid, 'orig', match[0], datetime.datetime.fromtimestamp(float(match[1]))
system("curl {uri} > '{date}'.png&&convert '{date}'.png '{date}'.jpg&&rm '{date}.png'&&exiftool -overwrite_original -DateTimeOriginal='{date}' -CreateDate='{date}' -ModifyDate='{date}' '{date}'.jpg".format(uri=match[0], date=datetime.datetime.fromtimestamp(float(match[1]))))
def main():
client = imaplib.IMAP4_SSL(environ.get('PS_IMAP_SERVER', 'imap.gmail.com'))
rv, data = client.login(environ.get('PS_IMAP_USER', 'matthias.granberry@gmail.com'),
environ.get('PS_IMAP_PASSWORD'))
# print rv, data
rv, boxes = client.list()
# print rv, boxes
rv, box = client.select(environ.get('PS_IMAP_FOLDER'))
# print rv, box
process(client)
if __name__ == '__main__':
main()
@mgranberry
Copy link
Author

BTW it requires exiftool, ImageMagick, curl, and "less secure authentication" turned on if you are using gmail. There is no error-checking, so add some prints if you have a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment