summaryrefslogtreecommitdiff
path: root/plugin.video.tvvn/tvvn.py
blob: 3c6f62711c25fc607646e7fe8f6cf962feb4ef8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# -*- coding: UTF-8 -*-
#---------------------------------------------------------------------
# File: tvvn.py
# Version: 0.9.11
# By:   Binh Nguyen <b@zecoj.com>
# Date: Tue Sep 19 10:29:01 AEST 2017
#---------------------------------------------------------------------
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------

import os, re, sys, gzip, urllib, urllib2, string, xbmcaddon, xbmcplugin, xbmcgui, xbmcvfs
from StringIO import StringIO
from cookielib import CookieJar

try:
        import json
except:
        import simplejson as json

addon = xbmcaddon.Addon('plugin.video.tvvn')
mysettings = xbmcaddon.Addon(id='plugin.video.tvvn')
profile = xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('profile')).decode('utf-8')
home = xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('path')).decode('utf-8')
fanart = xbmc.translatePath(os.path.join(home, 'fanart.jpg'))
datafile = xbmc.translatePath(os.path.join(home, 'data.json'))

data = json.loads(open(datafile,"r").read())

mode=None

def get_params():
        param=[]
        paramstring=sys.argv[2]
        if len(paramstring)>=2:
                params=sys.argv[2]
                cleanedparams=params.replace('?','')
                if (params[len(params)-1]=='/'):
                        params=params[0:len(params)-2]
                pairsofparams=cleanedparams.split('&')
                param={}
                for i in range(len(pairsofparams)):
                        splitparams={}
                        splitparams=pairsofparams[i].split('=')
                        if (len(splitparams))==2:
                                param[splitparams[0]]=splitparams[1]

        return param

params=get_params()

try:         chn=urllib.unquote_plus(params["chn"])
except: pass
try:         src=urllib.unquote_plus(params["src"])
except: pass
try:         mode=int(params["mode"])
except: pass

def construct_menu(namex):
        name = data['directories'][namex]['title']
        lmode = '2'
        iconimage = ''
        desc = data['directories'][namex]['desc']

        menu_items = data['directories'][namex]['content']
        for menu_item in menu_items:
                #type == channel
                if (menu_item['type'] == "chn"):
                        add_chn_link (menu_item['id'])
                #type == directory
                if (menu_item['type'] == "dir"):
                        add_dir_link (menu_item['id'])
        xbmcplugin.endOfDirectory(int(sys.argv[1]))
        return

def add_chn_link (namex):
        ok = True
        title = data['channels'][namex]['title']
        name = title
        iconimage = data['channels'][namex]['logo']
        desc = data['channels'][namex]['desc']
        ref = data['channels'][namex]['src']['referer']
        stream_name = data['channels'][namex]['src']['playpath']
        src = data['channels'][namex]['src']['id']

        if (iconimage == ''):
                iconimage = 'default.png'
        if (mysettings.getSetting('descriptions')=='true' and desc != ''):
                if mysettings.getSetting('descriptions_on_right') == 'false':
                        name = desc+"    "+name
                else:
                        name = name+"    "+desc

        give_url = sys.argv[0]+"?mode=1&chn="+namex+"&src="+src
        liz = xbmcgui.ListItem(name)
        liz.setInfo(type="video", infoLabels={"title": name, "mediatype": "video"})
        liz.setArt({
           "icon": xbmc.translatePath(os.path.join(home, 'resources', 'logos', iconimage)),
           "thumb": xbmc.translatePath(os.path.join(home, 'resources', 'logos', iconimage)),
           "fanart": fanart
           })
        ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=give_url,listitem=liz)

def add_dir_link (namex):
        name = '['+data['directories'][namex]['title']+']'
        desc = data['directories'][namex]['desc']
        iconimage = data['directories'][namex]['logo']
        if (iconimage == ''):
                iconimage = 'default.png'
        if (mysettings.getSetting('descriptions')=='true' and desc != ''):
                if mysettings.getSetting('descriptions_on_right') == 'false':
                        name = desc+"    "+name
                else:
                        name = name+"    "+desc
        li = xbmcgui.ListItem(name)
        li.setInfo(type="video", infoLabels={"title": name, "mediatype": "video"})
        li.setArt({
           "icon": xbmc.translatePath(os.path.join(home, 'resources', 'logos', iconimage)),
           "thumb": xbmc.translatePath(os.path.join(home, 'resources', 'logos', iconimage)),
           "fanart": fanart
           })
        give_url = sys.argv[0]+"?mode=2&chn="+namex
        return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=give_url, listitem=li, isFolder=True)

def update_chn_list():
        url = mysettings.getSetting('json_url')
        request = urllib2.Request(url)
        request.add_header('Accept-encoding', 'gzip')
        url_error = "no"
        try:
                response = urllib2.urlopen(request)
        except urllib2.URLError, e:
                url_error = "yes"

        if (url_error == "no"):
                buf = StringIO(response.read())
                f = gzip.GzipFile(fileobj=buf)
                ff=f.read()
                r_data=json.loads(ff)
                if (data['timestamp'] < r_data['timestamp']):
                        day_diff = str((int(r_data['timestamp']) - int(data['timestamp'])) /60/60/24)
                        dialog = xbmcgui.Dialog()
                        ack = dialog.yesno(addon.getLocalizedString(30005), addon.getLocalizedString(30006)+" "+day_diff+" day(s) old",addon.getLocalizedString(30007))
                        if ack:
                                d_progress = xbmcgui.DialogProgress()
                                d_progress.create(addon.getLocalizedString(30008), addon.getLocalizedString(30009))
                                d = open(datafile, 'w')
                                d.seek(0)
                                d.write(ff)
                                d.close()
                                construct_menu("root")
                                d_progress.close()
                                d_ok = xbmcgui.Dialog().ok(addon.getLocalizedString(30008),addon.getLocalizedString(30010))

def play_link(chn, src):
        print("xxyyzz 001")
        xbmc.log("xxyyzz 001")
        item = xbmcgui.ListItem(chn)
        d_progress = xbmcgui.DialogProgress()
        d_progress.create("", addon.getLocalizedString(30009))

        cj = CookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

        #login if required
        if src in data['sources'] and 'login' in data['sources'][src] and data['sources'][src]['login'] == "true":
            url = data['sources'][src]['url']
            values = data['sources'][src]['post']
            post_data = urllib.urlencode(values)
            response = opener.open(url, post_data)
            the_page = response.read()

        #m3u8 url from fpt
        if data['channels'][chn]['src']['playpath'] == "m3u8_fpt":
            url = 'http://fptplay.net/show/getlinklivetv'
            page_id = data['channels'][chn]['src']['page_id']
            page_q = data['channels'][chn]['src']['page_q']
            values={'id': page_id, 'type': 'newchannel', 'quality': page_q, 'mobile': 'web'}
            post_data = urllib.urlencode(values)
            header = {'Content-Type': 'application/x-www-form-urlencoded', 'Host':'fptplay.net','Origin':'http://fptplay.net','X-Requested-With':'XMLHttpRequest', 'Referer':'http://fptplay.net/livetv'}
            req = urllib2.Request(url, post_data, header)
            response = urllib2.urlopen(req)
            the_page = response.read()
            the_data = json.loads(the_page)
            full_url=the_data['stream']

        #m3u8 url from tvnet
        elif data['channels'][chn]['src']['playpath'] == "m3u8_tvnet":
            url = 'http://au.tvnet.gov.vn/kenh-truyen-hinh/'+data['channels'][chn]['src']['page_id']
            stringA = opener.open(url).read().decode('utf-8')
            stringB = 'data-file="'
            stringC = '"'
            url = re.search(stringB+"(.*?)"+re.escape(stringC),stringA).group(1)
            
            stringA = opener.open(url).read().decode('utf-8')
            stringB = '"url": "'
            stringC = '"'
            full_url_BC = re.search(stringB+"(.*?)"+re.escape(stringC),stringA).group(1)
            full_url = full_url_BC
            print full_url

        #m3u8 url from vtvgo
        elif data['channels'][chn]['src']['playpath'] == "m3u8_vtvgo":
            url = data['channels'][chn]['src']['page_url']
            stringA = opener.open(url).read().decode('utf-8')
            stringB = "addPlayer('"
            stringC = "'" 
            full_url = re.search(re.escape(stringB)+"(.*?)"+re.escape(stringC),stringA).group(1) + '|Referer=http%3A%2F%2Fvtvgo.vn%2F'
            full_url = "http://play.sohatv.vn/?v=dnR2MQ==&t=20170617133921&autoplay=true" + '|Referer=http://vtv.vn/truyen-hinh-truc-tuyen/vtv1.htm'
            print full_url

        #m3u8 url using before & after marker
        elif data['channels'][chn]['src']['playpath'] == "m3u8_bau":
            if data['channels'][chn]['src'].get('header'):
                header=(data['channels'][chn]['src']['header'])
            else:
                header=None
            if data['channels'][chn]['src'].get('post'):
                post=data['channels'][chn]['src']['post']
            else:
                post=None

            if header==None:
                req = urllib2.Request(data['channels'][chn]['src']['page_url'], post)
            else:
                req = urllib2.Request(data['channels'][chn]['src']['page_url'], post, header)

            response = urllib2.urlopen(req)
            #print(response)
            the_page = response.read()
            #print(the_page)

            stringA=the_page
            stringB=(data['channels'][chn]['src']['url_before'])
            stringC=(data['channels'][chn]['src']['url_after'])
            full_url=re.search(re.escape(stringB)+"(.*?)"+re.escape(stringC),stringA).group(1)
            print(full_url)

        #traditional rtmp(e)
        else:
            videoUrl = data['sources'][src]['url']
            playpath = data['channels'][chn]['src']['playpath']
            if (playpath != ''):
                    videoUrl = videoUrl+"/"+playpath

            url_protocol = videoUrl.split(':')[0]
            if (url_protocol == "http"):
                    full_url = videoUrl
            elif (url_protocol in ["rtmp", "rtmpe"]):
                    swfUrl = data['sources'][src]['swfurl']
                    pageUrl = data['sources'][src]['pageurl']
                    if (data['channels'][chn]['src']['referer'] != ''):
                            pageUrl = pageUrl+"/"+data['channels'][chn]['src']['referer']
                    flashVer = 'LNX_11,2,202,233'
                    token = data['sources'][src]['token']
                    app = data['sources'][src]['app']

            full_url = videoUrl+' swfVfy=1 live=1 token='+token+' playpath='+playpath+' flashVer='+flashVer+' pageUrl='+pageUrl+' tcUrl='+videoUrl+' swfUrl='+swfUrl

        d_progress.close()
        xbmc.Player().play(full_url)
        return

def Init():
        construct_menu("root")
        if (mysettings.getSetting('json_url_auto_update')=='true' and mysettings.getSetting('json_url')!=''):
                update_chn_list()

if mode==None:
        Init()
elif mode==1:
        play_link(chn, src)
elif mode==2:
        construct_menu(chn)