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 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 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 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 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 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 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 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()