Skip to content

Instantly share code, notes, and snippets.

@aksiksi
Created March 10, 2012 20:49
Show Gist options
  • Save aksiksi/2013111 to your computer and use it in GitHub Desktop.
Save aksiksi/2013111 to your computer and use it in GitHub Desktop.
Grabs Metascore of games in text file.
# Uses httplib2 instead of urllib2 - runs faster
#import urllib2
import httplib2
import time
s = time.time()
game_console = {}
# Populate dictionary with game:console from file.
f = open('game.txt')
for line in f:
arr = line.partition(':')
game_console[''.join(arr[0])] = (''.join(arr[2])).rstrip('\n')
f.close()
count = 0
total = len(game_console)
f = open('scores.txt', 'w')
f.write('Metacritic Score Grabber v0.1 - Output\n')
f.write('--------------------------------------\n\n')
print "\nMetacritic Score Grabber v0.1"
print "-----------------------------\n"
print "Working...\n"
for g, c in game_console.items():
# Gets source code of game page
Http = httplib2.Http()
source = Http.request("http://www.metacritic.com/game/{0}/{1}".format(c.replace(' ', '-').lower(), g.replace(' ', '-').lower()))[1]
# Checks if game name and/or console name is correct.
if '<span class="error_type">Page Not Found</span>' in source:
f.write('{0} on {1}: game name or console is incorrect\n\n'.format(g, c))
else:
f.write('{0} on {1}: \n\n'.format(g, c))
# Finds metascore
index = source.find('"v:average">')
index += len('"v:average">')
meta_score = source[index] + source[index+1]
# Finds userscore
index = source.find('"score_value">')
index += len('"score_value">')
try:
user_score = float(source[index] + '.' + source[index+2])
except ValueError:
user_score = 'Not available'
# Finds combined score
try:
combined_score = ((int(meta_score) / 10.0) + float(user_score)) / 2.0
except ValueError:
combined_score = 'Not available'
# Writes results to file
f.write('- Metascore is: ' + meta_score + '\n')
f.write('- Userscore is: ' + str(user_score) + '\n')
f.write('- Combined score is: ' + str(combined_score) + '\n')
f.write('\n\n')
count += 1
print "{0}/{1} completed...".format(count, total)
f.write("Done in %f seconds." % (time.time()-s))
f.close()
print "\nDone. Check output file."
wait = input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment