Project

General

Profile

RE: Multiple services in one chanel ยป default.py

Jose Antonio, 2018-04-28 14:47

 
1
# -*- coding: utf-8 -*-
2
# Iris 9900HD XBMC Addon
3

    
4
import sys
5
import httplib
6

    
7
import urllib, urllib2, cookielib, datetime, time, re, os, string
8
import xbmcplugin, xbmcgui, xbmcaddon, xbmcvfs, xbmc
9
import cgi, gzip
10
from StringIO import StringIO
11
import json
12

    
13
canal=None
14

    
15
channelListUrl= '/json/channel/list'
16
#switchChannel= this.sendCmd('channel/switch', { cid: cid }, callback);
17

    
18
UTF8          = 'utf-8'
19

    
20
addon         = xbmcaddon.Addon('plugin.video.iris-9900-hd')
21
__addonname__ = addon.getAddonInfo('name')
22
__language__  = addon.getLocalizedString
23

    
24

    
25
home          = addon.getAddonInfo('path').decode(UTF8)
26
icon          = xbmc.translatePath(os.path.join(home, 'icon.png'))
27
addonfanart   = xbmc.translatePath(os.path.join(home, 'fanart.jpg'))
28

    
29
qp  = urllib.quote_plus
30
uqp = urllib.unquote_plus
31

    
32
def log(txt):
33
    message = '%s: %s' % (__addonname__, txt.encode('ascii', 'ignore'))
34
    xbmc.log(msg=message, level=xbmc.LOGDEBUG)
35
    
36
def cleanname(name):    
37
    return name.replace(''',"'").replace('’',"'").replace('&','&').replace(''',"'").replace('"','"')
38

    
39
def demunge(munge):
40
        try:
41
            munge = urllib.unquote_plus(munge).decode(UTF8)
42
        except:
43
            pass
44
        return munge
45

    
46

    
47
USER_AGENT    = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36'
48
defaultHeaders = {'User-Agent':USER_AGENT, 
49
                 'Accept':"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 
50
                 'Accept-Encoding':'gzip,deflate,sdch',
51
                 'Cookie': 'qlqhzyhjljcfffyy=21636d1e642c874112d9674ac8e8b002',
52
                 'Accept-Language':'en-US,en;q=0.8'} 
53

    
54
def getRequest(url, user_data=None, headers = defaultHeaders , alert=True):
55

    
56
              log("getRequest URL:"+str(url))
57
              opener = urllib2.build_opener()
58
              urllib2.install_opener(opener)
59

    
60
              log("getRequest URL:"+str(url))
61
              req = urllib2.Request(url.encode(UTF8), user_data, headers)
62

    
63
              try:
64
                 response = urllib2.urlopen(req)
65
                 if response.info().getheader('Content-Encoding') == 'gzip':
66
                    log("Content Encoding == gzip")
67
                    buf = StringIO( response.read())
68
                    f = gzip.GzipFile(fileobj=buf)
69
                    link1 = f.read()
70
                 else:
71
                    link1=response.read()
72

    
73
              except urllib2.URLError, e:
74
                 if alert:
75
                     xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' % ( __addonname__, e , 10000) )
76
                 link1 = ""
77

    
78
              if not (str(url).endswith('.zip')):
79
                 link1 = str(link1).replace('\n','')
80
              return(link1)
81

    
82

    
83
def as_complex(lista):
84
    return complex(dct['current'], dct['chnlist_head'], dct['main'], dct['subs'], dct['chnlist'])
85

    
86

    
87
def ListaCanales(fanart):
88
              ilist = []
89
              azheaders = defaultHeaders
90
              azheaders['X-Requested-With'] = 'XMLHttpRequest'
91
              url='http://' + addon.getSetting('ip_deco') + channelListUrl
92
              log('Ruta para obtener canales: ' + url)              
93
              pg = getRequest(url, None, azheaders)
94
              lista=json.loads(pg)
95
              lista2=lista['chnlist']
96
              #print(lista2)
97
              for item in lista2:
98
                    try:
99
                         #log('Nombre: ' + item['name'])
100
                         #log('ID: ' + str(item['id']))
101
                         name = cleanname(item['name']).encode(UTF8)
102
                         id = cleanname(str(item['id'])).encode(UTF8)
103
                         #log(name)
104
                         #log(id)
105
                         mode = 'VC'
106
                         pathe='http://' + addon.getSetting('ip_deco') + '/live.ts'
107
                         u = '%s?url=%s&name=%s&mode=%s&id=%s' % (sys.argv[0],qp(url), qp(name), mode, qp(id))
108
                         print u
109
                         liz=xbmcgui.ListItem(name, '','DefaultVideo.png', icon,path=pathe)
110
                         liz.setProperty( "Folder", "false" )
111
                         liz.setProperty("IsPlayable", "true")
112
                         liz.setInfo( 'Video', { "Title": name, })
113
                         ilist.append((u, liz, True))
114
                    except:
115
                      pass
116
              xbmcplugin.addDirectoryItems(int(sys.argv[1]), ilist, len(ilist))
117

    
118
def VerCanal(cid):
119
        log("++++++++++++++++++++++++++++++++++ VOY A CAMBIAR DE CANAL +++++++++++++++++++++++++++++++++")
120
	azheaders = defaultHeaders
121
	azheaders['X-Requested-With'] = 'XMLHttpRequest'
122
	url='http://' + addon.getSetting('ip_deco') + '/api/channel/switch'
123
        url2='http://' + addon.getSetting('ip_deco') + channelListUrl
124
	values = {'cid': qp(cid)}
125
	data = urllib.urlencode(values)            
126
        current_id=''
127
        while str(cid) != str(current_id):
128
            pg2 = getRequest(url, data, azheaders)
129
            xbmc.sleep(2000)           
130
            pg = getRequest(url2, None, azheaders)
131
            lista=json.loads(pg)
132
            lista2=lista['current']
133
            current_id=lista2['channel_id']
134
            log (str(cid) + " ----> " + str(current_id))
135
	url = 'http://' + addon.getSetting('ip_deco') + '/live.ts'
136
        print url.encode(UTF8, 'ignore')
137
        item=xbmcgui.ListItem(path=url.encode(UTF8, 'ignore'), iconImage="DefaultVideo.png", thumbnailImage=icon)
138
        item.setProperty("IsPlayable", "true")
139
        item.setProperty( "Folder", "false" )
140
        xbmcplugin.setResolvedUrl(int(sys.argv[1]), False, item)
141
        xbmc.Player().play(item=url, listitem=item)
142
        return
143

    
144

    
145
# PROCEDIMIENTO PRINCIPAL
146
xbmcplugin.setContent(int(sys.argv[1]), 'movies')
147

    
148
parms = {}
149
try:
150
    parms = dict( arg.split( "=" ) for arg in ((sys.argv[2][1:]).split( "&" )) )
151
    for key in parms:
152
       parms[key] = demunge(parms[key])
153
except:
154
    parms = {}
155

    
156
p = parms.get
157

    
158
mode = p('mode',None)
159

    
160
if mode==  None:  ListaCanales(p('fanart'))
161
elif mode=='VC':  VerCanal(p('id'))
162

    
163
xbmcplugin.endOfDirectory(int(sys.argv[1]))
    (1-1/1)