# Embedded file name: /usr/lib/enigma2/python/Plugins/Extensions/Albatros/jag.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_(name, link, img):
    res = [(name, link, img)]
    res.append(MultiContentEntryText(pos=(0, 0), size=(800, 40), font=8, text=name, flags=RT_HALIGN_CENTER | RT_VALIGN_CENTER))
    return res


class jagobd(Screen):

    def __init__(self, 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()
        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,
         'red': self.exit,
         'cancel': self.exit}, -1)
        self['menulist'] = m2list([])
        self['title'] = Label('jagobd.com')
        self['red'] = Label(_('Exit'))
        self['green'] = Label('')
        self['yellow'] = Label(_(' '))
        self['blue'] = Label('')
        self['Menu'] = Label(_(' '))
        self['text'] = Label('')
        self['name'] = 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.dir()
        self.onLayoutFinish.append(self.abc)

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

    def jobix(self):
        if JobManagerInstalled:
            self.session.open(JobManagerFrontEnd)

    def dnitnow(self):
        if JobManagerInstalled and self.check == 'letter_movie':
            self.dnfile = 'True'
            link = self['menulist'].getCurrent()[0][1]
            self.filmname = self['menulist'].getCurrent()[0][0]
            self.get_play_link(self.filmname, link)

    def up(self):
        self[self.currentList].up()
        self.load_poster()

    def down(self):
        self[self.currentList].down()
        self.load_poster()

    def left(self):
        self[self.currentList].pageUp()
        self.load_poster()

    def right(self):
        self[self.currentList].pageDown()
        self.load_poster()

    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 abc(self):
        self.loading_ok = False
        self.check = 'abc'
        self.currentList = 'menulist'
        self.letter_movielist = []
        self['menulist'].selectionEnabled(1)
        self['menulist'].show()
        self.genre_name = 'Al Cinema'
        self.load_data_channels()

    def load_data_channels(self):
        self.index = '1'
        self.letter_list = []
        bangalian_list = [('http://www.jagobd.com/atn-bangla.html', 'ATN Bangla', 'http://www.jagobd.com/wp-content/uploads/2015/07/ATNBanglaLogo.jpg'),
         ('http://www.jagobd.com/atnislamictv.html', 'ATN Islamic TV', 'http://www.jagobd.com/wp-content/uploads/2014/09/atnislamictv1.jpg'),
         ('http://www.jagobd.com/atnmusic.html', 'ATN Music', 'http://www.jagobd.com/wp-content/uploads/2014/06/atnmusic.png'),
         ('http://www.jagobd.com/atnnews.html', 'ATN News', 'http://www.jagobd.com/wp-content/uploads/2012/06/atnnwsa.jpg'),
         ('http://www.jagobd.com/banglatvuk.html', 'Bangla TV UK', 'http://www.jagobd.com/wp-content/uploads/2012/09/nFj5E.jpg'),
         ('http://www.jagobd.com/bangla21tv.html', 'Bangla21TV', 'http://www.jagobd.com/wp-content/uploads/2012/09/Bangla21.tv_.jpg'),
         ('http://www.jagobd.com/banglavision.html', 'Banglavision', 'http://www.jagobd.com/wp-content/uploads/2012/09/Banglavision.png'),
         ('http://www.jagobd.com/bijoytv.html', 'Bijoy TV', 'http://www.jagobd.com/wp-content/uploads/2013/05/bijoytvs.jpg'),
         ('http://www.jagobd.com/boishakhitv.html', 'Boishakhi TV', 'http://www.jagobd.com/wp-content/uploads/2015/10/Z8iRD.jpg'),
         ('http://www.jagobd.com/btvworld.html', 'Btv World', 'http://www.jagobd.com/wp-content/uploads/2012/08/Fusion5_1260301374_1-BTV_World_300-250x300.jpg'),
         ('http://www.jagobd.com/channel24.html', 'Channel 24', 'http://www.jagobd.com/wp-content/uploads/2012/09/UDmzF.jpg'),
         ('http://www.jagobd.com/channeli.html', 'Channel i BD', 'http://www.jagobd.com/wp-content/uploads/2013/04/channel_ise.jpg'),
         ('http://www.jagobd.com/channelieurope.html', 'Channel i Europe', 'http://www.jagobd.com/wp-content/uploads/2012/09/Channel+i+Europe.jpg'),
         ('http://www.jagobd.com/channelsuk.html', 'Channel S UK', 'http://www.jagobd.com/wp-content/uploads/2012/09/IGmTP-260x300.jpg'),
         ('http://www.jagobd.com/ekattortv.html', 'Ekattor TV', 'http://www.jagobd.com/wp-content/uploads/2012/08/ekattor-upsd1.jpg'),
         ('http://www.jagobd.com/ekushey-tv.html', 'Ekushey TV', 'http://www.jagobd.com/wp-content/uploads/2012/09/ekushey-tv.jpg'),
         ('http://www.jagobd.com/gaanbangla.html', 'GaanBangla TV', 'http://www.jagobd.com/wp-content/uploads/2013/11/gb1.jpg'),
         ('http://www.jagobd.com/independent.html', 'Independent TV', 'http://www.jagobd.com/wp-content/uploads/2012/09/inde.jpg'),
         ('http://www.tv.jagobd.com/embed.php?u=jamunass&vw=100%&vh=400', 'Jamuna Television', 'http://www.jagobd.com/wp-content/uploads/2013/11/jamunatv6.jpg'),
         ('http://www.jagobd.com/millenniumtv.html', 'MillenniumTV (Bangladesh)', 'http://www.jagobd.com/wp-content/uploads/2014/02/millenniumtvsm.jpg'),
         ('http://www.jagobd.com/mohonatv.html', 'Mohona TV', 'http://www.jagobd.com/wp-content/uploads/2012/09/rhsaN.jpg'),
         ('http://www.jagobd.com/moviebanglatv.html', 'Movie BanglaTV', 'http://www.jagobd.com/wp-content/uploads/2014/09/mbangla1.jpg'),
         ('http://www.jagobd.com/mytv.html', 'My TV', 'http://www.jagobd.com/wp-content/uploads/2012/09/sIJRE.jpg'),
         ('http://www.jagobd.com/natunsomoy.html', 'Natunsomoy TV', 'http://www.jagobd.com/wp-content/uploads/2015/06/natunsomoys.jpg'),
         ('http://www.jagobd.com/ntveurope.html', 'Ntv Europe', 'http://www.jagobd.com/wp-content/uploads/2012/10/ntvuk.jpg'),
         ('http://www.jagobd.com/satv.html', 'SA TV (Bangladesh)', 'http://www.jagobd.com/wp-content/uploads/2013/02/SATV.jpg'),
         ('http://www.jagobd.com/satveurope.html', 'SA TV (Europe)', 'http://www.jagobd.com/wp-content/uploads/2014/04/satveurope.jpg'),
         ('http://www.jagobd.com/somoynews.html', 'Somoy News', 'http://www.jagobd.com/wp-content/uploads/2012/09/Somoy+TV+logo1.jpg'),
         ('http://www.jagobd.com/tbn24.html', 'TBN 24 TV', 'http://www.jagobd.com/wp-content/uploads/2014/08/tbn24.jpg'),
         ('http://www.jagobd.com/timetv.html', 'Time Televison', 'http://www.jagobd.com/wp-content/uploads/2015/11/timetvs.jpg'),
         ('http://www.jagobd.com/uvislamictv.html', 'Universal Vision', 'http://www.jagobd.com/wp-content/uploads/2015/05/uvstudio.jpg')]
        for each in bangalian_list:
            url, title, img = each
            self.letter_list.append(show_(title, url, img))

        self['menulist'].l.setList(self.letter_list)
        self['menulist'].l.setItemHeight(40)
        self['menulist'].moveToIndex(0)
        self.load_poster()

    def ok(self):
        rest = ' flashVer=WIN%2015,0,0,189 app=live/ swfUrl=http://p.jwpcdn.com/6/6/jwplayer.flash.swf pageUrl=http://yoolive.com live=1 timeout=15'
        from cookielib import CookieJar, DefaultCookiePolicy
        if self.check == 'abc':
            cj = cookielib.CookieJar()
            opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
            selectedEntry = self['menulist'].l.getCurrentSelection()[0][1]
            selectedName = self['menulist'].l.getCurrentSelection()[0][0]
            self.loading_ok = False
            opener.addheaders = [('Referer', 'http://www.jagobd.com/banglavision.html')]
            opener.addheaders = [('Host', 'www.tv.jagobd.com')]
            opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')]
            page = opener.open(selectedEntry)
            response = page.read()
            page.close()
            print response
#<div class="stremb">
#																								<a href="//tv.jagobd.com/embed.php?u=bvisionhq&amp;vw=100%&amp;vh=400" target="ifram2">HD</a>
            iframe = re.findall('<div class="stremb">.*?<a href="(.*?)" target="ifram2">', response,re.DOTALL)
            m3u8 = re.findall('file:"(http://.*?)"', response)
            linki = re.findall('</div><p><iframe src="(.*?)" name="ifram2"', response)
            print linki
            if linki:
                linki = 'http:' + linki[0]
                try:
                    req1 = urllib2.Request(linki)
                    req1.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
                    req1.add_header('Referer', selectedEntry)
                    req1.add_header('Host', 'www.tv.jagobd.com')
                    response = urllib2.urlopen(req1)
                    link = response.read()
                    print response.info()
                    rtmp_link = re.findall('file:"(http://.*?)"', link)
                    if rtmp_link:
                        print rtmp_link
                        final_url = rtmp_link[0]
                        self.play_that_shit(final_url)
                    else:
                        link = re.findall('<a href="(.*?m3u8.*?)" >', link)
                        if link:
                            print link
                            final_url = link[0]
                            self.play_that_shit(final_url)
                except:
                    print 'i am here'

            elif iframe:
                print iframe
                iframe = 'http:' + iframe[0]
                if 'http://www.tv.jagobd.com/embed.php?u=atnnewsoder' in iframe:
                    iframe = 'http://www.tv.jagobd.com/embed.php?u=atnnewsss&vw=100%&vh=400'
                elif 'http://www.tv.jagobd.com/embed.php?u=natunsomoyhd' in iframe:
                    iframe = 'http://www.tv.jagobd.com/embed.php?u=natunsomoy&vw=100%&vh=400'
                elif 'http://www.jagobd.com/official' in iframe:
                    iframe = 'http://www.tv.jagobd.com/embed.php?u=tbn24&vw=100%&vh=400'
                elif 'http://www.tv.jagobd.com/embed.php?u=btvbds' in iframe:
                    iframe = 'http://www.tv.jagobd.com/embed.php?u=btvworlds&vw=100%&vh=400'
                elif 'http://www.tv.jagobd.com/embed.php?u=ekattor' in iframe:
                    iframe = 'http://www.tv.jagobd.com/embed.php?u=ekattor12&vw=100%&vh=400'
                elif 'http://www.tv.jagobd.com/embed.php?u=gboff' in iframe:
                    iframe = 'http://www.tv.jagobd.com/embed.php?u=gaanbss&vw=100%&vh=400'
                elif 'www.tv.jagobd.com/embed.php?u=bijoy16lo' in iframe:
                    iframe = 'https://www.tv.jagobd.com/embed.php?u=bijoy16lo&vw=100%&vh=400'
                try:
                    req1 = urllib2.Request(iframe)
                    req1.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
                    req1.add_header('Referer', selectedEntry)
                    req1.add_header('Host', 'www.tv.jagobd.com')
                    response = urllib2.urlopen(req1)
                    link = response.read()
                    print response.info()
                    if 'embed.php?u=bijoy16lo' in iframe:
                        rtmp = 'rtmpe://live.jagobd.com/live app=live swfUrl=https://tv.jagobd.com/player/player.swf playpath=bijoy00.stream token=%bvmve(nKa@#'
                        self.play_that_shit(rtmp)
                    else:
                        rtmp_link = re.findall('file:"(http://.*?)"', link)
                        if rtmp_link:
                            print rtmp_link
                            final_url = rtmp_link[0]
                            self.play_that_shit(final_url)
                        else:
                            link = re.findall('<a href="(.*?m3u8.*?)" >', link)
                            if link:
                                print link
                                final_url = link[0]
                                self.play_that_shit(final_url)
                except:
                    pass

    def play_that_shit(self, data):
        selectedEntry = self['menulist'].l.getCurrentSelection()[0][1]
        selectedName = self['menulist'].l.getCurrentSelection()[0][0]
        sref = eServiceReference(4097, 0, data)
        sref.setName(selectedName)
        self.session.open(MoviePlayer, sref)

    def exit(self):
        self.close()

    def load_poster(self):
        jp_link = self['menulist'].getCurrent()[0][2]
        tmp_image = jpg_store = '/tmp/teledunet_poster.jpg'
        try:
            downloadPage(jp_link, tmp_image).addCallback(self.downloadPic, tmp_image)
        finally:
            print "Error: can't find file or read data"

    def show_cover(self):
        jpg_link = '/usr/lib/enigma2/python/Plugins/Extensions/teledunet/images/'
        jpg_name = 'no_cover.png'
        jpg_store = jpg_link + jpg_name
        if not os.path.exists(jpg_store):
            self.downloadPic(file_name, jpg_store)
        else:
            self.poster_resize(jpg_store)

    def downloadPic(self, data, jpg_store):
        if fileExists(jpg_store):
            self.poster_resize(jpg_store)
        else:
            print '[teledunet] logo not found'

    def poster_resize(self, poster_path):
        self['poster'].instance.setPixmap(None)
        self['poster'].hide()
        sc = AVSwitch().getFramebufferScale()
        self.picload = ePicLoad()
        size = self['poster'].instance.size()
        self.picload.setPara((size.width(),
         size.height(),
         sc[0],
         sc[1],
         False,
         1,
         '#FF000000'))
        if self.picload.startDecode(poster_path, 0, 0, False) == 0:
            ptr = self.picload.getData()
            if ptr != None:
                self['poster'].instance.setPixmap(ptr)
                self['poster'].show()
            else:
                print '[teledunet] no cover.. error'
        return

    def stream_not_found(self):
        message = self.session.open(MessageBox, _('Stream not found, try again later on'), MessageBox.TYPE_INFO, timeout=3)

    def errorload2(self, error):
        print '[teledunet] error %s' % error
        count_movies = len(self.letter_movielist)
        self.loading_ok = True
        self.loading = 0
        self.count = 0

    def errorload(self, error):
        print error

    def dir(self):
        if not pathExists('/tmp/teledunet/'):
            os.system('mkdir /tmp/teledunet/')
        else:
            print '[teledunet] /tmp/teledunet/ allready present'