-
Notifications
You must be signed in to change notification settings - Fork 0
/
rssmerge.py
140 lines (121 loc) · 4.27 KB
/
rssmerge.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# a at foo dot be - Alexandre Dulaunoy - https://git.foo.be/adulau/rss-tools
#
# rssmerge.py is a simple script designed to aggregate RSS feeds and merge them in reverse chronological order.
# It outputs the merged content in text, HTML, or Markdown format. This tool is useful for tracking recent events
# from various feeds and publishing them on your website.
#
# Sample usage:
#
# python3 rssmerge.py "https://git.foo.be/adulau.rss" "http://api.flickr.com/services/feeds/photos_public.gne?id=31797858@N00&lang=en-us&format=atom"
# "https://github.com/adulau.atom" -o markdown --maxitem 20
import feedparser
import sys, os
import time
import datetime
import hashlib
from optparse import OptionParser
import html
from bs4 import BeautifulSoup
from urllib.parse import urlparse
feedparser.USER_AGENT = "rssmerge.py +https://github.com/adulau/rss-tools"
def RenderMerge(itemlist, output="text"):
i = 0
if output == "text":
for item in itemlist:
i = i + 1
# Keep consistent datetime representation if not use allitem[item[1]]['updated']
link = allitem[item[1]]["link"]
title = html.escape(allitem[item[1]]["title"])
timestamp = datetime.datetime.fromtimestamp(
allitem[item[1]]["epoch"]
).ctime()
print(f'{i}:{title}:{timestamp}:{link}')
if i == int(options.maxitem):
break
if output == "phtml":
print("<ul>")
for item in itemlist:
i = i + 1
# Keep consistent datetime representation if not use allitem[item[1]]['updated']
link = allitem[item[1]]["link"]
title = html.escape(allitem[item[1]]["title"])
timestamp = datetime.datetime.fromtimestamp(
allitem[item[1]]["epoch"]
).ctime()
print(f'<li><a href="{link}"> {title}</a> --- (<i>{timestamp}</i>)</li>')
if i == int(options.maxitem):
break
print("</ul>")
if output == "markdown":
for item in itemlist:
i = i + 1
title = html.escape(allitem[item[1]]["title"])
link = allitem[item[1]]["link"]
timestamp = datetime.datetime.fromtimestamp(
allitem[item[1]]["epoch"]
).ctime()
domain = urlparse(allitem[item[1]]["link"]).netloc
print(f'- {domain} [{title}]({link}) @{timestamp}')
if i == int(options.maxitem):
break
usage = "usage: %prog [options] url"
parser = OptionParser(usage)
parser.add_option(
"-m",
"--maxitem",
dest="maxitem",
default=200,
help="maximum item to list in the feed, default 200",
)
parser.add_option(
"-s",
"--summarysize",
dest="summarysize",
default=60,
help="maximum size of the summary if a title is not present",
)
parser.add_option(
"-o",
"--output",
dest="output",
default="text",
help="output format (text, phtml, markdown), default text",
)
# 2007-11-10 11:25:51
pattern = "%Y-%m-%d %H:%M:%S"
(options, args) = parser.parse_args()
allitem = {}
for url in args:
d = feedparser.parse(url)
for el in d.entries:
if "modified_parsed" in el:
eldatetime = datetime.datetime.fromtimestamp(
time.mktime(el.modified_parsed)
)
else:
eldatetime = datetime.datetime.fromtimestamp(
time.mktime(el.published_parsed)
)
elepoch = int(time.mktime(time.strptime(str(eldatetime), pattern)))
h = hashlib.md5()
h.update(el.link.encode("utf-8"))
linkkey = h.hexdigest()
allitem[linkkey] = {}
allitem[linkkey]["link"] = str(el.link)
allitem[linkkey]["epoch"] = int(elepoch)
allitem[linkkey]["updated"] = el.updated
if "title" in el:
allitem[linkkey]["title"] = html.unescape(el.title)
else:
cleantext = BeautifulSoup(el.summary, "lxml").text
allitem[linkkey]["title"] = cleantext[: options.summarysize]
itemlist = []
for something in list(allitem.keys()):
epochkeytuple = (allitem[something]["epoch"], something)
itemlist.append(epochkeytuple)
itemlist.sort()
itemlist.reverse()
RenderMerge(itemlist, options.output)