def create_athlete_records(): team_dict = get_team_dict() filenames = get_json_files("data/rosters/json") for filename in filenames: file = open(filename, "r") contents = file.read() file.close() athletes = json.loads(contents) for athlete in athletes: team_code = athlete["team_code"] team = team_dict[team_code] last_name_comp = athlete['last_name'].split(' ') if len(last_name_comp) == 2: last_name = last_name_comp[0] suffix = last_name_comp[1] elif len(last_name_comp) == 1: last_name = last_name_comp[0] else: print("weird name: " + athlete['last_name']) last_name = athlete['last_name'] Athlete.get_or_create( first_name = athlete["first_name"], last_name = last_name, team = team, number = athlete["number"], position = athlete["position"], height = athlete["height"], weight = athlete["weight"], year = athlete["year"] )
def next(): global activity_counter, club_activities if not club_activities: access_token = Athlete.select()[0].access_token club_activities = requests.get( 'https://www.strava.com/api/v3/clubs/198580/activities?access_token=' + access_token).json() current_activity = club_activities[activity_counter] # if current_activity['type'] != "Run" and current_activity['type'] != "Ride": # app.logger.debug('Stumbled upon an activity of type: %s, moving on to the next!' % current_activity['type']) # activity_counter += 1 # if activity_counter == len(club_activities): activity_counter = 0 # next() access_token = Athlete.get( Athlete.user_id == current_activity['athlete']['id']).access_token r = requests.get( 'https://www.strava.com/api/v3/activities/%s/streams/latlng,time?access_token=%s' % (current_activity['id'], access_token)) activity_counter += 1 if activity_counter == len(club_activities): activity_counter = 0 app.logger.debug('Activity counter: %s' % activity_counter) data = current_activity data['streams'] = r.json() return json.dumps(data)
def add_bio(): form = forms.AddBioForm() if request.method == 'POST' and form.validate_on_submit(): try: photo_src = save_athlete_images(form.usa_id.data, form.photo.data) except Exception: form.photo.errors.append("Error saving submitted photo.") app.logger.exception("Error saving submitted photo.") else: athlete = Athlete(form.usa_id.data, form.gender.data, form.firstname.data.title(), form.lastname.data.title(), form.weight.data, form.weight_class.data, form.snatch.data, form.clean_jerk.data, form.description.data, has_photo=bool(photo_src)) app.logger.debug("Adding new %r to database" % athlete) db.session.add(athlete) db.session.commit() flash("ADDED athlete bio for ' %s %s '." % (athlete.firstname, athlete.lastname)) return redirect(url_for('add_bio')) return render_template('main/bios_admin.html', form=form, type='Add')
def load_data(): engine = create_engine(SQLALCHEMY_DATABASE_URI) Session = sessionmaker() Session.configure(bind=engine) session = Session() athletes_data = scraper() insertion_data = [] for athlete, athlete_data in athletes_data.iteritems(): athlete = Athlete( name= athlete, age= clean_string(athlete_data['age']), hieght= clean_string(athlete_data['hieght']), wieght= clean_string(athlete_data['wieght']), run_5k= athlete_data['Run 5k'], back_squat= clean_string(athlete_data['Back Squat']), clean_and_jerk= clean_string(athlete_data['Clean & Jerk']), snatch= clean_string(athlete_data['Snatch']), deadlift= clean_string(athlete_data['Deadlift']), max_pullups= athlete_data['Max Pull-ups'], ) insertion_data.append(athlete) session.add_all(insertion_data) session.commit()
def get_ranking(self, event="10K", sex="M", year="2014", age_group="ALL"): r = requests.get("http://www.thepowerof10.info/rankings/rankinglist.aspx", params={"event": event, "agegroup": age_group, "sex": sex, "year": 2014}) soup = BeautifulSoup(r.content) rankings_table = soup.find(id="ctl00_cphBody_lblCachedRankingList").find_all('table')[0] ranking_rows = [row for row in rankings_table.find_all("tr") if row["class"][0] not in ["rankinglisttitle", "rankinglistheadings", "rankinglistsubheader"]] rankings = [] for row in ranking_rows: if row.find_all("td")[0].string is None: continue r = Ranking({"athlete": Athlete(), "event": event, "year": year, "age_group": age_group}) r.rank = int(row.find_all("td")[0].string) r.time = row.find_all("td")[1].string r.athlete.name = row.find_all("td")[6].string.encode("utf-8") r.athlete.id = int(row.find_all("td")[6].a["href"].split("=")[1]) r.venue = row.find_all("td")[11].string r.date = row.find_all("td")[12].string rankings.append(r) return rankings
def get_athlete(self, id): r = requests.get("http://www.thepowerof10.info/athletes/profile.aspx", params={"athleteid": id}) if r.status_code != 200: raise AttributeError("Unable to find athlete with id %s." % id) soup = BeautifulSoup(r.content) a = Athlete({"id": id}) name = soup.find_all(class_="athleteprofilesubheader")[0].h2.string.strip().encode("utf-8") a.name = name info = soup.find(id="ctl00_cphBody_pnlAthleteDetails").find_all('table')[2] extra_details = {row.find_all("td")[0].string: row.find_all("td")[1].string for row in info.find_all("tr")} a.import_data(extra_details) try: coach = soup.find(id="ctl00_cphBody_pnlAthleteDetails").find_all('table')[3].find("a").string.encode("utf-8") coach_url = soup.find(id="ctl00_cphBody_pnlAthleteDetails").find_all('table')[3].find("a").get('href') a.coach = coach except: pass return a
def add_athlete(): # Add new athlete body = request.get_json() name = body.get('name', None) goal = body.get('goal', None) weight = body.get('weight', None) height = body.get('height', None) age = body.get('age', None) try: new_athlete = Athlete( name=name, goal=goal, weight=weight, height=height, age=age, ) new_athlete.insert() return jsonify({'success': True, 'athlete': new_athlete.format()}) except: abort(422)
def find_athlete(quarterback_name_number): args = quarterback_name_number.split(' ') if len(args) == 3: #first last number first_name, last_name, number = args elif len(args) == 4: first_name, last_name, suffix, number = args else: print("cannot parse: " + quarterback_name_number) return None athlete = Athlete.get( first_name = first_name, last_name = last_name, number = number ) return athlete
def token_exchange(): code = request.args.get('code') r = requests.post( strava_exchange_url, { 'client_id': strava_client_id, 'client_secret': strava_client_secret, 'code': code }) if r.status_code != 200: return r.text athlete = Athlete.create_or_get(user_id=r.json()['athlete']['id'], access_token=r.json()['access_token']) return redirect('/')
def parse_player(self, player): self.log.info(player) first_name, last_name = player.text.split(" ") for p in player.parents: if p.name == "td": parent = p break matches = re.search(r"\((.+?) - (.+?)\)", parent.text) school = matches.group(1).strip() year = matches.group(2).strip() matches = re.search(r"([0-9.]+) years old", parent.text) age = matches.group(1).strip() matches = re.search(r"(([0-9])+'([0-9])+\")", parent.text) height = matches.group(1).strip() height_inches = (int(matches.group(2)) * 12) + int(matches.group(3)) matches = re.search(r"([0-9]+) lbs", parent.text) weight = matches.group(1) rank = parent.previous_sibling.previous_sibling.previous_sibling.previous_sibling.text.strip( ". ") self.log.info( "Rank: %s, First Name: %s, Last Name: %s, School: %s, Year: %s, Age: %s, Height: %s, Weight: %s" % (rank, first_name, last_name, school, year, age, height, weight)) return Athlete(first_name=first_name, last_name=last_name, school=school, year=year, age=age, height=height, height_inches=height_inches, weight=weight, draft_express_rank=rank)
def index(request): access_token = request.session.get('access_token', None) if access_token is None: # if access_token is NOT in session, kickoff the oauth exchange client = Client() url = client.authorization_url( client_id=os.environ.get('STRAVA_CLIENT_ID', None), redirect_uri='http://' + settings.ALLOWED_HOSTS[0] + '/authorization' # redirect_uri='http://127.0.0.1:8000/authorization' ) context = {'auth_url': url} return render(request, 'index.html', context) # otherwise, load authorized user client = Client(access_token=access_token) athlete_id = request.session.get('athlete_id', None) if athlete_id is None: # if athlete is NOT already in session, get athlete # (attempting to reduce calls to the API) athlete = client.get_athlete() request.session['athlete_id'] = athlete.id request.session['firstname'] = athlete.firstname try: Athlete.objects.get(strava_id=athlete.id) except Athlete.DoesNotExist: new_athlete = Athlete() new_athlete.strava_id = athlete.id new_athlete.first_name = athlete.firstname new_athlete.last_name = athlete.lastname new_athlete.city = athlete.city new_athlete.state = athlete.state new_athlete.country = athlete.country new_athlete.save() return render(request, 'welcome_landing.html')
def get_athletes(directory: str, verbose: bool = False) -> List[Athlete]: athletes = [] for file_name in Path(directory).glob("*.json"): with open(file_name) as f: try: data = json.load(f) athletes.append( Athlete( name=data["name"], height=data["height"], date_of_birth=datetime.datetime.strptime( data["date_of_birth"], "%m/%d/%Y"), )) except Exception as e: if verbose: print(f"Unable to parse {file_name}: {e}") if verbose: print(f"Successfully loaded {len(athletes)} athletes.\n") return athletes
def create_athlete(token): body = request.get_json() first_name = body.get('first_name', None) last_name = body.get('last_name', None) if not ('first_name' in body and 'last_name' in body): abort(404) try: athlete = Athlete(first_name=first_name, last_name=last_name) db.session.add(athlete) db.session.commit() return jsonify({ 'success': True, 'created athlete_id': athlete.id, 'total_athlete': len(Athlete.query.all()) }) except: abort(422) finally: db.session.close()
def hello(): if len(Athlete.select()) > 0: return render_template('index.html') else: return render_template('strava_auth.html', strava_auth_url=strava_auth_url)