-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatfeed.py
374 lines (289 loc) · 10.4 KB
/
catfeed.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/python
# coding=utf-8
import logging as log
import argparse
import os
import datetime
import time
import urllib
import mimetypes
import BaseHTTPServer
import socket
from operator import attrgetter
VERBOSITY = (
log.ERROR,
log.WARNING,
log.INFO,
log.DEBUG,
)
class Config:
__version__ = '0.1'
__website__ = 'http://github.com/tbienko/catfeed'
chunk_size = 1024*1024
# configurable via commandline
verbosity = 2
catalog = '.'
after_download = 'move'
moveto = 'Downloaded'
host = 'auto'
port = '8888'
title = ''
class FileDescriptor(object):
path = ""
name = ""
date = None
size = 0
def __repr__(self):
return self.name
@classmethod
def scan_catalog(self):
items = []
for subdir, dirs, files in os.walk(Config.catalog):
for file in files:
if Config.after_download == 'move' and \
subdir.startswith(Config.moveto):
continue
path = os.path.join(subdir, file)
(mode, ino, dev, nlink, uid, gid,
size, atime, mtime, ctime) = os.stat(path)
desc = FileDescriptor()
desc.path = path
desc.name = os.path.splitext(file)[0]
desc.date = ctime
desc.size = size
items.append(desc)
return items
@classmethod
def get_for_path(self, urlpath):
for item in FileDescriptor.scan_catalog():
if item.urlpath == urlpath:
return item
return None
def delete(self):
log.debug("Deleting: %s", self.path)
os.remove(self.path)
def move(self):
target = os.path.join(Config.moveto, self.relativepath)
targetdir = os.path.dirname(target)
log.debug("Moving file from: %s to: %s", self.path, target)
if not os.path.isdir(targetdir):
os.makedirs(targetdir)
os.rename(self.path, target)
@property
def relativepath(self):
return self.path[len(Config.catalog)+1:]
@property
def urlpath(self):
name = self.relativepath
name = name.replace(' ', '-')
name = urllib.quote(name)
return name
@property
def mime(self):
mime = mimetypes.guess_type(self.path)[0]
return mime if mime is not None else "text/plain"
class CatFeed:
def setup_argparser(self):
parser = argparse.ArgumentParser(
description='Simple feed server for your files. \
Generate feed for local catalog and download files directly \
in your favourite podcast player!',
epilog='Visit ' + Config.__website__ + ' to get the newest \
script. You can also contribute to this project there.'
)
parser.add_argument(
'-H', '--host', type=str, default=Config.host,
help='host to start server on (default: ' + Config.host + ')'
)
parser.add_argument(
'-p', '--port', type=int, default=Config.port,
help='port to start server on (default: ' + Config.port + ')'
)
parser.add_argument(
'-m', '--moveto', type=str, default=Config.moveto, metavar='PATH',
help='catalog to move downloaded files (absolute or relative path,\
default: ' + Config.moveto + ')'
)
parser.add_argument(
'-d', '--delete', action='store_true',
help='remove downloaded files instead of moving to other catalog'
)
parser.add_argument(
'-t', '--title', type=str,
help='title of feed (default: name of catalog)'
)
parser.add_argument(
'-v', '--verbosity', type=int,
choices=range(len(VERBOSITY)), default=Config.verbosity,
help='level of logging (0-ERROR ... 3-DEBUG)'
)
parser.add_argument(
'catalog', type=str,
help='catalog to generate feed from (absolute or relative path)'
)
return parser
def args_to_config(self, args):
log.debug("Raw args from argparse: %s", args)
# loading arguments to class object
Config.verbosity = args.verbosity
log.basicConfig(level=VERBOSITY[Config.verbosity])
Config.catalog = os.path.abspath(args.catalog)
log.info("Files will be served from: %s", Config.catalog)
if args.delete:
Config.after_download = 'delete'
log.info("Downloaded files will be deleted")
else:
Config.after_download = 'move'
Config.moveto = self.generate_move_path(args.moveto)
log.info("Downloaded files will be moved to: %s", Config.moveto)
if args.host == "auto":
Config.host = self.find_ip()
log.info("Host automatically resolved to: %s", Config.host)
else:
Config.host = args.host
log.info("Host set to: %s", Config.host)
Config.port = args.port
log.info("Port set to: %s", Config.port)
if args.title is None:
Config.title = os.path.basename(Config.catalog)
log.info("Feed title generated from name of catalog: %s",
Config.title)
else:
Config.title = args.title
log.info("Feed title set to: %s", Config.title)
def find_ip(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("google.com", 80))
return s.getsockname()[0]
s.close()
except:
return "127.0.0.1"
def generate_move_path(self, move):
path = os.path.realpath(move)
if os.path.isdir(path):
return path
return os.path.normpath("%s/%s" % (Config.catalog, move))
def start_server(self):
print "Your feed is served on %s" % Feed().feed_url()
httpd = BaseHTTPServer.HTTPServer((Config.host, Config.port),
RequestHandler)
try:
httpd.serve_forever()
except (KeyboardInterrupt, SystemExit):
log.info('Stopping...')
httpd.server_close()
def __init__(self):
log.basicConfig(level=VERBOSITY[Config.verbosity],
format='[%(levelname)s] %(message)s')
log.info("Starting CatFeed version %s", Config.__version__)
parser = self.setup_argparser()
args = parser.parse_args()
self.args_to_config(args)
self.start_server()
log.info('Stopped.')
class Feed:
def base_url(self):
return "http://%s:%s/" % (Config.host, Config.port)
def feed_url(self):
return self.base_url()
def item_url(self, item):
return self.base_url() + item.urlpath
def atom_date(self, timestamp):
return datetime.datetime.fromtimestamp(timestamp).isoformat("T") + "Z"
def generate_feed(self, items):
items = sorted(items, key=attrgetter('date'), reverse=True)
feed = []
params = {
'feed': self.feed_url(),
'base': self.base_url(),
'title': Config.title,
'updated': self.atom_date(time.time())
}
feed.append("""<feed xmlns="http://www.w3.org/2005/Atom">
<id>%(feed)s</id>
<title>%(title)s</title>
<updated>%(updated)s</updated>
<link href="%(base)s" />
<link rel="self" href="%(feed)s" />
<author>
<name>CatFeed</name>
</author>""" % params)
for item in items:
params = {
'name': item.name,
'link': self.item_url(item),
'date': self.atom_date(item.date),
'mime': item.mime,
'size': item.size
}
feed.append("""
<entry>
<id>%(link)s</id>
<title>%(name)s</title>
<updated>%(date)s</updated>
<link href="%(link)s" />
<summary></summary>
<link rel="enclosure"
type="%(mime)s"
title="%(name)s"
href="%(link)s"
length="%(size)d" />
</entry>\n""" % params)
feed.append("</feed>")
return ''.join(feed)
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(self):
self.serve(self.path, "HEAD")
def do_GET(self):
self.serve(self.path, "GET")
def serve(self, path, request_type):
log.debug("New request %s: %s", request_type, self.path)
if self.path == '/':
self.serve_feed(request_type)
return
item = FileDescriptor.get_for_path(self.path.lstrip('/'))
if item is not None:
log.debug("Requested path found")
self.serve_file(request_type, item)
else:
self.serve_404(request_type)
def serve_404(self, request_type):
log.debug("Response to %s: %s - 404", request_type, self.path)
self.send_response(404)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("404 Not Found")
def serve_feed(self, request_type):
log.debug("Response to %s: %s - Feed", request_type, self.path)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
if request_type == 'HEAD':
return
items = FileDescriptor.scan_catalog()
feed = Feed().generate_feed(items)
self.wfile.write(feed)
def serve_file(self, request_type, item):
log.debug("Response to %s: %s - Serving file %s",
request_type, self.path, item.path)
f = open(item.path, 'rb')
self.send_response(200)
self.send_header('Content-type', item.mime)
self.end_headers()
if request_type == 'HEAD':
return
while True:
chunk = f.read(Config.chunk_size)
if chunk:
self.wfile.write(chunk)
else:
break
f.close()
log.debug("File downloaded: %s", item.path)
callback = getattr(item, Config.after_download)
callback()
def main():
CatFeed()
if __name__ == "__main__":
main()