-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsspush.py
65 lines (48 loc) · 2.13 KB
/
rsspush.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
import os.path
import urllib2
from BeautifulSoup import BeautifulSoup
from errbot import BotPlugin, botcmd, re_botcmd
from feedgen.feed import FeedGenerator
class RssPush(BotPlugin):
"""RssPush plugin for Err"""
@re_botcmd(pattern=r"^http(|$)", prefixed=False, flags=re.IGNORECASE)
def listen_for_urls(self, msg, match):
url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', str(msg))
p = re.compile('\/(.*)')
user = re.search(p, str(msg.getFrom())).group()[1:]
if len(url) == 1:
url = str(url[0])
filename = '/mnt/extern1/SYSTEM/www/foorss/' + user + '.xml'
fg = FeedGenerator()
# Some pages block urllib2 so we need a fake user agent
header = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
req = urllib2.Request(url, headers=header)
try:
soup = BeautifulSoup(urllib2.urlopen(req))
except urllib2.HTTPError, e:
print e.fp.read()
yield "Error while parsing the website..."
if os.path.isfile(filename):
fg.from_rss(filename)
else:
fg.id(user)
fg.title('Some Testfeed')
fg.link( href='http://nix.da', rel='alternate' )
fg.description('This is a cool feed!')
if soup.title != None:
title = soup.title.string
else:
title = url
fe = fg.add_entry()
fe.id(url)
fe.title(title)
fe.description('Description')
fe.link([{'href': url}])
fg.rss_file(filename)
yield title + ' from ' + user + ' (rss updated)'