-
Notifications
You must be signed in to change notification settings - Fork 3
/
ls2rss.py
executable file
·137 lines (111 loc) · 3.52 KB
/
ls2rss.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
#!/usr/bin/python
'''These program creates a RSS2 xml from a given directory
20090410 - pmoranga
Initial Version
20090415 - pmoranga
Reads config with ConfigParser
TODO: Config from args
'''
import os
import re
import sys
import time
import PyRSS2Gen
import datetime
import ConfigParser
from stat import *
'''Places where config file can exist'''
configfiles = [ "./ls2rss.cfg", os.path.expanduser("~")+"/.ls2rss.cfg", "/etc/ls2rss.cfg" ]
'''parameters to read from config file'''
params = [ ('title','s'), ('description','s'), ('baseurl','s'),\
('root','s'), ('maxdepth','i'), ('outfile','s'),\
('maxdays','i'),('showleaf','b'), ('recursive','b'),\
('dateformat','s'), ('valid_extension','s') ]
def help():
print("usage: "+sys.argv[0]+" [file.cfg]\n"+ \
" or create config file at:")
for f in configfiles:
print (" "+f)
# print ("required parameters are:")
# for
'''Append fisrt argument to the possible config files'''
if (len(sys.argv) > 1):
configfiles.insert(0,sys.argv[1])
'''Verifies which config file exists'''
for f in configfiles:
if (os.path.exists(f)):
cfgfile=f
break
if 'cfgfile' not in globals():
help()
exit(1)
print "Reading config file: "+cfgfile
try:
cfgreader = ConfigParser.ConfigParser()
cfgreader.readfp(open(cfgfile))
cfg = {}
for v,type in params:
if (type == 'i'):
cfg[v] = cfgreader.getint('DEFAULT',v)
elif (type == 'b'):
cfg[v] = cfgreader.getboolean('DEFAULT',v)
else:
cfg[v] = cfgreader.get('DEFAULT',v)
except ConfigParser.NoOptionError, e:
print "Needed options not set into cfg file: " + e.option
exit(1)
print cfg
class item:
'''Class that identify an item'''
def __init__(self,file,timestamp):
self.file = file
self.timestamp = timestamp
def get_timestamp(self):
return self.timestamp
def get_file(self):
return self.file
def get_mdate(self):
return time.strftime(cfg['dateformat'], time.gmtime(self.timestamp))
mintimestamp = time.time() - (cfg['maxdays'] * 60*60*24)
vext = re.compile( cfg['valid_extension'] )
'''The set of items'''
filelist = [ ]
def processDir(dirname,deep):
'''Process childs from a directory and call itself for each subdir, until "maxdeep.'''
global cfg, vext
for file in os.listdir(dirname):
f = os.path.join( dirname,file )
if (deep < cfg['maxdepth'] and os.path.isdir(f)):
processDir(f, deep+1)
else:
#print file + " " + str( vext.match( file ))
if (vext.match( file ) or (cfg['showleaf'] and os.path.isdir(f) )):
mtime = os.stat(f)[ST_MTIME]
i = item(f,mtime)
filelist.append(i)
'''First process all files.'''
processDir(cfg['root'],0)
'''Sort the list by timestamp'''
filelist.sort(key=lambda i: i.timestamp)
filelist.reverse()
rss = PyRSS2Gen.RSS2(
title = "RSS hellraiser",
link = cfg['baseurl'],
description = "Hellraiser's Feed",
lastBuildDate = datetime.datetime.now() )
for i in filelist:
if (i.get_timestamp() > mintimestamp):
f = i.get_file()
print ("%s %s" % ( i.get_mdate(),i.get_file() ))
f = f.replace(cfg['root'],cfg['baseurl'])
rss.items.append(
PyRSS2Gen.RSSItem(
title = f.replace(cfg['baseurl'] ,'/'),
#"This is Item 1",
link = f,
guid = f,
pubDate = datetime.datetime.fromtimestamp(i.get_timestamp())
))
rss.write_xml(open(cfg['outfile'],"w"))
rss.write_xml(sys.stdout)
print "\n"