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