-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
420 lines (313 loc) · 14.1 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import json
from logging import exception
from pyexpat.errors import messages
import weakref
from flask import Flask, render_template, request, send_file, session, redirect, flash, send_from_directory, jsonify
import os
from text_to_audio_book import pdf_to_audio
from werkzeug.utils import secure_filename
from werkzeug.exceptions import RequestEntityTooLarge
from pdf_to_image_to_text import covert_pdf_to_image
from threading import Timer
import random
import shutil
# import redis
import time
# from apscheduler.schedulers.background import BackgroundScheduler
from multiprocessing import Process
from deletingmod import deletingdic
from onlypdftoimage import covert_pdffile_to_image
with open('app_config.json', 'r') as configData:
params = json.load(configData)["params"]
app = Flask(__name__)
# r = redis.Redis()
app.secret_key = 'super^-&web&-k&ey'
ALLOWED_EXTENSIONS = set(['pdf'])
app.config['PDF_UPLOAD_FOLDER'] = params["pdf_upload_folder_local"]
app.config['AUDIO_PATH'] = params["audio_upload_local"]
app.config['MAX_CONTENT_LENGTH'] = 300 * 1024 * 1024
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# app.app_context().push()
# def timeoutfunc(dname):
# print("stating deleting")
# shutil.rmtree(dname)
# dname = app.config['PDF_UPLOAD_FOLDER'] + session['dname']
# timeoutfunc(dname)
@app.route('/')
def home():
link = "/converter"
return render_template('home.html', link=link)
@app.route('/converter/<string:typeofcon>')
def converter(typeofcon):
try:
if(typeofcon=="pdftoaudio"):
message = "make sure your file is less then 300MB"
converter = "/uploader"
return render_template('converter.html', message=message, upurl = converter)
elif(typeofcon=="pdftotext"):
message = "make sure your file is less then 300MB"
converter = "/uploaderforimageandtext/pdftotext"
return render_template('converter.html', message=message, upurl = converter)
elif(typeofcon=="pdftoimage"):
title = "pdf to image"
urlcon = "pdftoimage"
message = "make sure your file is less then 300MB"
return render_template('pdftoimage.html', message=message, title=title, urlcon=urlcon)
else:
return redirect("/")
except Exception as error:
return redirect("/")
@app.route('/uploaderforimageandtext/<string:typeofcon>', methods = ['GET', 'POST'])
def uploaderforimagandtext(typeofcon):
if(request.method == 'POST'):
try:
if 'file1' not in request.files:
return redirect("/pdftoaudio/" + typeofcon)
# getting pdf from user
try:
f = request.files['file1']
start = request.form.get('start')
end = request.form.get('end')
pdflang = request.form.get('lagpann')
except Exception as geterror :
return redirect("/pdftoaudio/" + typeofcon)
# checking file error
if f.filename == '':
return redirect("/pdftoaudio/" + typeofcon)
filename = secure_filename(f.filename)
if f and allowed_file(filename):
#sending pdf to audio converting configration in session
try:
session['pdfname'] = str(filename)
session['start']= int(start)
session['end']= int(end)
session['pdflangu'] = pdflang
#creating folder for current pdf
randintnum = random.randint(1,1000)
directname = os.path.splitext(filename)[0] + str(randintnum)
session['dname'] = directname
makenewdirect = app.config['PDF_UPLOAD_FOLDER'] + directname
os.mkdir(makenewdirect)
fullpathofpdf = app.config['PDF_UPLOAD_FOLDER'] + directname + '\\'
#add path in session
session["pdfpath"] = fullpathofpdf
session["contype"] = typeofcon
#upload it on this path
f.save(os.path.join(app.config['PDF_UPLOAD_FOLDER'] + directname + '\\' + filename))
return redirect('/download')
except RequestEntityTooLarge as errorlog:
message = "your file size greater thrn 300MB we can't converte it"
redirect("/pdftoaudio/" + typeofcon)
else:
return redirect("/pdftoaudio/" + typeofcon)
except Exception as o:
return redirect("/pdftoaudio/" + typeofcon)
@app.route('/uploader', methods = ['GET', 'POST'])
def uploader():
if(request.method == 'POST'):
try:
if 'file1' not in request.files:
return redirect("/converter")
# getting pdf from user
try:
f = request.files['file1']
start = request.form.get('start')
end = request.form.get('end')
pdflang = request.form.get('lagpann')
except Exception as geterror :
return redirect('/converter')
# checking file error
if f.filename == '':
return redirect("/converter")
filename = secure_filename(f.filename)
if f and allowed_file(filename):
#sending pdf to audio converting configration in session
try:
session['pdfname'] = str(filename)
session['start']= int(start)
session['end']= int(end)
session['pdflangu'] = pdflang
#creating folder for current pdf
randintnum = random.randint(1,1000)
directname = os.path.splitext(filename)[0] + str(randintnum)
session['dname'] = directname
makenewdirect = app.config['PDF_UPLOAD_FOLDER'] + directname
os.mkdir(makenewdirect)
fullpathofpdf = app.config['PDF_UPLOAD_FOLDER'] + directname + '\\'
#add path in session
session["pdfpath"] = fullpathofpdf
#upload it on this path
f.save(os.path.join(app.config['PDF_UPLOAD_FOLDER'] + directname + '\\' + filename))
return redirect('/down')
except RequestEntityTooLarge as errorlog:
message = "your file size greater thrn 300MB we can't converte it"
return redirect('/converter', message=message)
else:
return redirect('/converter')
except Exception as o:
redirect('/converter')
@app.route('/downloadFile/<string:path>', methods = ['GET', 'POST'])
def downloadFile(path):
try:
#getting audio file for download
#file name form download path in session
file = session['audio_name']
audiopath = session["pdfpath"] + 'audio\\' + file
#connecting file path of audo file for download
directory = os.path.join(audiopath)
return send_file(directory, as_attachment=True)
except Exception as error:
return redirect('/')
@app.route('/downloadimageortext/<string:filename>', methods = ['GET', 'POST'])
def downloadtoimage(filename):
try:
#getting audio file for download
#file name form download path in session
filepath = session["pdfpath"] + 'image\\' + filename
#connecting file path of audo file for download
directory = os.path.join(filepath)
return send_file(directory, as_attachment=True)
except Exception as error:
return redirect('/')
@app.route('/downloadtotext/<string:filename>', methods = ['GET', 'POST'])
def downloadtotext(filename):
try:
#getting audio file for download
#file name form download path in session
filepath = session["pdfpath"] + 'text\\' + filename
#connecting file path of audo file for download
directory = os.path.join(filepath)
return send_file(directory, as_attachment=True)
except Exception as error:
return redirect('/')
@app.route('/download', methods = ['GET', 'POST'])
def downloadforrimgandtext():
try:
checkytype =session["contype"]
if(checkytype=="pdftoimage"):
try:
#pdf to audio convertion configration session form upload path
name = session['pdfname']
s = session['start']
e = session['end']
fullpath = session["pdfpath"]
#storing upload folder path for sending for converting pdf to audio
pdf_name = fullpath + name
#call audio to pdf function form pdf_to_audio_book.py
#give him to pdf name , start form page, end from page, and audio file storing path
dname = app.config['PDF_UPLOAD_FOLDER'] + session['dname']
imagefile = covert_pdffile_to_image(pdf_name, s, e, fullpath)
session['text_file'] = imagefile
dname = app.config['PDF_UPLOAD_FOLDER'] + session['dname']
begin = processClass(dname)
return render_template('downloadimgandtext.html', name=name, main_name = imagefile)
except Exception as a:
return redirect("/")
elif(checkytype=="pdftotext"):
try:
name = session['pdfname']
s = session['start']
e = session['end']
fullpath = session["pdfpath"]
pdflang = session['pdflangu']
pdflangcheck = ""
pdflangforaudio = ""
#condition for image lang
if(pdflang=="marathi" or pdflang=="hindi"):
pdflangcheck = "Devanagari"
elif(pdflang=="english"):
pdflangcheck = "eng"
else:
pdflangcheck = "eng"
#storing upload folder path for sending for converting pdf to audio
pdf_name = fullpath + name
#call audio to pdf function form pdf_to_audio_book.py
#give him to pdf name , start form page, end from page, and audio file storing path
dname = app.config['PDF_UPLOAD_FOLDER'] + session['dname']
textfile = covert_pdf_to_image(pdf_name, s, e, fullpath, pdflangcheck)
session['text_file'] = textfile
dname = app.config['PDF_UPLOAD_FOLDER'] + session['dname']
foldname = session['dname']
begin = processClass(dname)
#sending audio file name, file name to download page
return render_template('downloadtext.html', name=name, main_name = textfile)
except Exception as error:
return redirect("/")
except Exception as e:
return redirect("/")
@app.route('/down', methods = ['GET', 'POST'])
def download():
try:
#pdf to audio convertion configration session form upload path
name = session['pdfname']
s = session['start']
e = session['end']
fullpath = session["pdfpath"]
pdflang = session['pdflangu']
pdflangcheck = ""
pdflangforaudio = ""
#condition for image lang
if(pdflang=="marathi" or pdflang=="hindi"):
pdflangcheck = "Devanagari"
elif(pdflang=="english"):
pdflangcheck = "eng"
else:
pdflangcheck = "eng"
#condition for audio lang
if(pdflang=="hindi"):
pdflangforaudio = "hi"
elif(pdflang=="marathi"):
pdflangforaudio = "mr"
elif(pdflang=="english"):
pdflangforaudio = "en"
else:
pdflangforaudio = "en"
#storing upload folder path for sending for converting pdf to audio
pdf_name = fullpath + name
#call audio to pdf function form pdf_to_audio_book.py
#give him to pdf name , start form page, end from page, and audio file storing path
dname = app.config['PDF_UPLOAD_FOLDER'] + session['dname']
textfile = covert_pdf_to_image(pdf_name, s, e, fullpath, pdflangcheck)
session['text_file'] = textfile
main_name = pdf_to_audio(fullpath+ "text\\" + textfile, fullpath, dname, pdflangforaudio)
session['audio_name'] = main_name
dname = app.config['PDF_UPLOAD_FOLDER'] + session['dname']
foldname = session['dname']
begin = processClass(dname)
#sending audio file name, file name to download page
return render_template('download.html', name=name, main_name=main_name, foldername=foldname)
#if pdf is not converting to audio for error handling
except Exception as e:
return redirect("/")
@app.route('/offline.html')
def offline():
error = "your offline"
return render_template('error.html', error=error)
@app.route('/service-worker.js')
def sw():
return app.send_static_file('service-worker.js')
@app.route('/about')
def about():
return render_template('about.html')
#page not found error page
@app.errorhandler(404)
def page_not_found(e):
return render_template('errorfourzerofour.html')
@app.route('/PrivacyPolicy')
def PrivacyPolicy():
return render_template('PrivacyPolicy.html')
class processClass:
def __init__(self, path):
self.path = path
p = Process(target=self.run, args=())
p.daemon = True # Daemonize it
p.start() # Start the execution
def run(self):
#
# This might take several minutes to complete
time.sleep(60)#second
deletingdic(self.path)
#
if __name__ == "__main__":
app.run(debug=True, threaded=True)