Created
December 11, 2015 21:56
-
-
Save vitorio/5b6fc4b0915ee63172ba to your computer and use it in GitHub Desktop.
Flickr API: find and fix "taken on" dates
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
#CC0 public domain dedication | |
#Find all the EXIF fields with the letters "date" and output them and their content | |
import flickrapi | |
api_key = u'' | |
api_secret = u'' | |
my_photoset = u'' | |
my_user_id = u'' | |
flickr = flickrapi.FlickrAPI(api_key, api_secret, format=u'parsed-json') | |
if not flickr.token_valid(perms=u'write'): | |
flickr.get_request_token(oauth_callback=u'oob') | |
print flickr.auth_url(perms=u'write') | |
verifier = unicode(raw_input('Verifier code: ')) | |
flickr.get_access_token(verifier) | |
print flickr.token_valid(perms=u'write') | |
setphotos = flickr.photosets.getPhotos(photoset_id=my_photoset, user_id=my_user_id, extras=u'date_taken') | |
for b in setphotos['photoset']['photo']: | |
p = flickr.photos.getExif(photo_id=b['id']) | |
for c in p['photo']['exif']: | |
if u'tag' in c: | |
if u'date' in c['tag'].lower(): | |
print c['tag'], c['raw']['_content'] | |
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
#CC0 public domain dedication | |
#After you've figured out all the possible date formats, iterate through a | |
#photo set to find the earliest date for each photo and set date_taken to that | |
import flickrapi, arrow | |
mysql_datetime = 'YYYY-MM-DD HH:mm:ss' | |
date_formats = ['YYYY:MM:DD HH:mm:ss', 'YYYY/MM/DD HH:mm:ss', 'YYYY:MM:DD HH:mm:ssZ', mysql_datetime] | |
api_key = u'' | |
api_secret = u'' | |
my_photoset = u'' | |
my_user_id = u'' | |
flickr = flickrapi.FlickrAPI(api_key, api_secret, format=u'parsed-json') | |
if not flickr.token_valid(perms=u'write'): | |
flickr.get_request_token(oauth_callback=u'oob') | |
print flickr.auth_url(perms=u'write') | |
verifier = unicode(raw_input('Verifier code: ')) | |
flickr.get_access_token(verifier) | |
print flickr.token_valid(perms=u'write') | |
# didn't realize I was paginating | |
setphotos = flickr.photosets.getPhotos(photoset_id=my_photoset, user_id=my_user_id, page=u'2', extras=u'date_taken') | |
print setphotos['photoset']['total'] | |
for b in setphotos['photoset']['photo']: | |
p = flickr.photos.getExif(photo_id=b['id']) | |
right_now = arrow.get() | |
earliest_date = right_now | |
date_taken = arrow.get(b['datetaken'], date_formats) | |
for c in p['photo']['exif']: | |
if u'tag' in c: | |
if u'Date' in c['tag']: | |
this_particular_date = arrow.get(c['raw']['_content'], date_formats) | |
if this_particular_date < earliest_date: | |
earliest_date = this_particular_date | |
if earliest_date == right_now: | |
print 'No EXIF date for', b['id'], b['title'] | |
elif date_taken == earliest_date: | |
print 'Good taken date for', b['id'], b['title'] | |
elif earliest_date < date_taken: | |
ok = flickr.photos.setDates(photo_id=b['id'], date_taken=earliest_date.format(mysql_datetime)) | |
print 'Updating taken date for', b['id'], b['title'], 'from', date_taken.format(mysql_datetime), 'to', earliest_date.format(mysql_datetime), ok |
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
#CC0 public domain dedication | |
#For files without EXIF dates, check the filesystem you uploaded them from | |
#and manually copy-and-paste the correct date from the correct file | |
import flickrapi, arrow, os, fnmatch | |
def find(pattern, path): | |
result = [] | |
for root, dirs, files in os.walk(path): | |
for name in files: | |
if fnmatch.fnmatch(name.lower(), pattern.lower()): | |
result.append(os.path.join(root, name)) | |
return result | |
mysql_datetime = 'YYYY-MM-DD HH:mm:ss' | |
date_formats = ['YYYY:MM:DD HH:mm:ss', 'YYYY/MM/DD HH:mm:ss', 'YYYY:MM:DD HH:mm:ssZ', mysql_datetime] | |
api_key = u'' | |
api_secret = u'' | |
my_photoset = u'' | |
my_user_id = u'' | |
searchpath = '' | |
flickr = flickrapi.FlickrAPI(api_key, api_secret, format=u'parsed-json') | |
if not flickr.token_valid(perms=u'write'): | |
flickr.get_request_token(oauth_callback=u'oob') | |
print flickr.auth_url(perms=u'write') | |
verifier = unicode(raw_input('Verifier code: ')) | |
flickr.get_access_token(verifier) | |
print flickr.token_valid(perms=u'write') | |
# didn't realize I was paginating | |
setphotos = flickr.photosets.getPhotos(photoset_id=my_photoset, user_id=my_user_id, extras=u'date_taken') | |
print setphotos['photoset']['total'] | |
for b in setphotos['photoset']['photo']: | |
p = flickr.photos.getExif(photo_id=b['id']) | |
if b['datetakenunknown']: | |
print '\nNo taken date for', b['id'], b['title'] | |
orig_files = find('{}*.*'.format(b['title']), searchpath) | |
for f in orig_files: | |
print arrow.Arrow.fromtimestamp(os.path.getmtime(f)).format(mysql_datetime), f | |
# print arrow.Arrow.fromtimestamp(os.path.getctime(f)).format(mysql_datetime), f | |
new_date = unicode(raw_input('paste the datetime stamp to use: ')) | |
print flickr.photos.setDates(photo_id=b['id'], date_taken=new_date) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment