metadata.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import flickrapi, json, os, boto
  2. import settings_local as settings
  3. from boto.s3.key import Key
  4. from os import walk
  5. ROOT = os.path.dirname(os.path.realpath(__file__))
  6. # Instantiate aws client
  7. aws = boto.connect_s3(settings.AWS_KEY, settings.AWS_SECRET)
  8. bucket = aws.get_bucket(settings.AWS_BUCKET)
  9. # Instantiate flickr client
  10. flickr = flickrapi.FlickrAPI(settings.FLICKR_KEY, settings.FLICKR_SECRET)
  11. (token, frob) = flickr.get_token_part_one(perms='write')
  12. if not token: raw_input("Press ENTER after you authorized this program")
  13. flickr.get_token_part_two((token, frob))
  14. def update_exifs_from_string(photo_id, key):
  15. print ('Updating %s' % key)
  16. k = Key(bucket)
  17. k.key = key
  18. j = json.loads(k.get_contents_as_string())
  19. if not j.has_key('exif'):
  20. print ('-- %s not set' % key)
  21. exifs = flickr.photos_getExif(photo_id=photo_id)[0]
  22. exifs = {
  23. exif.attrib['label'].lower(): {
  24. 'space': exif.attrib['tagspace'],
  25. 'id': exif.attrib['tagspaceid'],
  26. 'tag': exif.attrib['tag'],
  27. 'raw': exif[0].text,
  28. } for exif in exifs
  29. }
  30. j['exif'] = exifs
  31. k.set_contents_from_string(json.dumps(j, indent=2))
  32. print ('-- Updated %s' % key)
  33. else:
  34. print ('-- %s already set' % key)
  35. data_dir = 'data'
  36. for (dirpath, dirnames, filenames) in walk(os.path.join(ROOT, data_dir)):
  37. for f in filenames:
  38. json_file = open(os.path.join(ROOT, data_dir, f))
  39. data = json.load(json_file)
  40. for photo in data['photos']:
  41. update_exifs_from_string(
  42. photo['id'],
  43. '%s/%s.%s' % (data['id'], photo['id'], 'json')
  44. )