1#!/usr/bin/env python3
2from __future__ import unicode_literals
3
4import datetime
5import io
6import json
7import textwrap
8
9
10atom_template = textwrap.dedent("""\
11    <?xml version="1.0" encoding="utf-8"?>
12    <feed xmlns="http://www.w3.org/2005/Atom">
13        <link rel="self" href="http://ytdl-org.github.io/youtube-dl/update/releases.atom" />
14        <title>yt-dlp releases</title>
15        <id>https://yt-dl.org/feed/yt-dlp-updates-feed</id>
16        <updated>@TIMESTAMP@</updated>
17        @ENTRIES@
18    </feed>""")
19
20entry_template = textwrap.dedent("""
21    <entry>
22        <id>https://yt-dl.org/feed/yt-dlp-updates-feed/yt-dlp-@VERSION@</id>
23        <title>New version @VERSION@</title>
24        <link href="http://ytdl-org.github.io/yt-dlp" />
25        <content type="xhtml">
26            <div xmlns="http://www.w3.org/1999/xhtml">
27                Downloads available at <a href="https://yt-dl.org/downloads/@VERSION@/">https://yt-dl.org/downloads/@VERSION@/</a>
28            </div>
29        </content>
30        <author>
31            <name>The yt-dlp maintainers</name>
32        </author>
33        <updated>@TIMESTAMP@</updated>
34    </entry>
35    """)
36
37now = datetime.datetime.now()
38now_iso = now.isoformat() + 'Z'
39
40atom_template = atom_template.replace('@TIMESTAMP@', now_iso)
41
42versions_info = json.load(open('update/versions.json'))
43versions = list(versions_info['versions'].keys())
44versions.sort()
45
46entries = []
47for v in versions:
48    fields = v.split('.')
49    year, month, day = map(int, fields[:3])
50    faked = 0
51    patchlevel = 0
52    while True:
53        try:
54            datetime.date(year, month, day)
55        except ValueError:
56            day -= 1
57            faked += 1
58            assert day > 0
59            continue
60        break
61    if len(fields) >= 4:
62        try:
63            patchlevel = int(fields[3])
64        except ValueError:
65            patchlevel = 1
66    timestamp = '%04d-%02d-%02dT00:%02d:%02dZ' % (year, month, day, faked, patchlevel)
67
68    entry = entry_template.replace('@TIMESTAMP@', timestamp)
69    entry = entry.replace('@VERSION@', v)
70    entries.append(entry)
71
72entries_str = textwrap.indent(''.join(entries), '\t')
73atom_template = atom_template.replace('@ENTRIES@', entries_str)
74
75with io.open('update/releases.atom', 'w', encoding='utf-8') as atom_file:
76    atom_file.write(atom_template)
77