-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
148 lines (104 loc) · 3.93 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
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
from flask import Flask, render_template , request , abort
import pickle
import requests
import os
import json
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.model_selection import train_test_split # FOR train_test_split function
from sklearn.metrics import confusion_matrix, accuracy_score
#print(twee.statuses_count,twee.followers_count,twee.friends_count,twee.favourites_count,twee.listed_count)
app=Flask(__name__)
# To set your enviornment variables in your terminal run the following line:
# export 'BEARER_TOKEN'='<your_bearer_token>'
os.environ['TOKEN'] = 'AAAAAAAAAAAAAAAAAAAAAAU9UgEAAAAA4nxUc8ytyli45TIiDGEduvGLXLM%3DydT6bOgTeAy7mhxGyq2nyfQPyEIdpXqVvFmA1aM5vO3xbRPEVH'
bearer_token = os.environ.get('TOKEN')
def bearer_oauth(r):
"""
Method required by bearer token authentication.
"""
r.headers["Authorization"] = f"Bearer {bearer_token}"
r.headers["User-Agent"] = "v2UserLookupPython"
return r
@app.route('/')
def index():
return render_template('index.html')
@app.errorhandler(404)
def page_not_found(e):
return render_template("404error.html"),404
@app.route('/', methods=['POST'])
def my_form_post():
global username
global auth
username = request.form['username']
search_url = "https://api.twitter.com/1.1/users/show.json?screen_name="+username
def connect_to_endpoint(url):
response = requests.request("GET", search_url, auth=bearer_oauth,)
if response.status_code != 200:
abort(404,description="Not found.")
return response.json()
url = search_url
json_response = connect_to_endpoint(url)
x=json.dumps(json_response, indent=4, sort_keys=True)
global y,screen_name
y=json.loads(x)
global statuses_count
statuses_count=int(y['statuses_count'])
global followers_count
followers_count=int(y['followers_count'])
global friends_count
friends_count=int(y['friends_count'])
global favourites_count
favourites_count=int(y['favourites_count'])
global listed_count
listed_count=int(y['listed_count'])
global full_name
full_name=y['name']+" "+y['screen_name']
screen_name=y['screen_name']
name=y['name']
def maxSubsequence(screen_name, name):
# find the length of the strings
global m,n
m = len(screen_name)
n = len(name)
# declaring the array for storing the dp values
global L
L = [[None]*(n + 1) for i in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0 :
L[i][j] = 0
elif screen_name[i-1] == name[j-1]:
L[i][j] = L[i-1][j-1]+1
else:
L[i][j] = max(L[i-1][j], L[i][j-1])
# L[m][n] contains the length of LCS of X[0..n-1] & Y[0..m-1]
return L[m][n]
print(statuses_count,followers_count,friends_count,favourites_count,listed_count)
def normaliseNameWeight(y):
name = y['name']
subseq = maxSubsequence(screen_name,name)
return (subseq/len(name))
name_wt=normaliseNameWeight(y)
arr = np.array([[name_wt,statuses_count,followers_count,friends_count,favourites_count,listed_count]])
global z
z=pd.DataFrame(arr)
print(z)
with open('pickleOutput', 'rb') as f:
mp = pickle.load(f)
pickleTest = mp.predict(z)
if pickleTest==0:
auth='Real'
print("The value of pickleTest is", auth)
print(pickleTest)
return render_template('index.html',res='real',value=screen_name)
else:
auth='Fake'
print("The value of pickleTest is", auth)
return render_template('index.html',res='fake',value=screen_name)
@app.route('/predict')
def predict():
return "<h1>{{pickleTest}}</h1>"
if __name__ == "__main__":
app.run(debug=True)