Created
January 4, 2020 19:16
-
-
Save Akdeniz/86bfedf65fae96a73e4a0d286cd1743c to your computer and use it in GitHub Desktop.
Adjust criticker.com movie ratings by percentile.
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
import xml.etree.ElementTree as ET | |
import http.client | |
import urllib.parse | |
import argparse | |
class Film: | |
def __init__(self, filmid, rating, percentile): | |
self.filmid = filmid | |
self.rating = rating | |
self.percentile = percentile | |
def normalized_rating(self): | |
return int(round(self.percentile/10)) | |
def __str__(self): | |
return "filmid: {}, rating: {}, percentile: {}".format(self.filmid, self.rating, self.percentile) | |
def extract_films(xml_file): | |
recentratings = ET.parse(xml_file).getroot() | |
films = [] | |
for film in recentratings.findall('./film'): | |
filmid = film.find('filmid').text | |
rating = int(film.find('rating').text) | |
percentile = float(film.find('percentile').text) | |
films.append(Film(filmid, rating, percentile)) | |
return films | |
def submit_rating(id_to_rating, cookie): | |
data = {"fi_batchedit_input_{}".format(id): rating for id, rating in id_to_rating.items()} | |
data['fl_submit_batch'] = '' | |
params = urllib.parse.urlencode(data) | |
# print(params) | |
headers = {"Content-type": "application/x-www-form-urlencoded", "Cookie": cookie} | |
conn = http.client.HTTPSConnection("www.criticker.com") | |
conn.request("POST", "/ratings/?", params, headers) | |
return conn.getresponse() | |
def authenticate(username, password): | |
params = urllib.parse.urlencode({'si_username': username, 'si_password': password}) | |
headers = {"Content-type": "application/x-www-form-urlencoded"} | |
conn = http.client.HTTPSConnection("www.criticker.com") | |
conn.request("POST", "/authenticate.php", params, headers) | |
cookie = conn.getresponse().getheader('Set-Cookie') | |
assert cookie, "Authentication failed!" | |
return cookie | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Adjusts movie ratings by rounding its percentile/10 to nearest integer. eg. 74 => 7') | |
parser.add_argument('-r', '--ratings', required=True, help='Ratings XML file from https://www.criticker.com/resource/ratings/conv.php?type=xml') | |
parser.add_argument('-u', '--username', required=True, help='Criticker username') | |
parser.add_argument('-p', '--password', required=True, help='Criticker password') | |
args = parser.parse_args() | |
films = extract_films(args.ratings) | |
print("Found {} film ratings in xml file". format(len(films))) | |
cookie = authenticate(args.username, args.password) | |
response = submit_rating({film.filmid: film.normalized_rating() for film in films}, cookie) | |
print(response.status, response.reason) | |
#print(response.read()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment