Debian Codegraph @

resources/scripts/scrapers/search-xml-feeds.py

raw symbols SCIP
1 # Produces the list of links to XML feeds as extracted from input list of generic URLs.
2 # This script expects to have the file path passed as the only input parameter
3
4 import re
5 import sys
6 import urllib.request
7 from urllib.parse import urljoin
8
9 urls_file = sys.argv[1]
10
11 with open(urls_file) as f:
12 urls_lines = [line.rstrip() for line in f]
13
14 regexp_link = re.compile("<link[^>]+type=\"application\/(?:atom\+xml|rss\+xml|feed\+json|json)\"[^>]*>")
15 regexp_href = re.compile("href=\"([^\"]+)\"")
16
17 for url in urls_lines:
18 # Download HTML data.
19 try:
20 url_response = urllib.request.urlopen(url)
21 html = url_response.read().decode("utf-8")
22 except:
23 continue
24
25 # Search for XML feeds with regexps.
26 for link_tag in re.findall(regexp_link, html):
27 for link_xml_feed in re.findall(regexp_href, link_tag):
28 if link_xml_feed.startswith("/"):
29 print(urljoin(url, "/") + link_xml_feed[1:])
30 else:
31 print(link_xml_feed)