Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
import pprint
import re
import sys
try:
from flickrapi import FlickrAPI
from flickrapi.exceptions import FlickrError
except ImportError:
sys.stderr.write('Please, install FlickrAPI for Python first!\n')
sys.exit(1)
__author__ = 'Igor Davydenko'
__version__ = '0.1'
# Get your own Flickr API key at http://www.flickr.com/services/api/keys/
FLICKR_API_KEY = 'top-secret-key'
FLICKR_RE = re.compile(
r'^http://farm(?P<farm>[0-9]+).static.flickr.com/' + \
r'(?P<server>\w+)/(?P<photo_id>\w+)_(?P<secret>\w+)_.*'
)
def main():
if len(sys.argv) == 1:
sys.stderr.write('Please, enter one or more Flickr URLs! (Separate ' \
'URLs by space)\n')
sys.exit(1)
flickr = FlickrAPI(FLICKR_API_KEY, format='etree')
urls = sys.argv[1:]
try:
flickr.test_echo(foo='bar')
except FlickrError:
sys.stderr.write('Please, check your Flick API key!\n')
sys.exit(1)
for url in urls:
print_info(flickr, url)
def print_info(flickr, url):
print url
print '-' * len(url)
if not FLICKR_RE.match(url):
print 'Your URL could not matched as Flickr URL\n'
return False
matched = FLICKR_RE.search(url).groupdict()
try:
info_tag = flickr.photos_getInfo(photo_id=matched['photo_id'],
secret=matched['secret'])
except FlickrError:
print 'Could not get info for this URL\n'
return False
print
owner = info_tag.find('photo/owner')
title = info_tag.find('photo/title')
description = info_tag.find('photo/description')
urls = info_tag.findall('photo/urls/url')
print 'Owner\n~~~~~\n'
print 'NSID:', owner.get('nsid')
print 'Username:', owner.get('username')
print 'Real name:', owner.get('realname'), '\n'
print 'Information\n~~~~~~~~~~~\n'
print 'Title:', title.text
print 'Description:', description.text
print 'URLs:', [url.text for url in urls], '\n'
return True
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment