cloudstream/.github/site-list.py

55 lines
2 KiB
Python
Raw Normal View History

2021-10-04 14:57:57 +00:00
#!/usr/bin/python3
from glob import glob
from re import findall, compile, DOTALL
from json import dump, load
2021-10-04 17:19:39 +00:00
from typing import List, Dict
2021-10-04 14:57:57 +00:00
# Globals
2021-10-04 17:19:39 +00:00
URL_REGEX = compile(
2022-03-16 21:19:59 +00:00
"override\sva[lr]\smainUrl[^\"']+[\"'](https?://[a-zA-Z0-9\.-]+)[\"']")
NAME_REGEX = compile("([A-Za-z0-9]+)(?:.kt)$")
JSON_PATH = "docs/providers.json"
2021-10-04 14:57:57 +00:00
GLOB = "app/src/main/java/com/lagradost/cloudstream3/*providers/*Provider.kt"
old_sites: Dict[str, Dict] = load(open(JSON_PATH, "r", encoding="utf-8"))
sites: Dict[str, Dict] = {}
2021-10-04 14:57:57 +00:00
# parse all *Provider.kt files
2021-10-04 14:57:57 +00:00
for path in glob(GLOB):
with open(path, "r", encoding='utf-8') as file:
try:
2021-10-04 17:19:39 +00:00
site_text: str = file.read()
name: str = findall(NAME_REGEX, path)[0]
provider_url: str = [*findall(URL_REGEX, site_text), ""][0]
if name in old_sites.keys(): # if already in previous list use old status and name
sites[name] = {
"name": old_sites[name]['name'],
"url": provider_url if provider_url else old_sites[name]['url'],
"status": old_sites[name]['status']
}
else: # if not in previous list add with new data
display_name = name
if display_name.endswith("Provider"):
display_name = display_name[:-len("Provider")]
sites[name] = {
"name": display_name,
"url": provider_url if provider_url else "",
"status": 1
}
2021-10-04 17:19:39 +00:00
2021-10-04 14:57:57 +00:00
except Exception as ex:
print("{0}: {1}".format(path, ex))
# add sites from old_sites that are missing in new list
for name in old_sites.keys():
if name not in sites.keys():
sites[name] = {
"name": old_sites[name]['name'],
"url": old_sites[name]['url'],
"status": old_sites[name]['status']
}
dump(sites, open(JSON_PATH, "w+", encoding="utf-8"), indent=4, sort_keys=True)