All pastes #480403 Raw Edit

Stuff

public text v1 · immutable
·published 2007-05-10 08:10 UTC
rendered paste body
#!/usr/bin/env python

"""This module is used to parse the Google Picasa Web API feeds"""

import urllib2
import datetime
import xml.dom.minidom as minidom

class Object:
    def __init__ (self):
        self.title = ''
        self.summary = ''
        self.last_updated = ''

    def get_title (self, xml_node):
        title = xml_node.getElementsByTagName('title')
        if title and title[0].childNodes:
            return title[0].childNodes[0].nodeValue
        # raise exception here
        return ''

    def get_summary (self, xml_node):
        summary = xml_node.getElementsByTagName('summary')
        if summary and summary[0].childNodes:
            return summary[0].childNodes[0].nodeValue
        return ''

    def get_last_updated (self, xml_node):
        updated = xml_node.getElementsByTagName('updated')[0].childNodes[0].nodeValue
        date = updated[:10].split('-')
        time = updated[11:19].split(':')
        return datetime.datetime (*map(int, (date[0], date[1], date[2],
                                             time[0], time[1], time[2])))
        
    def set_values (self, xml_node):
        self.last_updated = self.get_last_updated(xml_node)
        self.title = self.get_title(xml_node)
        self.summary = self.get_summary(xml_node)
        
class Photo(Object):
    def __init__ (self, xml_node):
        self.image = ''
        self.width = 0
        self.height = 0
        self.size = 0
        self.url = ''
        

class Album(Object):
    url = 'http://picasaweb.google.com/data/feed/api/user/'
    def __init__ (self, xml_node, username):
        self.keywords = []
        self.list_of_photos = []
        self.set_values(xml_node)
        self.rights = self.get_rights(xml_node)
        self.numphotos = self.get_numphotos(xml_node)
        self.owner = username
        self.http_handler = urllib2.HTTPHandler()

    def get_rights(self, xml_node):
        rights = xml_node.getElementsByTagName('rights')
        if rights:
            return rights[0].nodeValue
        return 'public'

    def get_numphotos(self, xml_node):
        num = xml_node.getElementsByTagName('numphotos')
        if num:
            return num[0].nodeValue
        return 0
    
    def populate_album(self):
        """Populate the album with photos"""
        url = self.url + self.owner + '/album/' + self.title + '?kind=photo'
        req = urllib2.Request (url)
        res = self.http_handler.http_open(req).read()
        xml_file = minidom.parseString(response)
        for entry in xml_files.getElementsByTagName('entry'):
            self.list_of_photos.append(Photo(entry))
            
        
class Users(Object):
    url = 'http://picasaweb.google.com/data/feed/api/user/'
    def __init__(self, username=''):
        self.subtitle = ''
        self.total_results = 0
        self.itemsPerPage = 0
        self.http_handler = urllib2.HTTPHandler()
        self.albums = []
        self.username = username
        
    def login(self, username, password):
        """do login stuff here"""
        pass

    def populate_user(self):
        url = self.url + self.username + '?kind=album'
        req = urllib2.Request(url)
        response = self.http_handler.http_open(req).read()
        xml_file = minidom.parseString(response)
        self.set_values(xml_file)
        for entry in xml_file.getElementsByTagName('entry'):
            self.albums.append(Album(entry, self.username))
            
        
class PicasaWeb:
    def __init__ (self):
        self.list_of_users = []

    def get_user(self, user):
        self.list_of_users.append(Users(user))