1 | #!/usr/bin/python |
---|
2 | # Author: Eli Carter elicarter@retracile.net |
---|
3 | # License: MIT |
---|
4 | import argparse |
---|
5 | import sys |
---|
6 | import time |
---|
7 | import os |
---|
8 | import re |
---|
9 | import datetime |
---|
10 | |
---|
11 | import PyRSS2Gen |
---|
12 | |
---|
13 | |
---|
14 | def main(argv): |
---|
15 | """Create an ad-hoc RSS2 XML feed from a set of audio files |
---|
16 | """ |
---|
17 | parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
---|
18 | description=main.__doc__) |
---|
19 | parser.add_argument('--feed-title', |
---|
20 | help="RSS feed title") |
---|
21 | parser.add_argument('--url', required=True, |
---|
22 | help="base URL for this rss2.xml") |
---|
23 | parser.add_argument('--base-url', required=True, |
---|
24 | help="base URL for listed files, including trailing '/'") |
---|
25 | parser.add_argument('--filename-regex', default="(?P<filename>.*)", |
---|
26 | help="Python regex to parse filenames") |
---|
27 | parser.add_argument('--title-pattern', default="%(filename)s", |
---|
28 | help="Pattern to convert parsed filename to a title") |
---|
29 | parser.add_argument('--output', default="-", |
---|
30 | help="output file, '-' for stdout") |
---|
31 | parser.add_argument('files', nargs='+', |
---|
32 | help="mp3 files from which to build an RSS2 feed") |
---|
33 | args = parser.parse_args(argv[1:]) |
---|
34 | |
---|
35 | filename_parser = re.compile(args.filename_regex) |
---|
36 | |
---|
37 | # Generate an entry for each file |
---|
38 | rss_items = [] |
---|
39 | for filename in args.files: |
---|
40 | match = filename_parser.search(filename) |
---|
41 | if not match: |
---|
42 | sys.stderr.write("Failed to match %r with pattern %r\n" % |
---|
43 | (filename, args.filename_regex)) |
---|
44 | return 1 |
---|
45 | parsed = match.groupdict() |
---|
46 | title = args.title_pattern % parsed |
---|
47 | file_time = time.localtime(os.stat(filename).st_mtime) |
---|
48 | url = args.base_url + filename |
---|
49 | item = PyRSS2Gen.RSSItem( |
---|
50 | title=title, |
---|
51 | description=title, |
---|
52 | guid=PyRSS2Gen.Guid(os.path.basename(filename)), |
---|
53 | enclosure=PyRSS2Gen.Enclosure(url=url, |
---|
54 | length=os.stat(filename).st_size, |
---|
55 | type='audio/mpeg'), |
---|
56 | pubDate=datetime.datetime(file_time.tm_year, file_time.tm_mon, |
---|
57 | file_time.tm_mday, 0, 0), |
---|
58 | ) |
---|
59 | rss_items.append(item) |
---|
60 | |
---|
61 | # Create the feed containing all the entries |
---|
62 | rss = PyRSS2Gen.RSS2( |
---|
63 | title=args.feed_title, |
---|
64 | link=args.url, |
---|
65 | description=args.feed_title, |
---|
66 | lastBuildDate=datetime.datetime.now(), |
---|
67 | items=rss_items, |
---|
68 | ) |
---|
69 | |
---|
70 | # Write it out |
---|
71 | if args.output == '-': |
---|
72 | output = sys.stdout |
---|
73 | else: |
---|
74 | output = open(args.output, 'w') |
---|
75 | rss.write_xml(output) |
---|
76 | |
---|
77 | return 0 |
---|
78 | |
---|
79 | |
---|
80 | if __name__ == '__main__': |
---|
81 | sys.exit(main(sys.argv)) |
---|