django-activitysync / activitysync / providers / redditprovider.py

from django.conf import settings
from activitysync.providers import ActivityProvider, ActivityInfo

import time
import datetime
import feedparser

class RedditProvider(ActivityProvider):
    """
    Provider for accessing liked Reddit items for one user.
    """
    
    def get_activity(self):
        item_list = []

        print 'Attempting to parse Reddit feed'
        username = settings.ACTIVITYSYNC_SETTINGS['REDDIT_USERNAME']
        parsed_feed = feedparser.parse("http://www.reddit.com/user/%s/liked/.rss" % username)

        for entry in parsed_feed.entries:
            title = entry.title.encode(parsed_feed.encoding, "xmlcharrefreplace")
            guid = entry.get("id", entry.link).encode(parsed_feed.encoding, "xmlcharrefreplace")
            link = entry.link.encode(parsed_feed.encoding, "xmlcharrefreplace")

            if not guid:
                guid = link

            if entry.has_key('author'):
                author = entry.author.encode(parsed_feed.encoding, "xmlcharrefreplace")
            else:
                author = u''

            try:
                if entry.has_key('published_parsed'):
                    date_published = datetime.datetime.fromtimestamp(time.mktime(entry.published_parsed) - time.timezone)
                elif entry.has_key('updated_parsed'):
                    date_published = datetime.datetime.fromtimestamp(time.mktime(entry.updated_parsed) - time.timezone)
                elif entry.has_key('modified_parsed'):
                    date_published = datetime.datetime.fromtimestamp(time.mktime(entry.modified_parsed) - time.timezone)
                else:
                    date_published = datetime.datetime.now()
            except TypeError:
                date_published = datetime.datetime.now()
                    
            activity_info = ActivityInfo(title=title, link=link, pub_date=date_published, guid=guid, username=username, author=author)
            item_list.append(activity_info)

        return item_list


    def name(self):
        return 'Reddit'

    def prefix(self):
        return 'Liked'

    def link(self):
        return ('http://www.reddit.com/user/%s/' %
            settings.ACTIVITYSYNC_SETTINGS['REDDIT_USERNAME'])

    def sourceid(self):
        return 'reddit'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.