#!/usr/bin/env python # coding=utf8 # # This file is part of PlayStream - enigma2 plugin to play video streams from various sources # Copyright (c) 2016 ivars777 (ivars777@gmail.com) # Distributed under the GNU GPL v3. For full terms see http://www.gnu.org/licenses/gpl-3.0.en.html # try: import json except: import simplejson as json import os import urllib2, urllib import datetime, time, re, sys import ssl if "_create_unverified_context" in dir(ssl): ssl._create_default_https_context = ssl._create_unverified_context from SourceBase import SourceBase import util API_URL = 'https://replay.lsm.lv/%s/' headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()]) headers0 = headers2dict(""" User-Agent: Mozilla/5.0 (Linux; U; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 """) import HTMLParser h = HTMLParser.HTMLParser() from YouTubeVideoUrl import YouTubeVideoUrl class Source(SourceBase): def __init__(self,country="lv",cfg_path=None): self.name = "replay" self.title = "Replay.lv (LTV)" self.img = "replay.png" self.desc = "LSM replay.lv satura skatīšanās" self.country=country self.pic_size = "327x250" #"1000x765" def get_content(self, data): print "[replay] get_content:", data source, data, path, plist, clist, params, qs = self.parse_data(data) lang = qs["lang"] if "lang" in qs else self.country content=[] content.append(("..return", "back","back.png","Return back")) if clist=="home": content.extend([ ("Live streams", "replay::tiesraide","","TV live streams"), ("Search LV", "replay::meklet?term={0}&lang=lv","","Search content LV"), ("Last videos LV", "replay::visi/jaunakie?mtype=video&page=1&lang=lv","","Last aired videos LV"), ("Last videos by categories LV", "replay::kategorijas?lang=lv","","Last videos by categories LV"), ("Last chance LV", "replay::visi/pedeja-iespeja?lang=lv","","Last videos by categories LV"), ("Videos by popularity LV", "replay::visi/popularie?mtype=video&lang=lv","","Programs by popularity"), ("All programs LV", "replay::raidijumi?mtype=video&lang=lv","","All programs by name LV"), ("Programs by categories LV", "replay::categories?lang=lv","","All programs by categories LV"), #("Channels", "replay::channels?language=%s"%self.country,"","TV channels"), ("Search RU", "replay::poisk?term={0}&lang=ru","","Search content RU"), ("Last videos RU", "replay::vse/novie?mtype=video&lang=ru","","Last aired videos RU"), ("Last videos by categories RU", "replay::kategorijas?lang=ru","","Last videos by categories RU"), ("All programs RU", "replay::peredachi?lang=ru&mtype=video","","All programs by name"), ("Programs by categories RU", "replay::categories?lang=ru","","Programs by categories RU") ]) return content ### programmu kategorijas ### elif clist=="categories": url = "https://replay.lsm.lv/lv/raidijumi" if lang =="lv" else "https://replay.lsm.lv/ru/peredachi" r = self._http_request(url) icons = { #TODO "1": "https://www.iconfinder.com/icons/314161/download/png/256", } result = re.findall(r'([^<]+)', r) for item in result: title = item[2].strip() data2 = item[1].replace("&", "&") + "&lang=%s" % item[0] #theme = item[0].split("theme=")[1] img = self.img # TODO icons["1"] desc = title content.append((title,self.name+"::"+data2,img,desc)) return content ### jaunāko raidijumu kategorijas ### elif clist=="kategorijas": url = "https://replay.lsm.lv/lv/" if lang =="lv" else "https://replay.lsm.lv/ru/" r = self._http_request(url) for m in re.finditer(r'.+?([^<]+)', r, re.DOTALL): title = m.group(3) data2 = "%s?lang=%s"%(m.group(2), m.group(1)) img = self.img desc = title content.append((title,self.name+"::"+data2,img,desc)) return content ### Raidijumi (programmas) ### elif clist in ( "raidijumi","peredachi"): r = self.call(data, lang=lang) result = re.findall(r'([^<]+)', r, re.DOTALL) for item in result: title = item[2].strip() data2 = item[1]+"?lang=%s"%item[0] img = "" desc = title content.append((title,self.name+"::"+data2,img,desc)) return content ### Tiešraides kanānālu saraksts elif path=="tiesraide": url = "https://replay.lsm.lv/lv/tiesraide/ltv1/" #
r = self._http_request(url) result = re.findall(r'class="small-guide.+?href="https://replay\.lsm\.lv/lv/([^"]+)".+?title="([^"]+)".+?
([^<]+)
.+?', r, re.DOTALL) for item in result: data2 = item[0] ch = data2.split("/")[1] title = item[1] img = ch + ".png" desc = item[2].strip() desc = desc + " [%s]" % item[3] title = title + " - " + desc content.append((title,self.name+"::"+data2,img,desc)) url = "https://ltv.lsm.lv/lv/tieshraide/" r = self._http_request(url) result = re.findall('
([^<]+)', r, re.IGNORECASE | re.DOTALL | re.MULTILINE) for item in result: data2 = item[0] ch = data2.split("/")[1] title = item[2] + " (ltv.lsm.lv)" img = "https://ltv.lsm.lv/" + item[1] desc = title content.append((title,self.name+"::"+data2,img,desc)) return content ### Meklēsana ### # TODO (jāsakrārto pēc jaunā templeita) elif clist in ("search"): r = self.call(data, lang=lang) if not r: return content #for r2 in re.findall('
(.+?)
', r2, re.DOTALL): for item in re.findall('itemprop="image" data-image="([^"]+)".+?
', r): title = item[2] data2 = item[1].replace("/%s/"%lang,"")+"?lang=%s"%lang img = "https://replay.lsm.lv" + item[0] desc = title content.append((title,self.name+"::"+data2,img,desc)) #for item in re.findall('itemprop="image" data-image="([^"]+)".+?

([^<]+)

.*?
.+?datetime="([^"]+)" class="thumbnail__date ">([^<]+)', r2): for item in re.findall('itemprop="image" data-image="([^"]+)".+? class="icon-(ltv|lr).+?

([^<]+)

.*?
.+?datetime="([^"]+)" class="thumbnail__date ">([^<]+)', r): if item[1]=="lr":continue title = "%s - %s (%s)"%(item[3],item[5],item[7]) data2 = item[4].replace("/%s/"%lang,"")+"?lang=%s"%lang img = item[0] #.replace("https:","http:") img = img.replace("lv//", "lv/") # TODO eksperiments desc = title content.append((title,self.name+"::"+data2,img,desc)) # Raidijumu saraksti elif True: r = self.call(data, lang=lang) if not r: return content result = re.findall("(.+?)
", r, re.DOTALL) for item in result: m = re.search(r'

([^<]+)

', item, re.DOTALL) if not m: continue title = m.group(3).strip() desc = title data2 = m.group(2) lang = m.group(1) m = re.search(r'style="background-image: url\(([^\)]+)\)"', item, re.DOTALL) img = m.group(1) if m else "" m = re.search('datePublished" datetime="([^"]+)"', item, re.DOTALL) if m: published = m.group(1) try: published = util.datetime_from_zulu_to_utc(published) published = util.datetime_from_utc_to_local(published) published_datetime = datetime.datetime.strftime(published,"%d.%m.%Y %H:%M") published_date = datetime.datetime.strftime(published,"%d.%m.%Y") title = title + " (%s)" % published_date desc = desc + "\nPublished: %s" % published_datetime except: a = 1 pass m = re.search('thumbnail__duration">([^<]+)<', item, re.DOTALL) if m: desc = desc + "\nDuration: %s" % m.group(1) m = re.search(r'([^<]+)', item, re.DOTALL) if m: desc = desc + "\nProgram: %s" % m.group(1) m = re.search('
  • (Vēl[^<]+)
  • ', item, re.DOTALL) if m: desc = desc + "\nRemaining: %s" % m.group(1) content.append((title,self.name+"::"+data2,img,desc)) m = re.search(r'page=(\d+)" rel="next"', r, re.DOTALL) if m: page = int(m.group(1)) if "page="in data: data2 = re.sub("page=\d+","page=%s"%page,data) else: if "?" in data: data2 =data+"&page=%s"%page else: data2 =data+"?page=%s"%page content.append(("Next page",self.name+"::"+data2,"next.png","Next page")) return content #=========================================================================== def get_streams(self, data): source, data, path, plist, clist, params, qs = self.parse_data(data) lang = "ru" if clist == 'statja' else "lv" headers2 = {} nfo = {} ### Kanāla tiesraide if clist == "tiesraide" and "/" in data or clist == "tieshraide": if clist == "tiesraide": url = "https://replay.lsm.lv/lv/" + data #r = self.call(data, lang=lang) else: url = "https://ltv.lsm.lv/lv/" + data r = self._http_request(url) ch = data.split('/')[1] veids = "audio" if "lr" in ch else "video" img = ch + ".png" m = re.search('span class="badge badge-primary">Tagad([^<]+)<.+?', r, re.DOTALL) if clist == "tieshraide": m = re.search("

    ([^<]+)

    ", r) title = m.group(1).strip() if m else ch.upper() desc = title else: tagad = m.group(1).strip() if m else "" laiks = m.group(2).strip() if m else "" laiks = h.unescape(laiks).encode("utf8") title = m.group(1).strip() if m else path.split("/")[1].upper() dd = datetime.datetime.now().strftime("%d.%m.%Y") title = "%s - %s (%s) [%s]"%(ch.upper(), title, dd, laiks) desc = title if veids == "video": m = re.search('([^<]+)', r) m = re.search('', r) title = m.group(1) title = title.replace("- REplay.lv", "") title = title.strip() m = re.search('', r) img = m.group(1) if m else "" m = re.search('
    (.+?)
    ', r, re.DOTALL) if m: plot = m.group(1) rr = re.sub("<.+?>", " ", m.group(1)) rr = re.sub("[\t\n]", " ", rr) plot = re.sub(" {2,100}", " ", rr).strip() else: plot = "" desc = [title] m = re.search(r'
    ([^<]+)', r, re.IGNORECASE) if m: desc.append("Programma: %s [%s]" % (m.group(2), m.group(1).upper())) m = re.search('
      (.+?)
    ', r, re.DOTALL) if m: for m2 in re.finditer("([^<]+) *()*([^<]+)*<", m.group(1)): desc.append("%s %s" % (m2.group(1).strip(),m2.group(3).strip() if m2.group(3) else "")) m2 = re.search('li class="[^"]*">(Pieejam.+)', m.group(1)) if m2: desc.append(m2.group(1)) desc.append(plot) desc = "\n".join(desc) nfo["title"] = title nfo["thumb"] = img #nfo["runtime"] = None #nfo["quality"] = None nfo["plot"] = desc nfo["tagline"] = plot headers = headers2dict(""" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 Referer: https://replay.lsm.lv/%s/%s """% (lang, data)) r2 = self._http_request(url2,headers=headers) m = re.search(r"", r2, re.DOTALL) #m = re.search('"file":"([^"]+)', r2) if m: js = json.loads(m.group(1)) if js["ext_video_id"]: data2 = "https://www.youtube.com/watch?v=" + js["ext_video_id"] elif "playlist" in js["player"]: data2 = js["player"]["playlist"][0]["sources"][0]["file"] elif "clip" in js["player"]: if len(js["player"]["clip"]["sources"]) > 0: data2 = js["player"]["clip"]["sources"][0]["src"] else: raise Exception("No streams found!") elif "file" in js["player"]: data2 = js["player"]["file"] else: raise Exception("No stram found") else: # futbols u.c. m = re.search('
    .+?