#!/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 urllib2, urllib
import datetime, re, sys,os
from collections import OrderedDict
from SourceBase import SourceBase

API_URL = 'http://replay.lsm.lv/'
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()

class Source(SourceBase):

    def __init__(self,country="lv"):
        self.name = "tvdom"
        self.title = "TVDom.tv"
        self.img = "https://tvdom.tv/front/assets/images/logo.png"
        self.desc = "TVDom.tv portāla satura skatīšanās"
        self.headers = headers0

        self.country=country
        self.session = None
        self.token = None

        cur_directory = os.path.dirname(os.path.abspath(__file__))
        self.config_file = os.path.join(cur_directory,self.name+".cfg")
        self.options = OrderedDict([("user","lietotajs"),("password","parole")])
        self.options_read()

    def login(self,user="",password=""):
        self.options_read()        
        if not user: user=self.options["user"]
        if not password: password = self.options["password"]
        headers = headers2dict("""
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: https://tvdom.tv/
        """)
        url = "https://tvdom.tv/infinity/on_register_user"
        params = "email=%s&password=%s&remember=false&auth_type=login"%(user,password)
        import requests
        r = requests.post(url, data=params, headers=headers)	
        js = json.loads(r.content)
        if 'success' in r.content:
            self.token = js["access_token"] 
            if 'PHPSESSID' in r.cookies:
                self.session = r.cookies["PHPSESSID"]
            return True
        else:
            raise Exception(js["error"])


    def get_content(self, data):
        print "[tvdom] 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","","Return back"))

        if clist=="home":
            content.extend([
                ("Live stream", "tvdom::tiesraides","","TV live streams"),
                ("Archive - categories", "tvdom::arhivs","","Video archive by categories"),
                ("Search", "tvdom::search/?srch-term={0}","","Search archive"),

                #("Archive - all", "tvdom::arhivs_all","","Video archive all"),
            ])
            return content

        ### Tiesraides kanalu saraksts ###
        elif data=="tiesraides":
            ch_name = {"49":"Дом Кино","50":"Карусель","51":"Время","52":"Музыка Первого","53":"Телекафе"}
            url = "https://tvdom.tv/"
            r = self._http_request(url)
            #channels1 = re.findall(r'data-xprs_status="\d" data-href="/tiesraides/([^/]+)/[^"]+">.+?src="([^"]+)" alt="([^"]+)">', r, re.DOTALL)
            channels2 = re.findall(r'<a class="channel-name">([^<]*)</a>\s+<div class="redirect-to-url" data-href="/tiesraides/([^/]+)/[^"]+">.+?<img style="width:100%;" src="([^"]+)".+?<h3>([^<]+)</h3>.+?<p class="unix">([^<]+)</p>', r, re.DOTALL)
            channels = {}
            for item in channels2:
                title = item[0]
                title =  h.unescape(title.decode("utf8")).encode("utf8")
                img = "https://tvdom.tv"+item[2]
                data2 = "tiesraides/%s/"%item[1]
                desc = "%s\n%s\n%s"%(title,item[3],item[4])		    
                channels[item[1]]={"title":title,"img":img,"desc":desc}
                #content.append((title,self.name+"::"+data2,img,desc))

            for r2 in re.findall(r'<div class="thumbnail-menu pull-left updater"(.+?)</div>\s+</div>', r, re.DOTALL):
                ch = re.search('data-href="/tiesraides/([^/]+)/[^"]+"', r2, re.DOTALL).group(1)
                data2 = "tiesraides/"+ch
                m = re.search('src="(.+?)" alt="(.+?)">', r2, re.DOTALL)
                title = m.group(2)
                img = m.group(1)
                desc = title
                if ch in channels:
                    img = channels[ch]["img"]
                    desc = channels[ch]["desc"]
                else:
                    m = re.search('<img src="([^"]+)" alt="aaaaaa([^"]+)"><h3>.+<p class="unix">([^<]+)</p>', r2, re.DOTALL)
                    if m:
                        desc  = "%s\n%s\n%s"%(title,m.group(1),m.group(3))
                        img = m.group(2)
                content.append((title,self.name+"::"+data2,img,desc))
            return content

        elif clist == "tiesraides":
            if not self.session:
                self.login()
            url = "https://tvdom.tv/" + data
            headers = self.headers
            headers["Cookie"] = "PHPSESSID=%s; neverending_story=1;"%self.session
            r = self._http_request(url,headers=headers)
            m = re.search("var streamConnectionUrl = '([^']+)'", r, re.DOTALL)
            if m:
                data2 = m.group(1)
            else:
                return ("No stream found %s"%data,"","","No stream found")
            m = re.search('title: "([^"]+)"', r, re.DOTALL)
            title = m.group(1) if m else data2
            m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
            desc = m.group(1) if m else title
            m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
            desc = m.group(1) if m else title 
            m = re.search('var promo_image *= "([^"]+)', r, re.DOTALL)
            img = m.group(1) if m else ""            
            return (title,data2,img,desc)

        ### Search ###
        elif clist=="search":
            url = "https://tvdom.tv/" + data
            r = self._http_request(url)
            for item in re.findall(r'<li data-xprs-search="\d+" data-href="([^"]+)".*?<img class="img-responsive" src="([^"]+)".*?<h3>([^<]+)</h3>.*?<h5>([^<]+)</h5>', r, re.DOTALL):
                title = item[2] + " "+ item[3]
                title =  h.unescape(title.decode("utf8")).encode("utf8")
                img = "https://tvdom.tv" + item[1]
                data2 = item[0][1:]
                desc = title
                content.append((title,self.name+"::"+data2,img,desc))
            return content

        ### Arhīva kategorijas ###
        elif data=="arhivs":
            url = "https://tvdom.tv/"+data
            r = self._http_request(url)
            for item in re.findall('pointer" href="/([^"]+)">([^<]+)</a>', r, re.DOTALL):
                title = item[1]
                title =  h.unescape(title.decode("utf8")).encode("utf8")
                img = ""
                data2 = item[0]
                desc = title
                content.append((title,self.name+"::"+data2,img,desc))
            return content

        ### Arhīva kategorijas programmas ###
        elif clist=="arhivs":
            url = "https://tvdom.tv/"+data
            r = self._http_request(url)
            for item in re.findall(r"""<li><div class="thumbnail pull-left" onclick="location\.href='([^']+)'" data-toggle="popover" title="([^"]+)" data-content="([^"]*)".+?<img class="img-responsive archive-image" src="([^"]+)""", r, re.DOTALL):
            #for item in re.findall(r"""<li><div class="thumbnail pull-left" onclick="location\.href='([^']+)'" data-toggle="popover" title="([^"]+)" data-content="([^"]+)".+?<img class="img-responsive archive-image" src="([^"]+)""", r, re.DOTALL):
                title = item[1].replace("&lt;br&gt;"," - ")
                title =  h.unescape(title.decode("utf8")).encode("utf8")
                img = "https://tvdom.tv"+item[3]
                data2 = item[0][1:]
                desc = item[2]
                content.append((title,self.name+"::"+data2,img,desc))
            return content

        ### Arhīva programmas video saraksts ###
        elif clist=="play_arhivs" and len(data.split("/"))==3 and not re.search("_\d+",plist[2]):
            url = "https://tvdom.tv/"+data
            r = self._http_request(url)
            vid=re.search(r"id:(\d+), type: type", r, re.DOTALL).group(1)
            data2 = data+"_"+vid
            m = re.search('program_title        = "([^"]+)"', r, re.DOTALL)
            title = m.group(1) if m else data2
            m = re.search('<a class="episode">Pārraides laiks ēterā: <span>([^<]+)</span></a>', r, re.DOTALL)
            datums = m.group(1) if m else ""
            title = title + " " + datums
            m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
            desc = m.group(1) if m else title
            m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
            desc = m.group(1) if m else title 
            m = re.search('var share_image *= "([^"]+)', r, re.DOTALL)
            img = m.group(1) if m else ""            
            content.append((title,self.name+"::"+data2,img,desc)) 
            i = r.find('<span class="slider-top-title"')
            if i>0: r = r[:i]
            for item in re.findall('<div class="col-md-9 redirect-to-url same-event" data-href="/([^"]+)">.+?image" src="([^"]+)".+?<h3 class="same-title">([^<]+)</h3>.*?<h5 class="same-online">([^<]+)</h5>', r, re.DOTALL):
                title = item[2] + " " + item[3]
                title =  h.unescape(title.decode("utf8")).encode("utf8")
                img = "https://tvdom.tv"+item[1]
                data2 = item[0]
                desc = title # TODO
                content.append((title,self.name+"::"+data2,img,desc))
            return content

        ### Arhīva video
        elif clist=="play_arhivs" and len(data.split("/"))==3 and re.search("_\d+",plist[2]):
            url = "https://tvdom.tv/" + data
            headers = self.headers
            headers["Cookie"] = "PHPSESSID=%s; neverending_story=1;"%self.session
            r = self._http_request(url,headers=headers)
            m = re.search('var streamConnectionUrl  = "([^"]+)"', r, re.DOTALL)
            if m:
                data2 = m.group(1)
            else:
                return ("No stream found %s"%data,"","","No stream found")
            m = re.search('program_title        = "([^"]+)"', r, re.DOTALL)
            title = m.group(1) if m else data2
            m = re.search('<a class="episode">Pārraides laiks ēterā: <span>([^<]+)</span></a>', r, re.DOTALL)
            datums = m.group(1) if m else ""
            title = title + " " + datums
            m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
            desc = m.group(1) if m else title
            m = re.search('<div id="panel">([^<]+)<', r, re.DOTALL)
            desc = m.group(1) if m else title 
            m = re.search('var share_image *= "([^"]+)', r, re.DOTALL)
            img = m.group(1) if m else ""            
            return (title,data2,img,desc)


    def is_video(self,data):
        source,data,path,plist,clist,params,qs = self.parse_data(data)
        cmd = data.split("/")
        if cmd[0] in ("tiesraides") and len(cmd)>1:
            return True
        elif cmd[0]=="play_arhivs" and len(cmd)==3 and re.search("_\d+",plist[2]):
            return True
        else:
            return False

    def call(self, data,headers=headers0,lang=""):
        if not lang: lang = self.country
        url = API_URL%lang + data
        #print "[TVPlay Api] url: ",url
        result = []
        content = self._http_request(url)
        return content

if __name__ == "__main__":
    country= "lv"
    c = Source(country)
    if len(sys.argv)>1:
        data= sys.argv[1]
    else:
        data = "home"
    content = c.get_content(data)
    for item in content:
        print item
    #cat = api.get_categories(country)
    #chan = api.get_channels("lv")
    #prog = api.get_programs(channel=6400)
    #prog = api.get_programs(category=55)
    #seas = api.get_seasons(program=6453)
    #str = api.get_streams(660243)
    #res = api.get_videos(802)
    #formats = api.getAllFormats()
    #det = api.detailed("1516")
    #vid = api.getVideos("13170")
    pass