Spaces:
Sleeping
Sleeping
hussein2000
commited on
Commit
•
c8e724a
1
Parent(s):
03e7882
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,133 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
import requests
|
3 |
-
|
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
-
# Function to fetch
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
params = {'q': search_query, 's': offset}
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
if response.status_code != 200:
|
27 |
-
return {"error": f"Failed to retrieve data: {response.status_code}"}
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
'title': title_tag.text.strip(),
|
43 |
-
'description': snippet_tag.text.strip(),
|
44 |
-
'icon_url': "https:" + icon_tag['src'] if icon_tag else None,
|
45 |
-
'url': url_tag['href']
|
46 |
}
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
# Run the Flask application
|
68 |
if __name__ == '__main__':
|
69 |
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
import requests
|
3 |
+
import json
|
4 |
+
import base64
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
+
# GitHub credentials from environment variables
|
9 |
+
GITHUB_TOKEN = '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' # Set your token in environment variables
|
10 |
+
REPO_OWNER = 'hussein2000-oo'
|
11 |
+
REPO_NAME = 'dbailloolloloolollhrthlnewrgnk'
|
12 |
+
USER_FILE_NAME = 'user.json'
|
13 |
|
14 |
+
# Function to fetch user data from GitHub
|
15 |
+
def fetch_user_data():
|
16 |
+
url = f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/contents/{USER_FILE_NAME}'
|
17 |
+
headers = {'Authorization': f'token {GITHUB_TOKEN}'}
|
18 |
+
response = requests.get(url, headers=headers)
|
19 |
|
20 |
+
if response.status_code == 200:
|
21 |
+
content = response.json()
|
22 |
+
user_data = json.loads(base64.b64decode(content['content']).decode('utf-8'))
|
23 |
+
|
24 |
+
# Ensure user_data is a dictionary
|
25 |
+
if not isinstance(user_data, dict):
|
26 |
+
print("User data is not in the expected format. Initializing empty user data.")
|
27 |
+
user_data = {}
|
28 |
+
|
29 |
+
return user_data, content['sha'] # Return the SHA for updating the file
|
30 |
+
else:
|
31 |
+
print("Failed to fetch user data:", response.status_code, response.json())
|
32 |
+
return {}, None # Return an empty dict if fetching fails
|
33 |
+
|
34 |
+
|
35 |
+
# Function to update user data on GitHub
|
36 |
+
def update_user_data(user_data, sha):
|
37 |
+
url = f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/contents/{USER_FILE_NAME}'
|
38 |
+
headers = {'Authorization': f'token {GITHUB_TOKEN}'}
|
39 |
|
40 |
+
updated_content = base64.b64encode(json.dumps(user_data).encode('utf-8')).decode('utf-8')
|
|
|
41 |
|
42 |
+
payload = {
|
43 |
+
"message": "Update user.json with new user",
|
44 |
+
"content": updated_content,
|
45 |
+
"sha": sha
|
46 |
+
}
|
47 |
|
48 |
+
response = requests.put(url, headers=headers, json=payload)
|
|
|
|
|
49 |
|
50 |
+
if response.status_code == 200:
|
51 |
+
print("User data updated successfully.")
|
52 |
+
else:
|
53 |
+
print("Failed to update user data:", response.status_code, response.json())
|
54 |
+
|
55 |
+
|
56 |
+
# API endpoint to create a user account
|
57 |
+
@app.route('/api/create_user', methods=['POST'])
|
58 |
+
def create_user():
|
59 |
+
data = request.json
|
60 |
+
username = data.get('username')
|
61 |
+
password = data.get('password')
|
62 |
+
first_name = data.get('first_name')
|
63 |
+
last_name = data.get('last_name')
|
64 |
+
birthday = data.get('birthday')
|
65 |
+
security_questions = data.get('security_questions')
|
66 |
+
|
67 |
+
user_data, sha = fetch_user_data()
|
68 |
|
69 |
+
if user_data is not None:
|
70 |
+
if username in user_data:
|
71 |
+
return jsonify({"message": "User already exists."}), 400
|
72 |
+
else:
|
73 |
+
user_data[username] = {
|
74 |
+
"password": password,
|
75 |
+
"first_name": first_name,
|
76 |
+
"last_name": last_name,
|
77 |
+
"birthday": birthday,
|
78 |
+
"security_questions": security_questions # Store security questions
|
|
|
|
|
|
|
|
|
79 |
}
|
80 |
+
update_user_data(user_data, sha)
|
81 |
+
return jsonify({"message": f"User {username} created successfully."}), 201
|
82 |
+
else:
|
83 |
+
return jsonify({"message": "Could not create user. User data fetch failed."}), 500
|
84 |
+
|
85 |
+
|
86 |
+
# API endpoint to sign in
|
87 |
+
@app.route('/api/sign_in', methods=['POST'])
|
88 |
+
def sign_in():
|
89 |
+
data = request.json
|
90 |
+
username = data.get('username')
|
91 |
+
password = data.get('password')
|
92 |
+
|
93 |
+
user_data, _ = fetch_user_data()
|
94 |
|
95 |
+
if user_data is not None and isinstance(user_data, dict):
|
96 |
+
if username in user_data:
|
97 |
+
if user_data[username]['password'] == password: # Corrected password check
|
98 |
+
return jsonify({"message": "Signed in successfully!"}), 200
|
99 |
+
else:
|
100 |
+
return jsonify({"message": "Sign in failed."}), 401
|
101 |
+
else:
|
102 |
+
return jsonify({"message": "User not found."}), 404
|
103 |
+
else:
|
104 |
+
return jsonify({"message": "Unexpected data format in user.json."}), 500
|
105 |
+
|
106 |
+
|
107 |
+
# API endpoint to reset password using security questions
|
108 |
+
@app.route('/api/reset_password', methods=['POST'])
|
109 |
+
def reset_password():
|
110 |
+
data = request.json
|
111 |
+
username = data.get('username')
|
112 |
+
answers = data.get('answers')
|
113 |
+
|
114 |
+
user_data, _ = fetch_user_data()
|
115 |
|
116 |
+
if username in user_data:
|
117 |
+
questions = user_data[username]['security_questions ']
|
118 |
+
|
119 |
+
# Check if answers match
|
120 |
+
if all(user_data[username]['security_questions'][q] == answers[q] for q in questions):
|
121 |
+
new_password = data.get('new_password')
|
122 |
+
user_data[username]['password'] = new_password
|
123 |
+
update_user_data(user_data, _)
|
124 |
+
return jsonify({"message": "Password reset successfully."}), 200
|
125 |
+
else:
|
126 |
+
return jsonify({"message": "Security answers do not match."}), 401
|
127 |
+
else:
|
128 |
+
return jsonify({"message": "User not found."}), 404
|
129 |
+
|
130 |
+
|
131 |
|
|
|
132 |
if __name__ == '__main__':
|
133 |
app.run(host="0.0.0.0", port=7860)
|