Created
March 3, 2011 21:13
-
-
Save jmhobbs/853586 to your computer and use it in GitHub Desktop.
This file contains 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 -*- | |
# Copyright (c) 2010 John Hobbs | |
# | |
# http://github.com/jmhobbs/googlecode-irc-bot | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
import feedparser | |
import shelve | |
import shared | |
class GoogleFeedReader: | |
_schema = '' | |
_name = 'base' | |
def __init__( self, project, *args ): | |
self.schema_keys = [project,] | |
self.schema_keys.extend( args ) | |
self.shelf = shelve.open( shared.SHELF + project + '.shelf' ) | |
try: | |
if dict != type( self.shelf[self._name] ): | |
raise KeyError ( 'not a dict' ) | |
except KeyError, e: | |
self.shelf[self._name] = {} | |
self.last_id = self.read( 'last_id' ) | |
# Prevent first run spamming | |
if None == self.read( 'first_run' ): | |
self.update() | |
self.write( 'first_run', False ) | |
def __del__ ( self ): | |
self.write( 'last_id', self.last_id ) | |
self.shelf.close() | |
def read ( self, key ): | |
""" | |
Get a value for a key from the shelf data store. | |
""" | |
if self.shelf[self._name].has_key( key ): | |
return self.shelf[self._name][key] | |
else: | |
return None | |
def write ( self, key, value ): | |
""" | |
Write a key/value pair to the shelf data store. | |
""" | |
self.shelf[self._name][key] = value | |
def update ( self ): | |
"""Returns list of new items.""" | |
# TODO: ETag & Last-Modified | |
for item in self.schema_keys: | |
print item | |
feed = feedparser.parse( self._schema % self.schema_keys ) | |
if feed.status != 200: | |
return () | |
return self.parse( feed ) | |
def parse ( self, feed ): | |
""" | |
Parses the content returned from update() | |
""" | |
added = [] | |
for entry in feed['entries']: | |
if entry['id'] == self.last_id: | |
break | |
added.append( entry ) | |
if 1 <= len( feed['entries'] ): | |
self.last_id = feed['entries'][0]['id'] | |
return added | |
def get_message ( self, entry ): | |
return '%s: %s' % ( shared.strip_tags( entry['title'] ), entry['link'] ) | |
class IssueUpdatesReader ( GoogleFeedReader ): | |
_schema = 'http://code.google.com/feeds/p/%s/issueupdates/basic' | |
_name = 'issues' | |
def get_message ( self, entry ): | |
return '[Issues] %s: %s' % ( shared.strip_tags( entry['title'] ), entry['link'] ) | |
class DownloadsReader ( GoogleFeedReader ): | |
_schema = 'http://code.google.com/feeds/p/%s/downloads/basic' | |
_name = 'downloads' | |
def get_message ( self, entry ): | |
return '[Downloads] %s: %s' % ( shared.strip_tags( entry['title'] ), entry['link'] ) | |
class WikiReader ( GoogleFeedReader ): | |
_schema = 'http://code.google.com/feeds/p/%s/%schanges/basic?path=/wiki/' | |
_name = 'wiki' | |
def get_message ( self, entry ): | |
return '[Wiki] %s: %s' % ( shared.strip_tags( entry['title'] ), entry['link'] ) | |
class CodeUpdatesReader ( GoogleFeedReader ): | |
_schema = 'http://code.google.com/feeds/p/%s/%schanges/basic' | |
_name = 'code' | |
def __init__( self, project, vcs ): | |
GoogleFeedReader.__init__( self, project ) | |
self.schema_keys.append( vcs ) | |
def get_message ( self, entry ): | |
return '[Code] %s: %s' % ( shared.strip_tags( entry['title'] ), entry['link'] ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment