-
Notifications
You must be signed in to change notification settings - Fork 0
/
ig-users-ms.py
81 lines (64 loc) · 2.23 KB
/
ig-users-ms.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
import mysql.connector
import json
from flask import Flask, make_response, request
from flask_cors import CORS
import sys
app = Flask(__name__)
CORS(app)
@app.route("/save", methods=['POST'])
def save():
print("New request!", file=sys.stderr)
# connect to db
db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="ig_users"
)
cursor = db.cursor()
# get body data
body = request.data
parsed = json.loads(body)
id = parsed['id']
username = parsed['username']
fullName = parsed['fullName']
isPrivate = parsed['isPrivate']
isVerified = parsed['isVerified']
picId = parsed['picId']
picUrl = parsed['picUrl']
hasStory = parsed['hasStory']
originUsername = parsed['originUsername'] # the username of the original account
print("ID: " + str(id), file=sys.stderr)
print("Username: " + str(username), file=sys.stderr)
# check if exists
userExists = False
cursor.execute("SELECT * FROM users WHERE id = " + str(id) + " OR username = '" + str(username) + "'")
results = cursor.fetchall()
if len(results) > 0:
print("ID: " + str(id) + " already exists!", file=sys.stderr)
userExists = True
# insert into db
if not userExists:
print("ID: " + str(id) + " does not exist!", file=sys.stderr)
sql = "INSERT INTO users (id, username, fullName, isPrivate, isVerified, picId, picUrl, hasStory) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
val = (str(id), username, fullName, isPrivate, isVerified, picId, picUrl, hasStory)
cursor.execute(sql, val)
db.commit()
# check if exists on follows table
userExists = False
cursor.execute("SELECT * FROM follows WHERE username = '" + str(originUsername) + "' AND follows = '" + str(username) + "'")
results = cursor.fetchall()
if len(results) > 0:
print("Follow relationship already exists!", file=sys.stderr)
userExists = True
# insert into follows table
if not userExists:
print("Follow relationship does not exist!", file=sys.stderr)
sql = "INSERT INTO follows (username, follows) VALUES (%s, %s)"
val = (originUsername, username)
cursor.execute(sql, val)
db.commit()
response = make_response({}, 200)
return response
if __name__ == '__main__':
app.run(debug=True)