-
-
Save Ordoviz/c89e158ed1a35171a8aad1f8bb164d6e to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
# SPDX-License-Identifier: Unlicense | |
import requests | |
import xml.etree.ElementTree as ET | |
import sys | |
def is_short(video_id) -> bool: | |
"""Return whether this is a YouTube Short""" | |
return requests.head(f"https://www.youtube.com/shorts/{video_id}").status_code == 200 | |
# If you also want to remove other short videos, use this instead (slower): | |
# import yt_dlp | |
# with yt_dlp.YoutubeDL() as ydl: | |
# info = ydl.extract_info(f'https://www.youtube.com/watch?v={video_id}', download=False) | |
# print(info["duration_string"]) # human-friendly | |
# return info["duration"] < 60 | |
def fetch_dearrow_title(video_id) -> str | None: | |
"""Get best non-clickbaity title from DeArrow's community database""" | |
# https://wiki.sponsor.ajay.app/w/API_Docs/DeArrow | |
r = requests.get(f"https://dearrow.minibomba.pro/sbserver/api/branding/?videoID={video_id}").json() | |
if not r["titles"]: | |
return None | |
best = r["titles"][0] | |
if best["locked"] or best["votes"] >= 0: | |
return best["title"] | |
else: | |
return None | |
def main(): | |
tree = ET.parse(sys.stdin) | |
root = tree.getroot() | |
namespaces = { | |
"yt": "http://www.youtube.com/xml/schemas/2015", | |
"media": "http://search.yahoo.com/mrss/", | |
"": "http://www.w3.org/2005/Atom", | |
} | |
for k, v in namespaces.items(): | |
ET.register_namespace(k, v) | |
for entry in root.findall("entry", namespaces): | |
video_id = entry.find("yt:videoId", namespaces).text | |
if is_short(video_id): | |
root.remove(entry) | |
continue | |
if new_title := fetch_dearrow_title(video_id): | |
title_element = entry.find("title", namespaces) | |
if title_element is not None: | |
title_element.text = f"{new_title} ◎ {title_element.text}" | |
sys.stdout.buffer.write(ET.tostring(root, encoding="utf-8")) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment