Spaces:
Runtime error
Runtime error
#code by princesslucy | |
import json, time, requests, os, sys | |
from tqdm import tqdm | |
from util.dbquery import load_json, insert_data, getDataNum | |
def fetch_and_store_data(): | |
print("μ΅μ λ°μ΄ν°λ₯Ό λ°μμ€λ μ€μ λλ€. λ°μ΄ν°λ₯Ό λ°λ λμ€ νλ‘κ·Έλ¨μ μ’ λ£νμ§ λ§μμμ€.") | |
KEY = os.environ['PRODUCT_PRICE_API'] | |
i = 1 | |
while i + 999 <= 100000: | |
url = f'http://openAPI.seoul.go.kr:8088/{KEY}/json/ListNecessariesPricesService/{i}/{i+999}' | |
response = requests.get(url) | |
decoded = response.content.decode('utf-8') | |
data = json.loads(decoded) | |
# λ°μ΄ν°λ₯Ό λ°λ‘ μ²λ¦¬νκ³ λ°μ΄ν°λ² μ΄μ€μ μ½μ | |
inserted_rows = insert_data(data) | |
if inserted_rows == 0: # μλ‘ μ½μ λ λ°μ΄ν°κ° μμΌλ©΄ 루ν νμΆ | |
print("\nλ°μ΄ν°λ² μ΄μ€μ μλ‘μ΄ λ°μ΄ν°κ° μμ΅λλ€. μ λ°μ΄νΈλ₯Ό μ€λ¨ν©λλ€.") | |
break | |
i += 1000 | |
sys.stdout.write('\r') | |
completion = min(int(i/1000), 100) | |
sys.stdout.write("[%-50s] %d%%" % ('='*int(completion/2), completion)) | |
sys.stdout.flush() | |
current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) | |
print(f"\nλ°μ΄ν° μ λ°μ΄νΈ μλ£ μκ°: {current_time}") | |
print("μ λ°μ΄νΈ μλ£") |