Skip to content

Instantly share code, notes, and snippets.

@konfiot
Created October 30, 2014 17:36
Show Gist options
  • Save konfiot/ed72de3746920e70a1c6 to your computer and use it in GitHub Desktop.
Save konfiot/ed72de3746920e70a1c6 to your computer and use it in GitHub Desktop.
C+ broadcasts to RSS
import re
import xml.etree.ElementTree as ET
import urllib
import m3u8
import shutil
import os
import pytz
import json
from datetime import datetime
from feedgen.feed import FeedGenerator
RSS_FILENAME = "rss.xml"
ROOT = "https://82.237.11.61/lpj/"
MAX_ITEMS = 10
fg = FeedGenerator()
fg.title('Youtube Subscriptions')
fg.id(ROOT)
fg.language('en')
fg.updated(datetime.now(tz=pytz.utc))
fg.link({'href':ROOT + RSS_FILENAME, 'rel':'self'})
fg.load_extension('podcast')
fg.description("Youtube subscriptions")
downloaded = json.load(open("downloads.json"))
downloaded_cpy = [i for i in downloaded]
maintree = ET.parse(urllib.urlopen("http://service.canal-plus.com/video/rest/getMEAs/cplus/249"))
counter = 0
for i in maintree.findall('.//MEA/ID'):
counter += 1
tree = ET.parse(urllib.urlopen("http://service.canal-plus.com/video/rest/getVideos/cplus/" + i.text))
playlist = m3u8.load(ET.tostring(tree.findall('.//VIDEOS/HLS')[0], method='text').decode("ascii"))
name = tree.findall('.//TITRAGE/TITRE')[0].text + " - " + tree.findall('.//TITRAGE/SOUS_TITRE')[0].text
fe = fg.add_entry()
fe.id("http://service.canal-plus.com/video/rest/getVideos/cplus/" + i.text)
fe.title(name)
fe.description(tree.findall('.//INFOS/DESCRIPTION')[0].text)
#fe.published(downloaded[download]["published"])
fe.link({'href':"http://service.canal-plus.com/video/rest/getVideos/cplus/" + i.text, 'rel':'alternate', 'type':'text/html'})
fe.author({'name':ET.tostring(tree.findall('.//INFOS/AUTEUR')[0], method='text').decode("utf-8")})
fe.category({'term': ET.tostring(tree.findall('.//RUBRIQUAGE/UNIVERS')[0], method='text').decode("utf-8")})
fe.podcast.itunes_image(ET.tostring(tree.findall('.//IMAGES/GRAND')[0], method='text').decode("utf-8"))
if counter > MAX_ITEMS : break
if i.text in downloaded_cpy :
del downloaded_cpy[downloaded_cpy.index(i.text)]
fe.enclosure(ROOT + i.text + ".mp4", type = "video/mp4", length = str(os.path.getsize(i.text + ".mp4")))
continue
else : downloaded.append(i.text)
print ("Processing " + name)
for chunk in playlist.playlists :
maximum = chunk.uri
filelist = m3u8.load(maximum)
urllist = []
for segment in filelist.segments:
urllist.append(segment.uri)
urllist.sort(key = lambda a : int(re.search(r"\/segment(\d+)_.+\.ts+", a).group(1)))
for uri in urllist :
print("Downloading part " + re.search(r"\/segment(\d+)_.+\.ts+", uri).group(1))
dlobject = urllib.urlopen(uri)
shutil.copyfileobj(dlobject, open(i.text + ".mp4", "ab"))
fe.enclosure(ROOT + i.text + ".mp4", type = "video/mp4", length = str(os.path.getsize(i.text + ".mp4")))
json.dump(downloaded, open("downloads.json", "w"))
for i in downloaded_cpy :
del downloaded[downloaded.index(i)]
os.remove(i + ".mp4")
json.dump(downloaded, open("downloads.json", "w"))
fg.rss_file(RSS_FILENAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment