mirror of
https://github.com/adulau/rss-tools.git
synced 2024-11-24 02:57:05 +00:00
chg: [rssdir] Make rssdir work in the modern world - move to Python 3
This commit is contained in:
parent
2240a563d2
commit
93ced8398e
1 changed files with 98 additions and 64 deletions
162
bin/rssdir.py
162
bin/rssdir.py
|
@ -2,12 +2,11 @@
|
||||||
# a at foo dot be - Alexandre Dulaunoy - http://www.foo.be/cgi-bin/wiki.pl/RssAny
|
# 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.
|
# rssdir is a simply-and-dirty script to rssify any directory on the filesystem.
|
||||||
#
|
#
|
||||||
# an example of use on the current directory :
|
# an example of use on the current directory :
|
||||||
#
|
#
|
||||||
# python2.5 /usr/local/bin/rssdir.py --prefix http://www.foo.be/cours/ . >rss.xml
|
# python3 /usr/local/bin/rssdir.py --prefix http://www.foo.be/cours/ . >rss.xml
|
||||||
#
|
#
|
||||||
# Don't really need python2.5 except for ElementTree but you are free to install it.
|
|
||||||
|
|
||||||
import os, fnmatch
|
import os, fnmatch
|
||||||
import time
|
import time
|
||||||
|
@ -15,103 +14,138 @@ import sys
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
version = "0.1"
|
version = "0.2"
|
||||||
|
|
||||||
# recursive list file function from the ASPN cookbook
|
# recursive list file function from the ASPN cookbook
|
||||||
def all_files(root, patterns='*', single_level=False, yield_folders=False):
|
def all_files(root, patterns="*", single_level=False, yield_folders=False):
|
||||||
patterns = patterns.split(';')
|
patterns = patterns.split(";")
|
||||||
for path, subdirs, files in os.walk(root):
|
for path, subdirs, files in os.walk(root):
|
||||||
if yield_folders:
|
if yield_folders:
|
||||||
files.extend(subdirs)
|
files.extend(subdirs)
|
||||||
files.sort()
|
files.sort()
|
||||||
for name in files:
|
for name in files:
|
||||||
for pattern in patterns:
|
for pattern in patterns:
|
||||||
if fnmatch.fnmatch(name, pattern):
|
if fnmatch.fnmatch(name, pattern):
|
||||||
yield os.path.join(path, name)
|
yield os.path.join(path, name)
|
||||||
break
|
break
|
||||||
if single_level:
|
if single_level:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
def date_files(filelist):
|
def date_files(filelist):
|
||||||
date_filename_list = []
|
date_filename_list = []
|
||||||
|
|
||||||
for filename in filelist:
|
for filename in filelist:
|
||||||
stats = os.stat(filename)
|
stats = os.stat(filename)
|
||||||
last_update = stats[8]
|
last_update = stats[8]
|
||||||
date_filename_tuple = last_update, filename
|
date_filename_tuple = last_update, filename
|
||||||
date_filename_list.append(date_filename_tuple)
|
date_filename_list.append(date_filename_tuple)
|
||||||
|
return date_filename_list
|
||||||
|
|
||||||
return date_filename_list
|
|
||||||
|
|
||||||
def date_as_rfc(value):
|
def date_as_rfc(value):
|
||||||
return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(value))
|
return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(value))
|
||||||
|
|
||||||
|
|
||||||
def build_rss(myitem,maxitem):
|
def build_rss(myitem, maxitem):
|
||||||
|
|
||||||
RSSroot = ET.Element( 'rss', {'version':'2.0'} )
|
RSSroot = ET.Element("rss", {"version": "2.0"})
|
||||||
RSSchannel = ET.SubElement( RSSroot, 'channel' )
|
RSSchannel = ET.SubElement(RSSroot, "channel")
|
||||||
|
|
||||||
ET.SubElement( RSSchannel, 'title' ).text = 'RSS feed of ' + str(title)
|
ET.SubElement(RSSchannel, "title").text = "RSS feed of " + str(title)
|
||||||
ET.SubElement( RSSchannel, 'link' ).text = link
|
ET.SubElement(RSSchannel, "link").text = link
|
||||||
ET.SubElement( RSSchannel, 'description' ).text = 'A directory RSSified by rssdir.py ' + version
|
ET.SubElement(RSSchannel, "description").text = (
|
||||||
ET.SubElement( RSSchannel, 'generator' ).text = 'A directory RSSified by rssdir.py ' + version
|
"A directory RSSified by rssdir.py " + version
|
||||||
ET.SubElement( RSSchannel, 'pubDate' ).text = date_as_rfc(time.time())
|
)
|
||||||
|
ET.SubElement(RSSchannel, "generator").text = (
|
||||||
|
"A directory RSSified by rssdir.py " + version
|
||||||
|
)
|
||||||
|
ET.SubElement(RSSchannel, "pubDate").text = date_as_rfc(time.time())
|
||||||
|
|
||||||
for bloodyitem in myitem[0:maxitem]:
|
for bloodyitem in myitem[0:maxitem]:
|
||||||
|
|
||||||
RSSitem = ET.SubElement ( RSSchannel, 'item' )
|
RSSitem = ET.SubElement(RSSchannel, "item")
|
||||||
ET.SubElement( RSSitem, 'title' ).text = bloodyitem[1]
|
ET.SubElement(RSSitem, "title").text = bloodyitem[1]
|
||||||
ET.SubElement( RSSitem, 'pubDate' ).text = date_as_rfc(bloodyitem[0])
|
ET.SubElement(RSSitem, "pubDate").text = date_as_rfc(bloodyitem[0])
|
||||||
ET.SubElement( RSSitem, 'description').text = prefixurl+bloodyitem[1]
|
ET.SubElement(RSSitem, "description").text = prefixurl + bloodyitem[1]
|
||||||
ET.SubElement( RSSitem, 'guid').text = prefixurl+bloodyitem[1]
|
ET.SubElement(RSSitem, "guid").text = prefixurl + bloodyitem[1]
|
||||||
|
|
||||||
RSSfeed = ET.ElementTree(RSSroot)
|
RSSfeed = ET.ElementTree(RSSroot)
|
||||||
feed = ET.tostring(RSSroot)
|
feed = ET.tostring(RSSroot)
|
||||||
return feed
|
return feed
|
||||||
|
|
||||||
|
|
||||||
def complete_feed(myfeed):
|
def complete_feed(myfeed):
|
||||||
|
|
||||||
myheader = '<?xml version="1.0"?>'
|
myheader = '<?xml version="1.0"?>'
|
||||||
return myheader + str(myfeed)
|
return myheader + str(myfeed)
|
||||||
|
|
||||||
|
|
||||||
usage = "usage: %prog [options] directory"
|
usage = "usage: %prog [options] directory"
|
||||||
parser = OptionParser(usage)
|
parser = OptionParser(usage)
|
||||||
|
|
||||||
parser.add_option("-p","--prefix",dest="prefix",help="http prefix to be used for each entry, default none")
|
parser.add_option(
|
||||||
parser.add_option("-t","--title",dest="title",help="set a title to the rss feed, default using prefix",type="string")
|
"-p",
|
||||||
parser.add_option("-l","--link",dest="link",help="http link set, default is prefix and none if prefix not set")
|
"--prefix",
|
||||||
parser.add_option("-m","--maxitem",dest="maxitem",help="maximum item to list in the feed, default 32",type="int")
|
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",
|
||||||
|
)
|
||||||
|
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
if options.prefix is None:
|
if options.prefix is None:
|
||||||
prefixurl = ''
|
prefixurl = ""
|
||||||
else :
|
else:
|
||||||
prefixurl = options.prefix
|
prefixurl = options.prefix
|
||||||
|
|
||||||
if options.link is None:
|
if options.link is None:
|
||||||
link = options.prefix
|
link = options.prefix
|
||||||
else :
|
else:
|
||||||
link = options.link
|
link = options.link
|
||||||
|
|
||||||
if options.title is None:
|
if options.title is None:
|
||||||
title = options.prefix
|
title = options.prefix
|
||||||
else :
|
else:
|
||||||
title = options.title
|
title = options.title
|
||||||
|
|
||||||
if options.maxitem is None:
|
if options.maxitem is None:
|
||||||
maxitem = 32
|
maxitem = 32
|
||||||
else :
|
else:
|
||||||
maxitem = options.maxitem
|
maxitem = options.maxitem
|
||||||
|
|
||||||
|
if not args:
|
||||||
|
parser.print_help()
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
mylist = date_files(all_files(args[0]))
|
file_to_list = []
|
||||||
|
for x in all_files(args[0]):
|
||||||
|
file_to_list.append(x)
|
||||||
|
|
||||||
|
mylist = date_files(file_to_list)
|
||||||
|
|
||||||
mylist.sort()
|
mylist.sort()
|
||||||
mylist.reverse()
|
mylist.reverse()
|
||||||
|
|
||||||
print complete_feed(build_rss(mylist,maxitem))
|
print(complete_feed(build_rss(mylist, maxitem)))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue