-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
175 lines (146 loc) · 5.13 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
import dataset
import datetime as dt
import pytz
import hashlib as hl
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
db = dataset.connect("sqlite:///teal.sqlite")
user = db["user"]
tweet = db["tweet"]
@app.get("/view")
def viewAllTweet():
try:
data = tweet.find()
text = []
for d in data:
text.append(d)
return text
except:
return {"result": "None"}
@app.get("/view/{user_id}")
def viewUserTweet(user_id: str):
try:
data = tweet.find(userid=user_id)
text = []
for d in data:
text.append(d)
return text
except:
return {"result": "None"}
@app.get("/view/{user_id}/{tweet_id}")
def viewUserTweet(user_id: str, tweet_id: int):
try:
data = tweet.find(userid=user_id, id=tweet_id)
text = []
for d in data:
text.append(d)
return text
except:
return {"result": "None"}
@app.get("/user/{user_id}")
def userProfile(user_id: str):
try:
userData = user.find_one(userid=user_id)
return {"id": userData["id"], "userid": userData["userid"], "icon_url": userData["icon_url"], "detail": userData["detail"]}
except:
return {"result": "None"}
@app.get("/users")
def viewAllUser():
try:
data = user.find()
users = []
for d in data:
x = {"id": d["id"], "userid": d["userid"], "icon_url": d["icon_url"], "detail": d["detail"]}
users.append(x)
return users
except:
return {"result": "None"}
@app.post("/signup")
def signUp(email: str, password: str, userid: str):
try:
icon_md5 = hl.md5(email.encode()).hexdigest()
icon = "https://www.gravatar.com/avatar/" + icon_md5 + "?s=1000"
password = hl.sha256(password.encode()).hexdigest()
user.insert({"email": email, "password": password,
"userid": userid, "icon_url": icon, "detail": ""})
return {"result": "POST"}
except:
return {"result": "None"}
@app.post("/new")
def newTweet(email: str, password: str, text: str):
try:
userData = user.find_one(email=email)
password = hl.sha256(password.encode()).hexdigest()
if userData["password"] == password:
userid = userData["userid"]
nowDate = dt.datetime.now(pytz.timezone('Asia/Tokyo'))
icon_md5 = hl.md5(email.encode()).hexdigest()
icon = "https://www.gravatar.com/avatar/" + icon_md5 + "?s=1000"
tweet.insert({"userid": userid, "tweet": text,
"to": None, "icon_url": icon, "date": nowDate, "like": 0})
return {"result": "POST"}
except:
return {"result": "None"}
@app.post("/reply/{user_id}")
def replyTweet(user_id: str, email: str, password: str, reply: str):
try:
userData = user.find_one(email=email)
password = hl.sha256(password.encode()).hexdigest()
if userData["password"] == password:
userid = userData["userid"]
nowDate = dt.datetime.now(pytz.timezone('Asia/Tokyo'))
icon_md5 = hl.md5(email.encode()).hexdigest()
icon = "https://www.gravatar.com/avatar/" + icon_md5 + "?s=1000"
tweet.insert({"userid": userid, "tweet": reply,
"to": user_id, "icon_url": icon, "date": nowDate, "like": 0})
return {"result": "POST"}
except:
return {"result": "None"}
@app.post("/like/{tweet_id}")
def likeTweet(tweet_id: int):
try:
tweetData = tweet.find_one(id=tweet_id)
newLike = tweetData["like"] + 1
tweet.update({"id": tweet_id, "like": newLike}, ["id"])
return {"result": "POST"}
except:
return {"result": "None"}
@app.post("/user/{user_id}")
def changeProfile(user_id: str, email: str, password: str, detail: str):
try:
userData = user.find_one(email=email)
password = hl.sha256(password.encode()).hexdigest()
if userData["password"] == password:
user.update({"userid": user_id, "detail": detail}, ["userid"])
return {"result": "POST"}
except:
return {"result": "None"}
@app.post("/delete/tweet/{user_id}/{tweet_id}")
def deleteTweet(user_id: str, tweet_id: int, email: str, password: str):
try:
userData = user.find_one(email=email)
password = hl.sha256(password.encode()).hexdigest()
if userData["password"] == password:
tweet.delete(userid=user_id, id=tweet_id)
return {"result": "POST"}
except:
return {"result": "None"}
@app.post("/delete/user/{user_id}")
def deleteUser(user_id: str, email: str, password: str):
try:
userData = user.find_one(email=email)
password = hl.sha256(password.encode()).hexdigest()
if userData["password"] == password:
tweet.delete(userid=user_id)
user.delete(userid=user_id)
return {"result": "POST"}
except:
return {"result": "None"}