<s> import re <EOL> import json <EOL> import urllib <EOL> import logging <EOL> from urlparse import urlparse <EOL> from django . core . management . base import BaseCommand <EOL> from chronam . core . management . commands import configure_logging <EOL> from chronam . core . models import Page , FlickrUrl <EOL> configure_logging ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> _log = logging . getLogger ( __name__ ) <EOL> class Command ( BaseCommand ) : <EOL> args = '<STR_LIT>' <EOL> help = '<STR_LIT>' <EOL> def handle ( self , key , ** options ) : <EOL> _log . debug ( "<STR_LIT>" ) <EOL> create_count = <NUM_LIT:0> <EOL> for flickr_url , chronam_url in flickr_chronam_links ( key ) : <EOL> _log . info ( "<STR_LIT>" % <EOL> ( flickr_url , chronam_url ) ) <EOL> path = urlparse ( chronam_url ) . path <EOL> page = Page . lookup ( path ) <EOL> if not page : <EOL> _log . error ( "<STR_LIT>" % chronam_url ) <EOL> continue <EOL> f , created = FlickrUrl . objects . get_or_create ( value = flickr_url , <EOL> page = page ) <EOL> if created : <EOL> create_count += <NUM_LIT:1> <EOL> f . save ( ) <EOL> _log . info ( "<STR_LIT>" % <EOL> ( page , flickr_url ) ) <EOL> else : <EOL> _log . info ( "<STR_LIT>" % flickr_url ) <EOL> _log . info ( "<STR_LIT>" % create_count ) <EOL> def photos_in_set ( key , set_id ) : <EOL> """<STR_LIT>""" <EOL> u = '<STR_LIT>' % ( key , set_id ) <EOL> photos = json . loads ( urllib . urlopen ( u ) . read ( ) ) <EOL> for p in photos [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> yield photo ( key , p [ '<STR_LIT:id>' ] ) <EOL> def photo ( key , photo_id ) : <EOL> """<STR_LIT>""" <EOL> u = '<STR_LIT>' % ( key , photo_id ) <EOL> return json . loads ( urllib . urlopen ( u ) . read ( ) ) <EOL> def flickr_url ( photo ) : <EOL> for url in photo [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT:url>' ] : <EOL> if url [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> return url [ '<STR_LIT>' ] <EOL> return None <EOL> def chronam_url ( photo ) : <EOL> """<STR_LIT>""" <EOL> for tag in photo [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> if '<STR_LIT>' in tag [ '<STR_LIT>' ] : <EOL> return tag [ '<STR_LIT>' ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> m = re . search ( '<STR_LIT>' , <EOL> photo [ '<STR_LIT>' ] [ '<STR_LIT:description>' ] [ '<STR_LIT>' ] ) <EOL> if m : <EOL> return m . group ( <NUM_LIT:1> ) <EOL> return None <EOL> def flickr_chronam_links ( key ) : <EOL> """<STR_LIT>""" <EOL> for set_id in [ <NUM_LIT> , <NUM_LIT> ] : <EOL> for photo in photos_in_set ( key , set_id ) : <EOL> chronam = chronam_url ( photo ) <EOL> if chronam : <EOL> yield flickr_url ( photo ) , chronam </s>
