# Embedded file name: /usr/lib/enigma2/python/Plugins/Extensions/Albatros/tvsporthd.py
from setup import *
from imports import *

class m2list(MenuList):

    def __init__(self, list):
        MenuList.__init__(self, list, False, eListboxPythonMultiContent)
        self.l.setFont(0, gFont('Regular', 14))
        self.l.setFont(1, gFont('Regular', 16))
        self.l.setFont(2, gFont('Regular', 18))
        self.l.setFont(3, gFont('Regular', 20))
        self.l.setFont(4, gFont('Regular', 22))
        self.l.setFont(5, gFont('Regular', 24))
        self.l.setFont(6, gFont('Regular', 26))
        self.l.setFont(7, gFont('Regular', 28))
        self.l.setFont(8, gFont('Regular', 32))


def show_(letter, link, img):
    res = [(letter, link, img)]
    res.append(MultiContentEntryText(pos=(0, 0), size=(800, 40), font=8, text=letter, flags=RT_HALIGN_CENTER | RT_VALIGN_CENTER))
    return res


class tvsporthd(Screen):

    def __init__(self, session):
        global old_ref
        sz_w = getDesktop(0).size().width()
        if sz_w == 1920:
            if config.Albatros.skin.value=='1':
                path = "/usr/lib/enigma2/python/Plugins/Extensions/Albatros/skin/defaultListScreen_new.xml"
            elif config.Albatros.skin.value=='2':
                path = "/usr/lib/enigma2/python/Plugins/Extensions/Albatros/skin2/defaultListScreen_new.xml"
            elif config.Albatros.skin.value=='3':
                path = "/usr/lib/enigma2/python/Plugins/Extensions/Albatros/skin3/defaultListScreen_new.xml"
        else:
            if config.Albatros.skin.value=='1':
                path = "/usr/lib/enigma2/python/Plugins/Extensions/Albatros/skin/defaultListScreen.xml"
            elif config.Albatros.skin.value=='2':
                path = "/usr/lib/enigma2/python/Plugins/Extensions/Albatros/skin2/defaultListScreen.xml"
            elif config.Albatros.skin.value=='3':
                path = "/usr/lib/enigma2/python/Plugins/Extensions/Albatros/skin3/defaultListScreen.xml"
        with open(path, 'r') as f:
            self.skin = f.read()
            f.close()
        self.session = session
        Screen.__init__(self, session)
        self['actions'] = ActionMap(['OkCancelActions',
         'ColorActions',
         'DirectionActions',
         'MovieSelectionActions'], {'up': self.up,
         'down': self.down,
         'left': self.left,
         'right': self.right,
         'ok': self.ok,
         'cancel': self.exit,
         'red': self.exit}, -1)
        self['menulist'] = m2list([])
        self['red'] = Label(_('Exit'))
        self['green'] = Label('')
        self['yellow'] = Label(_(' '))
        self['blue'] = Label('')
        self['Menu'] = Label(_(' '))
        self['title'] = Label('tv-sport-hd')
        self['name'] = Label('')
        self['text'] = Label('')
        self['poster'] = Pixmap()
        self.dnfile = 'False'
        self.currentList = 'menulist'
        self.menulist = []
        self.loading_ok = False
        self.check = 'abc'
        self.count = 0
        self.loading = 0
        self.index = ''
        old_ref = self.session.nav.getCurrentlyPlayingServiceReference()
        link = 'http://www.tv-sport-hd.com/lista/stream.html'
        getPage(link, timeout=8, method='GET', headers={'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
         'Content-Type': 'application/x-www-form-urlencoded'}).addCallback(self.get_matches)

    def about(self):
        self.session.open(AboutScreen)

    def up(self):
        self[self.currentList].up()
        auswahl = self['menulist'].getCurrent()[0][0]
        self['name'].setText(auswahl)

    def down(self):
        self[self.currentList].down()
        auswahl = self['menulist'].getCurrent()[0][0]
        self['name'].setText(auswahl)

    def left(self):
        self[self.currentList].pageUp()
        auswahl = self['menulist'].getCurrent()[0][0]
        self['name'].setText(auswahl)

    def right(self):
        self[self.currentList].pageDown()
        auswahl = self['menulist'].getCurrent()[0][0]
        self['name'].setText(auswahl)

    def switchList(self):
        if self.currentList == 'menulist':
            self['ButtonBlue'].hide()
            self['TextBlue'].hide()
            self['menulist'].selectionEnabled(0)
            self.currentList = 'menulist2'
            self.check = 'abc'
        else:
            self['ButtonBlue'].show()
            self['TextBlue'].show()
            self['menulist'].selectionEnabled(1)
            self.currentList = 'menulist'
            self.check = 'letter_movie'

    def get_matches(self, data):
        print data
        self.matchlist = []
        matches = re.findall('<font color="red"><b><span class="footzone">(.*?)</span><b></font>.*?<font color="red"><b><span class="footzone">(.*?)</span><b></font>.*?<font color="green">(.*?)</font>.*?<font color=".*?">(.*?)</font><b> </td>.*?onclick="window\\.location=\'(.*?)\'"', data, re.DOTALL)
        matches = matches[5:]
        print matches
        for time1, time2, liga, title, url in matches:
            info = time1 + '-' + time2 + ':' + title
            url = 'http://www.tv-sport-hd.com' + url
            self.matchlist.append(show_(info, url, None))

        self['menulist'].l.setList(self.matchlist)
        self['menulist'].l.setItemHeight(40)
        self['menulist'].moveToIndex(0)
        return

    def get_iframe(self, data):
        print data
        link = re.findall('<iframe scrolling="no" src="(.*?)" frameborder="0"', data)
        print link
        if link:
            link = link[0]
            getPage(link, timeout=8, method='GET', headers={'Cookie': '__cfduid=dc63b41e248ed7f5ee22a3fbaf38d4dd41448314717; _ugtopperm=1; noadvtday=0; _ugtopsecure=1; _ga=GA1.2.975292953.1448314726',
             'Referer': 'http://www.tv-sport-hd.com/lista/link1/dolce1_f.html',
             'Host': 'www.tv-sport-hd.com',
             'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
             'Content-Type': 'application/x-www-form-urlencoded'}).addCallback(self.get_id, link)

    def get_id(self, data, url):
        print data
        id = re.findall('<script type=\'text/javascript\'>fid=\'(.*?)\'; v_width=700; v_height=440;</script><script type=\'text/javascript\' src=\'http://www.rocktv.co/playergo.js\'></script>', data)
        if id:
            uri = 'http://rocktv.co/embedgox.php?live=%s&vw=700&vh=440' % id[0]
            getPage(uri, timeout=8, method='GET', headers={'Referer': url,
             'Host': 'rocktv.co',
             'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
             'Content-Type': 'application/x-www-form-urlencoded'}).addCallback(self.get_p2purl, url)
        else:
            id = re.findall("<center><script type='text/javascript'>id='(.*?)'.*?src='http://js.p2pcast.tv/p2pcast/player.js'></script>", data)
            if id:
                uri = 'http://p2pcast.tv/stream.php?id=%s&live=0&p2p=0&stretching=uniform' % id[0]
                getPage(uri, timeout=8, method='GET', headers={'Referer': url,
                 'Host': 'p2pcast.tv',
                 'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
                 'Content-Type': 'application/x-www-form-urlencoded'}).addCallback(self.get_p2purl, uri)
            else:
                id = re.findall("id='(.*?)'.*?src='http://weplayer.pw/player.js'></script>", data)
                print id
                uri = 'http://weplayer.pw/stream.php?id=%s&width=700&height=440&stretching=' % id[0]
                getPage(uri, timeout=8, method='GET', headers={'Referer': url,
                 'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
                 'Content-Type': 'application/x-www-form-urlencoded'}).addCallback(self.get_p2purl, uri)

    def get_p2purl(self, data, url):
        import base64
        print data
        curl = re.findall('file: "(.*?)"', data)
        if curl:
            print curl
            self.play_that_shit(curl[0], url)
        else:
            id = re.findall("id='(.*?)'.*?src='http://deltatv.pw/player.js'></script>", data)
            if id:
                print id
                fin = 'rtmp://195.154.209.100:80/liverepeater' + ' playpath=' + id[0] + ' pageUrl=http://deltatv.pw/stream.php?id=132679&width=700&height=440 swfurl=http://cdn.deltatv.pw/players.swf token=Fo5_n0w?U.rA6l3-70w47ch'
                self.play_that_shit(fin, url)

    def get_final_link(self, data, url, new_url):
        print data
        token = re.findall('\\{"token":"(.*?)"\\}', data)
        print token
        if token:
            play_link = new_url + 'dyFjES21-osLSpwBwtQs5A'
            print play_link
            self.play_that_shit(play_link, url)

    def play_that_shit(self, data, url):
        self.index = 'player'
        print data
        print url
        selectedName = self['menulist'].l.getCurrentSelection()[0][0]
        #if 'liverepeater' not in data:
        p2plink = data +' swfUrl=http://ssl.p.jwpcdn.com/player/v/7.6.1/jwplayer.flash.swf pageUrl=http://rocktv.co/embedgox.php?live=spstreamdolce2x&vw=700&vh=440'

        print p2plink
        sref = eServiceReference(4097, 0, p2plink)
        sref.setName(selectedName)
        self.session.open(MoviePlayer, sref)
        #else:
        #    sref = eServiceReference(4097, 0, data)
        #    sref.setName(selectedName)
        #    self.session.open(MoviePlayer, sref)

    def ok(self):
        link = self['menulist'].l.getCurrentSelection()[0][1]
        getPage(link, timeout=8, method='GET', headers={'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
         'Content-Type': 'application/x-www-form-urlencoded'}).addCallback(self.get_iframe)

    def exit(self):
        if self.index == 'player':
            self.show()
            self.session.nav.playService(old_ref)
            self.index = ''
            self['menulist'].l.setList(self.matchlist)
            self['menulist'].l.setItemHeight(40)
            self['menulist'].moveToIndex(0)
        else:
            self.close()