-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.py
75 lines (54 loc) · 1.92 KB
/
rss.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
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
import os
import feedparser
import urllib2
import alan.core.extension as extension
from alan.core.objects.separator import Header, Separator
from alan.core.objects.item import Item
from alan.core.objects.menu import Menu
from alan.core.objects.actions import ExecuteAction
CACHE = os.path.expanduser("~/.config/alan-rss/")
if not os.path.exists(CACHE): os.makedirs(CACHE)
class Extension(extension.Extension):
extensionName = "rss"
def sanitize_link(self, link):
""" Sanitizes the link. """
return link.replace(":","_").replace("/","_")
def generate(self):
""" Actually generate things. """
for _id in self.extension_settings["structure"].split(" "):
self.add(self.generate_menu(_id))
def generate_menu(self, _id):
""" Generates a submenu. """
count = 0
menu = Menu("rss_%s" % _id, label=self.extension_settings["%s_label" % _id], icon=self.IconPool.get_icon("gtk-directory"))
for feed in self.extension_settings["%s_feeds" % _id].split(" "):
try:
count += 1
feed = feedparser.parse(feed)
if "icon" in feed.feed:
try:
icon = os.path.join(CACHE, self.sanitize_link(feed.feed.link))
if not os.path.exists(icon):
with open(sanitized, "wb") as f:
i = urllib2.urlopen(feed.feed.icon)
f.write(i.read())
i.close()
except:
pass
else:
icon = self.IconPool.get_icon("applications-internet")
# Feed menu
feed_menu = Menu("rss_%s_%s" % (_id, count), label=feed.feed.title, icon=icon)
for new in feed.entries:
feed_menu.append(self.return_executable_item(new['title'], new['link'], icon=icon))
menu.append(feed_menu)
except:
pass
return menu
def return_executable_item(self, label, url, icon=None):
""" Returns an executable item. """
item = Item(label=label, icon=icon)
action = ExecuteAction("x-www-browser %s" % url)
item.append(action)
return item