Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
JamminCoder committed Jul 4, 2021
1 parent 007a391 commit 625315f
Show file tree
Hide file tree
Showing 29 changed files with 1,096 additions and 0 deletions.
64 changes: 64 additions & 0 deletions GetCareerStats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/python3

import os
import funcs
import Parsers
from bs4 import BeautifulSoup

funcs = funcs.MyFuncs()
stat_parser = Parsers.StatsParser()


class CareerStats:
def __init__(self):
self.links_dir = "data/links/"
self.players_dir = "data/players"
self.link_files = self.get_link_files()

def get_link_files(self):
file_names = os.listdir(self.links_dir)
for file in file_names:
yield self.links_dir + file

def get_stats(self):
for file in self.link_files:
cur_file = file.split("/")[-1][0] # Returns the first letter of the current file.
cur_dir = f"{self.players_dir}/{cur_file}"
# Makes a directory with the name of the first letter of the link file
# So that the players stats can be alphabetically organized.
try:
os.mkdir(cur_dir)
except FileExistsError:
pass
print(f"Made dir {cur_dir}")
# Gives all of the player names and links to stats
player_info = funcs.read_file(file).split("\n")
for player in player_info:
if player:
name, link = player.split(", ")
print(f"Working on {name}")
player_file = f"{cur_dir}/{name}.txt"

try:
funcs.write_file(player_file, "")
except FileExistsError:
pass

stats_page = funcs.soup_request(link)
# PARSE STATS PAGE ##################################
meta = stat_parser.parse_meta(stats_page)
summary = stat_parser.get_summary(stats_page)

# Write info to file
for text in meta:
funcs.append_file(player_file, text)

funcs.append_file(player_file, "\n" + summary)

stat_parser.get_nhl_standard(stats_page)
#############################################
break


stats = CareerStats()
stats.get_stats()
55 changes: 55 additions & 0 deletions GetPlayers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/python3

import funcs

funcs = funcs.MyFuncs()


class GetPlayers:
def __init__(self, url):
self.player_num = 0
self.chars = funcs.read_file("chars.txt")
# self.current_char = 0
self.data_dir = "./data"
self.players_dir = f"{self.data_dir}/players"
self.url = url
self.html_parser = "lxml"

# Returns all the "strong" tags in the players-div
# The players within the strong tags are the ones currently playing

def get_player_tags(self, char):
current_url = f"{self.url}/players/{char}/"
current_page = funcs.request(current_url) # HTML of current page
soup = funcs.soupify(current_page) # Makes beautiful soup with current page
players_div = soup.find("div", id="div_players") # Extracts the player div
souped_div = funcs.soupify(players_div) # Makes soup with players_div

# Finds all the p tags and returns them. The p tags hold the player name,
# and link to the player's page.
player_tags = funcs.soupify(souped_div.find_all("p", class_="nhl"))
return player_tags.find_all("strong")

# Returns a generator with the name and link in a dict
def get_players(self, char):
players = self.get_player_tags(char)
for player in players:
if not player.text == "None":
player_link = self.url + player.a["href"] # Returns the full url for the individual player's stats page
player_name = player.text # Player name/career duration.
yield {"name": player_name, "site_path": player_link}
# Tells program to move the next char
# self.current_char += 1

def run(self):
for char in self.chars:
print(f">> Working on {char}")
players = self.get_players(char)
link_file = f"{self.data_dir}/links/{char}.txt"
funcs.write_file(link_file, "")
for player in players:
self.player_num += 1
player_name = player["name"]
player_page = player["site_path"]
funcs.append_file(link_file, player_name + ", " + player_page)
print(f"Found {self.player_num} players total.")
63 changes: 63 additions & 0 deletions Parsers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/python3

import funcs
from bs4 import BeautifulSoup

funcs = funcs.MyFuncs()


class StatsParser:
def parse_meta(self, stats_page):
meta = funcs.soupify(stats_page.find("div", id="meta"))
data_div = funcs.soupify(meta.find("div", itemtype="https://schema.org/Person"))
p_tags = data_div.find_all("p")
for tag in p_tags:
yield tag.text

def get_nhl_standard(self, stats_page):
overheader_adjictives = []
overheader_dict = {}
standard_table = stats_page.find("table", id="stats_basic_plus_nhl")
thead_rows = standard_table.find_all("tr")
overheaders = thead_rows[0].find_all("th")
headers = thead_rows[1].find_all("th")
for i in range(len(overheaders)):
text = overheaders[i].text
if not text == "":
overheader_adjictives.append(text)
overheader_string = f"\t\t|\t{overheader_adjictives[0]}\t | {overheader_adjictives[1]} \t| {overheader_adjictives[2]}| {overheader_adjictives[3]}\t| {overheader_adjictives[4]}"
print(overheader_string)
header_string = ""
for header in headers:
header_string += header.text + " "
print(header_string)


def get_summary(self, stats_page):
career_summary = []
stats_pullout = stats_page.find("div", class_="stats_pullout")
stats_div = funcs.soupify(stats_pullout)
paragraph1 = stats_div.find("div", class_="p1").find_all("div")
paragraph2 = stats_div.find("div", class_="p2").find_all("div")

# Get career summary
for div in paragraph1:
div = funcs.soupify(div)
stat = div.h4.text
stat_value = div.p.text
career_summary.append({"stat": stat, "value": stat_value})
for div in paragraph2:
div = funcs.soupify(div)
stat = div.h4.text
stat_value = div.p.text
career_summary.append({"stat": stat, "value": stat_value})

out_text1 = "Career Summary\n"
out_text2 = "----------------------------------\n"
out_text3 = ""

for item in career_summary:
out_text3 += f"{item['stat']}\t{item['value']}\n"

parsed_summary = out_text1 + out_text2 + out_text3
return parsed_summary
1 change: 1 addition & 0 deletions chars.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abcdefghijklmnopqrstuvwxyz
28 changes: 28 additions & 0 deletions data/links/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Noel Acciari, https://www.hockey-reference.com/players/a/acciano01.html
Calen Addison, https://www.hockey-reference.com/players/a/addisca01.html
Kenny Agostino, https://www.hockey-reference.com/players/a/agostke01.html
Andrew Agozzino, https://www.hockey-reference.com/players/a/agozzan01.html
Sebastian Aho, https://www.hockey-reference.com/players/a/ahose01.html
Jake Allen, https://www.hockey-reference.com/players/a/allenja01.html
Mark Alt, https://www.hockey-reference.com/players/a/altma01.html
Michael Amadio, https://www.hockey-reference.com/players/a/amadimi01.html
Frederik Andersen, https://www.hockey-reference.com/players/a/anderfr01.html
Craig Anderson, https://www.hockey-reference.com/players/a/andercr01.html
Joey Anderson, https://www.hockey-reference.com/players/a/anderjo08.html
Josh Anderson, https://www.hockey-reference.com/players/a/anderjo05.html
Michael Anderson, https://www.hockey-reference.com/players/a/andermi02.html
Jaret Anderson-Dolan, https://www.hockey-reference.com/players/a/anderja01.html
Lias Andersson, https://www.hockey-reference.com/players/a/anderli01.html
Rasmus Andersson, https://www.hockey-reference.com/players/a/anderra01.html
Andy Andreoff, https://www.hockey-reference.com/players/a/andrean01.html
Anthony Angello, https://www.hockey-reference.com/players/a/angelan01.html
Artem Anisimov, https://www.hockey-reference.com/players/a/anisiar01.html
Mason Appleton, https://www.hockey-reference.com/players/a/applema01.html
Josh Archibald, https://www.hockey-reference.com/players/a/archijo01.html
Joel Armia, https://www.hockey-reference.com/players/a/armiajo01.html
Viktor Arvidsson, https://www.hockey-reference.com/players/a/arvidvi01.html
Rasmus Asplund, https://www.hockey-reference.com/players/a/asplura01.html
Zach Aston-Reese, https://www.hockey-reference.com/players/a/astonza01.html
Andreas Athanasiou, https://www.hockey-reference.com/players/a/athanan01.html
Cam Atkinson, https://www.hockey-reference.com/players/a/atkinca01.html
Nicolas Aube-Kubel, https://www.hockey-reference.com/players/a/aubekni01.html
81 changes: 81 additions & 0 deletions data/links/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
David Backes, https://www.hockey-reference.com/players/b/backeda01.html
Mikael Backlund, https://www.hockey-reference.com/players/b/backlmi01.html
Nicklas Backstrom, https://www.hockey-reference.com/players/b/backsni02.html
Josh Bailey, https://www.hockey-reference.com/players/b/bailejo01.html
Justin Bailey, https://www.hockey-reference.com/players/b/baileju01.html
Rudolfs Balcers, https://www.hockey-reference.com/players/b/balceru01.html
Alexander Barabanov, https://www.hockey-reference.com/players/b/barabal01.html
Ivan Barbashev, https://www.hockey-reference.com/players/b/barbaiv01.html
Aleksander Barkov, https://www.hockey-reference.com/players/b/barkoal01.html
Alex Barre-Boulet, https://www.hockey-reference.com/players/b/barreal01.html
Tyson Barrie, https://www.hockey-reference.com/players/b/barrity01.html
Matt Bartkowski, https://www.hockey-reference.com/players/b/bartkma01.html
Mathew Barzal, https://www.hockey-reference.com/players/b/barzama01.html
Nathan Bastian, https://www.hockey-reference.com/players/b/bastina01.html
Drake Batherson, https://www.hockey-reference.com/players/b/bathedr01.html
Jay Beagle, https://www.hockey-reference.com/players/b/beaglja01.html
Jake Bean, https://www.hockey-reference.com/players/b/beanja01.html
Ethan Bear, https://www.hockey-reference.com/players/b/bearet01.html
Nicolas Beaudin, https://www.hockey-reference.com/players/b/beaudni01.html
Nathan Beaulieu, https://www.hockey-reference.com/players/b/beaulna01.html
Anthony Beauvillier, https://www.hockey-reference.com/players/b/beauvan01.html
Pierre-Edouard Bellemare, https://www.hockey-reference.com/players/b/bellepi01.html
Kieffer Bellows, https://www.hockey-reference.com/players/b/belloki01.html
Louis Belpedio, https://www.hockey-reference.com/players/b/belpelo01.html
Emil Bemstrom, https://www.hockey-reference.com/players/b/bemstem01.html
Jamie Benn, https://www.hockey-reference.com/players/b/bennja01.html
Jordie Benn, https://www.hockey-reference.com/players/b/bennjo01.html
Sam Bennett, https://www.hockey-reference.com/players/b/bennesa01.html
Matt Benning, https://www.hockey-reference.com/players/b/bennima01.html
Patrice Bergeron, https://www.hockey-reference.com/players/b/bergepa01.html
Jonathan Bernier, https://www.hockey-reference.com/players/b/bernijo01.html
Tyler Bertuzzi, https://www.hockey-reference.com/players/b/bertuty01.html
Alex Biega, https://www.hockey-reference.com/players/b/biegaal01.html
Jordan Binnington, https://www.hockey-reference.com/players/b/binnijo01.html
Clark Bishop, https://www.hockey-reference.com/players/b/bishocl01.html
Anthony Bitetto, https://www.hockey-reference.com/players/b/bitetan01.html
Anders Bjork, https://www.hockey-reference.com/players/b/bjorkan01.html
Oliver Bjorkstrand, https://www.hockey-reference.com/players/b/bjorkol01.html
Tobias Bjornfot, https://www.hockey-reference.com/players/b/bjornto01.html
Nick Bjugstad, https://www.hockey-reference.com/players/b/bjugsni01.html
Colin Blackwell, https://www.hockey-reference.com/players/b/blackco01.html
Mackenzie Blackwood, https://www.hockey-reference.com/players/b/blackma01.html
Sammy Blais, https://www.hockey-reference.com/players/b/blaissa01.html
Joachim Blichfeld, https://www.hockey-reference.com/players/b/blichjo01.html
Anton Blidh, https://www.hockey-reference.com/players/b/blidhan01.html
Teddy Blueger, https://www.hockey-reference.com/players/b/bluegte01.html
Sergei Bobrovsky, https://www.hockey-reference.com/players/b/bobrose01.html
Brock Boeser, https://www.hockey-reference.com/players/b/boesebr01.html
Zach Bogosian, https://www.hockey-reference.com/players/b/bogosza01.html
Nick Bonino, https://www.hockey-reference.com/players/b/boninni01.html
Adam Boqvist, https://www.hockey-reference.com/players/b/boqviad01.html
Jesper Boqvist, https://www.hockey-reference.com/players/b/boqvije01.html
William Borgen, https://www.hockey-reference.com/players/b/borgewi01.html
Andreas Borgman, https://www.hockey-reference.com/players/b/borgman01.html
Mark Borowiecki, https://www.hockey-reference.com/players/b/borowma01.html
Robert Bortuzzo, https://www.hockey-reference.com/players/b/borturo01.html
Evan Bouchard, https://www.hockey-reference.com/players/b/bouchev01.html
Madison Bowey, https://www.hockey-reference.com/players/b/boweyma01.html
Travis Boyd, https://www.hockey-reference.com/players/b/boydtr01.html
Tyler Bozak, https://www.hockey-reference.com/players/b/bozakty01.html
Erik Brannstrom, https://www.hockey-reference.com/players/b/branner01.html
Derick Brassard, https://www.hockey-reference.com/players/b/brassde01.html
Jesper Bratt, https://www.hockey-reference.com/players/b/brattje01.html
Justin Braun, https://www.hockey-reference.com/players/b/braunju01.html
T.J. Brodie, https://www.hockey-reference.com/players/b/broditj01.html
Jonas Brodin, https://www.hockey-reference.com/players/b/brodijo01.html
Jonny Brodzinski, https://www.hockey-reference.com/players/b/brodzjo01.html
Mathias Brome, https://www.hockey-reference.com/players/b/bromema01.html
Adam Brooks, https://www.hockey-reference.com/players/b/brookad01.html
Laurent Brossoit, https://www.hockey-reference.com/players/b/brossla01.html
Connor Brown, https://www.hockey-reference.com/players/b/brownco02.html
Dustin Brown, https://www.hockey-reference.com/players/b/browndu01.html
Josh Brown, https://www.hockey-reference.com/players/b/brownjo01.html
Jacob Bryson, https://www.hockey-reference.com/players/b/brysoja01.html
Pavel Buchnevich, https://www.hockey-reference.com/players/b/buchnpa01.html
Connor Bunnaman, https://www.hockey-reference.com/players/b/bunnaco01.html
Andre Burakovsky, https://www.hockey-reference.com/players/b/burakan01.html
Brent Burns, https://www.hockey-reference.com/players/b/burnsbr01.html
Will Butcher, https://www.hockey-reference.com/players/b/butchwi01.html
Bowen Byram, https://www.hockey-reference.com/players/b/byrambo01.html
Paul Byron, https://www.hockey-reference.com/players/b/byronpa01.html
58 changes: 58 additions & 0 deletions data/links/c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Nicholas Caamano, https://www.hockey-reference.com/players/c/caamani01.html
Drake Caggiula, https://www.hockey-reference.com/players/c/caggidr01.html
Matt Calvert, https://www.hockey-reference.com/players/c/calvema01.html
Jack Campbell, https://www.hockey-reference.com/players/c/campbja01.html
Kyle Capobianco, https://www.hockey-reference.com/players/c/capobky01.html
Brandon Carlo, https://www.hockey-reference.com/players/c/carlobr01.html
John Carlson, https://www.hockey-reference.com/players/c/carlsjo01.html
Gabriel Carlsson, https://www.hockey-reference.com/players/c/carlsga01.html
Lucas Carlsson, https://www.hockey-reference.com/players/c/carlslu01.html
Ryan Carpenter, https://www.hockey-reference.com/players/c/carpery01.html
Daniel Carr, https://www.hockey-reference.com/players/c/carrda01.html
Connor Carrick, https://www.hockey-reference.com/players/c/carrico01.html
Alexandre Carrier, https://www.hockey-reference.com/players/c/carrial01.html
William Carrier, https://www.hockey-reference.com/players/c/carriwi01.html
Jeff Carter, https://www.hockey-reference.com/players/c/carteje01.html
Cody Ceci, https://www.hockey-reference.com/players/c/cecico01.html
Erik Cernak, https://www.hockey-reference.com/players/c/cernaer01.html
Thomas Chabot, https://www.hockey-reference.com/players/c/chaboth01.html
Michael Chaput, https://www.hockey-reference.com/players/c/chapumi01.html
Zdeno Chara, https://www.hockey-reference.com/players/c/charazd01.html
Jalen Chatfield, https://www.hockey-reference.com/players/c/chatfja01.html
Ben Chiarot, https://www.hockey-reference.com/players/c/chiarbe01.html
Alex Chiasson, https://www.hockey-reference.com/players/c/chiasal01.html
Filip Chlapik, https://www.hockey-reference.com/players/c/chlapfi01.html
Sasha Chmelevski, https://www.hockey-reference.com/players/c/chmelal01.html
Jakob Chychrun, https://www.hockey-reference.com/players/c/chychja01.html
Filip Chytil, https://www.hockey-reference.com/players/c/chytifi01.html
Anthony Cirelli, https://www.hockey-reference.com/players/c/cirelan01.html
Casey Cizikas, https://www.hockey-reference.com/players/c/cizikca01.html
Fredrik Claesson, https://www.hockey-reference.com/players/c/claesfr01.html
Kale Clague, https://www.hockey-reference.com/players/c/claguka01.html
Kyle Clifford, https://www.hockey-reference.com/players/c/cliffky01.html
Connor Clifton, https://www.hockey-reference.com/players/c/cliftco01.html
Cal Clutterbuck, https://www.hockey-reference.com/players/c/cluttca01.html
Braydon Coburn, https://www.hockey-reference.com/players/c/coburbr01.html
Dylan Coghlan, https://www.hockey-reference.com/players/c/coghldy01.html
Andrew Cogliano, https://www.hockey-reference.com/players/c/coglian01.html
Ian Cole, https://www.hockey-reference.com/players/c/coleia01.html
Blake Coleman, https://www.hockey-reference.com/players/c/colembl01.html
Ross Colton, https://www.hockey-reference.com/players/c/coltoro01.html
Blake Comeau, https://www.hockey-reference.com/players/c/comeabl01.html
J.T. Compher, https://www.hockey-reference.com/players/c/comphj.01.html
Eric Comrie, https://www.hockey-reference.com/players/c/comrier01.html
Max Comtois, https://www.hockey-reference.com/players/c/comtoma01.html
Kevin Connauton, https://www.hockey-reference.com/players/c/connake01.html
Brett Connolly, https://www.hockey-reference.com/players/c/connobr01.html
Kyle Connor, https://www.hockey-reference.com/players/c/connoky01.html
Andrew Copp, https://www.hockey-reference.com/players/c/coppan01.html
Nick Cousins, https://www.hockey-reference.com/players/c/cousini01.html
Logan Couture, https://www.hockey-reference.com/players/c/coutulo01.html
Sean Couturier, https://www.hockey-reference.com/players/c/coutuse01.html
Charlie Coyle, https://www.hockey-reference.com/players/c/coylech01.html
Dylan Cozens, https://www.hockey-reference.com/players/c/cozendy01.html
Sidney Crosby, https://www.hockey-reference.com/players/c/crosbsi01.html
Lawson Crouse, https://www.hockey-reference.com/players/c/crousla01.html
Josh Currie, https://www.hockey-reference.com/players/c/currijo01.html
Austin Czarnik, https://www.hockey-reference.com/players/c/czarnau01.html
Kevin Czuczman, https://www.hockey-reference.com/players/c/czuczke01.html
Loading

0 comments on commit 625315f

Please sign in to comment.