morss/morss

58 lines
1.0 KiB
Plaintext
Raw Normal View History

2013-02-25 14:50:32 +00:00
#! /usr/bin/env python2.7
import sys
2013-02-25 17:01:59 +00:00
import os
2013-02-25 14:50:32 +00:00
from lxml import etree
import urllib2
2013-02-25 17:01:59 +00:00
from os.path import expanduser
def log(str):
if (len(sys.argv) == 3):
print str
2013-02-25 14:50:32 +00:00
if len(sys.argv) < 2:
print "argument please"
sys.exit(1)
node = sys.argv[1]
xml = sys.stdin.read()
rss = etree.fromstring(xml)
items = rss.xpath('//item')
2013-02-25 17:01:59 +00:00
cache = expanduser("~") + "/.cache/morss"
if not os.path.exists(cache):
os.makedirs(cache)
2013-02-25 14:50:32 +00:00
for item in items:
title = item.findtext('title')
link = item.findtext('link')
desc = item.xpath('description')[0]
2013-02-25 17:01:59 +00:00
log(title)
log(link)
2013-02-25 14:50:32 +00:00
2013-02-25 17:01:59 +00:00
cached = cache + "/" + str(hash(link))
if os.path.exists(cached):
log("cached")
desc.text = open(cached, 'r').read()
else:
try:
data = urllib2.urlopen(link).read()
html = etree.HTML(data)
match = html.xpath(node)
2013-02-25 14:50:32 +00:00
2013-02-25 17:01:59 +00:00
if len(match):
text = etree.tostring(match[0])
log("ok txt")
desc.text = text
open(cached, 'w').write(text)
else:
log("no match")
2013-02-25 14:50:32 +00:00
2013-02-25 17:01:59 +00:00
except urllib2.HTTPError, error:
log("error")
2013-02-25 14:50:32 +00:00
if len(sys.argv) == 2:
print etree.tostring(rss)