feednotify / main.py

from datetime import datetime
import feedparser
import xmpp
import time
import logging

import settings

def fetch_feed():
    """
    Fetch and parse feed from habrahabr.ru. Return parsed publications.
    """

    feed = feedparser.parse('http://habrahabr.ru/rss/e7d875f8829bc7f3aac7125b1281ad2b/')
    entries = feed.entries
    for entry in entries:
        entry['date'] = datetime(*entry['updated_parsed'][:-2])
    return entries


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')


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 = 3
    for start in xrange(0, len(items), step):
        message = []
        for item in items[start:start + step]:
            message.append(u'%s (%s)' % (item['title'], item['link']))
        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.')


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.