Improve Atom feed creation (Fixes #2081)
[youtube-dl] / devscripts / gh-pages / update-feed.py
1 #!/usr/bin/env python3
2
3 import datetime
4 import io
5 import json
6 import textwrap
7
8
9 atom_template = textwrap.dedent("""\
10     <?xml version="1.0" encoding="utf-8"?>
11     <feed xmlns="http://www.w3.org/2005/Atom">
12         <title>youtube-dl releases</title>
13         <id>https://yt-dl.org/feed/youtube-dl-updates-feed</id>
14         <updated>@TIMESTAMP@</updated>
15         @ENTRIES@
16     </feed>""")
17
18 entry_template = textwrap.dedent("""
19     <entry>
20         <id>https://yt-dl.org/feed/youtube-dl-updates-feed/youtube-dl-@VERSION@</id>
21         <title>New version @VERSION@</title>
22         <link href="http://rg3.github.io/youtube-dl" />
23         <content type="xhtml">
24             <div xmlns="http://www.w3.org/1999/xhtml">
25                 Downloads available at <a href="https://yt-dl.org/downloads/@VERSION@/">https://yt-dl.org/downloads/@VERSION@/</a>
26             </div>
27         </content>
28         <author>
29             <name>The youtube-dl maintainers</name>
30         </author>
31         <updated>@TIMESTAMP@</updated>
32     </entry>
33     """)
34
35 now = datetime.datetime.now()
36 now_iso = now.isoformat() + 'Z'
37
38 atom_template = atom_template.replace('@TIMESTAMP@', now_iso)
39
40 versions_info = json.load(open('update/versions.json'))
41 versions = list(versions_info['versions'].keys())
42 versions.sort()
43
44 entries = []
45 for v in versions:
46     entry = entry_template.replace('@TIMESTAMP@', v.replace('.', '-') + 'T00:00:00Z')
47     entry = entry.replace('@VERSION@', v)
48     entries.append(entry)
49
50 entries_str = textwrap.indent(''.join(entries), '\t')
51 atom_template = atom_template.replace('@ENTRIES@', entries_str)
52
53 with io.open('update/releases.atom', 'w', encoding='utf-8') as atom_file:
54     atom_file.write(atom_template)
55