-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
232 lines (222 loc) · 10.2 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from flask import Flask, render_template, request, redirect, url_for, flash
from googlesearch import search
import PyPDF2
import os
from werkzeug.utils import secure_filename
path = os.getcwd()
UPLOAD_FOLDER = os.path.join(path, 'uploads')
ALLOWED_EXTENSIONS = {'pdf'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.add_url_rule(
"/uploads/<name>", endpoint="download_file", build_only=True
)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1000 * 1000
app.config['SECRET_KEY'] = 'super secret key'
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if request.form['text'] != '' and request.files['file'].filename == '':
word = request.form['text']
masukan = "word"
with open('word.txt', 'w', encoding='utf-8') as f:
f.write(word)
return redirect(url_for('plagiarism', name=masukan) + "#hasil")
elif request.files['file'].filename != '' and request.form['text'] == '':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('plagiarism', name=filename) + "#hasil")
else:
flash('Please fill the form')
return redirect(request.url)
return render_template("index.html")
@app.route('/plagiarism/<name>', methods=['GET', 'POST'])
def plagiarism(name):
domain = "co.id"
link_output = []
hasil_plagiarism = []
hasil_link = []
hasil_persen = 0
inputan_mentah = ""
inputan = []
filename = ""
text = ""
hasil_plagiarism_final = []
hasil_link_final = []
link_blocked = ["id.linkedin.com", "linkedin.com", "youtube.com", "instagram.com", "facebook.com", "tokopedia.com",
"twitter.com", "reddit.com", "bukalapak.com", "shopee.com", "blibli.com"]
if request.method == 'POST':
if request.form['text'] != '' and request.files['file'].filename == '':
word = request.form['text']
filename += "word"
with open('word.txt', 'w', encoding='utf-8') as f:
f.write(word)
elif request.files['file'].filename != '' and request.form['text'] == '':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
pdfFileObj = open('uploads/{}'.format(filename), 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
num_pages = pdfReader.numPages
count = 0
while count < num_pages:
pageObj = pdfReader.getPage(count)
count += 1
text += pageObj.extractText()
else:
flash('Please fill the form')
return redirect(request.url)
if filename == "word":
if request.method == 'POST':
inputan_mentah += request.form['text']
else:
f = open("word.txt", "r")
inputan_mentah += f.read()
inputan += inputan_mentah.replace("\n", " ").split(". ")
for i in range(len(inputan)):
query = '"' + inputan[i].strip().replace(".", "").replace('"', "'") + '"'
for j in range(len(list(search(query, tld=domain, num=10, stop=10, pause=2)))):
if i != j:
continue
hasil_plagiarism.append(inputan[i])
hasil_link.append(list(search(query, tld=domain, num=10, stop=10, pause=2))[j])
for i in range(len(hasil_plagiarism)):
for j in range(len(hasil_link)):
if i != j:
continue
while True:
for k in range(len(link_blocked)):
if link_blocked[k] in hasil_link[j]:
break
else:
hasil_plagiarism_final.append(hasil_plagiarism[i])
hasil_link_final.append(hasil_link[j])
break
break
count = len(inputan)
count_hasil = len(hasil_link_final)
hasil_persen += (count_hasil / count) * 100
for i in range(len(hasil_link_final)):
link_output.append(hasil_link_final[i])
else:
inputan += text.replace("\n", " ").split(". ")
for i in range(len(inputan)):
query = '"' + inputan[i].strip().replace(".", "").replace('"', "'") + '"'
for j in range(len(list(search(query, tld=domain, num=10, stop=10, pause=2)))):
if i != j:
continue
hasil_plagiarism.append(inputan[i])
hasil_link.append(list(search(query, tld=domain, num=10, stop=10, pause=2))[j])
for i in range(len(hasil_plagiarism)):
for j in range(len(hasil_link)):
if i != j:
continue
while True:
for k in range(len(link_blocked)):
if link_blocked[k] in hasil_link[j]:
break
else:
hasil_plagiarism_final.append(hasil_plagiarism[i])
hasil_link_final.append(hasil_link[j])
break
break
count = len(inputan)
count_hasil = len(hasil_link_final)
hasil_persen += (count_hasil / count) * 100
for i in range(len(hasil_link_final)):
link_output.append(hasil_link_final[i])
else:
if name == "word":
if request.method == 'POST':
inputan_mentah += request.form['text']
else:
f = open("word.txt", "r")
inputan_mentah += f.read()
inputan += inputan_mentah.replace("\n", " ").split(". ")
for i in range(len(inputan)):
query = '"' + inputan[i].strip().replace(".", "").replace('"', "'") + '"'
for j in range(len(list(search(query, tld=domain, num=10, stop=10, pause=2)))):
if i != j:
continue
hasil_plagiarism.append(inputan[i])
hasil_link.append(list(search(query, tld=domain, num=10, stop=10, pause=2))[j])
for i in range(len(hasil_plagiarism)):
for j in range(len(hasil_link)):
if i != j:
continue
while True:
for k in range(len(link_blocked)):
if link_blocked[k] in hasil_link[j]:
break
else:
hasil_plagiarism_final.append(hasil_plagiarism[i])
hasil_link_final.append(hasil_link[j])
break
break
count = len(inputan)
count_hasil = len(hasil_link_final)
hasil_persen += (count_hasil / count) * 100
for i in range(len(hasil_link_final)):
link_output.append(hasil_link_final[i])
else:
pdfFileObj = open('uploads/{}'.format(name), 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
num_pages = pdfReader.numPages
count = 0
text = ""
while count < num_pages:
pageObj = pdfReader.getPage(count)
count += 1
text += pageObj.extractText()
inputan += text.replace("\n", " ").split(". ")
for i in range(len(inputan)):
query = '"' + inputan[i].strip().replace(".", "").replace('"', "'") + '"'
for j in range(len(list(search(query, tld=domain, num=10, stop=10, pause=2)))):
if i != j:
continue
hasil_plagiarism.append(inputan[i])
hasil_link.append(list(search(query, tld=domain, num=10, stop=10, pause=2))[j])
for i in range(len(hasil_plagiarism)):
for j in range(len(hasil_link)):
if i != j:
continue
while True:
for k in range(len(link_blocked)):
if link_blocked[k] in hasil_link[j]:
break
else:
hasil_plagiarism_final.append(hasil_plagiarism[i])
hasil_link_final.append(hasil_link[j])
break
break
count = len(inputan)
count_hasil = len(hasil_link_final)
hasil_persen += (count_hasil / count) * 100
for i in range(len(hasil_link_final)):
link_output.append(hasil_link_final[i])
return render_template("index.html", hasil_persen=hasil_persen, data=inputan, hasil_plagiarism=hasil_plagiarism_final,
link_output=link_output, hasil_link=hasil_link_final)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=False)