# Embedded file name: /usr/lib/enigma2/python/Plugins/Extensions/ArabicEntertainment/coolsport.py
from Components.ActionMap import *
from Components.Button import Button
from Components.Label import Label
from Components.MenuList import MenuList
from Components.MultiContent import MultiContentEntryText, MultiContentEntryPixmap, MultiContentEntryPixmapAlphaTest
from Components.Pixmap import Pixmap
from enigma import ePicLoad
from Tools.LoadPixmap import LoadPixmap
from Components.AVSwitch import AVSwitch
from Components.Sources.StaticText import StaticText
from Plugins.Plugin import PluginDescriptor
from Screens.InfoBarGenerics import *
from Components.ServiceEventTracker import ServiceEventTracker, InfoBarBase
from Screens.InfoBar import MoviePlayer, InfoBar
from Screens.Screen import Screen
from Screens.MessageBox import MessageBox
from string import find
from Tools.Directories import fileExists, pathExists
from Components.config import *
from Components.ConfigList import ConfigList, ConfigListScreen
from Components.config import config, ConfigSubsection, ConfigText, getConfigListEntry, ConfigSelection, ConfigPIN
from enigma import eListboxPythonMultiContent, eListbox, gFont, RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_HALIGN_CENTER, RT_WRAP
from enigma import getDesktop, eServiceReference, iServiceInformation, iPlayableService
from enigma import loadJPG, loadPNG
from twisted.web import client
from twisted.web.client import downloadPage, getPage
from Screens.MessageBox import MessageBox
from Components.PluginComponent import plugins
import re, urllib, urllib2, os, cookielib, time
from urllib2 import urlopen, Request
from urllib import urlencode
import sha
from shahid import *
if fileExists('/usr/lib/enigma2/python/Plugins/Extensions/JobManager/plugin.pyo'):
    from Plugins.Extensions.JobManager.plugin import *
    JobManagerInstalled = True
else:
    JobManagerInstalled = False
UserAgent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.13 (KHTML, like Gecko) Chrome/24.0.1290.1 Safari/537.13'

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))


def show_letter_movie(name, link, image):
    res = [(name, link, image)]
    res.append(MultiContentEntryText(pos=(0, 0), size=(590, 24), font=4, text=name, flags=RT_HALIGN_LEFT, color=16777215, color_sel=16451625))
    return res


def show_channels(name, link):
    res = [(name, link)]
    res.append(MultiContentEntryText(pos=(0, 0), size=(590, 24), font=4, text=name, flags=RT_HALIGN_LEFT, color=16777215, color_sel=16451625))
    return res


def show_letter(letter, link):
    res = [(letter, link)]
    res.append(MultiContentEntryText(pos=(0, 0), size=(200, 16), font=1, text=letter, flags=RT_HALIGN_LEFT, color=16777215, color_sel=16451625))
    return res


class coolsport(Screen):
    skin = '\n\t<screen position="35,55" size="1210,640" title=" ">\n\t<widget name="logo" position="450,1" size="200,50" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/ArabicEntertainment/images/coolsport_logo.png" zPosition="1" alphatest="on" />\n\t<widget name="logo2" position="0,0" size="1200,630" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/ArabicEntertainment/images/logo2.png" zPosition="0" alphatest="on" />\n\t\t<widget source="session.VideoPicture" render="Pig"  position="750,120" size="340,228" zPosition="3" backgroundColor="transparent"/>\t\n\t<widget name="text" position="10,2" size="550,40" zPosition="0" font="Regular;24" halign="left" valign="center" transparent="1" foregroundColor="#ffffff" backgroundColor="#000000" />\n\t<widget name="poster" position="15,100" size="100,100" zPosition="4" alphatest="on" />\n\t<widget name="menulist" position="50,120" size="650,450" transparent="1" scrollbarMode="showOnDemand" />\n\t<widget name="ButtonRed" position="28,596" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/ArabicEntertainment/images/red.png" zPosition="10" size="150,30" transparent="1" alphatest="on" />\n\t<widget name="TextRed" position="28,600" size="140,24" valign="center" halign="center" zPosition="11" font="Regular;18" transparent="1" />\n\t<widget name="ButtonGreen" position="196,596" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/ArabicEntertainment/images/green.png" zPosition="10" size="150,30" transparent="1" alphatest="on" />\n\t<widget name="TextGreen" position="196,600" size="140,24" valign="center" halign="center" zPosition="11" font="Regular;18" transparent="1" />\t\t\t\n\t<widget name="ButtonBlue" position="532,596" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/ArabicEntertainment/images/blue.png" zPosition="10" size="150,30" transparent="1" alphatest="on" />\n\t<widget name="TextBlue" position="532,600" size="140,24" valign="center" halign="center" zPosition="11" font="Regular;18" transparent="1" />\n\t<widget name="ButtonYellow" position="364,596" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/ArabicEntertainment/images/yellow.png" zPosition="10" size="150,30" transparent="1" alphatest="on" />\n\t<widget name="TextYellow" position="364,600" size="140,24" valign="center" halign="center" zPosition="11" font="Regular;18" transparent="1" />\n\t<widget name="plugin_logo" position="750,330" size="390,260" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/ArabicEntertainment/images/logo_coolsport.png" zPosition="1" alphatest="on" />\n\t</screen>'

    def __init__(self, session):
        self.skin = coolsport.skin
        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,
         'yellow': self.about,
         'blue': self.dnitnow}, -1)
        self['menulist'] = m2list([])
        self['plugin_logo'] = Pixmap()
        self['ButtonBlue'] = Pixmap()
        self['ButtonRed'] = Pixmap()
        self['ButtonGreen'] = Pixmap()
        self['ButtonYellow'] = Pixmap()
        self['TextBlue'] = Label('Download')
        self['TextRed'] = Label('Exit')
        self['TextGreen'] = Label('Search')
        self['TextYellow'] = Label('About')
        self['TextGreen'].hide()
        self['TextBlue'].hide()
        self['logo'] = Pixmap()
        self['logo2'] = Pixmap()
        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.onLayoutFinish.append(self.abc)

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

    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()

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

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

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

    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.letter_list = []
        cmd = 'wget http://coolsport.se/ -O /tmp/cool.html &'
        os.system(cmd)
        os.system('sleep 2')
        file = '/tmp/cool.html'
        f = open(file, 'r')
        input = f.read()
        print input
        matches = re.findall('<font color="gold">.*?</font>(.*?)<font color="red">(.*?)</font><p>', input, re.DOTALL)
        print matches
        for each in matches:
            match, url = each
            url = url.replace(' ', '')
            url = url.lower()
            if url == 'stream1':
                url = url
            elif url == 'stream2':
                url = 'rtmp://37.220.32.44:443/liverepeater playpath=2 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=2&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream3':
                url = 'rtmp://37.220.36.68:443/liverepeater playpath=3 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=3&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream4':
                url = 'rtmp://37.220.36.68:443/liverepeater playpath=4 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=4&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream5':
                url = 'rtmp://37.220.32.50:443/liverepeater playpath=5 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=5&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream6':
                url = 'rtmp://37.220.32.56:443/liverepeater playpath=6 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=6&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream7':
                url = url
            elif url == 'stream8':
                url = 'rtmp://37.220.36.68:443/liverepeater playpath=8 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=8&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream9':
                url = 'rtmp://37.220.32.56:443/liverepeater playpath=9 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=9&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream10':
                url = 'rtmp://37.220.36.18:443/liverepeater playpath=10 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=10&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream11':
                url = 'rtmp://37.220.32.56:443/liverepeater playpath=11 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=11&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream12':
                url = 'rtmp://37.220.32.55:443/liverepeater playpath=12 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=12&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream13':
                url = 'rtmp://37.220.32.50:443/liverepeater playpath=13 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=13&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream14':
                url = 'rtmp://37.220.32.53:443/liverepeater playpath=14 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=14&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream15':
                url = url
            elif url == 'stream16':
                url = 'rtmp://37.220.32.54:443/liverepeater playpath=16 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=16&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream17':
                url = 'rtmp://37.220.34.61:443/liverepeater playpath=17 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=17&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream18':
                url = 'rtmp://37.220.32.54:443/liverepeater playpath=18 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=18&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream19':
                url = 'rtmp://37.220.36.64:443/liverepeater playpath=19 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=19&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream20':
                url = 'rtmp://37.220.34.18:443/liverepeater playpath=20 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=20&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream21':
                url = 'rtmp://37.220.32.56:443/liverepeater playpath=21 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=21&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream22':
                url = 'rtmp://37.220.32.44:443/liverepeater playpath=21 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=21&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream23':
                url = 'rtmp://37.220.32.52:443/liverepeater playpath=23 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player3.php?id=23&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream24':
                url = url
            elif url == 'stream25':
                url = 'rtmp://37.220.34.18:443/liverepeater playpath=25 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=25&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream26':
                url = 'rtmp://37.220.36.69:443/liverepeater playpath=57 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=57&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream27':
                url = 'rtmp://37.220.34.61:443/liverepeater playpath=27 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=27&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream28':
                url = 'rtmp://37.220.32.52:443/liverepeater playpath=28 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=28&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream29':
                url = url
            elif url == 'stream30':
                url = 'rtmp://37.220.32.44:443/liverepeater playpath=30 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=30&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream31':
                url = 'rtmp://37.220.36.69:443/liverepeater playpath=31 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=31&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream32':
                url = 'rtmp://37.220.36.69:443/liverepeater playpath=6 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=6&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream33':
                url = 'rtmp://37.220.32.53:443/liverepeater playpath=33 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=33&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream34':
                url = 'rtmp://37.220.32.52:443/liverepeater playpath=34 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=34&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream35':
                url = 'rtmp://37.220.32.56:443/liverepeater playpath=35 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=35&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream36':
                url = 'rtmp://37.220.36.65:443/liverepeater playpath=36 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=36&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream37':
                url = url
            elif url == 'stream38':
                url = 'rtmp://37.220.32.44:443/liverepeater playpath=38 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=38&width=640&height=460 token=#atd%#$ZH'
            elif url == 'stream39':
                url = url
            elif url == 'stream40':
                url = 'rtmp://37.220.36.65:443/liverepeater playpath=40 swfUrl=http://static.surk.tv/atdedead.swf pageUrl=http://filotv.pw/player2.php?id=40&width=640&height=460 token=#atd%#$ZH'
            print url
            self.letter_list.append(show_channels(match, url))

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

    def ok(self):
        if self.check == 'abc':
            selectedEntry = self['menulist'].l.getCurrentSelection()[0][1]
            selectedName = self['menulist'].l.getCurrentSelection()[0][0]
            self.loading_ok = False
            url = selectedEntry
            print url
            self.play_that_shit(url)

    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 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 '[coolsport] 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 main(session, **kwargs):
    session.open(coolsport)


def Plugins(path, **kwargs):
    global plugin_path
    plugin_path = path
    return [PluginDescriptor(name='coolsport', description='coolsport Streamer', where=[PluginDescriptor.WHERE_PLUGINMENU], fnc=main, icon='plugin.png')]