Skip to content

Instantly share code, notes, and snippets.

@ryukinix
Created March 21, 2024 03:12
Show Gist options
  • Save ryukinix/8024bbd865a872f35c52bbe0c14d168d to your computer and use it in GitHub Desktop.
Save ryukinix/8024bbd865a872f35c52bbe0c14d168d to your computer and use it in GitHub Desktop.
"""
Download a remote playlist as m3u from server.manoel.dev to watch by streaming.
Example
-------
> python downloadplaylist.py https://server.manoel.dev/extra_animes/Serial%20Experiments%20Lain/
Will write a serial-experiments-lain.m3u
You can play it with: mpv serial-experiments-lain.m3u
"""
from typing import List
import sys
import warnings
from urllib.parse import urlparse, unquote
import lxml.etree # type: ignore
import requests # type: ignore
def _normalize_name(name: str) -> str:
return name.lower().replace(" ", "-").replace("_", "-")
def get_anime_name(url: str):
url_object = urlparse(url)
path = unquote(url_object.path)
anime_name = path.strip("/").split("/")[-1]
return _normalize_name(anime_name)
def get_domain(url: str):
url_object = urlparse(url)
return url_object.scheme + "://" + url_object.netloc
def get_file_links(url: str) -> List[str]:
response = requests.get(url, verify=False)
tree = lxml.etree.HTML(response.text)
query = "//a[@class='file']/@href"
return tree.xpath(query)
def print_urls(urls: List[str]):
for i, url in enumerate(urls):
print(i + 1, url)
def generate_complete_urls(domain: str, links: List[str]) -> List[str]:
return [domain + link for link in links]
def main(url: str):
domain = get_domain(url)
links = get_file_links(url)
complete_urls = generate_complete_urls(domain, links)
anime_name = get_anime_name(url)
extension = "m3u"
filename = f"{anime_name}.{extension}"
print("Name: ", filename)
print_urls(complete_urls)
with open(filename, "w") as f:
f.write("\n".join(complete_urls))
if __name__ == "__main__":
warnings.simplefilter("ignore")
url = sys.argv[1]
main(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment