Created
February 18, 2024 00:04
-
-
Save tgray/495bedb09dd01123bdfbeb11c938167a to your computer and use it in GitHub Desktop.
Quick script to upload an image to flickr. The auth stuff should work too but has not been tested recently.
This file contains hidden or 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
#!/usr/bin/env python | |
# https://pypi.org/project/flickrapi/ | |
from flickrapi import FlickrAPI | |
from os.path import basename, splitext | |
import sys, os | |
import exiftool | |
import argparse | |
flickr_accounts = { | |
'username': 'flickrid', | |
} | |
# Flickr parameters | |
def get_rc(): | |
"""Get flickr credentials for ~/.flickr_rc.""" | |
# The blog's XMLRPC URL and username. | |
try: | |
f = open(os.path.join(os.environ['HOME'], '.flickr_rc')) | |
lines = f.read() | |
lines = lines.strip().split('\n') | |
userdat = dict([[a.strip() for a in l.split(':', 1)] for l in lines]) | |
f.close() | |
except: | |
print("""Must set flickr credentials in .flickr_rc file. Obtain at: | |
https://www.flickr.com/services/apps/create/apply/ | |
Example contents: | |
fuser: username | |
key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
secret: xxxxxxxxxxxxxxxx | |
""") | |
sys.exit(1) | |
return userdat | |
# the following two auth functions are taken from | |
# http://stuvel.eu/media/flickrapi-docs/documentation/3-auth.html | |
def auth_web(flickr): | |
flickr.authenticate_via_browser(perms='read') | |
def auth_cli(flickr): | |
import webbrowser | |
print('Step 1: authenticate') | |
# Only do this if we don't have a valid token already | |
if not flickr.token_valid(perms='read'): | |
# Get a request token | |
flickr.get_request_token(oauth_callback='oob') | |
# | |
# Open a browser at the authentication URL. Do this however | |
# you want, as long as the user visits that URL. | |
authorize_url = flickr.auth_url(perms='read') | |
webbrowser.open_new_tab(authorize_url) | |
# Get the verifier code from the user. Do this however you | |
# want, as long as the user gives the application the code. | |
verifier = unicode(raw_input('Verifier code: ')) | |
# Trade the request token for an access token | |
flickr.get_access_token(verifier) | |
def get_data(fn): | |
with exiftool.ExifToolHelper() as et: | |
metadata = et.get_metadata(fn)[0] | |
return metadata | |
def upload(fn, flickr=None, dryrun = False, public=False): | |
print("Uploading %s..." % fn) | |
t = splitext(basename(fn))[0] # file name w/o extension | |
# If you don't care about metadata and don't want to install exiftool and | |
# the python wrapper, you can probably just comment out this code and modify | |
# the flickr.upload call a few lines down. | |
try: | |
meta = get_data(fn) | |
except: | |
meta = {} | |
title = meta.get('IPTC:Headline', t) | |
if title == t: | |
title = meta.get('XMP:Headline', t) | |
descrip = meta.get('XMP:Description', ' ') | |
if not dryrun: | |
response = flickr.upload(filename=fn, title=title, description = | |
descrip, is_public=public, format='etree') | |
photoID = response.find('photoid').text | |
return photoID | |
else: | |
print(" --> dry run") | |
def main(argv=None): | |
try: | |
parser = argparse.ArgumentParser( | |
description = ('upload picture to flickr.')) | |
parser.add_argument('-p', '--private', | |
dest = 'private', | |
action = 'store_true', | |
help = 'make uploads private') | |
parser.add_argument('--dry-run', | |
dest = 'dryrun', | |
action = 'store_true', | |
help = 'dry run - does not upload and move files') | |
parser.add_argument('-a', '--auth', | |
dest = 'auth', | |
action = 'store_true', | |
help = "authenticate with flickr with a web browser") | |
parser.add_argument('--auth-cli', | |
dest = 'authcli', | |
action = 'store_true', | |
help = "authenticate with flickr from the CLI") | |
parser.add_argument('filename', | |
action = 'store', | |
default = None, | |
help = 'file to upload') | |
args = parser.parse_args() | |
except: | |
sys.exit(2) | |
if args.private: | |
public = 0 | |
else: | |
public = 1 | |
userdat = get_rc() | |
fuser = userdat['fuser'] | |
# Upload the files on the command line. | |
flickr = FlickrAPI(api_key=userdat['key'], secret=userdat['secret'], | |
format='parsed-json') | |
if args.auth or args.authcli: | |
if args.auth: | |
auth_web(flickr) | |
elif args.authcli: | |
auth_cli(flickr) | |
if flickr.token_valid(perms='write'): | |
print('authenticated') | |
sys.exit(0) | |
else: | |
print('error authenticating') | |
sys.exit(1) | |
# upload our image | |
fn = args.filename | |
photoID = upload(os.path.abspath(os.path.expanduser(fn)), flickr=flickr, | |
dryrun=args.dryrun, public=public) | |
if photoID: | |
photoURL = 'http://www.flickr.com/photos/%s/%s/' % (fuser, photoID) | |
print(" %s" % photoURL) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment