-
Notifications
You must be signed in to change notification settings - Fork 1
/
feedgroup.py
89 lines (73 loc) · 2.89 KB
/
feedgroup.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
feed grouping and indexing for rawdog
Copyright 2006 Zephaniah E. Hull <[email protected]>
Written and tested with rawdog 2.6, unknown if it works with older versions.
Writes out articles grouped by the define_group setting on feeds,
with an 'Other' group for articles from feeds without the setting.
Also generates a header with links to each group with articles.
"""
import os
import rawdoglib.plugins
from rawdoglib.rawdog import DayWriter, write_ascii, fill_template
from StringIO import StringIO
def output_write_files(rawdog, config, articles, article_dates):
f_hdr = StringIO()
rawdoglib.plugins.call_hook("output_items_begin", rawdog, config, f_hdr)
remaining = articles[:]
groups = {}
other_count = 0
for url, feed in rawdog.feeds.items():
if (feed.args.has_key("define_group")):
group = feed.args["define_group"]
group = group.replace("_", " ")
if (groups.has_key(group)):
groups[group]["feeds"].append(feed)
else:
groups[group] = {"count" : 0, "feeds" : [feed]}
f = StringIO()
for (key, group) in groups.items():
dw = DayWriter(f, config)
f.write("<hr>\n")
f.write('<h1><a name="' + key + '">' + key + '</a></h1>\n')
for article in remaining[:]:
for feed in group["feeds"]:
if article.feed == feed.url:
if not rawdoglib.plugins.call_hook("output_items_heading", rawdog, config, f, article, article_dates[article]):
dw.time(article_dates[article])
rawdog.write_article(f, article, config)
remaining.remove(article)
group["count"] += 1
dw.close()
f.write("<hr>\n")
if remaining:
dw = DayWriter(f, config)
f.write('<h1><a name="Other">Other</a></h1>\n')
for article in remaining:
if not rawdoglib.plugins.call_hook("output_items_heading", rawdog, config, f, article, article_dates[article]):
dw.time(article_dates[article])
rawdog.write_article(f, article, config)
other_count += 1
f_hdr.write("<ul>\n")
for (key, group) in groups.items():
if group["count"]:
f_hdr.write('<li><a href="#%(key)s">%(key)s (%(count)d items)</a></li>\n' % {'key' : key, 'count' : group["count"]})
if other_count:
f_hdr.write('<li><a href="#%(key)s">%(key)s (%(count)d items)</a></li>\n' % {'key' : 'Other', 'count' : other_count})
f_hdr.write("</ul>\n")
rawdoglib.plugins.call_hook("output_items_end", rawdog, config, f)
bits = rawdog.get_main_template_bits(config)
bits["items"] = f_hdr.getvalue() + f.getvalue()
bits["num_items"] = str(len(rawdog.articles))
rawdoglib.plugins.call_hook("output_bits", rawdog, config, bits)
s = fill_template(rawdog.get_template(config), bits)
outputfile = config["outputfile"]
if outputfile == "-":
write_ascii(sys.stdout, s, config)
else:
config.log("Writing output file: ", outputfile)
f = open(outputfile + ".new", "w")
write_ascii(f, s, config)
f.close()
os.rename(outputfile + ".new", outputfile)
return False
rawdoglib.plugins.attach_hook("output_write_files", output_write_files)