Anonymous avatar Anonymous committed 7195876

initial commit; works for me

Comments (0)

Files changed (1)

+#!/usr/bin/env python
+# vim: set fileencoding=utf-8 ts=4 sw=4 expandtab :
+
+# A script to crosspost any RSS/Atom feed to Jabber.
+# (c) jetxee@gmail.com 2009
+# LICENSE: BSD-like, 2-clause
+
+MAXLENGTH=2048
+SENTFILE="~/.rss2xmpp.sent"
+
+# jabber settings in ~/.rss2xmpp
+samplerc="""jid = mylogin@gmail.com
+login = mylogin
+pass = mypassword
+server = talk.google.com
+port = 5222"""
+
+from sys import argv, exit
+from os.path import basename, expanduser
+from re import sub
+from time import sleep
+import pickle
+
+# using the [Universal Feed Parser](http://www.feedparser.org/), BSD2
+from feedparser import parse
+
+# using [THE ASCIINATOR](http://www.aaronsw.com/2002/html2text/), GPLv2
+from html2text import html2text
+
+# using [xmpppy](http://xmpppy.sf.net/), GPLv3
+import xmpp
+
+def usage():
+    print 'usage:', basename(argv[0]), 'feed', 'tojid'
+
+def readrc():
+    """Read XMPP settings from ~/.rss2xmpp."""
+    rcfile=expanduser("~/.rss2xmpp")
+    try:
+        lines=file(rcfile).readlines()
+        lines=[sub('#.*','',l).strip() for l in lines]
+        lines=[l for l in lines if len(l) > 0]
+        settings=[l.split('=',1) for l in lines]
+        settings=[ [ v.strip() for v in s ] for s in settings ]
+    except IOError, e:
+        file(rcfile,'w').write(samplerc)
+        raise e
+    return dict(settings)
+
+def get_sent_ids():
+    sentfile=expanduser(SENTFILE)
+    try:
+        sentids=pickle.load(file(sentfile))
+    except:
+        sentids=[]
+    return sentids
+
+def rememberid(entryid):
+    """Save crossposted feed entry ID in file."""
+    sentids=get_sent_ids()
+    sentids.append(entryid)
+    pickle.dump(sentids,file(expanduser(SENTFILE),'w'))
+
+def issent(entryid):
+    """Check if entryid is sent already."""
+    sentids=get_sent_ids()
+    return entryid in sentids
+
+def connect_jid_client():
+    try:
+        cfg=readrc()
+    except IOError:
+        print 'please edit XMPP account settings in ~/.rss2xmpp'
+        exit(1)
+    jid=xmpp.protocol.JID(cfg['jid'])
+    cl=xmpp.Client(jid.getDomain(),debug=[])
+    if cfg.has_key('server'):
+        con=cl.connect(server=(cfg['server'],cfg['port']))
+    else:
+        con=cl.connect(jid.getDomain())
+    if not con:
+        print 'cannot connect'
+        exit(1)
+    auth=cl.auth(jid.getNode(),cfg['pass'],resource=jid.getResource())
+    if not auth:
+        print 'cannot authenticate'
+        exit(1)
+    return cl
+
+def send_message(client,tojid,message):
+    client.send(xmpp.protocol.Message(tojid,message))
+
+if __name__ == '__main__':
+    if len(argv) != 3:
+        usage()
+        exit(1)
+
+    feed  = argv[1]
+    tojid = argv[2]
+
+    cl=connect_jid_client()
+
+    d=parse(feed)
+    for i in reversed(d['entries']):
+        if not issent(i.id):
+            title,text,link,msg=i.title,html2text(i.summary),i.link,''
+            if title:
+                msg += title + '\n'
+            msg += text[:MAXLENGTH] + '\n' + link
+            send_message(cl,tojid,msg)
+            rememberid(i.id)
+            sleep(5)
+
+    cl.disconnect()
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.