11import requests
2+ import re
23import sys
34import xbmc
45import xbmcgui
@@ -35,7 +36,6 @@ def scrape_and_play_abc_weather_video():
3536 pass
3637
3738
38- # See bottom of this file for notes on matching the video links (& Store.py for the regex)
3939def get_abc_weather_video_link ():
4040 try :
4141 r = requests .get (Store .ABC_URL , timeout = 15 )
@@ -45,16 +45,47 @@ def get_abc_weather_video_link():
4545 error_msg = "ABC __NEXT_DATA__ script not found on page, couldn't extract ABC weather video link"
4646 Logger .error (error_msg )
4747 raise ValueError (error_msg )
48+
4849 json_object = json .loads (json_string .string )
50+
4951 # Logger.debug(json_object)
5052 # Put the json blob into: https://jsonhero.io/j/JU0I9LB4AlLU
5153 # Gives a path to the needed video as:
5254 # $.props.pageProps.channelpage.components.0.component.props.list.3.player.config.sources.1.file
5355 # Rather than grab the URL directly (as place in array might change), grab all the available URLs and get the best quality from it
5456 # See: https://github.com/bossanova808/weather.ozweather/commit/e6158d704fc160808bf66220da711805860d85c7
55- data = json_object ['props' ]['pageProps' ]['channelpage' ]['components' ][0 ]['component' ]['props' ]['list' ][3 ]
56- urls = [x for x in data ['player' ]['config' ]['sources' ] if x ['type' ] == 'video/mp4' ]
57- return sorted (urls , key = lambda x : x ['bitrate' ], reverse = True )[0 ]['file' ]
57+
58+ items = json_object ['props' ]['pageProps' ]['channelpage' ]['components' ][0 ]['component' ]['props' ]['list' ]
59+
60+ data = next ((item for item in items if item .get ('player' , {}).get ('config' , {}).get ('sources' )), None )
61+ if not data :
62+ Logger .error ("No player sources found in ABC JSON" )
63+ return ""
64+
65+ # urls_all = data['player']['config']['sources']
66+ # Logger.debug(f"ALL ABC video sources (unfiltered): {urls_all}")
67+
68+ urls = [x for x in data ['player' ]['config' ]['sources' ] if x .get ('type' ) == 'video/mp4' ]
69+ if not urls :
70+ Logger .error ("No MP4 sources found in ABC player config" )
71+ return ""
72+
73+ Logger .debug (f"ABC video sources available: { [{k : v for k , v in u .items () if k != 'type' } for u in urls ]} " )
74+
75+ def quality_key (source ):
76+ if 'fileSize' in source :
77+ return source ['fileSize' ]
78+ match = re .search (r'(\d+)p' , source .get ('label' , '0' ))
79+ return int (match .group (1 )) if match else 0
80+
81+ url = sorted (urls , key = quality_key , reverse = True )[0 ].get ('file' , '' )
82+
83+ if not url or not re .match (r'^https?://[^/\s]+' , url ):
84+ Logger .error (f"ABC returned a weird video URL?!: { url } " )
85+ return ""
86+
87+ Logger .debug (f"Selected ABC video: { url } (from { len (urls )} sources)" )
88+ return url
5889
5990 except Exception as inst :
6091 Logger .error (f"Couldn't get ABC video URL from scraped page: { inst } " )
0 commit comments