Paste Description for XMPP Atomstream
Extend the Atomsteam.py script to publish ATOM feeds into XHTML-IM messages.
To fix, to enhance, to optimize, etc...
- XMPP Atomstream
- Wednesday, September 3rd, 2008 at 12:38:13pm MDT
- # coding: utf-8
- """
- Extending Atomstream.py with XMPP support
- author : xmpp:kael@jabber.org
- """
- """
- atomstream: a more efficient way to get feed updates
- http://www.aaronsw.com/2002/atomstream/
- >>> import atomstream
- >>> for update in atomstream.connect():
- ... print "'%s...'" % update[:20]
- >>> import feedparser
- >>> from itertools import imap
- >>> for update in imap(feedparser.parse, atomstream.connect()):
- print update.entries[0].title.encode('utf8')
- Валєрій Нуґатов / З циклу "ФРІЛАНС"
- Ну и зайчик...
- Squee.
- линейная графика
- Как я боролась с павианами
- And you thought I was obsessed....
- 一个比利时的 dapper 源
- Видеоподборка
- Synchronisma Promotional Video... and More
- ^C
- """
- __author__ = "Aaron Swartz <http://www.aaronsw.com/>"
- __version__ = "1.1"
- __license__ = "public domain"
- import urllib, logging
- from xml.sax.handler import EntityResolver, DTDHandler, ContentHandler, ErrorHandler
- from xml.sax.saxutils import XMLGenerator
- from xml.sax import make_parser
- from StringIO import StringIO
- import feedparser
- from itertools import imap
- import xmpp
- JID = JID
- PWD = PWD
- options = {
- 'JID': JID,
- 'Password': PWD,
- }
- class Streamer(EntityResolver, DTDHandler, ContentHandler, ErrorHandler):
- def __init__(self):
- self.time = 0
- self.reset()
- def reset(self):
- self.mode = None
- self.chr = []
- self.results = []
- def startElement(self, name, attr):
- if name == 'atomStream':
- self.mode = 'atomStream'
- elif self.mode == 'atomStream' and name == 'time':
- self.mode = 'time'
- self.chr = []
- elif name == 'sorryTooSlow':
- logging.warn('too slow, you missed'+dict(attr)['youMissed'])
- elif name == 'feed':
- self.mode = 'feed'
- self.feeddata = StringIO()
- self.feedster = XMLGenerator(self.feeddata)
- if self.mode == 'feed':
- self.feedster.startElement(name, attr)
- def characters(self, ch):
- if self.mode == 'feed':
- self.feedster.characters(ch)
- else:
- self.chr.append(ch)
- def endElement(self, name):
- if self.mode == 'feed':
- self.feedster.endElement(name)
- if self.mode == 'time' and name == 'time':
- self.mode = 'atomStream'
- self.time = int(''.join(self.chr))
- self.chr = []
- elif self.mode == 'feed' and name == 'feed':
- self.results.append(self.feeddata.getvalue())
- self.mode = 'atomStream'
- class RunProcess:
- def __init__(self, conn):
- self.conn = conn
- def connect(self, feed="http://updates.sixapart.com/atom-stream.xml"):
- print "start"
- s = Streamer()
- p = make_parser()
- p.setContentHandler(s)
- while 1:
- print "yes"
- try:
- d = urllib.urlopen(feed+'?since='+str(s.time))
- for line in iter(d.readline, None):
- p.feed(line)
- while s.results: yield s.results.pop(0)
- except:
- raise
- s.reset()
- def get_stream(self):
- for update in imap(feedparser.parse, self.connect()):
- title = update.entries[0].title
- content = update.entries[0].content[0].value
- self.send_xhtml(title,content)
- def send_xhtml(self, title, content):
- try :
- message=xmpp.protocol.Message(to=JID, subject=title.encode('utf-8'), typ='chat', body='foo')
- payload=xmpp.simplexml.XML2Node("<body xmlns='http://www.w3.org/1999/xhtml'><center><h2>%s</h2></center>%s</body>" % (title.encode('utf-8'), content.encode('utf-8')))
- message.addChild('html', {}, [payload], xmpp.NS_XHTML_IM)
- self.conn.send(message)
- except:
- pass
- class XmppConn(RunProcess):
- def __init__(self, JID, Password):
- jid = xmpp.JID(JID)
- self.conn = xmpp.Client(jid.getDomain(), debug=['always', 'nodebuilder'])
- result = self.conn.connect()
- if result is None:
- raise ConnectionError
- result = self.conn.auth(jid.getNode(), Password,'SixApart')
- if result is None:
- raise AuthorizationError
- self.conn.sendInitPresence(requestRoster=1)
- self.bot = RunProcess(self.conn)
- self.bot.get_stream()
- def loop(self):
- try:
- while self.conn.Process(1):
- self.conn.send(' ')
- pass
- except KeyboardInterrupt:
- return 0
- return 1
- bot = XmppConn(**options)
- bot.loop()
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.