ldhldh commited on
Commit
7a02370
Β·
verified Β·
1 Parent(s): a71c1b4

Update util/getdata.py

Browse files
Files changed (1) hide show
  1. 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
- list = 1
10
- json_data = []
11
- data_count = getDataNum() #sql μ„œλ²„μ˜ ν˜„μž¬ 데이터 수
12
- file_path = 'data/init_data.json'
13
-
14
- # μ„œμšΈμ‹œ 곡곡데이터 λ°μ΄ν„°λ² μ΄μŠ€μ˜ ν˜„μž¬ 데이터 수
15
- url = f'http://openAPI.seoul.go.kr:8088/{KEY}/json/ListNecessariesPricesService/{list}/{list}'
16
- response = requests.get(url)
17
- decoded = response.content.decode('utf-8')
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
- for data in json_data:
49
- insert_data(load_json(data))
50
- with open(file_path, 'w', encoding='utf-8') as f:
51
- json.dump(json_data, f, ensure_ascii=False, indent=4)
52
-
53
-
54
-
 
 
 
55
  current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
56
- print(f"\nData stored {current_time}")
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("μ—…λ°μ΄νŠΈ μ™„λ£Œ")