Last active
February 17, 2016 21:18
-
-
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)
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
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', '[email protected]'), | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.