update-feed.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python3
  2. from __future__ import unicode_literals
  3. import datetime
  4. import json
  5. import os.path
  6. import textwrap
  7. import sys
  8. dirn = os.path.dirname
  9. sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))
  10. from utils import write_file
  11. atom_template = textwrap.dedent("""\
  12. <?xml version="1.0" encoding="utf-8"?>
  13. <feed xmlns="http://www.w3.org/2005/Atom">
  14. <link rel="self" href="http://ytdl-org.github.io/youtube-dl/update/releases.atom" />
  15. <title>youtube-dl releases</title>
  16. <id>https://yt-dl.org/feed/youtube-dl-updates-feed</id>
  17. <updated>@TIMESTAMP@</updated>
  18. @ENTRIES@
  19. </feed>""")
  20. entry_template = textwrap.dedent("""
  21. <entry>
  22. <id>https://yt-dl.org/feed/youtube-dl-updates-feed/youtube-dl-@VERSION@</id>
  23. <title>New version @VERSION@</title>
  24. <link href="http://ytdl-org.github.io/youtube-dl" />
  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 youtube-dl maintainers</name>
  32. </author>
  33. <updated>@TIMESTAMP@</updated>
  34. </entry>
  35. """)
  36. now = datetime.datetime.now()
  37. now_iso = now.isoformat() + 'Z'
  38. atom_template = atom_template.replace('@TIMESTAMP@', now_iso)
  39. versions_info = json.load(open('update/versions.json'))
  40. versions = list(versions_info['versions'].keys())
  41. versions.sort()
  42. entries = []
  43. for v in versions:
  44. fields = v.split('.')
  45. year, month, day = map(int, fields[:3])
  46. faked = 0
  47. patchlevel = 0
  48. while True:
  49. try:
  50. datetime.date(year, month, day)
  51. except ValueError:
  52. day -= 1
  53. faked += 1
  54. assert day > 0
  55. continue
  56. break
  57. if len(fields) >= 4:
  58. try:
  59. patchlevel = int(fields[3])
  60. except ValueError:
  61. patchlevel = 1
  62. timestamp = '%04d-%02d-%02dT00:%02d:%02dZ' % (year, month, day, faked, patchlevel)
  63. entry = entry_template.replace('@TIMESTAMP@', timestamp)
  64. entry = entry.replace('@VERSION@', v)
  65. entries.append(entry)
  66. entries_str = textwrap.indent(''.join(entries), '\t')
  67. atom_template = atom_template.replace('@ENTRIES@', entries_str)
  68. write_file('update/releases.atom', atom_template)