2013-04-14 12:05:30 +00:00
|
|
|
# rssdir.py
|
|
|
|
# a at foo dot be - Alexandre Dulaunoy - http://www.foo.be/cgi-bin/wiki.pl/RssAny
|
|
|
|
#
|
|
|
|
# rssdir is a simply-and-dirty script to rssify any directory on the filesystem.
|
2024-02-11 12:15:23 +00:00
|
|
|
#
|
2013-04-14 12:05:30 +00:00
|
|
|
# an example of use on the current directory :
|
|
|
|
#
|
2024-02-11 12:15:23 +00:00
|
|
|
# python3 /usr/local/bin/rssdir.py --prefix http://www.foo.be/cours/ . >rss.xml
|
2013-04-14 12:05:30 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
import os, fnmatch
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2024-02-11 12:15:23 +00:00
|
|
|
version = "0.2"
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
# recursive list file function from the ASPN cookbook
|
2024-02-11 12:15:23 +00:00
|
|
|
def all_files(root, patterns="*", single_level=False, yield_folders=False):
|
|
|
|
patterns = patterns.split(";")
|
|
|
|
for path, subdirs, files in os.walk(root):
|
|
|
|
if yield_folders:
|
|
|
|
files.extend(subdirs)
|
|
|
|
files.sort()
|
|
|
|
for name in files:
|
|
|
|
for pattern in patterns:
|
|
|
|
if fnmatch.fnmatch(name, pattern):
|
|
|
|
yield os.path.join(path, name)
|
|
|
|
break
|
|
|
|
if single_level:
|
|
|
|
break
|
|
|
|
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
def date_files(filelist):
|
2024-02-11 12:15:23 +00:00
|
|
|
date_filename_list = []
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 12:15:23 +00:00
|
|
|
for filename in filelist:
|
|
|
|
stats = os.stat(filename)
|
|
|
|
last_update = stats[8]
|
|
|
|
date_filename_tuple = last_update, filename
|
|
|
|
date_filename_list.append(date_filename_tuple)
|
|
|
|
return date_filename_list
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def date_as_rfc(value):
|
2024-02-11 12:15:23 +00:00
|
|
|
return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(value))
|
|
|
|
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 12:15:23 +00:00
|
|
|
def build_rss(myitem, maxitem):
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 12:15:23 +00:00
|
|
|
RSSroot = ET.Element("rss", {"version": "2.0"})
|
|
|
|
RSSchannel = ET.SubElement(RSSroot, "channel")
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 12:15:23 +00:00
|
|
|
ET.SubElement(RSSchannel, "title").text = "RSS feed of " + str(title)
|
|
|
|
ET.SubElement(RSSchannel, "link").text = link
|
|
|
|
ET.SubElement(RSSchannel, "description").text = (
|
|
|
|
"A directory RSSified by rssdir.py " + version
|
|
|
|
)
|
|
|
|
ET.SubElement(RSSchannel, "generator").text = (
|
|
|
|
"A directory RSSified by rssdir.py " + version
|
|
|
|
)
|
|
|
|
ET.SubElement(RSSchannel, "pubDate").text = date_as_rfc(time.time())
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 12:15:23 +00:00
|
|
|
for bloodyitem in myitem[0:maxitem]:
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 12:15:23 +00:00
|
|
|
RSSitem = ET.SubElement(RSSchannel, "item")
|
|
|
|
ET.SubElement(RSSitem, "title").text = bloodyitem[1]
|
|
|
|
ET.SubElement(RSSitem, "pubDate").text = date_as_rfc(bloodyitem[0])
|
|
|
|
ET.SubElement(RSSitem, "description").text = prefixurl + bloodyitem[1]
|
|
|
|
ET.SubElement(RSSitem, "guid").text = prefixurl + bloodyitem[1]
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 12:15:23 +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 12:15:23 +00:00
|
|
|
myheader = '<?xml version="1.0"?>'
|
|
|
|
return myheader + str(myfeed)
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
usage = "usage: %prog [options] directory"
|
|
|
|
parser = OptionParser(usage)
|
|
|
|
|
2024-02-11 12:15:23 +00:00
|
|
|
parser.add_option(
|
|
|
|
"-p",
|
|
|
|
"--prefix",
|
|
|
|
dest="prefix",
|
|
|
|
default="",
|
|
|
|
help="http prefix to be used for each entry, default none",
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"-t",
|
|
|
|
"--title",
|
|
|
|
dest="title",
|
|
|
|
help="set a title to the rss feed, default using prefix",
|
|
|
|
type="string",
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"-l",
|
|
|
|
"--link",
|
|
|
|
dest="link",
|
|
|
|
help="http link set, default is prefix and none if prefix not set",
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"-m",
|
|
|
|
"--maxitem",
|
|
|
|
dest="maxitem",
|
|
|
|
help="maximum item to list in the feed, default 32",
|
|
|
|
default=32,
|
|
|
|
type="int",
|
|
|
|
)
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if options.prefix is None:
|
2024-02-11 12:15:23 +00:00
|
|
|
prefixurl = ""
|
|
|
|
else:
|
|
|
|
prefixurl = options.prefix
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
if options.link is None:
|
2024-02-11 12:15:23 +00:00
|
|
|
link = options.prefix
|
|
|
|
else:
|
|
|
|
link = options.link
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
if options.title is None:
|
2024-02-11 12:15:23 +00:00
|
|
|
title = options.prefix
|
|
|
|
else:
|
|
|
|
title = options.title
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
if options.maxitem is None:
|
2024-02-11 12:15:23 +00:00
|
|
|
maxitem = 32
|
|
|
|
else:
|
|
|
|
maxitem = options.maxitem
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 12:15:23 +00:00
|
|
|
if not args:
|
2024-02-11 12:16:55 +00:00
|
|
|
print("Missing directory")
|
2024-02-11 12:15:23 +00:00
|
|
|
parser.print_help()
|
|
|
|
sys.exit(0)
|
2013-04-14 12:05:30 +00:00
|
|
|
|
2024-02-11 12:15:23 +00:00
|
|
|
file_to_list = []
|
|
|
|
for x in all_files(args[0]):
|
|
|
|
file_to_list.append(x)
|
|
|
|
|
|
|
|
mylist = date_files(file_to_list)
|
2013-04-14 12:05:30 +00:00
|
|
|
|
|
|
|
mylist.sort()
|
|
|
|
mylist.reverse()
|
|
|
|
|
2024-02-11 12:15:23 +00:00
|
|
|
print(complete_feed(build_rss(mylist, maxitem)))
|