-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added get_roster function that takes in team abbreviation and year to… #276
base: v4
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import requests | ||
|
||
from basketball_reference_web_scraper.errors import InvalidSeason, InvalidDate, InvalidPlayerAndSeason | ||
from basketball_reference_web_scraper.errors import InvalidSeason, InvalidDate, InvalidPlayerAndSeason, InvalidTeam | ||
from basketball_reference_web_scraper.http_service import HTTPService | ||
from basketball_reference_web_scraper.output.columns import BOX_SCORE_COLUMN_NAMES, SCHEDULE_COLUMN_NAMES, \ | ||
PLAYER_SEASON_TOTALS_COLUMN_NAMES, \ | ||
|
@@ -11,8 +11,8 @@ | |
from basketball_reference_web_scraper.output.writers import CSVWriter, JSONWriter, FileOptions, OutputOptions, \ | ||
SearchCSVWriter | ||
from basketball_reference_web_scraper.parser_service import ParserService | ||
|
||
|
||
from datetime import datetime | ||
from basketball_reference_web_scraper.data import TEAM_TO_TEAM_ABBREVIATION | ||
def standings(season_end_year, output_type=None, output_file_path=None, output_write_option=None, | ||
json_options=None): | ||
try: | ||
|
@@ -212,6 +212,35 @@ def team_box_scores(day, month, year, output_type=None, output_file_path=None, o | |
) | ||
return output_service.output(data=values, options=options) | ||
|
||
def get_roster(team, year=None, output_type=None, output_file_path=None, output_write_option=None, json_options=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, can we rename |
||
try: | ||
http_service = HTTPService(parser=ParserService()) | ||
if year == None: | ||
today = datetime.now() | ||
year = today.year | ||
if today.month >=7: | ||
year += 1 | ||
if len(team) > 3: | ||
team=TEAM_TO_TEAM_ABBREVIATION[team.upper()] | ||
values=http_service.get_team_roster(team=team, year=year) | ||
except requests.exceptions.HTTPError as http_error: | ||
if http_error.response.status_code == requests.codes.not_found: | ||
raise InvalidTeam(team=team, year=year) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this error's name is slightly inaccurate - the I'd prefer to call this error (Note that I've made similar inaccurate naming mistakes in other methods, like |
||
else: | ||
raise http_error | ||
|
||
options = OutputOptions.of( | ||
file_options=FileOptions.of(path=output_file_path, mode=output_write_option), | ||
output_type=output_type, | ||
json_options=json_options, | ||
csv_options={"column_names": "Players"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
) | ||
|
||
output_service = OutputService( | ||
json_writer=JSONWriter(value_formatter=BasketballReferenceJSONEncoder), | ||
csv_writer=CSVWriter(value_formatter=format_value) | ||
) | ||
return output_service.output(data=values, options=options) | ||
|
||
def play_by_play(home_team, day, month, year, output_type=None, output_file_path=None, output_write_option=None, | ||
json_options=None): | ||
|
@@ -250,3 +279,5 @@ def search(term, output_type=None, output_file_path=None, output_write_option=No | |
csv_writer=SearchCSVWriter(value_formatter=format_value) | ||
) | ||
return output_service.output(data=values, options=options) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -870,6 +870,17 @@ def game_url_paths(self): | |
game_links = self.html.xpath(self.game_url_paths_query) | ||
return [game_link.attrib['href'] for game_link in game_links] | ||
|
||
class TeamRoster: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we create a Most of all the client methods refer to a top-level |
||
def __init__(self, html): | ||
self.html = html | ||
|
||
@property | ||
def roster_query(self): | ||
return '//table[@id="roster"]//td[@data-stat="player"]' | ||
@property | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: let's have a new line between lines 879 and 880. |
||
def team_roster(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's create |
||
players = self.html.xpath(self.roster_query) | ||
return [player.text_content() for player in players] | ||
|
||
class SchedulePage: | ||
def __init__(self, html): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
#!/Users/jaebradley/projects/basketball_reference_web_scraper/bin/python3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
#!/bin/sh | ||
'''exec' "/Users/paramgattupalli/Documents/Fall 2024/CEN 3031/basketball_reference_web_scraper/bin/python" "$0" "$@" | ||
' ''' | ||
# -*- coding: utf-8 -*- | ||
import re | ||
import sys | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make
year
a non-optional argument. I don't think there's a pattern for anyyear
values being optional (intentionally).