feednotify / main.py

#!/usr/bin/env python
from datetime import datetime
import feedparser
import xmpp
import time
import logging
from urlparse import urlsplit

import settings
import shorturl

def fetch_feed():
    """
    Fetch and parse feeds.
    """

    result = []

    for url in settings.FEEDS:
        feed = feedparser.parse(url)
        entries = feed.entries[:settings.LIMIT_PER_FEED]
        result.extend(entries)

    return result


def send_message(items):
    """
    Compose and send message to XMPP conference.
    """

    client = setup_xmpp_client()
    client.send(xmpp.Presence(to='%s/%s' % (settings.NOTIFY_ROOM, settings.XMPP_NICK)))

    step = settings.LIMIT_PER_MESSAGE
    for start in xrange(0, len(items), step):
        message = []
        for item in items[start:start + step]:
            if settings.USE_SHORT_URL:
                link = shorturl.get(item['link'])
                host = urlsplit(item['link']).hostname
                line = u'%s %s (%s)' % (item['title'], link, host)
            else:
                link = item['link']
                line = u'%s %s' % (item['title'], link)
            message.append(line)
        message = '\n'.join(message)
        client.send(xmpp.Message(to=settings.NOTIFY_ROOM, typ='groupchat', body=message))
        time.sleep(settings.MESSAGE_DELAY)


def setup_xmpp_client():
    """
    Connect to xmpp server and authenticate.
    """

    jid = xmpp.protocol.JID(settings.XMPP_USER)
    client = xmpp.Client(jid.getDomain(), debug=[])
    if client.connect():
        if client.auth(jid.getNode(), settings.XMPP_PASSWORD):
            return client
        else:
            raise Exception('Could not authorize.')
    else:
        raise Exception('Could not connect.')


def main():
    entries = fetch_feed()
    new_items = []

    try:
        gids = [x.strip() for x in file('gids.txt')]
    except IOError:
        gids = []

    for entry in entries:
        if not entry['id'] in gids:
            new_items.append(entry)
            gids.append(entry['id'])

    if new_items:
        logging.debug('Found %d new publications' % (len(new_items)))
        file('gids.txt', 'w').write('\n'.join(gids))
        send_message(new_items)

    logging.debug('Done')


if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    main()
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.