summaryrefslogtreecommitdiff
path: root/plugin.video.mlbtv/main.py
blob: 5a885a0e9de7e4fb1fd1f0e79cd6a383ef92f774 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
from resources.lib.globals import *


def categories():      
    addDir('Today\'s Games',100,ICON,FANART)
    addDir('Yesterday\'s Games',105,ICON,FANART)
    #addDir('Favorite Team Recent Games','favteam',500,ICON,FANART)
    addDir('Goto Date',200,ICON,FANART)  
        

def todaysGames(game_day):    
    if game_day == None:
        game_day = localToEastern()
           
    settings.setSetting(id='stream_date', value=game_day)    

    display_day = stringToDate(game_day, "%Y-%m-%d")
    url_game_day = display_day.strftime('year_%Y/month_%m/day_%d')            
    prev_day = display_day - timedelta(days=1)                

    addDir('[B]<< Previous Day[/B]',101,PREV_ICON,FANART,prev_day.strftime("%Y-%m-%d"))

    date_display = '[B][I]'+ colorString(display_day.strftime("%A, %m/%d/%Y"),GAMETIME_COLOR)+'[/I][/B]'

    addPlaylist(date_display,str(game_day),900,ICON,FANART)
    
    #addPlaylist(date_display,display_day,'/playhighlights',999,ICON,FANART)

    url = 'http://gdx.mlb.com/components/game/mlb/'+url_game_day+'/grid_ce.json'    

    req = urllib2.Request(url)    
    req.add_header('Connection', 'close')
    req.add_header('User-Agent', UA_PS4)

    try:    
        response = urllib2.urlopen(req)            
        json_source = json.load(response)                           
        response.close()                
    except HTTPError as e:
        xbmc.log('The server couldn\'t fulfill the request.')
        xbmc.log('Error code: ', e.code)
        sys.exit()

       
    global RECAP_PLAYLIST
    global EXTENDED_PLAYLIST
    RECAP_PLAYLIST.clear()
    EXTENDED_PLAYLIST.clear()

    
    try:
        itr = json_source['data']['games']['game']            
        if not isinstance(itr, list):
            itr = [itr]
        for game in itr:
            createGameListItem(game, game_day)
    except:
        # if there are no games on this date
        pass
    
    next_day = display_day + timedelta(days=1)
    addDir('[B]Next Day >>[/B]',101,NEXT_ICON,FANART,next_day.strftime("%Y-%m-%d"))    


def createGameListItem(game, game_day):
    #icon = getGameIcon(game['home_team_id'],game['away_team_id'])
    icon = ICON
    #http://mlb.mlb.com/mlb/images/devices/ballpark/1920x1080/2681.jpg
    #B&W
    #fanart = 'http://mlb.mlb.com/mlb/images/devices/ballpark/1920x1080/'+game['venue_id']+'.jpg'   
    #Color
    fanart = 'http://www.mlb.com/mlb/images/devices/ballpark/1920x1080/color/'+game['venue_id']+'.jpg'
    
    if TEAM_NAMES == "0":
        away_team = game['away_team_name']
        home_team = game['home_team_name']  
    else:
        away_team = game['away_name_abbrev']
        home_team = game['home_name_abbrev']  


    fav_game = False
    
    if game['away_team_name'].encode('utf-8') in FAV_TEAM :
        fav_game = True
        away_team = colorString(away_team,getFavTeamColor())           
    
    if game['home_team_name'].encode('utf-8') in FAV_TEAM:
        fav_game = True
        home_team = colorString(home_team,getFavTeamColor())
    

    game_time = ''
    if game['status'] == 'Preview':
        game_time = game_day+' '+game['event_time']        
        game_time = stringToDate(game_time, "%Y-%m-%d %I:%M %p")        
        game_time = easternToLocal(game_time)
       
        if TIME_FORMAT == '0':
             game_time = game_time.strftime('%I:%M %p').lstrip('0')
        else:
             game_time = game_time.strftime('%H:%M')

        game_time = colorString(game_time,UPCOMING)            

    else:
        game_time = game['status']

        if game_time == 'Final':                  
            game_time = colorString(game_time,FINAL)
            
        elif 'In Progress' in game_time:           
            if game['top_inning'] == 'Y':
                #up triangle
                #top_bottom = u"\u25B2"               
                top_bottom = "T"
            else:
                #down triangle
                #top_bottom = u"\u25BC"
                top_bottom = "B"                

            inning = game['inning']
            if int(inning) % 10 == 1 and int(inning) != 11:
                ordinal_indicator = "st"
            elif int(inning) % 10 == 2 and int(inning) != 12:
                ordinal_indicator = "nd"
            elif int(inning) % 10 == 3 and int(inning) != 13:
                ordinal_indicator = "rd"
            else:
                ordinal_indicator = "th"

            game_time = top_bottom + ' ' + inning + ordinal_indicator
            
            if int(inning) >= 9:
                color = CRITICAL
            else:
                color = LIVE

            game_time = colorString(game_time,color)
        
        else:            
            game_time = colorString(game_time,LIVE)
        
        
    event_id = str(game['calendar_event_id'])
    gid = game['id']

    live_feeds = 0
    archive_feeds = 0    
    teams_stream = game['away_code'] + game['home_code']
    stream_date = str(game_day)

    
    desc = ''       
    hide_spoilers = 0
    if NO_SPOILERS == '1' or (NO_SPOILERS == '2' and fav_game) or (NO_SPOILERS == '3' and game_day == localToEastern()) or (NO_SPOILERS == '4' and game_day < localToEastern()) or game['status'] == 'Preview':
        name = game_time + ' ' + away_team + ' at ' + home_team    
        hide_spoilers = 1
    else:    
        name = game_time + ' ' + away_team + ' ' + colorString(str(game['away_score']),SCORE_COLOR) + ' at ' + home_team + ' ' + colorString(str(game['home_score']),SCORE_COLOR)             

   
    name = name.encode('utf-8')
    if fav_game:
        name = '[B]'+name+'[/B]'
    
    title = away_team + ' at ' + home_team
    title = title.encode('utf-8')

    #Label free game of the day if applicable
    try:
        if game['game_media']['homebase']['media'][0]['free'] == "ALL":
            #and game_day >= localToEastern():
            name = colorString(name, FREE)
    except:
        pass
    
    #Set audio/video info based on stream quality setting
    audio_info, video_info = getAudioVideoInfo()
    #'duration':length
    info = {'plot':desc,'tvshowtitle':'MLB','title':title,'originaltitle':title,'aired':game_day,'genre':LOCAL_STRING(700),'mediatype':'video'}

    #Create Playlist for the days recaps and condensed
    
    try:         
        recap_url, condensed_url = getHighlightLinks(teams_stream, stream_date)
        global RECAP_PLAYLIST            
        listitem = xbmcgui.ListItem(title, thumbnailImage=icon)    
        listitem.setInfo( type="Video", infoLabels={ "Title": title })
        RECAP_PLAYLIST.add(recap_url, listitem)

        global EXTENDED_PLAYLIST    
        listitem = xbmcgui.ListItem(title, thumbnailImage=icon)      
        listitem.setInfo( type="Video", infoLabels={ "Title": title } )
        EXTENDED_PLAYLIST.add(condensed_url, listitem)
    except:
        pass
    
    addStream(name,title,event_id,gid,icon,fanart,info,video_info,audio_info,teams_stream,stream_date)



def streamSelect(event_id, gid, teams_stream, stream_date):    
    display_day = stringToDate(stream_date, "%Y-%m-%d")
    url_game_day = display_day.strftime('year_%Y/month_%m/day_%d')
    url = 'http://gdx.mlb.com/components/game/mlb/'+url_game_day+'/grid_ce.json'    
    
    req = urllib2.Request(url)    
    req.add_header('Connection', 'close')
    req.add_header('User-Agent', UA_PS4)

    try:    
        response = urllib2.urlopen(req)            
        json_source = json.load(response)                           
        response.close()                
    except HTTPError as e:
        xbmc.log('The server couldn\'t fulfill the request.')
        xbmc.log('Error code: ', e.code)
        sys.exit()

    #Find selected game
    teams = {}
    itr = json_source['data']['games']['game']
    if type(itr) is not list:
        itr = [itr]
        #xbmc.log(str(itr))
    for game in itr:        
        xbmc.log(game['id'])
        xbmc.log(gid)

        if gid == game['id']:
            try:
                epg = game['game_media']['homebase']['media']
                teams={'Home':game['home_name_abbrev'],'Away':game['away_name_abbrev']}
                break
            except:
                #no stream info, abort! abort!
                msg = "No playable streams found."
                dialog = xbmcgui.Dialog() 
                ok = dialog.ok('Streams Not Found', msg)        
                sys.exit()
     
                 
    stream_title = []    
    content_id = []
    free_game = []
    media_state = []
    playback_scenario = []    
    archive_type = ['Highlights','Recap','Condensed','Full Game']    
    #archive_type = ['Recap','Condensed','Full Game']
    try:  
        for item in epg:  
            xbmc.log(str(item))    
            xbmc.log(str(item['playback_scenario']))       
            xbmc.log(PLAYBACK_SCENARIO)
            if str(item['playback_scenario']) == PLAYBACK_SCENARIO:
                
                if item['enhanced'] == 'Y':
                    title = 'Enhanced'
                    stream_title.append(title)
                else:            
                    title = str(item['type'])[6:].title()   
                    try:
                        stream_title.append(teams[title] + " ("+item['display']+")")            
                    except:
                        stream_title.append(title+ " ("+item['display']+")")

                media_state.append(item['state'])             
                content_id.append(item['id'])  
                playback_scenario.append(str(item['playback_scenario']))       

            elif str(item['playback_scenario']) == "HTTP_CLOUD_AUDIO" and item['state'] != 'MEDIA_OFF':
                title = str(item['type']).title()
                title = title.replace('_', ' ')
                stream_title.append(title + " ("+item['display']+")")
                media_state.append(item['state'])             
                content_id.append(item['id'])  
                playback_scenario.append(str(item['playback_scenario']))          
    except:
        pass
    '''
    elif str(item['playback_scenario']) == "FLASH_2500K_1280X720" and item['type'] != 'condensed_game':
        title = str(item['type']).title()
        title = title.replace('_', ' ')
        stream_title.append(title + " ("+item['display']+")")
        media_state.append(item['state'])             
        content_id.append(item['id'])  
        playback_scenario.append("HTTP_CLOUD_WIRED_60") 
    '''
    #All past games should have highlights
    if len(stream_title) == 0 and stream_date > localToEastern():
        msg = "No playable streams found."
        dialog = xbmcgui.Dialog() 
        ok = dialog.ok('Streams Not Found', msg)        
        sys.exit()

    #Reverse Order for display purposes
    #stream_title.reverse()
    #ft.reverse()
    xbmc.log("MEDIA STATE")
    xbmc.log(str(media_state))

    stream_url = ''
    media_auth = ''
    
    play_highlights = 0
    if len(media_state) > 0:
        if media_state[0] == 'MEDIA_ARCHIVE':        
            dialog = xbmcgui.Dialog()         
            a = dialog.select('Choose Archive', archive_type)

            if a > -1:
                if a < 3:
                    recap, condensed, highlights = getHighlightLinks(teams_stream, stream_date)                
                    if a == 0:
                        play_highlights = 1
                    elif a == 1:
                        try:
                            stream_url = recap['url']
                        except:
                            dialog = xbmcgui.Dialog() 
                            ok = dialog.ok('Recap Not Available', 'The recap for this game is not yet available. \nPlease check back later.')                                                
                    else:            
                        try:
                            stream_url = condensed['url']
                        except:
                            dialog = xbmcgui.Dialog() 
                            ok = dialog.ok('Condensed Game Not Available', 'The condensed game is not yet available. \nPlease check back later.')                         
                           

                    if QUALITY == 'Always Ask' and ((len(highlights) > 0 and play_highlights == 1) or stream_url != '') :       
                        bandwidth = getStreamQuality(str(highlights[0][0]))
                    else:
                        bandwidth = find(QUALITY,'(',' kbps)')
                    
                    stream_url = createHighlightStream(stream_url, bandwidth)
                elif a == 3:        
                    dialog = xbmcgui.Dialog() 
                    n = dialog.select('Choose Stream', stream_title)
                    if n > -1:                            
                        stream_url, media_auth = fetchStream(content_id[n],event_id,playback_scenario[n])            
                        stream_url = createFullGameStream(stream_url,media_auth,media_state[n])                  
        else:
            #Add Highlights option to live games
            stream_title.insert(0,'Highlights')
            dialog = xbmcgui.Dialog()             
            n = dialog.select('Choose Stream', stream_title)
            if n > -1:  
                if stream_title[n] == 'Highlights':
                    recap, condensed, highlights = getHighlightLinks(teams_stream, stream_date)                
                    if len(highlights) > 0:
                        play_highlights = 1
                        if QUALITY == 'Always Ask':       
                            bandwidth = getStreamQuality(str(highlights[0][0]))
                        else:
                            bandwidth = find(QUALITY,'(',' kbps)')
                else:
                    try:
                        n = n -1
                        stream_url, media_auth = fetchStream(content_id[n],event_id,playback_scenario[n])            
                        stream_url = createFullGameStream(stream_url,media_auth,media_state[n])           
                    except:
                        pass
    else:
        archive_type = ['Highlights']
        dialog = xbmcgui.Dialog()         
        a = dialog.select('Choose Archive', archive_type)                
        if a == 0:
            #getHighlightLinks(teams_stream, stream_date)                
            play_highlights = 1
                                
        
    listitem = xbmcgui.ListItem(path=stream_url)        
    
    if '.m3u8' in stream_url:                
        xbmcplugin.setResolvedUrl(handle=addon_handle, succeeded=True, listitem=listitem)
        
    elif play_highlights == 1:    
        highlight_name = ['Play All']
        highlight_url = ['junk']
        for i in range(0,len(highlights)-1):
            #highlights.append([clip_url,headline,icon])
            highlight_url.append(highlights[i][0])
            highlight_name.append(highlights[i][1])
            

        dialog = xbmcgui.Dialog()         
        a = dialog.select('Choose Highlight', highlight_name)                     
        if a > 0:
            #listitem = xbmcgui.ListItem(thumbnailImage=highlights[a-1][2], path=highlights[a-1][0])                
            listitem = xbmcgui.ListItem(path=createHighlightStream(highlight_url[a], bandwidth))                
            listitem.setInfo( type="Video", infoLabels={ "Title": highlight_name[a] })         
            xbmcplugin.setResolvedUrl(handle=addon_handle, succeeded=True, listitem=listitem)            
        elif a == 0:           
            HIGHLIGHT_PLAYLIST= xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
            HIGHLIGHT_PLAYLIST.clear()
            xbmc.log(str(highlights))

            listitem = xbmcgui.ListItem('dummy', thumbnailImage='')                
            listitem.setInfo( type="Video", infoLabels={ "Title": 'dummy' })               
            HIGHLIGHT_PLAYLIST.add('http://cdn.gravlab.net/sparse/v1d30/2013/nightskyHLS/Lapse2.m3u8', listitem)

            for i in range(0,len(highlights)-1):
                #highlights.append([clip_url,headline,icon])                                                
                listitem = xbmcgui.ListItem(highlights[i][1], thumbnailImage=highlights[i][2])                
                listitem.setInfo( type="Video", infoLabels={ "Title": highlights[i][1] })            
                HIGHLIGHT_PLAYLIST.add(createHighlightStream(highlights[i][0], bandwidth), listitem)

            
    else:        
        #xbmcplugin.setResolvedUrl(addon_handle, False, listitem)
        xbmc.executebuiltin('Dialog.Close(all,true)')



def playAllHighlights():
    stream_title = ['Recap','Condensed'] 
    dialog = xbmcgui.Dialog() 
    n = dialog.select('View All', stream_title)
    
    if n == 0:
        xbmc.Player().play(RECAP_PLAYLIST)
    elif n == 1:
        xbmc.Player().play(EXTENDED_PLAYLIST)


def getGamesForDate(stream_date):
    stream_date_new = stringToDate(stream_date, "%Y-%m-%d")                
    year = stream_date_new.strftime("%Y")
    month = stream_date_new.strftime("%m")
    day = stream_date_new.strftime("%d")
    
    url = 'http://gdx.mlb.com/components/game/mlb/year_'+year+'/month_'+month+'/day_'+day+'/'    
    req = urllib2.Request(url)    
    req.add_header('Connection', 'close')
    req.add_header('User-Agent', UA_IPAD)

    try:    
        response = urllib2.urlopen(req)            
        html_data = response.read()                                 
        response.close()                
    except HTTPError as e:
        xbmc.log('The server couldn\'t fulfill the request.')
        xbmc.log('Error code: ', e.code)
        sys.exit()
    
    #<li><a href="gid_2016_03_13_arimlb_chamlb_1/"> gid_2016_03_13_arimlb_chamlb_1/</a></li>
    match = re.compile('<li><a href="gid_(.+?)/">(.+?)</a></li>',re.DOTALL).findall(html_data)   
    global RECAP_PLAYLIST
    global EXTENDED_PLAYLIST
    RECAP_PLAYLIST.clear()
    EXTENDED_PLAYLIST.clear()
    
    pDialog = xbmcgui.DialogProgressBG()
    pDialog.create('MLB Highlights', 'Retrieving Streams ...')
    match_count = len(match)
    if match_count == 0:
        match_count = 1 # prevent division by zero when no games
    perc_increments = 100/match_count
    first_time_thru = True
    bandwidth = find(QUALITY,'(',' kbps)')

    for gid, junk in match:        
        pDialog.update(perc_increments, message='Downloading '+gid)        
        try:
            recap, condensed, highlights = getHighlightLinks(None, stream_date, gid)   
           
            if first_time_thru and QUALITY == 'Always Ask':                
                bandwidth = getStreamQuality(str(recap['url']))
                first_time_thru = False
            
                       
            listitem = xbmcgui.ListItem(recap['title'], thumbnailImage=recap['icon'])    
            listitem.setInfo( type="Video", infoLabels={ "Title": recap['title'] })
            RECAP_PLAYLIST.add(createHighlightStream(recap['url'],bandwidth), listitem)
            
            listitem = xbmcgui.ListItem(condensed['title'], thumbnailImage=condensed['icon'])      
            listitem.setInfo( type="Video", infoLabels={ "Title": condensed['title'] } )
            EXTENDED_PLAYLIST.add(createHighlightStream(condensed['url'],bandwidth), listitem)
        except:
            pass

        perc_increments += perc_increments
        

    pDialog.close()


def createHighlightStream(url, bandwidth):    
    if bandwidth != '' and int(bandwidth) < 4500:
            url = url.replace('master_tablet_60.m3u8', 'asset_'+bandwidth+'K.m3u8')

    url = url + '|User-Agent='+UA_IPAD

    return url


def getHighlightLinks(teams_stream, stream_date, gid=None, bandwidth=None):
    #global HIGHLIGHT_PLAYLIST
    #HIGHLIGHT_PLAYLIST.clear()      
    stream_date = stringToDate(stream_date, "%Y-%m-%d")                
    year = stream_date.strftime("%Y")
    month = stream_date.strftime("%m")
    day = stream_date.strftime("%d")    

    if gid == None:
        away = teams_stream[:3].lower()
        home = teams_stream[3:].lower()
        url = 'http://gdx.mlb.com/components/game/mlb/year_'+year+'/month_'+month+'/day_'+day+'/gid_'+year+'_'+month+'_'+day+'_'+away+'mlb_'+home+'mlb_1/media/mobile.xml'
    else:
        url = 'http://gdx.mlb.com/components/game/mlb/year_'+year+'/month_'+month+'/day_'+day+'/gid_'+gid+'/media/mobile.xml'

    req = urllib2.Request(url)    
    req.add_header('Connection', 'close')
    req.add_header('User-Agent', UA_IPAD)
    try:    
        response = urllib2.urlopen(req)            
        xml_data = response.read()                                 
        response.close()                
    except HTTPError as e:
        xbmc.log('The server couldn\'t fulfill the request.')
        xbmc.log('Error code: ', e.code)
        sys.exit()
    
    match = re.compile('<media id="(.+?)"(.+?)<headline>(.+?)</headline>(.+?)<thumb type="22">(.+?)</thumb>(.+?)<url playback-scenario="HTTP_CLOUD_TABLET_60">(.+?)</url>',re.DOTALL).findall(xml_data)       
    bandwidth = find(QUALITY,'(',' kbps)') 
    
    recap = {}
    condensed = {}
    highlights = []

    for media_id, media_tag, headline, junk1, icon, junk2, clip_url in match:          
        if 'media-type="T"' in media_tag:
            #if bandwidth != '' and int(bandwidth) < 4500:
            #clip_url = clip_url.replace('master_tablet_60.m3u8', 'asset_'+bandwidth+'K.m3u8')
            
            #clip_url = clip_url + '|User-Agent='+UA_IPAD                        
            highlights.append([clip_url,headline,icon])
       

        if 'media-type="R"' in media_tag:           
            #icon = 'http://mediadownloads.mlb.com/mlbam/'+year+'/'+month+'/'+day+'/images/mlbf_'+media_id+'_th_43.jpg'  
            title = headline
            recap = {'url':clip_url, 'icon':icon, 'title':headline}
        elif 'media-type="C"' in media_tag:
            #icon = 'http://mediadownloads.mlb.com/mlbam/'+year+'/'+month+'/'+day+'/images/mlbf_'+media_id+'_th_43.jpg'            
            title = headline
            condensed = {'url':clip_url, 'icon':icon, 'title':headline}

    return recap, condensed, highlights



def createFullGameStream(stream_url, media_auth, media_state):
    #SD (800 kbps)|SD (1600 kbps)|HD (3000 kbps)|HD (5000 kbps)        
    bandwidth = ''
    bandwidth = find(QUALITY,'(',' kbps)')

    if QUALITY == 'Always Ask':
        bandwidth = getStreamQuality(stream_url)

    #Only set bandwidth if it's explicitly set
    if bandwidth != '':
        if media_state == 'MEDIA_ARCHIVE':                
            #ARCHIVE
            #stream_url = stream_url.replace(MASTER_FILE_TYPE, bandwidth+'K/'+bandwidth+'_complete_fwv2-trimmed.m3u8') 
            stream_url = stream_url.replace(MASTER_FILE_TYPE, bandwidth+'K/'+bandwidth+'_complete-trimmed.m3u8')
        elif media_state == 'MEDIA_ON':
            #LIVE   
            #stream_url = stream_url.replace(MASTER_FILE_TYPE, bandwidth+'K/'+bandwidth+'_slide_fwv2.m3u8') 
            stream_url = stream_url.replace(MASTER_FILE_TYPE, bandwidth+'K/'+bandwidth+'_complete.m3u8')

    #CDN
    akc_url = 'akc.mlb.com'
    l3c_url = 'l3c.mlb.com'
    if CDN == 'Akamai' and  akc_url not in stream_url:
        stream_url = stream_url.replace(l3c_url,akc_url)
    elif CDN == 'Level 3' and  l3c_url not in stream_url:
        stream_url = stream_url.replace(akc_url,l3c_url)
    
    #stream_url = stream_url + '|User-Agent='+UA_IPAD+'&Cookie='+media_auth
    stream_url = stream_url + '|User-Agent='+UA_PS4+'&Cookie='+media_auth
    
    return stream_url


def fetchStream(content_id,event_id,playback_scenario):        
    stream_url = ''
    media_auth = ''       
    identity_point_id = ''
    fingerprint = '' 

    expired_cookies = True
    try:
        cj = cookielib.LWPCookieJar(os.path.join(ADDON_PATH_PROFILE, 'cookies.lwp')) 
        cj.load(os.path.join(ADDON_PATH_PROFILE, 'cookies.lwp'),ignore_discard=True)        

        #Check if cookies have expired
        at_least_one_expired = False
        num_cookies = 0
        for cookie in cj:                        
            num_cookies += 1            
            if cookie.is_expired():
                at_least_one_expired = True
                break

        if not at_least_one_expired:
            expired_cookies = False
    except:
        pass

    if expired_cookies or num_cookies == 0 or USERNAME != OLD_USERNAME or PASSWORD != OLD_PASSWORD:
        #Remove cookie file
        cookie_file = xbmc.translatePath(os.path.join(ADDON_PATH_PROFILE+'cookies.lwp'))
        try:
            os.remove(cookie_file)
        except:
            pass
        login()


    cj = cookielib.LWPCookieJar(os.path.join(ADDON_PATH_PROFILE, 'cookies.lwp'))     
    cj.load(os.path.join(ADDON_PATH_PROFILE, 'cookies.lwp'),ignore_discard=True)
    for cookie in cj:         
        if cookie.name == "ipid":
            identity_point_id = cookie.value
        elif cookie.name == "fprt":
            fingerprint = cookie.value

    if identity_point_id == '' or fingerprint == '':
        return stream_url, media_auth


    session_key = getSessionKey(content_id,event_id,identity_point_id,fingerprint)
    #Reload Cookies
    cj = cookielib.LWPCookieJar(os.path.join(ADDON_PATH_PROFILE, 'cookies.lwp'))     
    cj.load(os.path.join(ADDON_PATH_PROFILE, 'cookies.lwp'),ignore_discard=True)

    if PROXY_ENABLED != 'true':
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))        
    else:
        proxy_url = 'http://'+PROXY_SERVER+':'+PROXY_PORT
        proxy_support = urllib2.ProxyHandler({ 'http': proxy_url, 'https': proxy_url })
        if PROXY_USER != '' and PROXY_PWD != '':
            auth_handler = urllib2.ProxyBasicAuthHandler()
            auth_handler.add_password(None, proxy_url, PROXY_USER, PROXY_PWD)
            opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), proxy_support, auth_handler)
        else:
            opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), proxy_support)

        urllib2.install_opener(opener)

    if session_key == '':
        return stream_url, media_auth
    elif session_key == 'blackout':
        msg = "The game you are trying to access is not currently available due to local or national blackout restrictions.\n Full game archives will be available 48 hours after completion of this game."
        dialog = xbmcgui.Dialog() 
        ok = dialog.ok('Game Blacked Out', msg) 
        return stream_url, media_auth

      
    url = 'https://mlb-ws-mf.media.mlb.com/pubajaxws/bamrest/MediaService2_0/op-findUserVerifiedEvent/v-2.3'   
    url = url + '?identityPointId='+identity_point_id
    url = url + '&fingerprint='+fingerprint
    url = url + '&contentId='+content_id    
    url = url + '&eventId='+event_id
    url = url + '&playbackScenario='+playback_scenario        
    url = url + '&subject=LIVE_EVENT_COVERAGE'
    url = url + '&sessionKey='+urllib.quote_plus(session_key)
    url = url + '&platform=PS4'
    url = url + '&format=json'    
    req = urllib2.Request(url)       
    req.add_header("Accept", "*/*")
    req.add_header("Accept-Encoding", "deflate")
    req.add_header("Accept-Language", "en-US,en;q=0.8")                       
    req.add_header("Connection", "keep-alive")    
    req.add_header("User-Agent", UA_PS4)

    response = opener.open(req)    
    json_source = json.load(response)                                   
    response.close()
    
    

    if json_source['status_code'] == 1:
        uv_media_item = json_source['user_verified_event'][0]['user_verified_content'][0]['user_verified_media_item'][0]        
        if 'BLACKOUT' in str(uv_media_item ['blackout_status']).upper():            
            msg = "We're sorry.  We have determined that you are blacked out of watching the game you selected due to Major League Baseball exclusivities."
            #try:
            if str(uv_media_item ['media_item']['state']).upper() == 'MEDIA_ARCHIVE':
                #cc_url = str(json_source['user_verified_event'][0]['user_verified_content'][0]['domain_specific_attributes'][3]['value'])
                for attribute in json_source['user_verified_event'][0]['user_verified_content'][0]['domain_specific_attributes']:
                    if str(attribute['name']).lower() == 'inning_index_location_xml':
                        inning_xml_url = str(attribute['value'])
                        #inning_xml_url = "http://mlb.mlb.com/mlb/mmls2016/447002.xml"
                        blackout_lift_min, blackout_lift_time = getBlackoutLiftTime(inning_xml_url)
                        msg = msg + " This blackout will expire in "+str(blackout_lift_min)+" minutes at approximately "+str(blackout_lift_time)+"."
                        break

            dialog = xbmcgui.Dialog() 
            ok = dialog.ok('Game Blacked Out', msg) 
            sys.exit()
            xbmc.executebuiltin('Dialog.Close(all,true)')
        elif str(uv_media_item ['auth_status']) == 'NotAuthorizedStatus':
            msg = "You do not have an active MLB.TV premium subscription. If you are using a Single Team or Free subscription please check this is enabled in the addon settings."
            dialog = xbmcgui.Dialog() 
            ok = dialog.ok('Account Not Authorized', msg) 
            sys.exit()
            xbmc.executebuiltin('Dialog.Close(all,true)')
        else:
            stream_url = uv_media_item ['url']                
            #Find subtitles
            '''
            for item in json_source['user_verified_event'][0]['user_verified_content'][0]['domain_specific_attributes']:
                if item['name'] == 'closed_captions_location_ttml':
                    subtitles_url = item['value']
                    convertSubtitles(subtitles_url)
            '''
            session_key = json_source['session_key']            
            #Update Session Key
            settings.setSetting(id='session_key', value=session_key)   
    else:
        msg = json_source['status_message']
        dialog = xbmcgui.Dialog() 
        ok = dialog.ok('Error Fetching Stream', msg)     
        
    
    for cookie in cj:         
        if cookie.name == "mediaAuth":
            media_auth = "mediaAuth="+cookie.value
            settings.setSetting(id='media_auth', value=media_auth)
    
    cj.save(ignore_discard=True)

    return stream_url, media_auth    
   



def getSessionKey(content_id,event_id,identity_point_id,fingerprint):    
    #session_key = ''
    session_key = str(settings.getSetting(id="session_key"))

    if session_key == '':               
        cj = cookielib.LWPCookieJar(os.path.join(ADDON_PATH_PROFILE, 'cookies.lwp')) 
        cj.load(os.path.join(ADDON_PATH_PROFILE, 'cookies.lwp'),ignore_discard=True)
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))  

        epoch_time_now = str(int(round(time.time()*1000)))           
        url = 'https://mlb-ws-mf.media.mlb.com/pubajaxws/bamrest/MediaService2_0/op-findUserVerifiedEvent/v-2.3'
        url = url + '?identityPointId='+identity_point_id
        url = url + '&fingerprint='+fingerprint
        url = url + '&eventId='+event_id
        url = url + '&subject=LIVE_EVENT_COVERAGE'
        url = url + '&platform=WIN8'
        url = url + '&frameworkURL=https://mlb-ws-mf.media.mlb.com&frameworkEndPoint=/pubajaxws/bamrest/MediaService2_0/op-findUserVerifiedEvent/v-2.3'
        url = url + '&_='+epoch_time_now

        req = urllib2.Request(url)       
        req.add_header("Accept", "*/*")
        req.add_header("Accept-Encoding", "deflate")
        req.add_header("Accept-Language", "en-US,en;q=0.8")                       
        req.add_header("Connection", "keep-alive")        
        req.add_header("User-Agent", UA_PC)
        req.add_header("Origin", "http://m.mlb.com")        
        req.add_header("Referer", "http://m.mlb.com/tv/e"+event_id+"/v"+content_id+"/?&media_type=video&clickOrigin=Media Grid&team=mlb&forwardUrl=http://m.mlb.com/tv/e"+event_id+"/v"+content_id+"/?&media_type=video&clickOrigin=Media%20Grid&team=mlb&template=mp5default&flowId=registration.dynaindex&mediaTypeTemplate=video")
        
        response = opener.open(req)
        xml_data = response.read()
        response.close()
                
        session_key = find(xml_data,'<session-key>','</session-key>')
        settings.setSetting(id='session_key', value=session_key)

        '''
        if json_source['status_code'] == 1:      
            if json_source['user_verified_event'][0]['user_verified_content'][0]['user_verified_media_item'][0]['blackout_status']['status'] == 'BlackedOutStatus':
                msg = "You do not have access to view this content. To watch live games and learn more about blackout restrictions, please visit NHL.TV"
                session_key = 'blackout'
            else:    
                session_key = str(json_source['session_key'])
                settings.setSetting(id='session_key', value=session_key)                              
        else:
            msg = json_source['status_message']
            dialog = xbmcgui.Dialog() 
            ok = dialog.ok('Error Fetching Stream', msg)            
        '''
    return session_key  
    

def myTeamsGames():    
    if FAV_TEAM != 'None':
        fav_team_id = getFavTeamId()

        end_date = localToEastern()
        end_date = stringToDate(end_date, "%Y-%m-%d")            
        start_date = end_date - timedelta(days=30) 
        start_date = start_date.strftime("%Y%m%d")
        end_date = end_date.strftime("%Y%m%d")
        season = start_date.strftime("%Y")
        

        url = 'http://mlb.mlb.com/lookup/named.schedule_vw.bam?end_date='+end_date+'&season='+season+'&team_id='+fav_team_id+'&start_date='+start_date
        #${expand},schedule.ticket&${optionalParams}'
        req = urllib2.Request(url)   
        req.add_header('User-Agent', UA_IPAD)
        response = urllib2.urlopen(req)    
        json_source = json.load(response)                           
        response.close()

        #<row game_pk="469406" record_source="S" game_id="2016/03/01/pitmlb-detmlb-1" game_type="S" month="3" game_date="2016-03-01T00:00:00" month_abbrev="Mar" month_full="March" day="3" day_abbrev="Tue" game_day="Tuesday" double_header_sw="N" gameday_sw="Y" interleague_sw="" game_nbr="1" series_nbr="1" series_game_nbr="1" game_time_et="2016-03-01T13:05:00" if_necessary="N" scheduled_innings="9" inning="9" top_inning_sw="N" away_team_id="134" away_all_star_sw="N" away_team_file_code="pit" away_team_city="Pittsburgh" away_team_full="Pittsburgh Pirates" away_team_brief="Pirates" away_team_abbrev="PIT" away_league_id="104" away_league="NL" away_sport_code="mlb" away_parent_id="" away_parent_org="" away_split_squad="N" home_team_id="116" home_all_star_sw="N" home_team_file_code="det" home_team_city="Detroit" home_team_full="Detroit Tigers" home_team_brief="Tigers" home_team_abbrev="DET" home_league_id="103" home_league="AL" home_sport_code="mlb" home_parent_id="" home_parent_org="" home_split_squad="N" venue_id="2511" venue="Joker Marchant Stadium" venue_short="" venue_city="Lakeland" venue_state="FL" venue_country="USA" milbtv_sw="N" home_tunein="" away_tunein="" game_time_local="3/1/2016 1:05:00 PM" time_zone_local="EST" game_time_home="3/1/2016 1:05:00 PM" time_zone_home="EST" game_time_away="3/1/2016 1:05:00 PM" time_zone_away="EST" resumed_on="" resumed_at="" resumed_from="" rescheduled_to="" rescheduled_at="" rescheduled_from="" game_status_ind="F" game_status_text="Final" reason="" home_probable_id="571510" home_probable="Boyd, Matt" home_probable_wl="0-0" home_probable_era="-.--" away_probable_id="543456" away_probable="Lobstein, Kyle" away_probable_wl="0-0" away_probable_era="-.--" home_team_wl="0-1" away_team_wl="1-0" home_score="2" away_score="4" home_result="L" away_result="W" win_pitcher_id="543746" win_pitcher="Scahill, Rob" win_pitcher_wl="1-0" win_pitcher_era="18.00" loss_pitcher_id="434137" loss_pitcher="Kensing, Logan" loss_pitcher_wl="0-1" loss_pitcher_era="18.00" editorial_stats_type="S" editorial_stats_season="2016"/>
        match = re.compile('<row (.+?)/">').findall(link)   

        for game_row in match:  
        
            for game in date['games']:        
                createGameListItem(game, date['date'])  

        
    else:
        msg = "Please select your favorite team from the addon settings"
        dialog = xbmcgui.Dialog() 
        ok = dialog.ok('Favorite Team Not Set', msg)


def login():    
    #Check if username and password are provided    
    global USERNAME
    if USERNAME == '':        
        dialog = xbmcgui.Dialog()
        USERNAME = dialog.input('Please enter your username', type=xbmcgui.INPUT_ALPHANUM)        
        settings.setSetting(id='username', value=USERNAME)

    global PASSWORD
    if PASSWORD == '':        
        dialog = xbmcgui.Dialog()
        PASSWORD = dialog.input('Please enter your password', type=xbmcgui.INPUT_ALPHANUM, option=xbmcgui.ALPHANUM_HIDE_INPUT)
        settings.setSetting(id='password', value=PASSWORD)

   
    if USERNAME != '' and PASSWORD != '':      
        settings.setSetting("old_username",USERNAME)
        settings.setSetting("old_password",PASSWORD)
        cj = cookielib.LWPCookieJar(os.path.join(ADDON_PATH_PROFILE, 'cookies.lwp')) 
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))   
         
        url = 'https://securea.mlb.com/authenticate.do'            
        login_data = 'uri=%2Faccount%2Flogin_register.jsp&registrationAction=identify&emailAddress='+USERNAME+'&password='+PASSWORD+'&submitButton='


        req = urllib2.Request(url, data=login_data, headers=
            {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
             "Accept-Encoding": "gzip, deflate",
             "Accept-Language": "en-US,en;q=0.8",
             "Content-Type": "application/x-www-form-urlencoded",                            
             "Origin": "https://securea.mlb.com",             
             "Connection": "keep-alive",
             #"Cookie": "SESSION_1=wf_forwardUrl===http://m.mlb.com/tv/e14-469412-2016-03-02/v545147283/?&media_type=video&clickOrigin=Media%20Grid&team=mlb~wf_flowId===registration.dynaindex~wf_template===mp5default~wf_mediaTypeTemplate===video~stage===3~flowId===registration.dynaindex~forwardUrl===http://m.mlb.com/tv/e14-469412-2016-03-02/v545147283/?&media_type=video&clickOrigin=Media%20Grid&team=mlb;",
             "Cookie": "SESSION_1=wf_forwardUrl%3D%3D%3Dhttp%3A%2F%2Fm.mlb.com%2Ftv%2Fe14-469412-2016-03-02%2Fv545147283%2F%3F%26media_type%3Dvideo%26clickOrigin%3DMedia%2520Grid%26team%3Dmlb%7Ewf_flowId%3D%3D%3Dregistration.dynaindex%7Ewf_template%3D%3D%3Dmp5default%7Ewf_mediaTypeTemplate%3D%3D%3Dvideo%7Estage%3D%3D%3D3%7EflowId%3D%3D%3Dregistration.dynaindex%7EforwardUrl%3D%3D%3Dhttp%3A%2F%2Fm.mlb.com%2Ftv%2Fe14-469412-2016-03-02%2Fv545147283%2F%3F%26media_type%3Dvideo%26clickOrigin%3DMedia%2520Grid%26team%3Dmlb%3B",
             "User-Agent": UA_PC})  
             
       

        
        try:
            response = opener.open(req) 
            # if url has not changed the login was not valid.
            if response.geturl() == url:
                msg = "Please check that your username and password are correct"
                dialog = xbmcgui.Dialog() 
                ok = dialog.ok('Invalid Login', msg)
                sys.exit()
            else:
                cj.save(ignore_discard=True);
        except HTTPError as e:
            xbmc.log('The server couldn\'t fulfill the request.')
            xbmc.log('Error code: ', e.code)
            xbmc.log(url)
            
            #Error 401 for invalid login
            if e.code == 401:
                msg = "Please check that your username and password are correct"
                dialog = xbmcgui.Dialog() 
                ok = dialog.ok('Invalid Login', msg)

        #response = opener.open(req)              
        #user_data = response.read()
        response.close()
      

def logout():    
    #Just delete the cookie file
    cookie_file = xbmc.translatePath(os.path.join(ADDON_PATH_PROFILE+'cookies.lwp'))
    try:
        os.remove(cookie_file)
    except:
        pass

    settings.setSetting(id='session_key', value='') 
    dialog = xbmcgui.Dialog() 
    title = "Logout Successful" 
    dialog.notification(title, 'Logout completed successfully', ICON, 5000, False)


    
params=get_params()
name=None
mode=None
game_day=None
event_id=None
gid=None
teams_stream=None
stream_date=None

try:
    name=urllib.unquote_plus(params["name"])
except:
    pass
try:
    mode=int(params["mode"])
except:
    pass
try:
    game_day=urllib.unquote_plus(params["game_day"])
except:    
    pass
try:
    event_id=urllib.unquote_plus(params["event_id"])
except:
    pass
try:
    gid=urllib.unquote_plus(params["gid"])
except:
    pass
try:
    teams_stream=urllib.unquote_plus(params["teams_stream"])
except:
    pass
try:
    stream_date=urllib.unquote_plus(params["stream_date"])
except:
    pass

if mode==None:        
    categories()  

elif mode == 100:      
    #Todays Games            
    todaysGames(None)    

elif mode == 101:
    #Prev and Next 
    todaysGames(game_day)    

elif mode == 104:    
    streamSelect(event_id, gid, teams_stream, stream_date)

elif mode == 105:
    #Yesterday's Games
    game_day = localToEastern()
    display_day = stringToDate(game_day, "%Y-%m-%d")            
    prev_day = display_day - timedelta(days=1)                
    todaysGames(prev_day.strftime("%Y-%m-%d"))

elif mode == 200:
    #Goto Date
    search_txt = ''
    dialog = xbmcgui.Dialog()
    game_day = dialog.input('Enter date (yyyy-mm-dd)', type=xbmcgui.INPUT_ALPHANUM)    
    mat=re.match('(\d{4})-(\d{2})-(\d{2})$', game_day)        
    if mat is not None:    
        todaysGames(game_day)
    else:    
        if game_day != '':    
            msg = "The date entered is not in the format required."
            dialog = xbmcgui.Dialog() 
            ok = dialog.ok('Invalid Date', msg)

        sys.exit()        

elif mode == 400:    
    logout()

elif mode == 500:
    myTeamsGames()

elif mode == 900:        
    getGamesForDate(stream_date)
    playAllHighlights()    

elif mode == 999:
    sys.exit()

if mode == 100:
    xbmcplugin.endOfDirectory(addon_handle, cacheToDisc=False)
elif mode == 101:
    xbmcplugin.endOfDirectory(addon_handle, cacheToDisc=False, updateListing=True)
else:
    xbmcplugin.endOfDirectory(addon_handle)