Skip to content

Instantly share code, notes, and snippets.

@larsenv
Last active April 1, 2021 16:41
Show Gist options
  • Save larsenv/9713bfd139389bfdc8fd17aac6315324 to your computer and use it in GitHub Desktop.
Save larsenv/9713bfd139389bfdc8fd17aac6315324 to your computer and use it in GitHub Desktop.
Locast Video Downloader
import json
import os
import requests
import subprocess
cities = {}
cities["Atlanta"] = [524, 33.7490, -84.3880]
cities["Baltimore"] = [512, 39.2904, -76.6122]
cities["Boston"] = [506, 42.3601, -71.0589]
cities["Charlotte"] = [517, 35.2271, -80.8431]
cities["Chicago"] = [602, 41.8781, -87.6298]
cities["Cleveland"] = [510, 41.4993, -81.6944]
cities["Dallas"] = [623, 32.7767, -96.7970]
cities["Denver"] = [751, 39.7392, -104.9903]
cities["Detroit"] = [505, 42.3314, -83.0458]
cities["Houston"] = [618, 29.7604, -95.3698]
cities["Indianapolis"] = [527, 39.7684, -86.1581]
cities["Los Angeles"] = [803, 34.0522, -118.2437]
cities["Madison"] = [669, 43.0731, -89.4012]
cities["Miami"] = [528, 25.7617, -80.1918]
cities["Minneapolis"] = [613, 44.9778, -93.2650]
cities["New York"] = [501, 40.7128, -74.0060]
cities["Orlando"] = [534, 28.5383, -81.3792]
cities["Philadelphia"] = [504, 39.9526, -75.1652]
cities["Phoenix"] = [753, 33.4484, -112.0740]
cities["Portland"] = [820, 45.5051, -122.6750]
cities["Puerto Rico"] = [490, 18.4655, -66.1057]
cities["Rapid City"] = [764, 44.0805, -103.2310]
cities["Sacramento"] = [862, 38.5816, -121.4944]
cities["San Francisco"] = [807, 33.7749, -122.4194]
cities["Scranton"] = [577, 41.4090, -75.6624]
cities["Seattle"] = [819, 47.6062, -122.3321]
cities["Sioux City"] = [624, 42.4963, -96.4049]
cities["Sioux Falls"] = [725, 43.5473, -96.7283]
cities["Tampa Bay"] = [539, 27.9506, -82.4572]
cities["Washington DC"] = [551, 38.9072, -77.0369]
cities["West Palm Beach"] = [548, 26.7153, -80.0534]
print("==========Locast Downloader==========\n")
i = 0
cities_index = []
for k,v in cities.items():
i += 1
cities_index.append(k)
print(str(i).zfill(3) + ". " + k)
selection = input("\nPlease enter the number of the city you want to download from: ")
print("\nPlease wait...")
item = cities[cities_index[int(selection) - 1]]
epg = json.loads(requests.get("https://api.locastnet.org/api/watch/epg/" + str(item[0])).text)
channels = {}
i = 0
for ids in epg:
i += 1
channels[i] = [ids["id"], ids["callSign"] + " - " + ids["name"]]
print(str(i + 100).zfill(1) + ". " + ids["callSign"] + " - " + ids["name"])
selection2 = input("\nPlease enter the number of the channel you want to download from: ")
print("\nPlease wait...")
if not os.path.exists("token.txt"):
token = input("\nNow we need to get an authorization token. Please do the following on Google Chrome or Firefox:" + \
"\n\n1. Go to https://locast.org/ and sign in if you haven't already." + \
"\n2. Select a city and select a channel (doesn't matter what it is.)" + \
"\n3. Right click and select Inspect Element." + \
"\n4. Select the Network tab and press Watch Now." + \
"\n5. Select the request with a decimal in it (with the type fetch)." + \
"\n6. Select Headers and copy and paste the code that's on the line named Authorization." + \
"\n7. Stop the stream."
"\n\nPlease enter the code: ")
with open("token.txt", "w") as f:
f.write(token)
else:
with open("token.txt", "r") as f:
token = f.read()
f.close()
token = "Bearer " + token.replace("authorization: ", "").replace("Bearer ", "")
headers = {"authorization": token,
"user-agent": "Locast"}
stream = json.loads(requests.get("https://api.locastnet.org/api/watch/station/" + str(channels[int(selection2) - 100][0]) + "/" + str(item[1]) + "/" + str(item[2]), headers=headers).text)
print("\nDownloading... Press Control+C or close the window to finish the downloading.")
while True:
k += 1
job = subprocess.Popen(["ffmpeg", "-i", stream["streamUrl"], str(channels[int(selection2) - 100][1]) + str(k) + ".mkv"])
try:
job.wait()
except KeyboardInterrupt:
try:
job.terminate()
except OSError:
pass
job.wait()
stream = json.loads(requests.get("https://api.locastnet.org/api/watch/station/" + str(channels[int(selection2) - 100][0]) + "/" + str(item[1]) + "/" + str(item[2]), headers=headers).text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment