-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
121 lines (93 loc) · 3.57 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
from flask import Flask,render_template,request,jsonify,send_file
from flask import after_this_request
import requests
import random
import os
import io
import img2pdf
from bs4 import BeautifulSoup
import time
import re
from os import listdir
import shutil
from pdfrw import PdfReader, PdfWriter, PageMerge
from PyPDF2 import PdfFileReader, PdfFileWriter
app=Flask(__name__)
@app.route("/")
def home():
return render_template("home.html",title="home")
@app.route("/download",methods=['GET','POST'])
def download():
if request.method=="POST":
realurl=request.form['comics']
rannumber=random.randint(0,111)
directory=f"ilikecomix{rannumber}"
os.mkdir(directory)
try:
response=requests.get(realurl)
Soup=BeautifulSoup(response.text,"html.parser")
gather=Soup.findAll("a")
urls=[images["href"] for images in gather]
for url in urls:
filename = re.search(r'/([\w_-]+[.](jpg|gif|png))$', url)
if not filename:
print("Regex didn't match with the url: {0}".format(url))
continue
with open(directory+"/"+filename.group(1), 'wb') as f:
if 'http' not in url:
url = '{}{}'.format(site, url)
response = requests.get(url)
f.write(response.content)
except Exception as e:
print(e)
ran=random.randint(1,11111)
pdfname=f"hello{ran}.pdf"
try:
with open(pdfname,"wb") as f:
try:
imgs = []
for fname in os.listdir(directory):
if not fname.endswith(".png"):
continue
path = os.path.join(directory, fname)
if os.path.isdir(path):
continue
imgs.append(path)
f.write(img2pdf.convert(imgs))
except:
print("not jpg or jpeg")
try:
imgs = []
for fname in os.listdir(directory):
if not fname.endswith(".jpg"):
continue
path = os.path.join(directory, fname)
if os.path.isdir(path):
continue
imgs.append(path)
f.write(img2pdf.convert(imgs))
except:
print("not png or jpeg")
try:
imgs = []
for fname in os.listdir(directory):
if not fname.endswith(".jpeg"):
continue
path = os.path.join(directory, fname)
if os.path.isdir(path):
continue
imgs.append(path)
f.write(img2pdf.convert(imgs))
except:
print("not png or jpg")
except Exception as e:
print(e)
with open(pdfname,'rb') as file:
return_data = io.BytesIO(file.read())
return_data.seek(0)
shutil.rmtree(directory)
os.remove(pdfname)
ran=random.randint(0,111)
return send_file(return_data,as_attachment=True,mimetype='application/pdf',attachment_filename=f"ilikecomics{ran}.pdf")
if __name__=="__main__":
app.run(debug=True,host="192.168.1.203")