Spaces:
Runtime error
Runtime error
| import requests | |
| import json | |
| from AppPub.User.Bean.User_Data import User_Data | |
| from urllib.parse import unquote | |
| class UserInfo: | |
| def __init__(self, sub, name, picture): | |
| self.sub = sub | |
| self.name = name | |
| self.picture = picture | |
| self.data = self.GetUserInfo() | |
| # self.mobile = "" | |
| def HttpGetUserData(self): | |
| # 发送GET请求 | |
| response = requests.get(f'https://tilents.sinaapp.com/assistant/search_userInfo.php?sub={self.sub}') | |
| # 检查响应状态码 | |
| if response.status_code == 200: | |
| # 成功获取数据 | |
| data = response.json() # 使用正确的字符编码 | |
| print(f"GetUserData{data}") | |
| return data | |
| else: | |
| print(f'Failed to fetch data. Status code: {response.status_code}') | |
| return None | |
| import requests | |
| def HttpPostUserData(self, push_data): | |
| # 定义要发送的数据,这里是一个字典 | |
| data = { | |
| "sub": self.sub, # 假设需要传递的参数名为 "sub" | |
| "uservalue": push_data | |
| # 添加其他参数和值 | |
| } | |
| # 发送POST请求 | |
| response = requests.post('https://tilents.sinaapp.com/assistant/insert_userinfo.php', data=data) | |
| # 检查响应状态码 | |
| if response.status_code == 200: | |
| # 成功获取数据 | |
| data = response.content.decode('utf-8') # 使用正确的字符编码 | |
| print(f"PostUserData: {data}") | |
| return data | |
| else: | |
| print(f'Failed to fetch data. Status code: {response.status_code}') | |
| return None | |
| def GetUserInfo(self): | |
| data = self.HttpGetUserData() | |
| if not data or "Failed to fetch data" in data or "null" in data: | |
| person = User_Data(self.name, self.picture) | |
| json_string = json.dumps(person.to_json()) | |
| print("新用戶:" + json_string) # 註冊一個用戶 | |
| result = self.HttpPostUserData(json_string) | |
| # 处理获取到的数据 | |
| if result is not None: | |
| # 在这里处理返回的数据 | |
| print(f"新用戶添加結果:{result}") # 註冊一個用戶 | |
| return person | |
| else: | |
| # 将JSON字符串解析为类的实例 | |
| person_dict = json.loads(data) | |
| # User_Data | |
| person = User_Data(**person_dict) | |
| person.picture = unquote(person.picture) | |
| return person | |