# -*- coding: utf-8 -*-
from imports import *
from about import *
cj={}
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_(name, link, image):
    res = [(name, link, image)]
    res.append(MultiContentEntryText(pos=(0, 0), size=(800, 40), font=6, text=name, flags=RT_HALIGN_CENTER | RT_VALIGN_CENTER))
    return res

class goal2hd(Screen):
    def __init__(self, session):
        self.session = session
        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()
        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(_('About'))
        self['blue'] = Label('')
        self['Menu'] = Label('')
        self['text'] = Label('')
        self['name'] = Label(' ')
        self['poster'] = Pixmap()
        self['title'] = Label('Goal2HD')
        self.menulist = []
        global old_ref
        old_ref=self.session.nav.getCurrentlyPlayingServiceReference()
        self.onLayoutFinish.append(self.list_channels)
	
    def list_channels(self):
        self.info='list_ch'	
        self.ch_list=[]
        goal2hd_list=[("astro SUPERSPORT 1","http://www.goal2hd.com/channel/astro-sport-1","http://www.goal2hd.com/data/channel-image/7254b4c9bf53982d2a795ebb47726422.png"),
        ("astro SUPERSPORT 2","http://www.goal2hd.com/channel/astro-sport-2","http://www.goal2hd.com/data/channel-image/9d7dc53ff77559e5b0a3884661fc9c47.png"),
        ("astro SUPERSPORT 3","http://www.goal2hd.com/channel/astro-sport-3","http://www.goal2hd.com/data/channel-image/abf72cb5e19f7100daa1410b2756feca.png"),
        ("astro SUPERSPORT 4","http://www.goal2hd.com/channel/astro-sport-4","http://www.goal2hd.com/data/channel-image/4f11ce75dd4be665df1e8f09660caa9f.png"),
        ("Fox SPORTS","http://www.goal2hd.com/channel/fox","http://www.goal2hd.com/data/channel-image/2bcb399f99b18147251669f3be45351f.png"),
        ("Fox SPORTS 2","http://www.goal2hd.com/channel/fox-2","http://www.goal2hd.com/data/channel-image/1e7037590124b9fd274ee7873e4822aa.png"),
        ("ไทยรัฐ-tv","http://www.goal2hd.com/channel/ไทยรัฐ-tv","http://www.goal2hd.com/data/channel-image/7db0f8b95c8cdf38208ee446d91adf2b.png"),
        ("PPTV HD","http://www.goal2hd.com/channel/pptv","http://www.goal2hd.com/data/channel-image/09cc928d3abd0841ef4da5085827042c.png"),
        ("true4U","http://www.goal2hd.com/channel/true-4u","http://www.goal2hd.com/data/channel-image/03ed3f9d4cc4d8aea6f513fa060530b3.png"),
        ("EXTRA 2","http://www.goal2hd.com/channel/extra-2","http://www.goal2hd.com/data/channel-image/9d27ccbffe50ed73de670acb70d8876d.png"),
        ("EXTRA 3","http://www.goal2hd.com/channel/extra-3","http://www.goal2hd.com/data/channel-image/397b94595a8a58c704cf289da28029b5.png"),
        ("EXTRA 4","http://www.goal2hd.com/channel/extra-4","http://www.goal2hd.com/data/channel-image/6274f38def547b0de91a72068f798c7a.png"),]
        for (titel,url,img) in goal2hd_list:
            self.ch_list.append(show_(titel,url,img))
        self['menulist'].l.setList(self.ch_list)
        self['menulist'].l.setItemHeight(40)
        self['menulist'].moveToIndex(0)
        auswahl = self['menulist'].getCurrent()[0][0]
        self['name'].setText(auswahl)
			
    def ok(self):
        url=self['menulist'].getCurrent()[0][1]
        print url
        src=self.get_channel_key(url)
        data=self.get_final_data(src)
        print data
        self.play_ch(data)
		
    def about(self):
        self.session.open(AboutScreen)

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

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

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

    def right(self):
        self['menulist'].pageDown()
        auswahl = self['menulist'].getCurrent()[0][0]
        self['name'].setText(auswahl)
		
    def exit(self):
        print self.info
        if re.match('(list_ch)', self.info, re.S | re.I):
            self.close(self.session, 'exit')
        elif self.info=='player':
            self.show()
            self.session.nav.playService(old_ref)
            self.info='list_ch'
            self['menulist'].l.setList(self.ch_list)
            self['menulist'].l.setItemHeight(40)
            self['menulist'].moveToIndex(0)

    def get_channel_key(self,url):
        ref='http://www.goal2hd.com/'
        host='www.goal2hd.com'
        channel_key_data=self.get_data(url,ref,host)
        src=re.findall('<iframe src="(.*?)" frameborder="0" marginwidth="0" marginheight="0" width="900" height="540px"></iframe>',channel_key_data)
        if src:        
            return src[0]
        else:
            return 'no src found'
	
    def get_final_data(self,url):
        ref='http://www.goal2hd.com/channel/true-4u'
        host='ball.qoo-p.com'
        print 'url to final_data: ' +url
        data=self.get_data(url,ref,host)
        print data
        file=re.findall('file: "(.*?)"',data)
        if file:
            file='http:'+file[0]
            return file
        else:
            return 'no file'
        

    def get_data(self,url,ref,host):
        try:
            print ref
            url=str(url)
            req = urllib2.Request(url)
            req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36')
            req.add_header('Referer',ref)
            req.add_header('Cookie','__cfduid=d6b34f697e9b76cddb620f580347942ec1475934111; HstCfa2920850=1475934113461; HstCmu2920850=1475934113461; HstCla2920850=1476550478941; HstPn2920850=1; HstPt2920850=5; HstCnv2920850=3; HstCns2920850=3; c_ref_2920850=http%3A%2F%2Fwww.yalla-shoot.com%2Fch%2Fbein2%2Fp3g-med.php; JSESSIONID=1ineyusjs34eo1lv054leazgpy')
            res = urllib2.urlopen(req)
            link=res.read()
            print link
            res.close()
            return link
        except:
            return "no data"
			
    def play_ch(self, link):
        self.info='player'	
        selectedName = self['menulist'].l.getCurrentSelection()[0][0]
        sref = eServiceReference(4097, 0, link)
        sref.setName(selectedName)
        self.session.nav.playService(sref)
        self.hide()			