Spaces:
Runtime error
Runtime error
Update util/getdata.py
Browse files- util/getdata.py +20 -46
util/getdata.py
CHANGED
@@ -6,52 +6,26 @@ from util.dbquery import load_json, insert_data, getDataNum
|
|
6 |
def fetch_and_store_data():
|
7 |
print("μ΅μ λ°μ΄ν°λ₯Ό λ°μμ€λ μ€μ
λλ€. λ°μ΄ν°λ₯Ό λ°λ λμ€ νλ‘κ·Έλ¨μ μ’
λ£νμ§ λ§μμμ€.")
|
8 |
KEY = os.environ['PRODUCT_PRICE_API']
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
init_data = json.loads(decoded)
|
19 |
-
list_total_count = init_data["ListNecessariesPricesService"]["list_total_count"]
|
20 |
-
#sql μλ²μ λ°μ΄ν° μμ μμΈμ λ°μ΄ν°λ² μ΄μ€μ λ°μ΄ν° μλ₯Ό λΉκ΅νμ¬ μ΅μ μ¬λΆ νμΈ
|
21 |
-
if(data_count >= list_total_count):# μ΅μ μΌκ²½μ°
|
22 |
-
print("νμ¬ λͺ¨λ λ°μ΄ν°κ° μ΅μ μ
λλ€.")
|
23 |
-
else:# μ
λ°μ΄νΈ νμν κ²½μ°
|
24 |
-
|
25 |
-
if os.path.exists(file_path):#κΈ°μ‘΄ init_data.json νμΌμ΄ μμκ²½μ° init_data.json νμΌμ λ°μ΄ν° μ΄μ΄μ°κΈ°
|
26 |
-
with open(file_path, "r", encoding = "utf-8") as file:
|
27 |
-
json_data = json.load(file)
|
28 |
-
|
29 |
-
list_total_count = list_total_count - data_count #μ
λ°μ΄νΈ νμν μλ§νΌλ§ μ
λ°μ΄νΈ
|
30 |
-
print(f"μλ‘μ΄ {list_total_count}κ°μ λ°μ΄ν°λ₯Ό μ
λ°μ΄νΈ ν©λλ€.")
|
31 |
-
pbar = tqdm(total=list_total_count)
|
32 |
-
|
33 |
-
while list <= list_total_count:
|
34 |
-
if(list+999 > list_total_count):
|
35 |
-
url = f'http://openAPI.seoul.go.kr:8088/{KEY}/json/ListNecessariesPricesService/{list}/{list_total_count}'
|
36 |
-
pbar.update(list_total_count - list + 1)
|
37 |
-
else:
|
38 |
-
url = f'http://openAPI.seoul.go.kr:8088/{KEY}/json/ListNecessariesPricesService/{list}/{list+999}'
|
39 |
-
pbar.update(1000)
|
40 |
-
|
41 |
-
response = requests.get(url)
|
42 |
-
decoded = response.content.decode('utf-8')
|
43 |
-
temp = json.loads(decoded)
|
44 |
-
json_data.append(temp["ListNecessariesPricesService"]["row"])
|
45 |
-
list += 1000
|
46 |
-
pbar.close()
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
55 |
current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
|
56 |
-
print(f"\
|
57 |
print("μ
λ°μ΄νΈ μλ£")
|
|
|
6 |
def fetch_and_store_data():
|
7 |
print("μ΅μ λ°μ΄ν°λ₯Ό λ°μμ€λ μ€μ
λλ€. λ°μ΄ν°λ₯Ό λ°λ λμ€ νλ‘κ·Έλ¨μ μ’
λ£νμ§ λ§μμμ€.")
|
8 |
KEY = os.environ['PRODUCT_PRICE_API']
|
9 |
+
i = 1
|
10 |
+
while i + 999 <= 100000:
|
11 |
+
url = f'http://openAPI.seoul.go.kr:8088/{KEY}/json/ListNecessariesPricesService/{i}/{i+999}'
|
12 |
+
response = requests.get(url)
|
13 |
+
decoded = response.content.decode('utf-8')
|
14 |
+
data = json.loads(decoded)
|
15 |
+
|
16 |
+
# λ°μ΄ν°λ₯Ό λ°λ‘ μ²λ¦¬νκ³ λ°μ΄ν°λ² μ΄μ€μ μ½μ
|
17 |
+
inserted_rows = insert_data(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
if inserted_rows == 0: # μλ‘ μ½μ
λ λ°μ΄ν°κ° μμΌλ©΄ 루ν νμΆ
|
20 |
+
print("\nλ°μ΄ν°λ² μ΄μ€μ μλ‘μ΄ λ°μ΄ν°κ° μμ΅λλ€. μ
λ°μ΄νΈλ₯Ό μ€λ¨ν©λλ€.")
|
21 |
+
break
|
22 |
+
|
23 |
+
i += 1000
|
24 |
+
sys.stdout.write('\r')
|
25 |
+
completion = min(int(i/1000), 100)
|
26 |
+
sys.stdout.write("[%-50s] %d%%" % ('='*int(completion/2), completion))
|
27 |
+
sys.stdout.flush()
|
28 |
+
|
29 |
current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
|
30 |
+
print(f"\nλ°μ΄ν° μ
λ°μ΄νΈ μλ£ μκ°: {current_time}")
|
31 |
print("μ
λ°μ΄νΈ μλ£")
|