Created
August 29, 2022 14:51
-
-
Save acristoffers/84b99d8d8fa442a71668b08eeb3803e6 to your computer and use it in GitHub Desktop.
Removes dead links from OPML file.
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 | |
from lxml import etree | |
import sys | |
import requests | |
def clean_file(opml): | |
remove = [] | |
for outline in opml.xpath("//outline[@type='rss']"): | |
print(f"Testing {outline.get('title')}") | |
try: | |
url = outline.get("xmlUrl") | |
headers = { | |
"User-Agent": | |
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:103.0) Gecko/20100101 Firefox/103.0" | |
} | |
response = requests.get(url, headers=headers, allow_redirects=True) | |
if response.status_code != 200 or len(response.content) == 0: | |
remove.append(outline) | |
continue | |
root = etree.fromstring(response.content) | |
if not root.tag.endswith("rss") and not root.tag.endswith("feed"): | |
remove.append(outline) | |
except Exception: | |
remove.append(outline) | |
print("") | |
for outline in remove: | |
print(f"Removing {outline.get('title')}") | |
outline.getparent().remove(outline) | |
opml.write("output.opml", pretty_print=True) | |
print(f"\nRemoved {len(remove)} items.") | |
if __name__ == "__main__": | |
if len(sys.argv) == 1: | |
sys.exit(1) | |
opml = etree.parse(sys.argv[1]) | |
clean_file(opml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment