Skip to content

Commit

Permalink
为aqi2增加接口
Browse files Browse the repository at this point in the history
  • Loading branch information
PKUJohnson authored and PKUJohnson committed Jul 22, 2019
1 parent e6ab444 commit ada1ac4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
9 changes: 9 additions & 0 deletions example/aqi2_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

from opendatatools import aqi2

# 获取当前的aqi(所有城市)
df, msg = aqi2.get_aqi_map("HOUR", "2019-07-22 08:00:00")
print(df)

# 获取某日的aqi(所有城市)
df, msg = aqi2.get_aqi_map("HOUR", "2019-07-21")
print(df)


# 获取城市列表
citylist = aqi2.get_city_list()
print(citylist)
Expand Down
2 changes: 1 addition & 1 deletion opendatatools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

import os

__version__ = '0.9.2'
__version__ = '0.9.3'
SOURCE_ROOT_DIR = os.path.dirname(os.path.abspath(__file__))

60 changes: 58 additions & 2 deletions opendatatools/aqi2/aqi2_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,51 @@ def get_server_data(self, city, type, startTime, endTime):
df.set_index("time", inplace=True)
return df, ""

# type : "DAY", "HOUR
def get_api_map(self, type, timepoint):
url = "https://www.aqistudy.cn/apinew/aqistudyapi.php"
appid = "1a45f75b824b2dc628d5955356b5ef18"
method = "GETMAPDATA"
timestamp = int(time.time() * 1000)
clienttype = "WEB"
object = {"type": type, "timepoint" : timepoint }
secret_key = appid + method + str(timestamp) + clienttype + "{\"timepoint\":\"%s\",\"type\":\"%s\"}" % (
object["timepoint"], object["type"]
)
secret = hashlib.md5(secret_key.encode("utf8")).hexdigest()
param = {
"appId": appid,
"method": method,
"timestamp": timestamp,
"clienttype": clienttype,
"object": object,
"secret": secret
}

param = base64.standard_b64encode(json.dumps(param).encode("utf8")).decode()
param = aes_encrypt(real_aes_client_key, real_aes_client_iv, param)

response = self.do_request(url, param={"d" : param}, method="POST")
if response is None:
return None, "获取数据失败"

#data = base64.standard_b64decode(response.encode("utf8")).decode()
data = decrypt_response(real_des_key, real_des_iv, real_aes_server_key, real_aes_server_iv, response)

jsonobj = json.loads(data)
success = jsonobj["success"]
errcode = jsonobj["errcode"]
errmsg = jsonobj["errmsg"]

if errcode != 0:
return None, errmsg

result = jsonobj["result"]["data"]["rows"]
df = pd.DataFrame(result)
if len(df) > 0:
df.set_index("time", inplace=True)
return df, ""

def get_city_list(self):
url = "https://www.aqistudy.cn/historydata/"
response = self.do_request(url, method="GET")
Expand All @@ -89,10 +134,21 @@ def get_city_list(self):
#city = aqi.get_city_list()
#print(city)

df, msg = aqi.get_daily_hour_aqi('北京', '2019-07-22')
print(df)
#df, msg = aqi.get_daily_hour_aqi('北京', '2019-07-22')
#print(df)
#print(msg)

#df, msg = aqi.get_hist_daily_aqi('北京','2018-05-01', '2019-05-01')
#print(df)
#print(msg)

param = "tdgHOYxwKdDSgYXe+RLPzYCgLvrddahasI5XXklB4gVLYqab+XRPpMD/oSqnJ/aEmFwzVEUhLnPzRy03+X1BI7EP40t1A25axHw6XgQEwrMCA8LPhIJdpk3JqlHQwF/0lHvOCRXiksQ2gxlp8zXTAHX2NeaTAHQeAHVjfS2WSo59EEsCNxnpiBc9JH6WSot2V5FpFF1Z0ElMw4F5ts9OERUzyzCtS80roZmhfEN2sQMsQdt+8keJnsk56MDt1HG9THFJI56TYqd+cFcfcjv5UXDQqjBrttIwXzTYLmMVW0ALdQH4MsmIyMsxiHPYMsto6FJWs/7Jh3L3giRN0yVlF8mW73XSkAsiW/aCRcua8psHxI4f5lyG1a2owB5CZS/FMCM3OHeSvmebXVLrPfOeWA=="
result = aes_decrypt(real_aes_client_key, real_aes_client_iv, param)
result = base64.standard_b64decode(result).decode()
print(result)

#df, msg = aqi.get_real_aqi_map("HOUR", "2019-07-22 08:00:00")
#print(df)

df, msg = aqi.get_api_map("DAY", "2019-07-21")
print(df)
4 changes: 4 additions & 0 deletions opendatatools/aqi2/aqi2_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ def get_hist_daily_aqi(city, begindate, enddate):

def get_daily_hour_aqi(city, date):
return aqistudy_agent.get_daily_hour_aqi(city, date)

def get_aqi_map(type, timepoint):
return aqistudy_agent.get_api_map(type, timepoint)

0 comments on commit ada1ac4

Please sign in to comment.