def sync_photo(id, flickr, check_dirty=False): print id db_photo = session.query(Photo).filter(Photo.flickr_id == id).first() if db_photo and not check_dirty: print 'Photo is already local.' return db_photo photo = simplejson.loads(flickr.photos_getInfo(photo_id=id, nojsoncallback=1)) p = photo['photo'] (id, title) = (int(p['id']), p['title']['_content']) url = url_for_photo(p) page_url = p['urls']['url'][0]['_content'] description = """%s\n %s Taken: %s in %s Flickr: %s""" % (p['title']['_content'], p['description']['_content'], p['dates']['taken'], loc_to_string(p), page_url) if db_photo: print "Photo %s already exists" % id if db_photo.title == title and db_photo.description == description: return db_photo db_photo.dirty = True db_photo.title = title db_photo.description = description else: url = url_for_photo(p) db_photo = Photo(title= title, description=description, flickr_id=id, dirty=False, url=url) if not p['visibility']['ispublic']: db_photo.private = True session.add(db_photo) sync_tags(db_photo, p) session.commit() return db_photo