Skip to content

Instantly share code, notes, and snippets.

@J535D165
Created January 26, 2020 21:23
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 J535D165/bd01e62279a6864430f3a357292623e9 to your computer and use it in GitHub Desktop.
Save J535D165/bd01e62279a6864430f3a357292623e9 to your computer and use it in GitHub Desktop.
Simple Python3 script to download all your photos from your Flickr profile
"""Flickr photo downloader.
Get credentials for the Flickr API from
https://github.com/alexis-mignon/python-flickr-api/wiki/Flickr-API-Keys-and-Authentication
# a = flickr_api.auth.AuthHandler()
# perms = "read" # set the required permissions
# url = a.get_authorization_url(perms)
# <rsp stat="ok">
# <method>flickr.test.echo</method>
# <api_key>**SECRET**</api_key>
# <oauth_token>**SECRET**</oauth_token>
# <oauth_verifier>**SECRET**</oauth_verifier>
# </rsp>
"""
from pathlib import Path
import json
import flickr_api
auth_handler = flickr_api.auth.AuthHandler(
key='**SECRET**',
secret='**SECRET**',
access_token_key="**SECRET**",
access_token_secret="**SECRET**")
flickr_api.set_auth_handler(auth_handler)
def get_location(p):
"""Retrieve location info from Photo object"""
try:
d = p.getLocation().__dict__
keys = [
'region', 'neighbourhood', 'latitude', 'longitude', 'accuracy',
'country', 'locality', 'county'
]
return {key: d[key] for key in keys}
except Exception as err:
# print(err)
return None
def get_info(p):
"""Retrieve photo info from Photo object"""
info = p.getInfo()
return {
'id': info.get('id', None),
'description': info.get('description', None),
'dateuploaded': info.get('dateuploaded', None),
'taken': info.get('taken', None),
'url': p.getPhotoUrl()
}
def get_exif(p):
"""Retrieve EXIF info from Photo object"""
result = []
exif = p.getExif()
for e in exif:
result.append({'tag': e.tag, 'raw': e.raw})
return result
user = flickr_api.test.login()
photos = user.getPhotos()
# get an overview of all attributes relevant for the download
# print([m for m in dir(photos[0]) if m.startswith("get")])
counter = 0
errors = []
for p in flickr_api.Walker(user.getPhotos):
try:
d = get_info(p)
Path("photos").mkdir(parents=True, exist_ok=True)
fp_meta = Path("photos", "{}-{}.json".format(d['dateuploaded'], d['id']))
if not fp_meta.exists():
d['location'] = get_location(p)
d['exif'] = get_exif(p)
with open(str(fp_meta), 'w') as outfile:
json.dump(d, outfile)
fp_photo = Path("photos", "{}-{}.jpeg".format(d['dateuploaded'], d['id']))
if not fp_photo.exists():
p.save(str(fp_photo))
counter += 1
print(counter)
except Exception:
errors.append("{}-{}".format(d['dateuploaded'], d['id']))
print(errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment