Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib.request, json
- import xml.etree.ElementTree
- import datetime;
- import sys;
- try:
- from BeautifulSoup import BeautifulSoup, SoupStrainer
- except ImportError:
- from bs4 import BeautifulSoup, SoupStrainer
- def get_livestreams(country_param):
- urlString = "http://www.laola1.tv/en-"+country_param+"/live-schedule"
- urls = {}
- with urllib.request.urlopen(urlString) as response:
- soup = BeautifulSoup(response, from_encoding=response.info().get_param('charset'))
- for ul in soup.find_all('ul', class_="list-day"):
- for classname in dict(ul.attrs)["class"]:
- if "day-" in classname:
- dateparts = classname[4:].split("-")
- entrydate = datetime.date(int(dateparts[0]),int(dateparts[1]),int(dateparts[2]))
- urls[entrydate] = set([])
- for a in ul.find_all('a', href=True):
- urls[entrydate].add(a['href'])
- return urls
- def get_config(videoid):
- urlString = "http://www.laola1.tv/service/player/videoConfig.php?videoid="+str(videoid)+"&partnerid=1&language=en&format=iphone";
- with urllib.request.urlopen(urlString) as url:
- data = json.loads(url.read().decode())
- d = {}
- d["streamName"] = get_stream_access(data["video"]["streamAccess"])
- d["metaInformation"] = data["video"]["metaInformation"]
- d["startDate"] = data["video"]["startDate"]
- d["title"] = data["video"]["title"]
- return d
- def get_stream_access(url):
- req = urllib.request.Request(url, data={}, headers={'content-type': 'application/json'})
- response = urllib.request.urlopen(req)
- unas_list = json.loads(response.read().decode())
- for unas_url in unas_list["data"]["stream-access"]:
- stream_name = get_stream_name(unas_url)
- if stream_name != None:
- return stream_name
- print("Config url '" + url + "' does not work (yet)")
- return None
- def get_stream_name(unas_url):
- try:
- with urllib.request.urlopen(unas_url) as url:
- e = xml.etree.ElementTree.fromstring(url.read().decode())
- for child in e:
- if child.tag == "token":
- streamurl = child.attrib["url"]
- parts = streamurl.split("/")
- if len(parts) > 2:
- return parts[-2]
- except :
- print("Unexpected error:", sys.exc_info()[0])
- return None
- def get_video_id(path):
- urlString = "https://www.laola1.tv" + path
- with urllib.request.urlopen(urlString) as response:
- soup = BeautifulSoup(response, from_encoding=response.info().get_param('charset'))
- for meta in soup.find_all('meta', attrs={"property" : "og:image"}):
- return meta["content"].split("/")[3].split("_")[0]
- def get_video_list(daysCount, country_param):
- all_livestreams = get_livestreams(country_param)
- livestreams = set()
- for x in range(0,daysCount+1):
- livestreams = livestreams | all_livestreams[datetime.date.today() + datetime.timedelta(days=x)]
- results = []
- print(livestreams)
- for livestream in livestreams:
- print ("Currently: "+ livestream )
- video_id = get_video_id(livestream)
- result = get_config(video_id)
- if result['streamName'] == None:
- print ("Not found " + livestream)
- else:
- results.append(result)
- with open('laola1.json', 'w') as outfile:
- sorted_results = sorted(results, key=lambda k: k['startDate'])
- json.dump(sorted_results, outfile)
- #de,int,cis,at
- country_param = "de"
- dayscount = 0
- if len(sys.argv)>1:
- dayscount = int(sys.argv[1])
- if len(sys.argv)>2:
- country_param = sys.argv[2]
- get_video_list(dayscount, country_param)
Advertisement
Add Comment
Please, Sign In to add comment