File size: 1,325 Bytes
2fe14e5
b8a5de3
 
f74c1d3
2fe14e5
00b352c
d4d9b2b
 
7a02370
 
 
 
 
 
 
 
 
00b352c
7a02370
 
 
 
 
 
 
 
 
 
2fe14e5
7a02370
ef4c471
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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("μ—…λ°μ΄νŠΈ μ™„λ£Œ")