๋ฉ”์ธ ๋‚ด์šฉ์œผ๋กœ ์ด๋™
๐Ÿ“œ์ž ๊น!
  • ์ด ๊ธ€์ด ์ž‘์„ฑ๋œ์ง€ 2๋…„ ์ด์ƒ ์ง€๋‚ฌ์Šต๋‹ˆ๋‹ค.
  • ์ƒˆ๋กœ์šด ์ผ๋“ค์ด ์ผ์–ด๋‚˜๊ธฐ ์ถฉ๋ถ„ํ•œ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค.
  • ์ €๋Š” ์ด ๊ธ€์— ๋” ์ด์ƒ ๋™์˜ํ•˜์ง€ ์•Š์„์ง€๋„ ๋ชจ๋ฆ…๋‹ˆ๋‹ค.
Google์—์„œ ์ƒˆ๋กœ์šด ์ž๋ฃŒ ์ฐพ์•„๋ณด๊ธฐ

API๋กœ ํฌ๋กค๋ง ๐Ÿชฃ

โ€ข ์ง‘๊ณ„ ์ค‘...

Bewareโ€‹

์ด ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํƒ€์ธ์˜ ์ง€์  ์žฌ์‚ฐ๊ถŒ์„ ์นจํ•ดํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ์ด ์ฝ”๋“œ์™€ The Noun Project API๊ฐ€ ์ž์‹ ์˜ ์‚ฌ์šฉ ์šฉ๋„์— ์ ํ•ฉํ•œ์ง€ ํ™•์ธํ•œ ํ›„์— ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค. ๋˜ํ•œ ๋ผ์ด์„ ์Šค์™€ API ๋ฌธ์„œ๋ฅผ ๊ผผ๊ผผํ•˜๊ฒŒ ๊ฒ€ํ† ํ•˜์‹ญ์‹œ์˜ค. The Noun Project์—์„œ ํ—ˆ๊ฐ€ํ•˜์ง€ ์•Š๋Š” ์‚ฌ์šฉ ์šฉ๋„๋“ค์€ ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด ๊ธ€๊ณผ ์ด ๊ธ€์˜ ๋ชจ๋“  ์ฝ”๋“œ๋Š” MIT ๋ผ์ด์„ ์Šค์ž„์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค.

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ถˆ๋Ÿฌ์˜ค๊ธฐโ€‹

import requests
import os
from tqdm import tqdm
from requests_oauthlib import OAuth1

์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์ด ์—†๋‹ค๋ฉด pip3 download ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

download ํ•จ์ˆ˜โ€‹

def download(url, pathname):
if not os.path.isdir(pathname):
os.makedirs(pathname)
response = requests.get(url, stream=True)
file_size = int(response.headers.get("Content-Length", 0))
filename = os.path.join(pathname, url.split("/")[-1])
if filename.find("?") > 0:
filename = filename.split("?")[0]
progress = tqdm(
response.iter_content(256),
f"Downloading {filename}",
total=file_size,
unit="B",
unit_scale=True,
unit_divisor=1024,
)
with open(filename, "wb") as f:
for data in progress:
f.write(data)
progress.update(len(data))

์ด ์ฝ”๋“œ๋Š” URL์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์™€ pathname์— ์ €์žฅํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

The Noun Project APIโ€‹

# ---

DOWNLOAD_ITERATION = 3
# 1๋ฒˆ์— ์•„์ด์ฝ˜์„ 50๊ฐœ์”ฉ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
# 3๋ฒˆ ์‹คํ–‰ํ•˜๋ฉด ์•„์ด์ฝ˜ 150๊ฐœ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

SEARCH_KEY = "tree" # ๊ฒ€์ƒ‰์–ด
SAVE_LOCATION = "./icons" # ์ €์žฅํ•  ์œ„์น˜
auth = OAuth1("API_KEY", "API_SECRET")

# ---

for iteration in range(DOWNLOAD_ITERATION):
endpoint = (
"http://api.thenounproject.com/icons/"
+ SEARCH_KEY
+ "?offset="
+ str(iteration * 50)
)
response = requests.get(endpoint, auth=auth).json()
for icon in response["icons"]:
download(icon["preview_url"], SAVE_LOCATION)

๋ณด๋‹ค ์„ธ๋ถ€์ ์ธ ๊ธฐ๋Šฅ์€ ์ด ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ๋œ๋‹ค. API Key์™€ API Secret์€ ์—ฌ๊ธฐ์—์„œ App์„ ๋“ฑ๋กํ•˜๋ฉด ๋ฐœ๊ธ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฒฐ๊ณผโ€‹

ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์•˜์„ ๋•Œ ์•„์ด์ฝ˜ 5,000๊ฐœ ์ •๋„๊นŒ์ง€๋Š” ๊ฐ€๋ฟํ•˜๊ฒŒ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์•˜์„ ๋•Œ ์•„์ด์ฝ˜ 5,000๊ฐœ ์ •๋„๊นŒ์ง€๋Š” ๊ฐ€๋ฟํ•˜๊ฒŒ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๋‹ค๋งŒ The Noun Project API๋Š” API ํ˜ธ์ถœ ํšŸ์ˆ˜์— ์ œํ•œ์ด ์žˆ์œผ๋‹ˆ ์ด๋ฅผ ์—ผ๋‘์— ๋‘๊ณ  ํ™œ์šฉํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.