Skip to content

Instantly share code, notes, and snippets.

@jl2
Created June 26, 2010 06:10
Show Gist options
  • Save jl2/453834 to your computer and use it in GitHub Desktop.
Save jl2/453834 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# flickrtests.py
# Copyright (c) 2010, Jeremiah LaRocco jeremiah.larocco@gmail.com
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import sys
import os.path
import hashlib
import urllib.request
from xml.dom.minidom import parseString
the_api_key = 'your api key goes here'
the_secret = 'your secret goes here'
auth_toke = None
def getElement(dom, tag):
""" Convenience function to grab text """
try:
dom.getElementsByTagName(tag)[0].firstChild.nodeValue
return dom.getElementsByTagName(tag)[0].firstChild.nodeValue
except Exception as e:
return None
def signApiCall(**args):
global the_api_key
global the_secret
args.update(api_key = the_api_key)
secstr = the_secret + ''.join('{}{}'.format(k, args[k]) for k in sorted(args))
api_sig = hashlib.md5(secstr.encode('utf-8')).hexdigest()
return '&'.join('{}={}'.format(k, args[k])
for k in sorted(args)) + '&api_sig='+api_sig
def callFlickr(meth, **args):
args.update(method= meth)
if auth_toke:
args.update(auth_token=auth_toke)
url = 'http://api.flickr.com/services/rest/?' + signApiCall(**args)
data = urllib.request.urlopen(url).readall()
dom = parseString(data)
return dom
def getFrob():
dom = callFlickr('flickr.auth.getFrob')
return getElement(dom, 'frob')
def getAuthToken(permission):
global the_api_key
if os.path.isfile('.auth_toke'):
with open('.auth_toke') as inf:
token = inf.readline()
else:
tfrob = getFrob()
url = 'http://flickr.com/services/auth/?' + signApiCall(perms=permission,
frob=tfrob)
input('Visit this URL to authenticate: ' + url +
'Press enter when finished approving the app...')
dom = callFlickr('flickr.auth.getToken', frob=tfrob)
token = getElement(dom, 'token')
with open('.auth_toke', 'w') as outf:
outf.write(token)
return token
def getUserNSID(uname):
dom = callFlickr('flickr.people.findByUsername', username=uname)
return dom.getElementsByTagName('user')[0].getAttribute('nsid')
def getUserInfo(uname):
dom = callFlickr('flickr.people.getInfo', user_id=getUserNSID(uname))
rv = {}
rv['username'] = getElement(dom, 'username')
rv['realname'] = getElement(dom, 'realname')
rv['location'] = getElement(dom, 'location')
rv['photosurl'] = getElement(dom, 'photosurl')
rv['profileurl'] = getElement(dom, 'profileurl')
return rv
def getUserSets(uname):
dom = callFlickr('flickr.photosets.getList', user_id=getUserNSID(uname))
retVal = []
for ps in dom.getElementsByTagName('photoset'):
retVal.append((ps.getAttribute('id'),
ps.getElementsByTagName('title')[0].firstChild.nodeValue))
return retVal
def main(args):
global auth_toke
uname = 'jl_2'
if len(args)>0:
uname = args[0]
auth_toke = getAuthToken('read')
pi = getUserInfo(uname)
for k in pi:
if pi[k]:
print('{} = {}'.format(k, pi[k]))
print(uname + "'s sets:")
print('{:60s} | {:20s}'.format('Title', 'ID'))
print('='*61 + '+' + '='*18)
for set_info in getUserSets(uname):
print("{1:60s} | {0:20s}".format(*set_info))
exit(0)
if __name__=='__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment