|
import requests |
|
import json |
|
|
|
from AppPub.User.Bean.User_Data import User_Data |
|
|
|
|
|
class UserInfo: |
|
def __init__(self, sub, name, picture): |
|
self.sub = sub |
|
self.name = name |
|
self.picture = picture |
|
self.data = self.GetUserInfo() |
|
|
|
|
|
def HttpGetUserData(self): |
|
|
|
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, |
|
"uservalue": push_data |
|
|
|
} |
|
|
|
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 json.loads(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: |
|
|
|
person_dict = json.loads(data) |
|
|
|
person = User_Data(**person_dict) |
|
return person |
|
|