tusfan

Untitled

May 9th, 2017
156
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import urllib.request, json
  2. import xml.etree.ElementTree
  3. import datetime;
  4. import sys;
  5. try:
  6.     from BeautifulSoup import BeautifulSoup, SoupStrainer
  7. except ImportError:
  8.     from bs4 import BeautifulSoup, SoupStrainer
  9.  
  10. def get_livestreams(country_param):
  11.     urlString = "http://www.laola1.tv/en-"+country_param+"/live-schedule"
  12.     urls = {}
  13.     with urllib.request.urlopen(urlString) as response:
  14.         soup = BeautifulSoup(response, from_encoding=response.info().get_param('charset'))
  15.         for ul in soup.find_all('ul', class_="list-day"):
  16.             for classname in dict(ul.attrs)["class"]:
  17.                 if "day-" in classname:
  18.                     dateparts = classname[4:].split("-")
  19.                     entrydate = datetime.date(int(dateparts[0]),int(dateparts[1]),int(dateparts[2]))
  20.                     urls[entrydate] = set([])
  21.                     for a in ul.find_all('a', href=True):
  22.                         urls[entrydate].add(a['href'])
  23.     return urls            
  24.                    
  25.  
  26. def get_config(videoid):
  27.     urlString = "http://www.laola1.tv/service/player/videoConfig.php?videoid="+str(videoid)+"&partnerid=1&language=en&format=iphone";
  28.     with urllib.request.urlopen(urlString) as url:
  29.         data = json.loads(url.read().decode())
  30.         d = {}
  31.         d["streamName"] = get_stream_access(data["video"]["streamAccess"])
  32.         d["metaInformation"] = data["video"]["metaInformation"]
  33.         d["startDate"] = data["video"]["startDate"]
  34.         d["title"] = data["video"]["title"]
  35.         return d
  36.  
  37. def get_stream_access(url):
  38.     req = urllib.request.Request(url, data={}, headers={'content-type': 'application/json'})
  39.     response = urllib.request.urlopen(req)
  40.     unas_list = json.loads(response.read().decode())
  41.     for unas_url in unas_list["data"]["stream-access"]:
  42.         stream_name = get_stream_name(unas_url)
  43.         if stream_name != None:
  44.             return stream_name
  45.     print("Config url '" + url + "' does not work (yet)")
  46.     return None
  47.    
  48. def get_stream_name(unas_url):
  49.     try:
  50.         with urllib.request.urlopen(unas_url) as url:
  51.             e = xml.etree.ElementTree.fromstring(url.read().decode())
  52.             for child in e:
  53.                 if child.tag == "token":
  54.                     streamurl = child.attrib["url"]
  55.                     parts = streamurl.split("/")
  56.                     if len(parts) > 2:
  57.                         return parts[-2]
  58.     except :
  59.         print("Unexpected error:", sys.exc_info()[0])          
  60.     return None
  61.    
  62. def get_video_id(path):
  63.     urlString = "https://www.laola1.tv" + path
  64.     with urllib.request.urlopen(urlString) as response:
  65.         soup = BeautifulSoup(response, from_encoding=response.info().get_param('charset'))
  66.         for meta in soup.find_all('meta', attrs={"property" : "og:image"}):
  67.             return meta["content"].split("/")[3].split("_")[0]
  68.    
  69.  
  70. def get_video_list(daysCount, country_param):
  71.     all_livestreams = get_livestreams(country_param)
  72.     livestreams = set()
  73.     for x in range(0,daysCount+1):
  74.          livestreams = livestreams | all_livestreams[datetime.date.today() + datetime.timedelta(days=x)]
  75.     results = []
  76.     print(livestreams)
  77.     for livestream in livestreams:
  78.         print ("Currently: "+ livestream )
  79.         video_id = get_video_id(livestream)
  80.         result = get_config(video_id)
  81.         if result['streamName'] == None:
  82.             print ("Not found " + livestream)
  83.         else:
  84.             results.append(result)
  85.     with open('laola1.json', 'w') as outfile:
  86.         sorted_results = sorted(results, key=lambda k: k['startDate'])
  87.         json.dump(sorted_results, outfile)
  88. #de,int,cis,at     
  89. country_param = "de"   
  90. dayscount = 0      
  91. if len(sys.argv)>1:
  92.     dayscount = int(sys.argv[1])   
  93. if len(sys.argv)>2:
  94.     country_param = sys.argv[2]    
  95.                
  96. get_video_list(dayscount, country_param)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×