Repository navigation
Expand file tree
/
Copy pathMetroRequester.py
More file actions
45 lines (39 loc) · 1.6 KB
/
Copy pathMetroRequester.py
File metadata and controls
45 lines (39 loc) · 1.6 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Created By Yigang Zhou (Mike)
# https:// MikeTech.it
import requests
import MetroUtil
import json
from StationManager import StationManager
from LineManager import LineManager
import os
url_shanghai_metro = "http://m.shmetro.com/core/shmetro/mdstationinfoback_new.ashx?act=getAllStations"
def request_shanghai_metro_data():
station_manager = StationManager()
line_manager = LineManager()
offline_file_exist = os.path.isfile("shanghai.json")
if offline_file_exist:
f = open("shanghai.json")
content = f.read()
data = json.loads(content, encoding="utf-8")
for each in data:
line_number = MetroUtil.get_line_number(each["key"])
station_name = str(each["value"])
station_manager.add_station(station_name, line_number)
line_manager.add_line(line_number, station_name)
# station_manager.print_all_info()
# line_manager.print_all_info()
return station_manager, line_manager
else:
print("offline data not found, downloading")
data = requests.get(url_shanghai_metro)
if data.status_code == 200:
for each in data.json():
line_number = MetroUtil.get_line_number(each["key"])
station_name = str(each["value"])
station_manager.add_station(station_name, line_number)
line_manager.add_line(line_number, station_name)
f = open("shanghai.json", "w")
json.dump(data.json(), f)
# station_manager.print_all_info()
# line_manager.print_all_info()
return station_manager, line_manager