2013-04-14 12:05:30 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# a at foo dot be - Alexandre Dulaunoy - http://www.foo.be/cgi-bin/wiki.pl/RssAny
|
|
|
|
#
|
|
|
|
# rsscluster.py is a simple script to cluster items from an rss feed based on a
|
|
|
|
# time interval (expressed in number of days). The maxitem is the
|
|
|
|
# number of item maximum after the clustering.
|
|
|
|
#
|
2024-02-11 09:49:20 +00:00
|
|
|
# an example use is for Mastodon where you can have a lot of toots during
|
2013-04-14 12:05:30 +00:00
|
|
|
# one day and you want to cluster them in one single item in RSS or in (X)HTML.
|
2024-02-11 09:49:20 +00:00
|
|
|
#
|
|
|
|
# example of use :
|
|
|
|
# python3 rsscluster.py --interval 5 --maxitem 20 "https://paperbay.org/@a.rss" >adulau.xml
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
import feedparser
|
2024-02-11 09:49:20 +00:00
|
|
|
import sys, os
|
2013-04-14 12:05:30 +00:00
|
|
|
import time
|
|
|
|
import datetime
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import hashlib
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2024-02-11 09:49:20 +00:00
|
|
|
# print sys.stdout.encoding
|
2013-04-14 12:05:30 +00:00
|
|
|
version = "0.2"
|
|
|
|
|
2024-02-11 09:49:20 +00:00
|
|
|
feedparser.USER_AGENT = "rsscluster.py " + version + " +http://www.foo.be/"
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def date_as_rfc(value):
|
2024-02-11 09:49:20 +00:00
|
|
|
return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(value))
|
|
|
|
|
|
|
|
|
|
|
|
def build_rss(myitem, maxitem):
|
|
|
|
|
|
|
|
RSSroot = ET.Element("rss", {"version": "2.0"})
|
|
|
|
RSSchannel = ET.SubElement(RSSroot, "channel")
|
|
|
|
|
|
|
|
ET.SubElement(RSSchannel, "title").text = (
|
|
|
|
"RSS cluster of " + str(url) + " per " + str(options.interval) + " days"
|
|
|
|
)
|
|
|
|
ET.SubElement(RSSchannel, "link").text = str(url)
|
|
|
|
ET.SubElement(RSSchannel, "description").text = (
|
|
|
|
"RSS cluster of " + str(url) + " per " + str(options.interval) + " days"
|
|
|
|
)
|
|
|
|
ET.SubElement(RSSchannel, "generator").text = "by rsscluster.py " + version
|
|
|
|
ET.SubElement(RSSchannel, "pubDate").text = date_as_rfc(time.time())
|
|
|
|
|
|
|
|
for bloodyitem in myitem[0:maxitem]:
|
|
|
|
|
|
|
|
RSSitem = ET.SubElement(RSSchannel, "item")
|
|
|
|
ET.SubElement(RSSitem, "title").text = (
|
|
|
|
"clustered data of "
|
|
|
|
+ date_as_rfc(float(bloodyitem[0]))
|
|
|
|
+ " for "
|
|
|
|
+ str(url)
|
|
|
|
)
|
|
|
|
ET.SubElement(RSSitem, "pubDate").text = date_as_rfc(float(bloodyitem[0]))
|
|
|
|
ET.SubElement(RSSitem, "description").text = bloodyitem[1]
|
2013-04-14 12:05:30 +00:00
|
|
|
h = hashlib.md5()
|
2024-02-11 09:49:20 +00:00
|
|
|
h.update(bloodyitem[1].encode("utf-8"))
|
|
|
|
ET.SubElement(RSSitem, "guid").text = h.hexdigest()
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 09:49:20 +00:00
|
|
|
RSSfeed = ET.ElementTree(RSSroot)
|
|
|
|
feed = ET.tostring(RSSroot)
|
|
|
|
return feed
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def complete_feed(myfeed):
|
|
|
|
|
2024-02-11 09:49:20 +00:00
|
|
|
myheader = '<?xml version="1.0"?>'
|
|
|
|
return myheader + str(myfeed)
|
|
|
|
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
def DaysInSec(val):
|
|
|
|
|
2024-02-11 09:49:20 +00:00
|
|
|
return int(val) * 24 * 60 * 60
|
|
|
|
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
usage = "usage: %prog [options] url"
|
|
|
|
parser = OptionParser(usage)
|
|
|
|
|
2024-02-11 09:49:20 +00:00
|
|
|
parser.add_option(
|
|
|
|
"-m",
|
|
|
|
"--maxitem",
|
|
|
|
dest="maxitem",
|
|
|
|
help="maximum item to list in the feed, default 200",
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"-i",
|
|
|
|
"--interval",
|
|
|
|
dest="interval",
|
|
|
|
help="time interval expressed in days, default 1 day",
|
|
|
|
)
|
|
|
|
|
|
|
|
# 2007-11-10 11:25:51
|
|
|
|
pattern = "%Y-%m-%d %H:%M:%S"
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2024-02-11 09:49:20 +00:00
|
|
|
if options.interval is None:
|
|
|
|
options.interval = 1
|
|
|
|
options.output = 1
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
if options.maxitem == None:
|
2024-02-11 09:49:20 +00:00
|
|
|
options.maxitem = 200
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
if len(args) != 1:
|
|
|
|
parser.print_help()
|
|
|
|
parser.error("incorrect number of arguments")
|
|
|
|
|
|
|
|
allitem = {}
|
|
|
|
url = args[0]
|
|
|
|
|
|
|
|
d = feedparser.parse(url)
|
|
|
|
|
2024-02-11 09:49:20 +00:00
|
|
|
if options.interval is None:
|
|
|
|
options.interval = 0
|
|
|
|
|
2013-04-14 12:05:30 +00:00
|
|
|
interval = DaysInSec(options.interval)
|
|
|
|
|
|
|
|
previousepoch = []
|
|
|
|
clusteredepoch = []
|
|
|
|
tcluster = []
|
|
|
|
|
|
|
|
for el in d.entries:
|
2024-02-11 09:49:20 +00:00
|
|
|
if 'modified_parsed' in el:
|
|
|
|
eldatetime = datetime.datetime.fromtimestamp(time.mktime(el.modified_parsed))
|
|
|
|
else:
|
|
|
|
eldatetime = datetime.datetime.fromtimestamp(time.mktime(el.published_parsed))
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 09:49:20 +00:00
|
|
|
elepoch = int(time.mktime(time.strptime(str(eldatetime), pattern)))
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
if len(previousepoch):
|
|
|
|
|
2024-02-11 09:49:20 +00:00
|
|
|
# print el.link, int(previousepoch[0])-int(elepoch), interval
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
if len(clusteredepoch):
|
|
|
|
value = clusteredepoch.pop()
|
|
|
|
else:
|
|
|
|
value = ""
|
2024-02-11 09:49:20 +00:00
|
|
|
if 'title' in el:
|
|
|
|
clusteredepoch.append(value + ' <a href="' + el.link + '">' + el.title + "</a>")
|
|
|
|
else:
|
|
|
|
clusteredepoch.append(value + ' <a href="' + el.link + '">' + el.summary + "</a>")
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 09:49:20 +00:00
|
|
|
if not ((int(previousepoch[0]) - int(elepoch)) < interval):
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
value = clusteredepoch.pop()
|
|
|
|
|
|
|
|
starttimetuple = datetime.datetime.fromtimestamp(previousepoch[0])
|
|
|
|
endttimetuple = datetime.datetime.fromtimestamp(previousepoch.pop())
|
2024-02-11 09:49:20 +00:00
|
|
|
clusteredepoch.append(
|
|
|
|
value
|
|
|
|
+ " from: "
|
|
|
|
+ str(starttimetuple.ctime())
|
|
|
|
+ " to: "
|
|
|
|
+ str(endttimetuple.ctime())
|
|
|
|
)
|
|
|
|
if previousepoch:
|
|
|
|
startdatelist = str(previousepoch[0]), str(
|
|
|
|
clusteredepoch[len(clusteredepoch) - 1]
|
|
|
|
)
|
|
|
|
tcluster.append(startdatelist)
|
|
|
|
del previousepoch[0 : len(previousepoch)]
|
|
|
|
del clusteredepoch[0 : len(clusteredepoch)]
|
2013-04-14 12:05:30 +00:00
|
|
|
else:
|
2024-02-11 09:49:20 +00:00
|
|
|
if 'title' in el:
|
|
|
|
clusteredepoch.append(' <a href="' + el.link + '">' + el.title + "</a>")
|
|
|
|
else:
|
|
|
|
clusteredepoch.append(' <a href="' + el.link + '">' + el.summary + "</a>")
|
|
|
|
|
2013-04-14 12:05:30 +00:00
|
|
|
previousepoch.append(elepoch)
|
|
|
|
|
|
|
|
# if last cluster list was not complete, we add the time period information.
|
|
|
|
if len(previousepoch):
|
|
|
|
value = clusteredepoch.pop()
|
|
|
|
starttimetuple = datetime.datetime.fromtimestamp(previousepoch[0])
|
|
|
|
endttimetuple = datetime.datetime.fromtimestamp(previousepoch.pop())
|
2024-02-11 09:49:20 +00:00
|
|
|
clusteredepoch.append(
|
|
|
|
value
|
|
|
|
+ " from: "
|
|
|
|
+ str(starttimetuple.ctime())
|
|
|
|
+ " to: "
|
|
|
|
+ str(endttimetuple.ctime())
|
|
|
|
)
|
|
|
|
del previousepoch[0 : len(previousepoch)]
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
tcluster.sort()
|
|
|
|
tcluster.reverse()
|
2024-02-11 09:49:20 +00:00
|
|
|
print(complete_feed(build_rss(tcluster, int(options.maxitem))))
|