summaryrefslogtreecommitdiff
path: root/plugin.video.mmlive/main.py
blob: 7cbbdc84cbf4da35ec8152f0b236489af15d3231 (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
from resources.globals import *
from resources.adobepass import ADOBE

#Add-on specific Adobepass variables
SERVICE_VARS = {'requestor_id':'MML',
                'public_key':'XfId78vskMBegCUx9fuiNQL3XvxP3SzN',
                'private_key':'60OKORsYmOkUMgDm',
                'activate_url':'ncaa.com/activate'
               }

def categories():               
    addDir('Today\'s Games','/live',1,ICON,FANART)
    addDir('Archive Games','/live',2,ICON,FANART)
    addDir('Classic Games','/classic',3,ICON,FANART)
    addDir('Deauthorize this Device','/deauth',4,ICON,FANART)    


def todaysGames(archive=None):   
    now = datetime.now()
    url = 'http://data.ncaa.com/mml/'+str(now.year)+'/mobile/bracket.json'
    
    req = urllib2.Request(url)    
    req.add_header('Connection', 'keep-alive')
    req.add_header('Accept', '*/*')
    req.add_header('User-Agent', UA_MMOD)
    req.add_header('Accept-Language', 'en-us')
    req.add_header('Accept-Encoding', 'deflate')

    response = urllib2.urlopen(req)    
    json_source = json.load(response)                           
    response.close() 

    tourn_day = json_source['bracket']['tournDay'] 
    teams = getTournamentInfo()         

    
    if not archive:
        setTodaysStream(tourn_day, json_source, teams)
    else:
        setArchiveStreams(tourn_day, json_source, teams)

    
def setTodaysStream(tourn_day, json_source, teams):
    tomorrow = str(int(tourn_day) + 86400)            
    try:
        current_games = getCurrentInfo()
    except:
        pass

    #Sort By Start Time
    json_source = sorted(json_source['bracket']['game'],key=lambda x:x['time'])
    
    for game in json_source:
        if game['time'] >= tourn_day and  game['time'] < tomorrow:        
            if game['tmH'] != '' and game['tmV'] != '':                
                game_id = game['id']                
                hTeam = getTeamInfo(teams, game['tmH'])
                vTeam = getTeamInfo(teams,game['tmV'])    
                game_time = time.strftime('%I:%M %p', time.localtime(int(game['time']))).lstrip('0')    
                state = game['state']
                archive_video = game['rcpV']                
                
                title = vTeam['school'] + ' vs ' + hTeam['school']

                if NO_SPOILERS == '1' or NO_SPOILERS == '2':
                    name =  title
                else:
                    name =  '#'+ vTeam['seed']+ ' ' + vTeam['school'] + ' ' + colorString(game['ptsV'], SCORE_COLOR) + ' vs #'+ hTeam['seed']+ ' ' + hTeam['school'] + ' ' + colorString(game['ptsH'], SCORE_COLOR)

                if state == "1":                    
                    name =  colorString(game_time, UPCOMING) + ' ' + name
                elif state == "4":
                    name =  colorString("FINAL", FINAL) + ' ' + name
                else:
                    clock = getGameClock(current_games, game_id)
                    if clock == '':
                        clock = 'LIVE'
                    name =  colorString(clock,GAMETIME_COLOR) + ' ' + name

                
                link_url = ''             
                addStream(name,link_url,title,game_id)


def classicGames():
    now = datetime.now()    
    url = 'http://data.ncaa.com/mml/'+str(now.year)+'/mobile/vod/classic_games.json'    
    req = urllib2.Request(url)    
    req.add_header('Connection', 'keep-alive')
    req.add_header('Accept', '*/*')
    req.add_header('User-Agent', UA_MMOD)
    req.add_header('Accept-Language', 'en-us')
    req.add_header('Accept-Encoding', 'deflate')

    response = urllib2.urlopen(req)    
    json_source = json.load(response)                           
    response.close() 

    for game in json_source['videos']:
        title = game['title']
        url = game['connected'] + '|User-Agent='+UA_MMOD
        icon = game['thumbnails']['large']
        fanart = game['thumbnails']['raw']
        addLink(title,url,icon,fanart)



def setArchiveStreams(tourn_day, json_source, teams):
    json_source = sorted(json_source['bracket']['game'],key=lambda x:x['time'], reverse=True)
    for game in json_source:        
        if game['time'] < tourn_day:
            if game['tmH'] != '' and game['tmV'] != '':                
                game_id = game['id']                
                hTeam = getTeamInfo(teams, game['tmH'])
                vTeam = getTeamInfo(teams,game['tmV'])     
                game_time = time.strftime('%I:%M %p', time.localtime(int(game['time']))).lstrip('0')
                live_video = game['video']
                archive_video = game['rcpV']
                
                title = vTeam['school'] + ' vs ' + hTeam['school']

                if NO_SPOILERS == '1' or NO_SPOILERS == '3':
                    name =  title
                else:                    
                    name =  '#'+ vTeam['seed']+ ' ' + vTeam['school'] + ' ' + colorString(game['ptsV'], SCORE_COLOR) + ' vs ' + '#'+ hTeam['seed']+ ' ' + hTeam['school'] + ' ' + colorString(game['ptsH'], SCORE_COLOR)
                
                name =  colorString('FINAL ',FINAL) + name               
                link_url = 'archive'
                addStream(name,link_url,title,game_id)


def startStream(game_id):
    stream_url = fetchStream(game_id,addon_url)
    
    if addon_url == 'archive':        
        playable_stream = stream_url + '|User-Agent='+UA_MMOD
    else:
        adobe = ADOBE(SERVICE_VARS)        
        resource_id = 'truTV'
        mvpd = adobe.authorizeDevice(resource_id)        
        media_token = adobe.mediaToken(resource_id)          
        playable_stream = tokenTurner(media_token,stream_url,mvpd)        

    listitem = xbmcgui.ListItem(path=playable_stream)        
    xbmcplugin.setResolvedUrl(handle=addon_handle, succeeded=True, listitem=listitem)



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()
addon_url=None
name=None
mode=None
game_id=None
icon_image = None

try:
    addon_url=urllib.unquote_plus(params["url"])
except:
    pass
try:
    name=urllib.unquote_plus(params["name"])
except:
    pass
try:
    mode=int(params["mode"])
except:
    pass
try:
    game_id=urllib.unquote_plus(params["game_id"])
except:
    pass
try:
    icon_image=urllib.unquote_plus(params["icon_image"])
except:
    pass


if mode==None:        
    categories()        
elif mode==1:        
    todaysGames() 
elif mode==2:
    todaysGames(archive=True)
elif mode==3:
    classicGames()
elif mode==4:
    msg = 'Are you sure you wish to deauthorize this device?'
    dialog = xbmcgui.Dialog() 
    answer = dialog.yesno('Deauthorize Devices', msg)
    if answer:
        adobe = ADOBE(SERVICE_VARS)
        adobe.deauthorizeDevice()
    sys.exit()

elif mode==104:        
    startStream(game_id)
    

#Don't cache todays games
if mode==1:
    xbmcplugin.endOfDirectory(addon_handle, cacheToDisc=False)
else:
    xbmcplugin.endOfDirectory(addon_handle)