allura
修订版 | 9fafe4f2f35d91013f9288beaade25e8492f5014 (tree) |
---|---|
时间 | 2012-05-17 07:18:18 |
作者 | bolkimen <bolkimen@yaho...> |
Commiter | Cory Johns |
ticket:48 add paster script
@@ -0,0 +1,86 @@ | ||
1 | +import feedparser | |
2 | +import html2text | |
3 | +from bson import ObjectId | |
4 | + | |
5 | +import base | |
6 | + | |
7 | +from pylons import c | |
8 | + | |
9 | +from allura import model as M | |
10 | +from forgeblog import model as BM | |
11 | +from forgeblog import version | |
12 | +from forgeblog.main import ForgeBlogApp | |
13 | +from allura.lib import exceptions | |
14 | + | |
15 | + | |
16 | +class RssFeedsCommand(base.Command): | |
17 | + summary = 'Rss feed client' | |
18 | + parser = base.Command.standard_parser(verbose=True) | |
19 | + parser.add_option('-a', '--appid', dest='appid', default='', | |
20 | + help='application id') | |
21 | + parser.add_option('-u', '--username', dest='username', default='root', | |
22 | + help='poster username') | |
23 | + | |
24 | + def command(self): | |
25 | + self.basic_setup() | |
26 | + | |
27 | + user = M.User.query.get(username=self.options.username) | |
28 | + c.user = user | |
29 | + | |
30 | + self.prepare_feeds() | |
31 | + for appid in self.feed_dict: | |
32 | + for feed_url in self.feed_dict[appid]: | |
33 | + self.process_feed(appid, feed_url) | |
34 | + | |
35 | + def prepare_feeds(self): | |
36 | + feed_dict = {} | |
37 | + if self.options.appid != '': | |
38 | + gl_app = BM.Globals.query.get(app_config_id=ObjectId(self.options.appid)) | |
39 | + if not gl_app: | |
40 | + raise exceptions.NoSuchGlobalsError("The globals %s " \ | |
41 | + "could not be found in the database" % self.options.appid) | |
42 | + if len(gl_app.external_feeds) > 0: | |
43 | + feed_dict[gl_app.app_config_id] = gl_app.external_feeds | |
44 | + else: | |
45 | + for gl_app in BM.Globals.query.find().all(): | |
46 | + if len(gl_app.external_feeds) > 0: | |
47 | + feed_dict[gl_app.app_config_id] = gl_app.external_feeds | |
48 | + self.feed_dict = feed_dict | |
49 | + | |
50 | + def process_feed(self, appid, feed_url): | |
51 | + appconf = M.AppConfig.query.get(_id=appid) | |
52 | + if not appconf: | |
53 | + return | |
54 | + | |
55 | + c.project = appconf.project | |
56 | + app = ForgeBlogApp(c.project, appconf) | |
57 | + c.app = app | |
58 | + | |
59 | + base.log.info("Get feed: %s" % feed_url) | |
60 | + f = feedparser.parse(feed_url) | |
61 | + if f.bozo: | |
62 | + base.log.exception("%s: %s" % (feed_url, f.bozo_exception)) | |
63 | + return | |
64 | + for e in f.entries: | |
65 | + title = e.title | |
66 | + if 'content' in e: | |
67 | + content = u'' | |
68 | + for ct in e.content: | |
69 | + if ct.type != 'text/html': | |
70 | + content = u"%s<p>%s</p>" % (content, ct.value) | |
71 | + else: | |
72 | + content = content + ct.value | |
73 | + else: | |
74 | + content = e.summary | |
75 | + | |
76 | + content = u'%s <a href="%s">link</a>' % (content, e.link) | |
77 | + content = html2text.html2text(content, e.link) | |
78 | + | |
79 | + post = BM.BlogPost(title=title, text=content, app_config_id=appid, | |
80 | + tool_version={'blog': version.__version__}, | |
81 | + state='draft') | |
82 | + post.neighborhood_id=c.project.neighborhood_id | |
83 | + post.make_slug() | |
84 | + post.commit() | |
85 | + # TODO remove me | |
86 | + #print "TITLE: %s\nCONTENT: %s\n\n" % (title, content) |
@@ -4,6 +4,7 @@ class ProjectOverlimitError(ForgeError): pass | ||
4 | 4 | class ToolError(ForgeError): pass |
5 | 5 | class NoSuchProjectError(ForgeError): pass |
6 | 6 | class NoSuchNeighborhoodError(ForgeError): pass |
7 | +class NoSuchGlobalsError(ForgeError): pass | |
7 | 8 | class MailError(ForgeError): pass |
8 | 9 | class AddressException(MailError): pass |
9 | 10 | class NoSuchNBFeatureError(ForgeError): pass |
@@ -113,6 +113,7 @@ setup( | ||
113 | 113 | create-neighborhood = allura.command:CreateNeighborhoodCommand |
114 | 114 | create-trove-categories = allura.command:CreateTroveCategoriesCommand |
115 | 115 | set-neighborhood-features = allura.command:SetNeighborhoodFeaturesCommand |
116 | + pull-rss-feeds = allura.command.rssfeeds:RssFeedsCommand | |
116 | 117 | |
117 | 118 | [easy_widgets.resources] |
118 | 119 | ew_resources=allura.config.resources:register_ew_resources |
@@ -15,6 +15,7 @@ FormEncode==1.2.4 | ||
15 | 15 | # dep of Creoleparser |
16 | 16 | Genshi==0.6 |
17 | 17 | # dep of oauth2 |
18 | +html2text==3.200.3 | |
18 | 19 | httplib2==0.7.4 |
19 | 20 | iso8601==0.1.4 |
20 | 21 | Jinja2==2.6 |