mirror of
https://github.com/ovosimpatico/radio.garden-to-m3u.git
synced 2026-01-15 14:12:53 -03:00
Merge pull request #1 from sebdvvv/main
Improved URL resolving by filling the M3U with real URLs
This commit is contained in:
23
main.py
23
main.py
@@ -8,10 +8,11 @@ parser = argparse.ArgumentParser(
|
|||||||
|
|
||||||
parser.add_argument('--country', help='Country name', type=str, required=True)
|
parser.add_argument('--country', help='Country name', type=str, required=True)
|
||||||
parser.add_argument('--state', help='State or province name', type=str, required=False)
|
parser.add_argument('--state', help='State or province name', type=str, required=False)
|
||||||
|
parser.add_argument('--onlinestream', help='Only include working streams (status 200)', action='store_true')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
BASE_URL = "https://radio.garden/api"
|
||||||
BASE_URL = "http://radio.garden/api"
|
|
||||||
|
|
||||||
def get_places():
|
def get_places():
|
||||||
|
|
||||||
@@ -25,6 +26,20 @@ def get_places():
|
|||||||
|
|
||||||
return sorted(list)
|
return sorted(list)
|
||||||
|
|
||||||
|
def resolve_final_stream_url(rg_proxy_url):
|
||||||
|
try:
|
||||||
|
# Make a HEAD request to follow redirects but not download stream content
|
||||||
|
response_api = requests.get(rg_proxy_url, allow_redirects=True, stream=True, timeout=10)
|
||||||
|
response_stream = requests.get(response_api.url, stream=True)
|
||||||
|
|
||||||
|
if args.onlinestream :
|
||||||
|
if response_stream.status_code != 200 :
|
||||||
|
return
|
||||||
|
return response_stream.url # This is the final resolved stream URL
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[ERROR] Couldn't resolve URL for {rg_proxy_url}: {e}")
|
||||||
|
return rg_proxy_url # fallback to original
|
||||||
def get_stations(place_id):
|
def get_stations(place_id):
|
||||||
list = []
|
list = []
|
||||||
|
|
||||||
@@ -47,9 +62,9 @@ def m3u(stations):
|
|||||||
|
|
||||||
for station in stations:
|
for station in stations:
|
||||||
station_name = station[0]
|
station_name = station[0]
|
||||||
stream_url = get_stream_url(station[1])
|
resolved_url = resolve_final_stream_url(get_stream_url(station[1]))
|
||||||
f.write(f'#EXTINF:-1 tvg-name="{station_name}", {station_name}\n')
|
f.write(f'#EXTINF:-1 tvg-name="{station_name}", {station_name}\n')
|
||||||
f.write(f'{stream_url}\n')
|
f.write(f'{resolved_url}\n')
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user