cloudstream/.github/site-list.py

64 lines
2 KiB
Python
Raw Normal View History

2021-10-04 14:57:57 +00:00
#!/usr/bin/python3
from glob import glob
2021-10-04 17:19:39 +00:00
from re import findall, compile, sub, DOTALL
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\.-]+)[\"']")
2021-10-04 17:28:09 +00:00
NAME_REGEX = compile("class (.+?) ?: \w+\(\)\s\{")
2021-10-04 14:57:57 +00:00
START_MARKER = "<!--SITE LIST START-->"
END_MARKER = "<!--SITE LIST END-->"
GLOB = "app/src/main/java/com/lagradost/cloudstream3/*providers/*Provider.kt"
2021-10-04 16:13:23 +00:00
MAIN_API = "app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt"
2021-10-04 17:19:39 +00:00
API_REGEX = compile(
2022-03-16 15:59:39 +00:00
"val\s*allProviders.*?{\s.*?arrayListOf\(([\W\w]*?)\)\s*\n*\s*}", DOTALL)
2021-10-04 14:57:57 +00:00
2021-10-04 17:19:39 +00:00
sites: Dict[str, str] = {}
enabled_sites: List[str] = []
2021-10-04 16:13:23 +00:00
with open(MAIN_API, "r", encoding="utf-8") as f:
apis = findall(API_REGEX, f.read())
for api_list in apis:
for api in api_list.split("\n"):
if not api.strip() or api.strip().startswith("/"):
continue
enabled_sites.append(api.strip().split("(")[0])
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: List[str] = findall(NAME_REGEX, site_text)
provider_text: str = findall(URL_REGEX, site_text)
2021-10-04 16:13:23 +00:00
if name:
if name[0] not in enabled_sites:
continue
2021-10-04 17:19:39 +00:00
sites[name[0]] = provider_text[0]
2021-10-04 14:57:57 +00:00
except Exception as ex:
print("{0}: {1}".format(path, ex))
2021-10-04 16:13:23 +00:00
2021-10-04 17:19:39 +00:00
2021-10-04 16:14:22 +00:00
with open("README.md", "r+", encoding='utf-8') as readme:
2021-10-04 14:57:57 +00:00
raw = readme.read()
if START_MARKER not in raw or END_MARKER not in raw:
raise RuntimeError("Missing start and end markers")
readme.seek(0)
2021-10-04 16:13:23 +00:00
2021-10-04 14:57:57 +00:00
readme.write(raw.split(START_MARKER)[0])
2021-10-04 14:59:53 +00:00
readme.write(START_MARKER+"\n")
2021-10-04 16:13:23 +00:00
2021-10-04 17:19:39 +00:00
for site in enabled_sites:
if site in sites:
readme.write(
2021-10-04 17:30:02 +00:00
"- [{0}]({1}) \n".format(sub("^https?://(?:www\.)?", "", sites[site]), sites[site]))
2021-10-04 16:13:23 +00:00
2021-10-04 15:03:19 +00:00
readme.write(END_MARKER)
2021-10-04 14:57:57 +00:00
readme.write(raw.split(END_MARKER)[-1])
2021-10-04 16:13:23 +00:00
2021-10-04 15:03:19 +00:00
readme.truncate()