- (here) Always Never (channel)
Last active
March 1, 2026 19:40
-
-
Save serrasqueiro/96d577e4ba8e4fdabee77cca85124cec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ downplaylists.py | |
| Downloads playlists found at remote link into a fixed dir! | |
| """ | |
| import sys | |
| import os | |
| import re | |
| import requests | |
| OUT_DIR = "playlists_output" | |
| def main(): | |
| """ Main script -- caller! """ | |
| do_script(sys.argv[1:]) | |
| def do_script(args): | |
| """ In-caller function """ | |
| res = do_run(args if args else ["play/"]) | |
| return 0 if res else 1 | |
| def do_run(param): | |
| """Main execution flow. | |
| Hint: | |
| import downplaylists, importlib; importlib.reload(downplaylists) | |
| lst = downplaylists.do_run(["play/"]) | |
| print(lst[-1]) # Print the last list string | |
| """ | |
| lst = [] | |
| suffix = param[0] | |
| url = f"http://git.declaratived.com/{suffix}" | |
| output_dir = OUT_DIR | |
| print("Fetching HTML from:", url) | |
| html = fetch_html(url) | |
| print(f"Splitting playlists (sized: {len(html)})") | |
| playlists = split_playlists(html) | |
| print(f"Found {len(playlists)} playlists.") | |
| os.makedirs(output_dir, exist_ok=True) | |
| for block in playlists: | |
| save_playlist(block, output_dir) | |
| lst.append(block) | |
| print(f"Saved {len(playlists)} files in '{output_dir}' directory.") | |
| return lst | |
| def fetch_html(url: str) -> str: | |
| """Fetch raw HTML from the given URL.""" | |
| headers = { | |
| "User-Agent": ( | |
| "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 " | |
| "(KHTML, like Gecko) Chrome/120.0 Safari/537.36" | |
| ) | |
| } | |
| response = requests.get(url, headers=headers) | |
| response.raise_for_status() | |
| return response.text | |
| def split_playlists(html: str) -> list: | |
| """ | |
| Split the HTML into playlist blocks. | |
| Each playlist starts with 'Playlist PXX' and continues until the next playlist. | |
| """ | |
| # Normalize whitespace | |
| text = re.sub(r"\s+", " ", html) | |
| # Find all playlist headers | |
| matches = list(re.finditer(r"(Playlist\s+P\d+)", text)) | |
| playlists = [] | |
| for i, match in enumerate(matches): | |
| start = match.start() | |
| end = matches[i + 1].start() if i + 1 < len(matches) else len(text) | |
| block = text[start:end].strip() | |
| playlists.append(block) | |
| return playlists | |
| def extract_playlist_name(block: str) -> str: | |
| """Extract playlist name like 'Playlist P103'.""" | |
| m = re.match(r"(Playlist\s+P\d+)", block) | |
| return m.group(1).replace(" ", "_") if m else "UnknownPlaylist" | |
| def save_playlist(block: str, output_dir: str): | |
| """Save a playlist block to a text file.""" | |
| name = extract_playlist_name(block) | |
| filename = os.path.join(output_dir, f"{name}.html") | |
| with open(filename, "w", encoding="utf-8") as fdout: | |
| fdout.write(block + "\n") | |
| return True | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment