Debian Codegraph @

resources/scripts/scrapers/metacritic.py

raw symbols SCIP
1 # Provides filtering of entries provided via Metacritic RSS feeds.
2 #
3 # Example input feed is: https://www.metacritic.com/rss/tv
4 #
5 # This script expects raw RSS 2.0 feed data as input and can be called like
6 # this on cli:
7 # curl 'https://www.metacritic.com/rss/tv' | python ./metacritic.py "<MINIMUM-SCORE>"
8 #
9 # Replace <MINIMUM-SCORE> with minimal numerical score your articles must have or pass nothing
10 # to keep all articles.
11
12 import json
13 import sys
14 import urllib.request
15 import xml.etree.ElementTree as ET
16 import re
17 import ssl
18
19 def get_score_of_url(article_url):
20 # Download HTML of article.
21 req = urllib.request.Request(article_url)
22 req.add_header("Accept", "*/*")
23 req.add_header("User-Agent", "curl/7.55.1")
24 response = urllib.request.urlopen(req, context=ssl.SSLContext())
25 text = response.read().decode("utf-8")
26 score = int(re.search(r'(metascore_w larger tvshow positive|ratingValue)">(\d{1,2})', text).group(2))
27 return score
28
29 def main():
30 minimal_score = int(sys.argv[1]) if len(sys.argv) >= 2 else -1
31
32 # Read RSS 2.0 feed data from input.
33 sys.stdin.reconfigure(encoding="utf-8")
34
35 #req = urllib.request.Request("https://www.metacritic.com/rss/tv")
36 #req.add_header("Accept", "*/*")
37 #req.add_header("User-Agent", "curl/7.55.1")
38 #feed_data = urllib.request.urlopen(req, context=ssl.SSLContext()).read()
39
40 feed_data = sys.stdin.read()
41 feed_document = ET.fromstring(feed_data)
42
43 # Process articles one by one.
44 feed_channel = feed_document.find(".//channel")
45 feed_articles = feed_channel.findall("item")
46
47 for article in feed_articles:
48 try:
49 article_score = get_score_of_url(article.find("link").text)
50 except:
51 article_score = minimal_score - 1
52 pass
53
54 if article_score < minimal_score:
55 feed_channel.remove(article)
56 else:
57 article_title = article.find("title")
58 article_title.text += " - {}".format(article_score)
59
60 out_xml = ET.tostring(feed_document)
61 out_decoded_xml = out_xml.decode()
62
63 print(out_decoded_xml)
64
65 if __name__ == '__main__':
66 main()