summaryrefslogtreecommitdiff
path: root/plugin.video.news12/resources/lib/news12.py
blob: 9287665215071d3343e534528ebd863ff407e1f9 (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
#   Copyright (C) 2017 Lunatixz
#
#
# This file is part of News12.
#
# News12 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.
#
# News12 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 News12.  If not, see <http://www.gnu.org/licenses/>.

# -*- coding: utf-8 -*-
import os, sys, time, datetime, re, traceback, feedparser
import urllib, urllib2, socket, json
import xbmc, xbmcgui, xbmcplugin, xbmcvfs, xbmcaddon

from simplecache import SimpleCache
from bs4 import BeautifulSoup

# Plugin Info
ADDON_ID      = 'plugin.video.news12'
REAL_SETTINGS = xbmcaddon.Addon(id=ADDON_ID)
ADDON_NAME    = REAL_SETTINGS.getAddonInfo('name')
SETTINGS_LOC  = REAL_SETTINGS.getAddonInfo('profile')
ADDON_PATH    = REAL_SETTINGS.getAddonInfo('path').decode('utf-8')
ADDON_VERSION = REAL_SETTINGS.getAddonInfo('version')
ICON          = REAL_SETTINGS.getAddonInfo('icon')
FANART        = REAL_SETTINGS.getAddonInfo('fanart')
LANGUAGE      = REAL_SETTINGS.getLocalizedString

## GLOBALS ##
TIMEOUT = 15
DEBUG   = REAL_SETTINGS.getSetting('Enable_Debugging') == 'true'
BITRATE = {0:LANGUAGE(30003),1:LANGUAGE(30004),2:LANGUAGE(30005)}[int(REAL_SETTINGS.getSetting('Video_Quality'))]
BASEURL = 'http://news12.com'
RSSURL  = '%s/?clienttype=rss'
BASEIMG = 'http://ftpcontent.worldnow.com/professionalservices/clients/news12/images/regions/selectregion_%i.jpg'
LIVEURL = 'http://adx.news12.com/livevideo/livevideo_iframe.html'
VODURL  = '%s/clip/%s/videoclip?clienttype=mrssjson'
LIVEMAP = {'N12%s':'%s - News','TW%s':'%s - Traffic & Weather','N12%sB2':'%s - Doppler Radar'}
IMGPARM = {'bx':BASEIMG%1,'bk':BASEIMG%2,'ct':BASEIMG%3,'hv':BASEIMG%4,'li':BASEIMG%5,'nj':BASEIMG%6,'wc':BASEIMG%7,'None':ICON,}

def log(msg, level=xbmc.LOGDEBUG):
    if DEBUG == True:
        if level == xbmc.LOGERROR:
            msg += ' ,' + traceback.format_exc()
        xbmc.log(ADDON_ID + '-' + ADDON_VERSION + '-' + (msg), level)
   
def getParams():
    param=[]
    if len(sys.argv[2])>=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
                 
socket.setdefaulttimeout(TIMEOUT)
class News12():
    def __init__(self):
        log('__init__')
        self.cache = SimpleCache()

            
    def openURL(self, url):
        log('openURL, url = ' + str(url))
        try:
            cacheResponse = self.cache.get(ADDON_NAME + '.openURL, url = %s'%url)
            if not cacheResponse:
                request = urllib2.Request(url)
                request.add_header('User-Agent','Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)')
                response = urllib2.urlopen(request, timeout=TIMEOUT).read()
                self.cache.set(ADDON_NAME + '.openURL, url = %s'%url, response, expiration=datetime.timedelta(hours=6))
            return self.cache.get(ADDON_NAME + '.openURL, url = %s'%url)
        except urllib2.URLError, e:
            log("openURL Failed! " + str(e), xbmc.LOGERROR)
        except socket.timeout, e:
            log("openURL Failed! " + str(e), xbmc.LOGERROR)
        except Exception, e:
            log("openURL Failed! " + str(e), xbmc.LOGERROR)
            xbmcgui.Dialog().notification(ADDON_NAME, LANGUAGE(30001), ICON, 4000)

            
    def buildRegionMenu(self):
        log('buildRegionMenu')
        soup = BeautifulSoup(self.openURL(BASEURL), "html.parser")
        data = soup('div' , {'class': 'region-links'})[0].find_all('a')
        for idx, region in enumerate(data):
            thumb = IMGPARM[region['class'][0]]
            art   = {"thumb":thumb,"poster":thumb,"icon":ICON,"fanart":FANART}
            self.addDir(region.get_text(),json.dumps({'region_ad':region['class'][0],'url':region.get('href')}),0,infoArt=art)

            
    def buildMenu(self, name, url):
        log('buildMenu')
        self.buildLive(name, url['region_ad'])
        thumb = IMGPARM[url['region_ad']]
        art = {"thumb":thumb,"poster":thumb,"icon":ICON,"fanart":FANART}
        self.addDir('%s - Video Clips'%name,json.dumps(url),1,infoArt=art)


    def buildLive(self, name, url):
        data = self.openURL(LIVEURL).replace(' ','').replace('(','{').replace(')','}')
        live = re.search(r'varstream_url="(.*)"', data.strip(), re.M|re.I).group(1).replace('"+streamname+"','%s')
        for line in data.split('\n'):
            search = re.match( r'if{region=="(.*)"}{region_ad="(.*)";streamname="(.*)";url="(.*)";}.*', line.strip(), re.M|re.I)
            if search and search.group(2).lower() == url:
                region = search.group(1).upper()
                for key in LIVEMAP:
                    if key%url.upper() == region:
                        art = {"thumb":IMGPARM[url],"poster":IMGPARM[url],"icon":ICON,"fanart":FANART}
                        self.addLink(LIVEMAP[key]%name, live%search.group(3),9,infoArt=art,total=len(LIVEMAP))
    
    
    def buildRSS(self, url, end=9):
        log('buildRSS, url = ' + str(url))
        start   = int(url.get('start','0') or '0')
        data    = feedparser.parse(RSSURL%url['url'])['entries']
        start   = 0 if start >= len(data) else start
        count   = 0
        for idx in range(start,len(data)):
            item = data[idx]
            if item and 'summary_detail' in item:
                try:
                    path, duration = self.resolveURL(url['url'], item['links'][0]['href'])
                    thumb = item['links'][1]['href']
                    label = item['title']
                    plot  = item['summary_detail']['value']
                    plot = '%s - %s'%(item['published'], plot)
                    infoLabel  = {"mediatype":"video","label":label,"title":label,"plot":plot,"plotoutline":plot,"genre":"News","duration":duration}                    
                    infoArt    = {"thumb":thumb,"poster":thumb,"icon":ICON,"fanart":FANART}
                    self.addLink(label,path,9,infoLabel,infoArt,len(data[start]))
                    count +=1
                    if count == end:
                        break
                except:
                    pass
        url['start'] = 0 if count == 0 else idx
        self.addDir('>> Next',json.dumps(url), 1)

        
    def resolveURL(self, name, url):
        log('resolveURL, url = ' + str(url))
        try:
            cacheResponse = self.cache.get(ADDON_NAME + '.resolveURL, url = %s.%s'%(url,str(BITRATE)))
            if not cacheResponse:
                data = self.openURL(url).replace(' ','').replace('(','{').replace(')','}')
                try:
                    clip = re.search(r'widgetVideoCanvasDS37.SetVariable{"clipId","(.*)"};', data.strip(), re.M|re.I).group(1)
                except:
                    return
                    
                response = None
                items = json.loads(self.openURL(VODURL%(name,clip)))
                if items and 'channel' in items:
                    for video in items['channel']['item']['media-group']['media-content']:
                        if BITRATE == LANGUAGE(30003) and int(video['@attributes']['bitrate']) > 1200:
                            continue
                        elif BITRATE == LANGUAGE(30004) and int(video['@attributes']['bitrate']) > 2200 and int(video['@attributes']['bitrate']) < 1200:
                            continue
                        elif BITRATE == LANGUAGE(30005) and int(video['@attributes']['bitrate']) < 2200:
                            continue
                        log('resolveURL, using bitrate = ' + str(video['@attributes']['bitrate']))
                        response = video['@attributes']['url'], int(video['@attributes']['duration'])
                        
                    if not response:
                        response = video['@attributes']['url'], int(video['@attributes']['duration'])
                self.cache.set(ADDON_NAME + '.resolveURL, url = %s.%s'%(url,str(BITRATE)), response, expiration=datetime.timedelta(hours=48))
            return self.cache.get(ADDON_NAME + '.resolveURL, url = %s.%s'%(url,str(BITRATE)))
        except Exception, e:
            log("resolveURL Failed! " + str(e), xbmc.LOGERROR)

            
    def playVideo(self, name, url, list=None):
        log('playVideo')
        if not list:
            list = xbmcgui.ListItem(name, path=url)
        xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, list)

           
    def addLink(self, name, u, mode, infoList=False, infoArt=False, total=0):
        name = name.encode("utf-8")
        log('addLink, name = ' + name)
        liz=xbmcgui.ListItem(name)
        liz.setProperty('IsPlayable', 'true')
        if infoList == False:
            liz.setInfo(type="Video", infoLabels={"mediatype":"video","label":name,"title":name,"genre":"News"})
        else:
            liz.setInfo(type="Video", infoLabels=infoList)
            
        if infoArt == False:
            liz.setArt({'thumb':ICON,'fanart':FANART})
        else:
            liz.setArt(infoArt)
        u=sys.argv[0]+"?url="+urllib.quote_plus(u)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
        xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,totalItems=total)


    def addDir(self, name, u, mode, infoList=False, infoArt=False):
        name = name.encode("utf-8")
        log('addDir, name = ' + name)
        liz=xbmcgui.ListItem(name)
        liz.setProperty('IsPlayable', 'false')
        if infoList == False:
            liz.setInfo(type="Video", infoLabels={"mediatype":"video","label":name,"title":name,"genre":"News"})
        else:
            liz.setInfo(type="Video", infoLabels=infoList)
        if infoArt == False:
            liz.setArt({'thumb':ICON,'fanart':FANART})
        else:
            liz.setArt(infoArt)
        u=sys.argv[0]+"?url="+urllib.quote_plus(u)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
        xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
  
params=getParams()
try:
    url=urllib.unquote_plus(params["url"])
except:
    url=None
try:
    name=urllib.unquote_plus(params["name"])
except:
    name=None
try:
    mode=int(params["mode"])
except:
    mode=None
    
log("Mode: "+str(mode))
log("URL : "+str(url))
log("Name: "+str(name))

if mode==None:  News12().buildRegionMenu()
elif mode == 0: News12().buildMenu(name, json.loads(url))
elif mode == 1: News12().buildRSS(json.loads(url))
elif mode == 9: News12().playVideo(name, url)

xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_NONE )
xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_LABEL )
xbmcplugin.endOfDirectory(int(sys.argv[1]),cacheToDisc=True)