Skip to content

Instantly share code, notes, and snippets.

@gauteh
Created April 19, 2010 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauteh/371011 to your computer and use it in GitHub Desktop.
Save gauteh/371011 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8
# This syncs/gets the latest photos from your flickr photostream and saves them in
# a Django model (FlickrPhoto)
# Gaute Hope <eg@gaute.vetsj.com> (c) 2010
# Licenced under Creative Commons v3.0
# http://creativecommons.org/licenses/by/3.0/
# this goes in models.py:
from django.db import models
from django.db.models import ObjectDoesNotExist
from datetime import datetime
class FlickrPhoto (models.Model):
title = models.CharField (max_length = 255, blank = True)
link = models.CharField (max_length = 255, blank = False)
description = models.TextField (blank = True)
pubDate = models.CharField (max_length = 255, blank = True)
img = models.CharField (max_length = 255, blank = False)
thumbnail = models.CharField (max_length = 255, blank = False)
lastsynced = models.DateTimeField (auto_now_add = True, auto_now = True, blank = False, default = datetime(1990, 12, 24))
def parse (self, rss):
# Parse rssitem
if rss:
ns = '{http://search.yahoo.com/mrss/}'
self.title = rss.find ('title').text
self.link = rss.find ('link').text
self.description = rss.find ('description').text
self.pubDate = rss.find ('pubDate').text
self.img = rss.find (ns + 'content').attrib['url']
self.thumbnail = rss.find (ns + 'thumbnail').attrib['url']
self.save ()
# this goes where you want to sync it..
import urllib
from xml.etree import ElementTree
def sync_photos ():
# get latest public flickr photos
flickrfeed = 'http://api.flickr.com/services/feeds/photos_public.gne?id=66016172@N00&lang=en-us&format=rss_200'
feed = None
try:
feed = urllib.urlopen (flickrfeed)
tree = ElementTree.parse (feed)
# clear old photos
photos = FlickrPhoto.objects.all ()
for p in photos: p.delete ()
for item in tree.findall ('channel/item'):
p = FlickrPhoto ()
p.parse (item)
except Exception as ex:
# failed to get/parse rss feed.. not much to doo
pass
finally:
if feed: feed.close ()
@gauteh
Copy link
Author

gauteh commented Apr 22, 2010

I normally check for last sync time on the first FlickrPhoto, then forks sync to a different process if I want to sync.. this also make python free the memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment