Commit d5baf172 authored by Etienne Nadji's avatar Etienne Nadji

toOpml

	modifié :         toOpml.py
parent 0520b116
......@@ -3,16 +3,16 @@
# Imports ===============================================================#
import os
import sys
import xml.etree.ElementTree as ET
# Programme =============================================================#
def get_datas(xml_file):
def _parse_emission(emissions,main_elem):
e = {"title":None,"urls":[]}
def _parse_emission(emissions, main_elem):
e = {"title": None, "urls": []}
for elem in main_elem:
if elem.tag == "title":
......@@ -41,15 +41,22 @@ def get_datas(xml_file):
if url_type == "web":
if "rssUrl" in link.attrib.keys():
e["urls"].append("{0}{1}".format(text,link.attrib["rssUrl"]))
e["urls"].append(
"{0}{1}".format(
text, link.attrib["rssUrl"]
)
)
if url_type == "yt":
if url_subtype in ["user","channel"]:
if url_subtype in ["user", "channel"]:
ref = {
"user":"https://www.youtube.com/feeds/videos.xml?user=",
"channel":"https://www.youtube.com/feeds/videos.xml?channel_id="
"user": "https://www.youtube.com/feeds/videos.xml?user=",
"channel": "https://www.youtube.com/feeds/videos.xml?channel_id="
}
e["urls"].append("{0}{1}".format(ref[url_subtype],text))
e["urls"].append("{0}{1}".format(
ref[url_subtype], text
)
)
if len(e["urls"]) > 0:
emissions.append(e)
......@@ -70,18 +77,22 @@ def get_datas(xml_file):
for subElem in supElem:
if subElem.tag == "emission":
emissions = _parse_emission(emissions,subElem)
emissions = _parse_emission(emissions, subElem)
if subElem.tag == "subCategory":
for elem in subElem:
if elem.tag == "emission":
emissions = _parse_emission(emissions,elem)
emissions = _parse_emission(emissions, elem)
return {"update": update, "emissions": emissions}
return {"update":update,"emissions":emissions}
def write_opml(datas):
global ADDED_URLS
def _get_emission_text(emission):
global ADDED_URLS
title = ""
if emission["title"] is not None:
......@@ -91,17 +102,26 @@ def write_opml(datas):
ret = ''
for url in emission["urls"]:
url = 'xmlUrl="{0}"'.format(url)
ret += ' <outline text="" {0}type="rss" {1}/>\n'.format(title,url)
if url not in ADDED_URLS:
url = 'xmlUrl="{0}"'.format(url)
ret += ' <outline text="" {0}type="rss" {1}/>\n'.format(
title, url
)
ADDED_URLS.append(url)
return ret
else:
if emission["urls"][0] not in ADDED_URLS:
url = 'xmlUrl="{0}"'.format(emission["urls"][0])
ADDED_URLS.append(url)
url = 'xmlUrl="{0}"'.format(emission["urls"][0])
return ' <outline text="" {0}type="rss" {1}/>\n'.format(title,url)
return ' <outline text="" {0}type="rss" {1}/>\n'.format(
title, url
)
if datas:
with open("reco.opml","w") as opmlf:
with open("reco.opml", "w") as opmlf:
opmlf.write('<?xml version="1.0" encoding="utf-8"?>\n')
opmlf.write('<opml version="2.0">\n')
......@@ -109,7 +129,11 @@ def write_opml(datas):
opmlf.write(' <title>Recommandations d\'émissions</title>\n')
if datas["update"] is not None:
opmlf.write(' <dateCreated>{0}</dateCreated>\n'.format(datas["update"]))
opmlf.write(
' <dateCreated>{0}</dateCreated>\n'.format(
datas["update"]
)
)
opmlf.write('</head>\n')
opmlf.write('<body>\n')
......@@ -120,8 +144,11 @@ def write_opml(datas):
opmlf.write('</body>\n')
opmlf.write('</opml>\n')
if __name__ == "__main__":
ADDED_URLS = []
datas = get_datas("reco.xml")
write_opml(datas)
sys.exit(0)
# vim:set shiftwidth=4 softtabstop=4:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment