Skip to content

Commit

Permalink
redesign get images to work from tba, accepts imgur and insta
Browse files Browse the repository at this point in the history
  • Loading branch information
00magikarp committed Jul 24, 2024
1 parent cc81165 commit fa63bee
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 164 deletions.
10 changes: 10 additions & 0 deletions image_errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(1014, IndexError('list index out of range'))
(1622, IndexError('list index out of range'))
(2485, IndexError('list index out of range'))
(4159, IndexError('list index out of range'))
(6988, IndexError('list index out of range'))
(7848, IndexError('list index out of range'))
(8513, IndexError('list index out of range'))
(9008, IndexError('list index out of range'))
(9416, IndexError('list index out of range'))
(9787, IndexError('list index out of range'))
116 changes: 0 additions & 116 deletions scoutingapp/getImagesTBA.py

This file was deleted.

Binary file removed scoutingapp/src/components/img/4099.jpeg
Binary file not shown.
65 changes: 35 additions & 30 deletions scoutingapp/src/getImages.py → scoutingapp/src/getImagesTBA.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import os
import time
import requests
import bs4

TBA_API_KEY = r''
IMGUR_CLIENT_ID = r''
IMGUR_ETAG = r''
EVENT_KEY = ''
EVENT_KEY = r''

if not (TBA_API_KEY and IMGUR_CLIENT_ID and IMGUR_ETAG and EVENT_KEY):
raise PermissionError("Some keys are missing! Go into code and insert your keys.")

FOLDER_PATH = './components/img'
FOLDER_PATH = './src/components/img'

# Headers for the API request
TBA_HEADERS = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0',
'Referer': 'https://www.thebluealliance.com/',
'X-TBA-Auth-Key': TBA_API_KEY
}

Expand All @@ -40,7 +41,7 @@ def fetch_teams(event_key) -> list[int]:
return sorted([team['team_number'] for team in response.json()])


def scrape_robot_image(team_number: int) -> None | str:
def scrape_robot_image(team_number: int) -> str:
"""
Returns the url of the location of the robot image used on TBA.
This function fails (raises TypeError or returns None) when there is no robot image found on TBA.
Expand All @@ -49,38 +50,40 @@ def scrape_robot_image(team_number: int) -> None | str:
:returns: The url of the image
"""

url = f"https://www.thebluealliance.com/team/{team_number}"
url = f'https://www.thebluealliance.com/api/v3/team/frc{team_number}/media/2024'
response = requests.get(url, headers=TBA_HEADERS)

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

soup = bs4.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']

return image_url
response.raise_for_status()
data = [x for x in response.json() if x['type'] not in ['avatar', 'youtube']]
return data[0]['direct_url']


def download_image(team_number: int, image_url: str) -> None:
def download_image(team_number: int, image_url: str, image_host: str) -> None:
"""
Downloads the image from the given url to the appropirate place in code.
Will download to scoutingapp/src/components/img and will download as team_number.jpeg.
:param team_number: The team number for the robot
:param image_url: The url location of the image that needs to be downloaded
:param image_host: Website hosting image (instagram | imgur)
:returns: None
"""

image_hash = image_url.split("/")[3].removesuffix(".jpg").removesuffix(".jpeg").removesuffix(".png")
image_ending = image_url.split(".")[-1]
image_ending = "jpeg" if image_ending == "jpg" else image_ending
if image_host == "imgur":
image_hash = image_url.split("/")[3].removesuffix(".jpg").removesuffix(".jpeg").removesuffix(".png")
image_ending = image_url.split(".")[-1]
image_ending = "jpeg" if image_ending == "jpg" else image_ending

image_url = f'https://i.imgur.com/{image_hash}.{image_ending}'

image_url = f'https://i.imgur.com/{image_hash}.{image_ending}'
image_response = requests.get(image_url, headers=IMGUR_HEADERS)
elif image_host == "instagram":
image_url = f'https://thebluealliance.com/{image_url}'
image_ending = "jpeg"

image_response = requests.get(image_url, headers=TBA_HEADERS)
else:
return

image_response = requests.get(image_url, headers=IMGUR_HEADERS)
image_response.raise_for_status()

file_path = os.path.join(FOLDER_PATH, f"{team_number}.{image_ending}")
Expand Down Expand Up @@ -108,7 +111,7 @@ def main(teams: tuple[int] = ()) -> None:
failed_teams = {}
for team in teams:
print(f'{team}: Starting...')
time.sleep(0.5)
time.sleep(2)

try:
url = scrape_robot_image(team)
Expand All @@ -118,14 +121,16 @@ def main(teams: tuple[int] = ()) -> None:
print(f"{team}: Failed to retrieve image link.")
failed_teams[team] = "Failed to retrieve image link."
continue
if "imgur" not in url:
print(f"{team}: Image is not imgur.")
failed_teams[team] = "Image is not imgur."
continue
if "imgur" in url:
download_image(team, url, "imgur")
elif "instagram" in url:
download_image(team, url, "instagram")
else:
print(f"{team}: Image host not recognized.")
failed_teams[team] = "Image host not recognized."

download_image(team, url)
print(f'{team}: Success.')
except TypeError:
except (TypeError, IndexError):
print(f"{team}: Image not found.")
failed_teams[team] = "Image not found."
except Exception as e:
Expand All @@ -138,7 +143,7 @@ def main(teams: tuple[int] = ()) -> None:
print("NOTE: Some teams did not run properly.")
print(*failed_teams.items(), sep="\n")

with open("image_errors.txt", 'w') as err_file:
with open("./src/image_errors.txt", 'w') as err_file:
for fail in failed_teams.items():
err_file.write(str(fail) + "\n")

Expand Down
18 changes: 0 additions & 18 deletions scoutingapp/src/image_errors.txt
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
(365, 'Image is not imgur.')
(1014, 'Image not found.')
(1279, 'Image is not imgur.')
(1622, 'Image not found.')
(2472, 'Image is not imgur.')
(2485, 'Image not found.')
(3654, 'Image is not imgur.')
(4159, 'Image not found.')
(5002, 'Image is not imgur.')
(5987, 'Image is not imgur.')
(6988, 'Image not found.')
(7848, 'Image not found.')
(8513, 'Image not found.')
(8727, 'Image is not imgur.')
(9008, 'Image not found.')
(9416, 'Image not found.')
(9564, 'Image is not imgur.')
(9787, 'Image not found.')

0 comments on commit fa63bee

Please sign in to comment.