update-feed.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. import sys
  3. import xml.etree.ElementTree as ET
  4. import xml.dom.minidom as minidom
  5. import datetime
  6. if len(sys.argv) <= 1:
  7. print('Specify the version number as parameter')
  8. sys.exit()
  9. version = sys.argv[1]
  10. out_file = "atom.atom"
  11. in_file = out_file
  12. now = datetime.datetime.now()
  13. now_iso = now.isoformat()
  14. atom_url = "http://www.w3.org/2005/Atom"
  15. #Some utilities functions
  16. def atom_tag(tag):
  17. #Return a tag in the atom namespace
  18. return "{{{}}}{}".format(atom_url,tag)
  19. def atom_SubElement(parent,tag):
  20. return ET.SubElement(parent,atom_tag(tag))
  21. class YDLUpdateAtomEntry(object):
  22. def __init__(self,parent,title,id ,link, downloads_link):
  23. self.entry = entry = atom_SubElement(parent, "entry")
  24. #We set the values:
  25. atom_id = atom_SubElement(entry, "id")
  26. atom_id.text = id
  27. atom_title = atom_SubElement(entry, "title")
  28. atom_title.text = title
  29. atom_link = atom_SubElement(entry, "link")
  30. atom_link.set("href", link)
  31. atom_content = atom_SubElement(entry, "content")
  32. atom_content.set("type", "xhtml")
  33. #Here we go:
  34. div = ET.SubElement(atom_content,"div")
  35. div.set("xmlns", "http://www.w3.org/1999/xhtml")
  36. div.text = "Downloads available at "
  37. a = ET.SubElement(div, "a")
  38. a.set("href", downloads_link)
  39. a.text = downloads_link
  40. #Author info
  41. atom_author = atom_SubElement(entry, "author")
  42. author_name = atom_SubElement(atom_author, "name")
  43. author_name.text = "The youtube-dl maintainers"
  44. #If someone wants to put an email adress:
  45. #author_email = atom_SubElement(atom_author, "email")
  46. #author_email.text = the_email
  47. atom_updated = atom_SubElement(entry,"updated")
  48. up = parent.find(atom_tag("updated"))
  49. atom_updated.text = up.text = now_iso
  50. @classmethod
  51. def entry(cls,parent, version):
  52. update_id = "youtube-dl-{}".format(version)
  53. update_title = "New version {}".format(version)
  54. downloads_link = "http://youtube-dl.org/downloads/{}/".format(version)
  55. #There's probably no better link
  56. link = "http://rg3.github.com/youtube-dl"
  57. return cls(parent, update_title, update_id, link, downloads_link)
  58. atom = ET.parse(in_file)
  59. root = atom.getroot()
  60. #Otherwise when saving all tags will be prefixed with a 'ns0:'
  61. ET.register_namespace("atom",atom_url)
  62. update_entry = YDLUpdateAtomEntry.entry(root, version)
  63. #Find some way of pretty printing
  64. atom.write(out_file,encoding="utf-8",xml_declaration=True)