Created
April 19, 2010 12:52
-
-
Save gauteh/371011 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 | |
# This syncs/gets the latest photos from your flickr photostream and saves them in | |
# a Django model (FlickrPhoto) | |
# Gaute Hope <[email protected]> (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 () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.