def _syncPhoto(self, photo_xml, refresh=False): """ Synchronize a flickr photo with the Django backend. Required Arguments photo_xml: A flickr photos in Flickrapi's REST XMLNode format """ if photo_xml.photo[0]['media'] != 'photo': # Ignore media like videos return None photo_id = photo_xml.photo[0]['id'] # if we're refreshing this data, then delete the Photo first... if refresh: try: p = Photo.objects.get(flickr_id = photo_id) p.delete() except ObjectDoesNotExist: pass sizes = self.getPhotoSizes(photo_id) # Removed urls = self.getPhotoSizeURLs(photo_id) exif_data = self.getExifInfo(photo_id) geo_data = self.getGeoLocation(photo_id) taken_date = datetime(*strptime(photo_xml.photo[0].dates[0]['taken'], "%Y-%m-%d %H:%M:%S")[:7]) upload_date = datetime.fromtimestamp(int(photo_xml.photo[0].dates[0]['posted'])) update_date = datetime.fromtimestamp(int(photo_xml.photo[0].dates[0]['lastupdate'])) proposed_slug = defaultfilters.slugify(photo_xml.photo[0].title[0].text.lower()) slug = get_unique_slug_for_photo(taken_date, proposed_slug) # Ignore tags if there are more chars than 255 tags, count = '', 0 for tag in [(t, len(t) + 1) for t in self._getXMLNodeTag(photo_xml).split()]: if 255 <= (count + tag[1] - 1): tags = tags[:-1] break if not tag[0].startswith('geo:'): # Exclude ugly geo-tags tags += u'%s ' % tag[0] count += tag[1] try: original_secret = photo_xml.photo[0]['originalsecret'] except KeyError: original_secret = '' default_dict = { 'flickr_id': photo_xml.photo[0]['id'], 'owner': photo_xml.photo[0].owner[0]['username'], 'owner_nsid': photo_xml.photo[0].owner[0]['nsid'], 'title': photo_xml.photo[0].title[0].text, # TODO: Typography 'slug': slug, 'description': photo_xml.photo[0].description[0].text, 'taken_date': taken_date, 'upload_date': upload_date, 'update_date': update_date, 'photopage_url': photo_xml.photo[0].urls[0].url[0].text, 'farm': photo_xml.photo[0]['farm'], 'server': photo_xml.photo[0]['server'], 'secret': photo_xml.photo[0]['secret'], 'original_secret': original_secret, 'thumbnail_width': sizes['Thumbnail']['width'], 'thumbnail_height': sizes['Thumbnail']['height'], 'small_width': sizes['Small']['width'], 'small_height': sizes['Small']['height'], 'medium_width': sizes['Medium']['width'], 'medium_height': sizes['Medium']['height'], 'medium_640_width': sizes['Medium 640']['width'], 'medium_640_height': sizes['Medium 640']['height'], 'large_width': sizes['Large']['width'], 'large_height': sizes['Large']['height'], 'original_width': sizes['Original']['width'] or 0, 'original_height': sizes['Original']['height'] or 0, # Removed 'square_url': urls['Square'], # Removed 'small_url': urls['Small'], # Removed 'medium_url': urls['Medium'], # Removed 'thumbnail_url': urls['Thumbnail'], 'tags': tags, 'license': photo_xml.photo[0]['license'], 'geo_latitude': geo_data['latitude'], 'geo_longitude': geo_data['longitude'], 'geo_accuracy': geo_data['accuracy'], 'geo_locality': geo_data['locality'], 'geo_county': geo_data['county'], 'geo_region': geo_data['region'], 'geo_country': geo_data['country'], 'exif_model': self.getExifKey(exif_data, 'Model'), 'exif_make': self.getExifKey(exif_data, 'Make'), 'exif_orientation': self.getExifKey(exif_data, 'Orientation'), 'exif_exposure': self.getExifKey(exif_data, 'Exposure'), 'exif_software': self.getExifKey(exif_data, 'Software'), 'exif_aperture': self.getExifKey(exif_data, 'Aperture'), 'exif_iso': self.getExifKey(exif_data, 'ISO Speed'), 'exif_metering_mode': self.getExifKey(exif_data, 'Metering Mode'), 'exif_flash': self.getExifKey(exif_data, 'Flash'), 'exif_focal_length': self.getExifKey(exif_data, 'Focal Length'), 'exif_color_space': self.getExifKey(exif_data, 'Color Space'), } obj, created = Photo.objects.get_or_create( flickr_id = photo_xml.photo[0]['id'], defaults=default_dict) # update if something changed if obj.update_date < update_date: # Never overwrite URL-relevant attributes default_dict['slug'] = obj.slug default_dict['taken_date'] = obj.taken_date updated_obj = Photo(pk=obj.pk, **default_dict) updated_obj.save() # Comments comments = self.getPhotoComments(obj.flickr_id) if comments is not None: for c in comments: c['photo'] = obj comment, created = PhotoComment.objects.get_or_create( flickr_id=c['flickr_id'], defaults=c) return obj
def _syncPhoto(self, photo_xml, refresh=False): """ Synchronize a flickr photo with the Django backend. Required Arguments photo_xml: A flickr photos in Flickrapi's REST XMLNode format """ if photo_xml.photo[0]['media'] != 'photo': # Ignore media like videos return None photo_id = photo_xml.photo[0]['id'] # if we're refreshing this data, then delete the Photo first... if refresh: try: p = Photo.objects.get(flickr_id=photo_id) p.delete() except ObjectDoesNotExist: pass sizes = self.getPhotoSizes(photo_id) # Removed urls = self.getPhotoSizeURLs(photo_id) exif_data = self.getExifInfo(photo_id) geo_data = self.getGeoLocation(photo_id) taken_date = datetime(*strptime(photo_xml.photo[0].dates[0]['taken'], "%Y-%m-%d %H:%M:%S")[:7]) upload_date = datetime.fromtimestamp( int(photo_xml.photo[0].dates[0]['posted'])) update_date = datetime.fromtimestamp( int(photo_xml.photo[0].dates[0]['lastupdate'])) proposed_slug = defaultfilters.slugify( photo_xml.photo[0].title[0].text.lower()) slug = get_unique_slug_for_photo(taken_date, proposed_slug) # Ignore tags if there are more chars than 255 tags, count = '', 0 for tag in [(t, len(t) + 1) for t in self._getXMLNodeTag(photo_xml).split()]: if 255 <= (count + tag[1] - 1): tags = tags[:-1] break if not tag[0].startswith('geo:'): # Exclude ugly geo-tags tags += u'%s,' % tag[0] count += tag[1] try: original_secret = photo_xml.photo[0]['originalsecret'] except KeyError: original_secret = '' default_dict = { 'flickr_id': photo_xml.photo[0]['id'], 'owner': photo_xml.photo[0].owner[0]['username'], 'owner_nsid': photo_xml.photo[0].owner[0]['nsid'], 'title': photo_xml.photo[0].title[0].text, # TODO: Typography 'slug': slug, 'description': photo_xml.photo[0].description[0].text, 'taken_date': taken_date, 'upload_date': upload_date, 'update_date': update_date, 'photopage_url': photo_xml.photo[0].urls[0].url[0].text, 'farm': photo_xml.photo[0]['farm'], 'server': photo_xml.photo[0]['server'], 'secret': photo_xml.photo[0]['secret'], 'original_secret': original_secret, 'thumbnail_width': sizes['Thumbnail']['width'], 'thumbnail_height': sizes['Thumbnail']['height'], 'small_width': sizes['Small']['width'], 'small_height': sizes['Small']['height'], 'medium_width': sizes['Medium']['width'], 'medium_height': sizes['Medium']['height'], 'medium_640_width': sizes['Medium 640']['width'], 'medium_640_height': sizes['Medium 640']['height'], 'large_square_width': sizes['Large Square']['width'], 'large_square_height': sizes['Large Square']['height'], 'large_width': sizes['Large']['width'], 'large_height': sizes['Large']['height'], 'original_width': sizes['Original']['width'] or 0, 'original_height': sizes['Original']['height'] or 0, # Removed 'square_url': urls['Square'], # Removed 'small_url': urls['Small'], # Removed 'medium_url': urls['Medium'], # Removed 'thumbnail_url': urls['Thumbnail'], 'license': photo_xml.photo[0]['license'], 'geo_latitude': geo_data['latitude'], 'geo_longitude': geo_data['longitude'], 'geo_accuracy': geo_data['accuracy'], 'geo_locality': geo_data['locality'], 'geo_county': geo_data['county'], 'geo_region': geo_data['region'], 'geo_country': geo_data['country'], 'exif_model': self.getExifKey(exif_data, 'Model'), 'exif_make': self.getExifKey(exif_data, 'Make'), 'exif_orientation': self.getExifKey(exif_data, 'Orientation'), 'exif_exposure': self.getExifKey(exif_data, 'Exposure'), 'exif_software': self.getExifKey(exif_data, 'Software'), 'exif_aperture': self.getExifKey(exif_data, 'Aperture'), 'exif_iso': self.getExifKey(exif_data, 'ISO Speed'), 'exif_metering_mode': self.getExifKey(exif_data, 'Metering Mode'), 'exif_flash': self.getExifKey(exif_data, 'Flash'), 'exif_focal_length': self.getExifKey(exif_data, 'Focal Length'), 'exif_color_space': self.getExifKey(exif_data, 'Color Space'), } if len(tags): default_dict['tags'] = [tag for tag in tags.split(',') if len(tag)] obj, created = Photo.objects.get_or_create( flickr_id=photo_xml.photo[0]['id'], defaults=default_dict) # update if something changed if obj.update_date < update_date: # Never overwrite URL-relevant attributes default_dict['slug'] = obj.slug default_dict['taken_date'] = obj.taken_date updated_obj = Photo(pk=obj.pk, **default_dict) updated_obj.save() # Comments comments = self.getPhotoComments(obj.flickr_id) if comments is not None: for c in comments: c['photo'] = obj comment, created = PhotoComment.objects.get_or_create( flickr_id=c['flickr_id'], defaults=c) return obj
def _syncPhoto(self, photo_xml, refresh=False): """ Synchronize a flickr photo with the Django backend. Required Arguments photo_xml: A flickr photos in Flickrapi's REST XMLNode format """ if photo_xml.photo[0]['media'] != 'photo': # Ignore media like videos return None photo_id = photo_xml.photo[0]['id'] # if we're refreshing this data, then delete the Photo first... if refresh: try: p = Photo.objects.get(flickr_id = photo_id) p.delete() except ObjectDoesNotExist: pass sizes = self.getPhotoSizes(photo_id) geo_data = self.getGeoLocation(photo_id) taken_date = datetime(*strptime(photo_xml.photo[0].dates[0]['taken'], "%Y-%m-%d %H:%M:%S")[:7]) upload_date = datetime.fromtimestamp(int(photo_xml.photo[0].dates[0]['posted'])) update_date = datetime.fromtimestamp(int(photo_xml.photo[0].dates[0]['lastupdate'])) proposed_slug = defaultfilters.slugify(photo_xml.photo[0].title[0].text.lower()) slug = get_unique_slug_for_photo(taken_date, proposed_slug) try: original_secret = photo_xml.photo[0]['originalsecret'] except KeyError: original_secret = '' default_dict = { 'flickr_id': photo_xml.photo[0]['id'], 'owner': photo_xml.photo[0].owner[0]['username'], 'owner_nsid': photo_xml.photo[0].owner[0]['nsid'], 'title': photo_xml.photo[0].title[0].text, # TODO: Typography 'slug': slug, 'description': photo_xml.photo[0].description[0].text, 'taken_date': taken_date, 'upload_date': upload_date, 'update_date': update_date, 'photopage_url': photo_xml.photo[0].urls[0].url[0].text, 'farm': photo_xml.photo[0]['farm'], 'server': photo_xml.photo[0]['server'], 'secret': photo_xml.photo[0]['secret'], 'original_secret': original_secret, 'thumbnail_width': sizes['Thumbnail']['width'], 'thumbnail_height': sizes['Thumbnail']['height'], 'small_width': sizes['Small']['width'], 'small_height': sizes['Small']['height'], 'medium_width': sizes['Medium']['width'], 'medium_height': sizes['Medium']['height'], 'large_width': sizes['Large']['width'], 'large_height': sizes['Large']['height'], 'original_width': sizes['Original']['width'] or 0, 'original_height': sizes['Original']['height'] or 0, 'license': photo_xml.photo[0]['license'], 'geo_latitude': geo_data['latitude'], 'geo_longitude': geo_data['longitude'], 'geo_accuracy': geo_data['accuracy'], 'geo_locality': geo_data['locality'], 'geo_county': geo_data['county'], 'geo_region': geo_data['region'], 'geo_country': geo_data['country'], } obj, created = Photo.objects.get_or_create(flickr_id=photo_xml.photo[0]['id'], defaults=default_dict) # update if something changed if obj.update_date < update_date: # Never overwrite URL-relevant attributes default_dict['slug'] = obj.slug default_dict['taken_date'] = obj.taken_date updated_obj = Photo(pk=obj.pk, **default_dict) updated_obj.save() return obj
def _syncPhoto(self, photo_xml, refresh=False, index=0): """ Synchronize a flickr photo with the Django backend. Required Arguments photo_xml: A flickr photos in Flickrapi's REST XMLNode format """ if photo_xml.photo[0]["media"] != "photo": # Ignore media like videos return None photo_id = photo_xml.photo[0]["id"] # if we're refreshing this data, then delete the Photo first... if refresh: try: p = Photo.objects.get(flickr_id=photo_id) p.delete() except ObjectDoesNotExist: pass sizes = self.getPhotoSizes(photo_id) # Removed urls = self.getPhotoSizeURLs(photo_id) exif_data = self.getExifInfo(photo_id) geo_data = self.getGeoLocation(photo_id) taken_date = datetime(*strptime(photo_xml.photo[0].dates[0]["taken"], "%Y-%m-%d %H:%M:%S")[:7]) upload_date = datetime.fromtimestamp(int(photo_xml.photo[0].dates[0]["posted"])) update_date = datetime.fromtimestamp(int(photo_xml.photo[0].dates[0]["lastupdate"])) proposed_slug = defaultfilters.slugify(photo_xml.photo[0].title[0].text.lower()) slug = get_unique_slug_for_photo(taken_date, proposed_slug) # Ignore tags if there are more chars than 255 tags, count = "", 0 for tag in [(t, len(t) + 1) for t in self._getXMLNodeTag(photo_xml).split()]: if 255 <= (count + tag[1] - 1): tags = tags[:-1] break if not tag[0].startswith("geo:"): # Exclude ugly geo-tags tags += u"%s " % tag[0] count += tag[1] try: original_secret = photo_xml.photo[0]["originalsecret"] except KeyError: original_secret = "" default_dict = { "flickr_id": photo_xml.photo[0]["id"], "owner": photo_xml.photo[0].owner[0]["username"], "owner_nsid": photo_xml.photo[0].owner[0]["nsid"], "title": photo_xml.photo[0].title[0].text, # TODO: Typography "slug": slug, "order": index, "description": photo_xml.photo[0].description[0].text, "taken_date": taken_date, "upload_date": upload_date, "update_date": update_date, "photopage_url": photo_xml.photo[0].urls[0].url[0].text, "farm": photo_xml.photo[0]["farm"], "server": photo_xml.photo[0]["server"], "secret": photo_xml.photo[0]["secret"], "original_secret": original_secret, "thumbnail_width": sizes["Thumbnail"]["width"], "thumbnail_height": sizes["Thumbnail"]["height"], "small_width": sizes["Small"]["width"], "small_height": sizes["Small"]["height"], "medium_width": sizes["Medium"]["width"], "medium_height": sizes["Medium"]["height"], "large_width": sizes["Large"]["width"], "large_height": sizes["Large"]["height"], "original_width": sizes["Original"]["width"] or 0, "original_height": sizes["Original"]["height"] or 0, # Removed 'square_url': urls['Square'], # Removed 'small_url': urls['Small'], # Removed 'medium_url': urls['Medium'], # Removed 'thumbnail_url': urls['Thumbnail'], "tags": tags, "license": photo_xml.photo[0]["license"], "geo_latitude": geo_data["latitude"], "geo_longitude": geo_data["longitude"], "geo_accuracy": geo_data["accuracy"], "geo_locality": geo_data["locality"], "geo_county": geo_data["county"], "geo_region": geo_data["region"], "geo_country": geo_data["country"], "exif_model": self.getExifKey(exif_data, "Model"), "exif_make": self.getExifKey(exif_data, "Make"), "exif_orientation": self.getExifKey(exif_data, "Orientation"), "exif_exposure": self.getExifKey(exif_data, "Exposure"), "exif_software": self.getExifKey(exif_data, "Software"), "exif_aperture": self.getExifKey(exif_data, "Aperture"), "exif_iso": self.getExifKey(exif_data, "ISO Speed"), "exif_metering_mode": self.getExifKey(exif_data, "Metering Mode"), "exif_flash": self.getExifKey(exif_data, "Flash"), "exif_focal_length": self.getExifKey(exif_data, "Focal Length"), "exif_color_space": self.getExifKey(exif_data, "Color Space"), } obj, created = Photo.objects.get_or_create(flickr_id=photo_xml.photo[0]["id"], defaults=default_dict) # update if something changed if obj.update_date < update_date or obj.order != index: # Never overwrite URL-relevant attributes default_dict["slug"] = obj.slug default_dict["taken_date"] = obj.taken_date updated_obj = Photo(pk=obj.pk, **default_dict) updated_obj.save() # Comments comments = self.getPhotoComments(obj.flickr_id) if comments is not None: for c in comments: c["photo"] = obj comment, created = PhotoComment.objects.get_or_create(flickr_id=c["flickr_id"], defaults=c) return obj
def _syncPhoto(self, photo_xml, refresh=False): """ Synchronize a flickr photo with the Django backend. Required Arguments photo_xml: A flickr photos in Flickrapi's REST XMLNode format """ if photo_xml.photo[0]['media'] != 'photo': # Ignore media like videos return None photo_id = photo_xml.photo[0]['id'] # if we're refreshing this data, then delete the Photo first... if refresh: try: p = Photo.objects.get(flickr_id = photo_id) p.delete() except ObjectDoesNotExist: pass sizes = self.getPhotoSizes(photo_id) # Removed urls = self.getPhotoSizeURLs(photo_id) exif_data = self.getExifInfo(photo_id) geo_data = self.getGeoLocation(photo_id) taken_granularity = photo_xml.photo[0].dates[0]['takengranularity'] taken = photo_xml.photo[0].dates[0]['taken'] if taken_granularity == '8': # When granularity is 'Circa...' then the month comes back as '00' # which makes for an invalid datetime, so we fix it. # eg, make '1956-00-01 00:00:00' # into '1956-01-01 00:00:00' taken = re.sub(r'^(?P<year>\d{,4}-)00(?P<remainder>.*?)$', r'\g<year>01\g<remainder>', taken) taken_date = datetime(*strptime(taken, "%Y-%m-%d %H:%M:%S")[:7]) upload_date = datetime.fromtimestamp(int(photo_xml.photo[0].dates[0]['posted'])) update_date = datetime.fromtimestamp(int(photo_xml.photo[0].dates[0]['lastupdate'])) proposed_slug = defaultfilters.slugify(photo_xml.photo[0].title[0].text.lower()) slug = get_unique_slug_for_photo(taken_date, proposed_slug) try: original_secret = photo_xml.photo[0]['originalsecret'] except KeyError: original_secret = '' default_dict = { 'flickr_id': photo_xml.photo[0]['id'], 'owner': photo_xml.photo[0].owner[0]['username'], 'owner_nsid': photo_xml.photo[0].owner[0]['nsid'], 'title': photo_xml.photo[0].title[0].text, # TODO: Typography 'slug': slug, 'description': photo_xml.photo[0].description[0].text, 'taken_date': taken_date, 'taken_granularity': taken_granularity, 'upload_date': upload_date, 'update_date': update_date, 'photopage_url': photo_xml.photo[0].urls[0].url[0].text, 'farm': photo_xml.photo[0]['farm'], 'server': photo_xml.photo[0]['server'], 'secret': photo_xml.photo[0]['secret'], 'original_secret': original_secret, 'thumbnail_width': sizes['Thumbnail']['width'], 'thumbnail_height': sizes['Thumbnail']['height'], 'small_width': sizes['Small']['width'], 'small_height': sizes['Small']['height'], 'medium_width': sizes['Medium']['width'], 'medium_height': sizes['Medium']['height'], 'large_width': sizes['Large']['width'], 'large_height': sizes['Large']['height'], 'original_width': sizes['Original']['width'] or 0, 'original_height': sizes['Original']['height'] or 0, # Removed 'square_url': urls['Square'], # Removed 'small_url': urls['Small'],sizes # Removed 'medium_url': urls['Medium'], # Removed 'thumbnail_url': urls['Thumbnail'], 'license': photo_xml.photo[0]['license'], 'geo_latitude': geo_data['latitude'], 'geo_longitude': geo_data['longitude'], 'geo_accuracy': geo_data['accuracy'], 'geo_locality': geo_data['locality'], 'geo_county': geo_data['county'], 'geo_region': geo_data['region'], 'geo_country': geo_data['country'], 'exif_model': self.getExifKey(exif_data, 'Model'), 'exif_make': self.getExifKey(exif_data, 'Make'), 'exif_orientation': self.getExifKey(exif_data, 'Orientation'), 'exif_exposure': self.getExifKey(exif_data, 'Exposure'), 'exif_software': self.getExifKey(exif_data, 'Software'), 'exif_aperture': self.getExifKey(exif_data, 'Aperture'), 'exif_iso': self.getExifKey(exif_data, 'ISO Speed'), 'exif_metering_mode': self.getExifKey(exif_data, 'Metering Mode'), 'exif_flash': self.getExifKey(exif_data, 'Flash'), 'exif_focal_length': self.getExifKey(exif_data, 'Focal Length'), 'exif_color_space': self.getExifKey(exif_data, 'Color Space'), } obj, created = Photo.objects.get_or_create( flickr_id = photo_xml.photo[0]['id'], defaults=default_dict) if created or obj.update_date < update_date: # This photo is new, or updated, so set the tags. self._syncPhotoTags(obj, photo_xml) # update if something changed if obj.update_date < update_date: # Never overwrite URL-relevant attributes default_dict['slug'] = obj.slug default_dict['taken_date'] = obj.taken_date updated_obj = Photo(pk=obj.pk, **default_dict) updated_obj.save() # Comments if self.sync_content['comments']: comments = self.getPhotoComments(obj.flickr_id) if comments is not None: for c in comments: c['photo'] = obj comment, created = PhotoComment.objects.get_or_create(flickr_id=c['flickr_id'], defaults=c) return obj