Skip to content

Instantly share code, notes, and snippets.

@jengel3
Created July 24, 2014 05:27
Show Gist options
  • Save jengel3/7f0f658f43a1e807a33c to your computer and use it in GitHub Desktop.
Save jengel3/7f0f658f43a1e807a33c to your computer and use it in GitHub Desktop.
Overcast Team Sorter
from lxml import html
import requests
from inspect import getmembers
from collections import OrderedDict
from prettytable import PrettyTable
matches = {'kills': 1, 'deaths': 2, 'wools': 3, 'cores': 4, 'monuments': 5, 'kd': 6, 'kk': 7, 'time': 8}
def run():
keys = ', '.join(matches.keys())
print 'Welcome to Team Sorter v1.0 by Jake0oo0. Check out the source at https://gist.github.com/Jake0oo0/7f0f658f43a1e807a33c'
team_url = raw_input('Team URL: ')
sort = raw_input('Sort by (Valid keys: %s): ' % keys)
if sort not in matches.keys():
print "Not a valid key to sort by. Valid choices: " + keys
exit(0)
players = []
try:
page = requests.get(team_url)
except:
print "Unable to retrieve stats."
exit(0)
tree = html.fromstring(page.text)
try:
page_count = len(tree.xpath("/html/body/div/section/div[3]/div/div[1]")[0].xpath('a'))
except:
page_count = 1
index = 1
while index <= page_count:
page = requests.get(team_url + "?page=" + str(index))
tree = html.fromstring(page.text)
team_players = tree.xpath(
"/html/body/div/section/div[3]/div/table/tbody/tr")
for team_player in team_players:
username = team_player.xpath("td")[
0].xpath("a")[0].text.replace('\n', '')
if username not in players:
players.append(username)
index += 1
ranked_players = []
for player in players:
profile_html = requests.get("https://oc.tc/" + player)
profile = html.fromstring(profile_html.text)
kills = int(profile.xpath("/html/body/div/section[1]/div[2]/div[1]/div/div[1]/div/div[2]/h2")[0].text.replace('\n', ''))
deaths = int(profile.xpath("/html/body/div/section[1]/div[2]/div[1]/div/div[2]/h2")[0].text.replace('\n', ''))
wools = int(profile.xpath('//*[@id="objectives"]/div/div[1]/h2')[0].text.replace('\n', ''))
cores = int(profile.xpath('//*[@id="objectives"]/div/div[2]/h2')[0].text.replace('\n', ''))
monuments = int(profile.xpath('//*[@id="objectives"]/div/div[3]/h2')[0].text.replace('\n', ''))
sidebar = profile.xpath('/html/body/div/section[1]/div[2]/div[3]')[0].xpath('h2')
kd = float(sidebar[0].text)
kk = float(sidebar[1].text)
time = float(sidebar[3].text)
ranked_players.append((player, kills, deaths, wools, cores, monuments, kd, kk, time))
while True:
print get_output(ranked_players, sort)
redo = raw_input("Would you like to resort the same data? ")
if redo.lower() == 'yes':
sort = raw_input('Sort by (Valid keys: %s): ' % keys)
print get_output(ranked_players, sort)
else:
exit(0)
def get_output(players, sort):
sort_index = matches[sort]
sort_dict = {}
for player in players:
sort_dict.update({player[0]: player[sort_index]})
sorted_dict = OrderedDict(
OrderedDict(sorted(sort_dict.items(), key=lambda t: t[1])).items()[::-1])
final_stats = []
for player in sorted_dict:
player_stats = ()
for stats in players:
if stats[0] == player:
player_stats = stats
break
final_stats.append(player_stats)
table = PrettyTable(
['Player', 'Kills', 'Deaths', 'Wools', 'Cores', 'Monuments', 'K/D', 'K/K', 'Time Played (Days)'])
for stats in final_stats:
table.add_row(
[stats[0], stats[1], stats[2], stats[3], stats[4], stats[5], stats[6], stats[7], stats[8]])
return table
try:
run()
except KeyboardInterrupt:
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment