-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapplication.py
More file actions
152 lines (127 loc) · 4.27 KB
/
application.py
File metadata and controls
152 lines (127 loc) · 4.27 KB
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
from flask import Flask, render_template, request, redirect, make_response
from flask import send_file
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from flask_bootstrap import Bootstrap
from fpdf import FPDF
from PIL import Image, ImageChops
import string
import os
import platform
import time
import random
# EB looks for an 'application' callable by default.
application = Flask(__name__)
bootstrap = Bootstrap(application)
def trim(im):
bg = Image.new(im.mode, im.size, im.getpixel((50,50)))
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -10)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
@application.route('/savepdf', methods = ['POST'])
def savepdf(url="", emailad="", emailpass=""):
# Check if it exists
url = request.form['url'].encode("ascii")
emailad = request.form['emailad'].encode("ascii")
emailpass = request.form['emailpass'].encode("ascii")
loc = url.rfind('/')
idname = url[24+1:]+'.pdf'
chrome_options = Options()
chrome_options.add_argument("--headless")
if platform.system() == "Darwin":
browser = webdriver.Chrome(r'./chromedriver')
else:
browser = webdriver.Chrome(r'./chromedriver_linux', chrome_options=chrome_options)
loc = str.find(url,"view")
ID = url[loc+5:]
browser.get(url)
time.sleep(2)
try:
element = WebDriverWait(browser, 4).until(
EC.presence_of_element_located((By.ID, "youtube-modal"))
)
except:
print("failed")
# Check if there's email input
# Check if there's password input
try:
email_ID = browser.find_element_by_name('visitor[email]')
email_ID.send_keys(emailad)
except:
print("no e-mail required")
time.sleep(1)
try:
pass_ID = browser.find_element_by_name('visitor[passcode]')
pass_ID.send_keys(emailpass)
except:
print("no password required")
time.sleep(1)
try:
email_ID.send_keys(Keys.TAB)
email_ID.send_keys(Keys.ENTER)
except:
print("ae")
exitflag = 0
browser.switch_to_active_element().send_keys(Keys.RIGHT)
while exitflag == 0:
#click right until no blank pagescheck until pages stabilizes
browser.switch_to_active_element().send_keys(Keys.RIGHT)
time.sleep(0.4+random.randint(1,100)/100)
pages = browser.find_elements_by_css_selector(".preso-view.page-view")
urls = []
for x in pages:
urls.append(x.get_attribute("src"))
if any("blank.gif" in s for s in urls):
exitflag = 0
else:
exitflag = 1
urls = []
for x in pages:
urls.append(x.get_attribute("src"))
c = 1
for x in urls:
browser.get(x)
browser.save_screenshot("AX"+str(c)+".png")
c = c+1
imagelist = []
for i in range(1,c):
imagelist.append("AX"+str(i)+".png")
time.sleep(10)
for img in imagelist:
im = Image.open(img)
im = trim(im)
im.save(img)
im = Image.open(imagelist[0])
wheight = im.size[0]
wwidth = im.size[1]
im.close()
pdf = FPDF("L","pt",[wwidth,wheight])
pdf.set_margins(0,0,0)
for image in imagelist:
pdf.add_page()
pdf.image(image,0,0)
for i in (imagelist):
os.remove(i)
cdir = os.getcwd()
browser.close()
# now serve the PDF
response = make_response(pdf.output(dest='S'))
response.headers.set('Content-Disposition', 'attachment', filename=ID + '.pdf')
response.headers.set('Content-Type', 'application/pdf')
return response
@application.route('/')
def hello_world():
return render_template('index.html')
# run the app.
if __name__ == "__main__":
# Setting debug to True enables debug output. This line should be
# removed before deploying a production app.
application.debug = True
application.run(host='0.0.0.0')