Permalink
Browse files

EXIF creation date will now be used if possible, rather than upload d…

…ate (fix #27)
  • Loading branch information...
theopolisme committed Jul 3, 2013
1 parent f40d592 commit 6afb92433d1c2a7745e4206afaaeecd3e0198def
Showing with 37 additions and 1 deletion.
  1. +37 −1 selfimages.py
View
@@ -3,8 +3,14 @@
from __future__ import unicode_literals
import sys
import time
+import os
+import cStringIO
import mwclient
+import requests
+
+from PIL import Image
+from PIL.ExifTags import TAGS
from theobot import password
from theobot import bot
@@ -26,15 +32,45 @@ def main():
else:
print "skipping {0}".format(page.page_title)
+def get_exif_date(image):
+ """Given a filename, downloads the file, gets its
+ EXIF creation date, and returns it.
+ """
+ try:
+ response = requests.get(image.imageinfo['url'])
+ except AttributeError:
+ return None # It wasn't even a file to begin with
+
+ imageobj = cStringIO.StringIO(response.content)
+
+ result = None
+ try:
+ i = Image.open(imageobj)
+ info = i._getexif()
+ for tag, value in info.items():
+ if result == None:
+ decoded = TAGS.get(tag, tag)
+ if decoded == "DateTime":
+ result = time.strptime(value, "%Y:%m:%d %H:%M:%S")
+ break
+ except AttributeError:
+ pass #This means that the image didn't have an EXIF data
+
+ return result
+
def process_page(page):
"""Given an image object, gets its uploader and
its upload date, fills in {{Information}} for it,
and saves the new page.
"""
print "processing {0}".format(page.page_title)
revision = page.revisions(dir='newer').next()
+
user = revision['user']
- date = time.strftime("%d %B %Y",revision['timestamp'])
+
+ date = get_exif_date(page)
+ if date == None:
+ date = time.strftime("%d %B %Y",revision['timestamp'])
contents = page.edit()

0 comments on commit 6afb924

Please sign in to comment.