-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
105 lines (92 loc) · 3.71 KB
/
app.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
# **************************************************************************** #
# #
# ::: :::::::: #
# app.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: msabwat <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2018/03/29 11:36:55 by msabwat #+# #+# #
# Updated: 2018/04/26 17:04:39 by msabwat ### ########.fr #
# #
# **************************************************************************** #
import requests
import re
from flask import Flask, request
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
fb_access_token="" ##Value to complete by creating a developper account on facebook or implementing OAUTH?
@app.route('/')
def hello():
return 'User Status challenge'
def get_byid(response, host, uid):
r = requests.get("https://graph.facebook.com/v2.12/" + uid + "?fields=link,name&access_token=" + fb_access_token)
if r.status_code == 200:
data = r.json()
response['account_handle'] = data['name']
response['account_url'] = data['link']
response['status'] = "up"
elif r.status_code == 190:
response['status'] = "Invalid token" #implement a refresh ?
else:
response['account_handle'] = 'Not Found'
response['account_url'] = 'Not Found'
response['status'] = "down"
return response
def get_byhandle(response, host, uid):
r = requests.get("https://facebook.com/" + uid)
data = str(r)
r = re.search('fb:\/\/profile\/(.+?)', data)
if 'profile' in str(r):
response['account_id'] = str(r)
response['account_url'] = "https://facebook.com/"+ uid
response['status'] = "up"
else:
r = re.search('\/help\/\?ref\=404', data)
if '404' in str(r):
response['account_id'] = 'Not found'
response['account_url'] = 'Not found'
response['status'] = "down"
else:
response['status'] = "Unknown error"
return response
def get_byurl(response, host, uid):
response['account_id'] = ''
response['account_handle'] = ''
response['status'] = ''
return response
def handle_request(host, uid, uid_type):
response = {}
response['hostingparty'] = host
if 'id' in uid_type:
response['account_id'] = uid
response = get_byid(response, host, uid)
elif 'handle' in uid_type:
response['account_handle'] = uid
response = get_byhandle(response, host, uid)
elif 'url' in uid_type:
response['account_url'] = uid
response = get_byurl(response, host, uid)
return response
def handle_input(host, user_id):
if 'id=' in user_id:
user_id = user_id.split('=', 1)
uid = user_id[1]
id_type = user_id[0]
elif 'handle=' in user_id:
user_id = user_id.split('=', 1)
uid = user_id[1]
id_type = user_id[0]
elif 'url=' in user_id:
user_id = user_id.split('=', 1)
uid = user_id[1]
id_type = user_id[0]
else:
return 'Error, invalid request'
return handle_request(host, uid, id_type)
class UserStatus(Resource):
def get(self, host, user_id):
return handle_input(host, user_id)
api.add_resource(UserStatus, '/user_status/<host>/<user_id>')
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=80)