Created
October 15, 2020 04:42
-
-
Save chrisleaman/456b9f8d2598f8e70fb5dbb4ecdaaf46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from urllib.parse import urljoin, urlparse | |
from pathlib import Path | |
TOPIC_ID = 37 | |
ROOT_ID = 269986 | |
OUTPUT_FOLDER = "." # save images in same directory as this file | |
SPOTTERON_API = "https://www.spotteron.com/api/v2/spots" | |
SPOTTERON_IMG = "https://files.spotteron.com/images/spots/" | |
def get_data(topic_id, root_id): | |
""" | |
Gets the json data for a particular topic_id and root_id. Returns a dictionary | |
containing data returned by api | |
""" | |
payload = {"filter[topic_id]": topic_id, "filter[root_id]": root_id} | |
r = requests.get(SPOTTERON_API, params=payload) | |
return r.json() | |
def get_image_urls(data): | |
""" | |
Parses data to get image urls. Returns a list of absolute urls | |
""" | |
images_str = [f"{x['attributes']['image']}.jpg" for x in data["data"]] | |
image_urls = [urljoin(SPOTTERON_IMG, x) for x in images_str] | |
print(f"Found {len(image_urls)} images to download") | |
return image_urls | |
def download_img(url, output_folder="."): | |
# Save on local drive with same filename | |
filename = Path(urlparse(url).path).name | |
output_filepath = Path(output_folder, filename) | |
print(f"Downloading {output_filepath}") | |
response = requests.get(url, stream=True) | |
if response.status_code == 200: | |
with open(output_filepath, "wb") as f: | |
f.write(response.content) | |
if __name__ == "__main__": | |
# Runs the script with the command: python spotteron_downloader.py | |
data = get_data(topic_id=TOPIC_ID, root_id=ROOT_ID) | |
img_urls = get_image_urls(data) | |
for url in img_urls: | |
download_img(url, OUTPUT_FOLDER) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment