Skip to content

Commit

Permalink
fetch teams will work, create (not done) scrape robot image function
Browse files Browse the repository at this point in the history
  • Loading branch information
00magikarp committed Jul 23, 2024
1 parent 21d629c commit 6f51aa7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion scoutingapp/src/components/img/RobotImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function RobotImage(props: ComponentSetup) {

return (
<img
style={{ width: "60%", height: "60%", alignItems: 'center', justifyContent: 'center', margin: "auto" }}
style={{ height: "60%", alignItems: 'center', justifyContent: 'center', margin: "auto" }}
src={`./src/components/img/${robotNumber}.png`}
alt={makeAltText(robotNumber)}
/>
Expand Down
48 changes: 42 additions & 6 deletions scoutingapp/src/getImages.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import os

import requests
import json
import urllib.request
from bs4 import BeautifulSoup

API_KEY = 'api_key'
API_KEY = 'INSERT_API_KEY_HERE'

EVENT_KEY = 'event_key'
EVENT_KEY = '2024cur'

FOLDER_PATH = 'path'
FOLDER_PATH = './img'

# Headers for the API request
HEADERS = {
'Auth-Key': API_KEY
'X-TBA-Auth-Key': API_KEY
}


def fetch_teams(event_key):
url = f'https://www.thebluealliance.com/api/v3/event/{event_key}/teams'
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
return response.json()
return [team['team_number'] for team in response.json()]


print(fetch_teams(EVENT_KEY))


def fetch_robot_image(team_key):
Expand All @@ -44,6 +49,8 @@ def main():
os.makedirs(FOLDER_PATH)

teams = fetch_teams(EVENT_KEY)

exit(1)
for team in teams:
team_key = team['key']
image_url = fetch_robot_image(team_key)
Expand All @@ -56,5 +63,34 @@ def main():
print(f"No image found for {team_key}")


def scrape_robot_image(team_number):
url = f"https://www.thebluealliance.com/team/{team_number}"
response = requests.get(url, headers=HEADERS)

if response.status_code != 200:
print(f"Failed to retrieve page for team {team_number}")
return

soup = BeautifulSoup(response.text, 'html.parser')
# Find the image by its class name or id. This may change, so inspect the page to find the correct identifier.
image_tag = soup.find('a', {'class': 'gallery'})
image_url = image_tag['href']

print(image_tag, image_url, sep="\n")

# Download the image
image_response = requests.get(image_url)
print(image_response.status_code)

if image_response.status_code == 200:
image_name = f"team_{team_number}_robot_image.jpg"
with open(image_name, 'wb') as file:
file.write(image_response.content)
print(f"Image successfully saved as {image_name}")
else:
print(f"Failed to download image for team {team_number}")


if __name__ == '__main__':
scrape_robot_image(6328)
main()

0 comments on commit 6f51aa7

Please sign in to comment.