Skip to content

Commit

Permalink
Merge pull request #2 from UNIkeEN/main
Browse files Browse the repository at this point in the history
fix: fix import error in Get-Eat-Data.py, add more error catch of invalid client id/secret
  • Loading branch information
Milvoid authored Dec 23, 2024
2 parents 06c02b0 + aa2c137 commit 836bbb2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
eat-data.json

**/__pycache__/

/.idea
/.vscode
**/.DS_Store
28 changes: 18 additions & 10 deletions Get-Eat-Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_access_token(authorization_code):
}

# 发起 POST 请求到 TOKEN_URL
response = requests.post(TOKEN_URL, headers=headers, auth=auth, data=data)
response = post(TOKEN_URL, headers=headers, auth=auth, data=data)

# 检查返回结果
if response.status_code == 200:
Expand All @@ -82,19 +82,27 @@ def get_eat_data(access_token, begin_date = BEGIN_DATE):

# 发起请求
try:
response = requests.get(API_URL, params=params)
response = get(API_URL, params=params)

# 检查请求是否成功
if response.status_code == 200:
# 解析响应 JSON 数据
data = response.json()
print("消费数据获取成功")

# 保存到文件
with open("eat-data.json", "w", encoding="utf-8") as file:
json.dump(data, file, ensure_ascii=False, indent=4)

print("\n消费数据已保存")
if data.get('errno', 0) != 0:
print(data)
error_message = data.get('error', '未知错误')
error_code = data.get('errno', '无错误码')
print(f"API 错误: {error_message} (错误码: {error_code})")
raise Exception()
else:
print("消费数据获取成功")

# 保存到文件
with open("eat-data.json", "w", encoding="utf-8") as file:
json.dump(data, file, ensure_ascii=False, indent=4)

print("\n消费数据已保存")

return data
else:
Expand Down Expand Up @@ -122,8 +130,8 @@ def get_eat_data(access_token, begin_date = BEGIN_DATE):
print(f"{access_token}\n")

# 获取消费数据
get_eat_data(access_token)
input("请前往 Annual-Report.py 以继续...")
if get_eat_data(access_token):
input("请前往 Annual-Report.py 以继续...")

except Exception:
print("Unknown Error:500")
Expand Down

0 comments on commit 836bbb2

Please sign in to comment.