-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpdfDownloading.py
More file actions
73 lines (57 loc) · 2.33 KB
/
Copy pathpdfDownloading.py
File metadata and controls
73 lines (57 loc) · 2.33 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
import PyPDF2
import requests
import time
import io
s = requests.Session()
user_agent = {'User-agent': 'Mozilla/5.0'}
PDF_LINKS = ["http://file.allitebooks.com/20181202/Amazon%20Web%20Services%20in%20Action,%202nd%20Edition.pdf","http://file.allitebooks.com/20190212/Android%203.0%20Application%20Development%20Cookbook.pdf","http://file.allitebooks.com/20190222/PostGIS%20Cookbook.pdf","http://file.allitebooks.com/20190222/Beginning%20REALbasic.pdf","http://file.allitebooks.com/20190222/DNS%20in%20Action.pdf"]
PDF_NAMES = ["Amazon Web Services in Action, 2nd Edition","Another name","PostGIS Cookbook","Beginning REALbasic","DNS in Action"]
NAME_LIST = [] # names of the pdfs
SIZE_LIST = [] # size of words inside pdf
def download_file(pdf_link):
# print("request time")
# start = time.time()
req = s.get(pdf_link,headers=user_agent)
# end = time.time()
# print(end - start)
return req
def get_number_of_lines(pdf_info):
# This function takes as input a pdf and returns the number of lines found in that pdf
number_of_lines = 0
# cProfile.run('re.compile("get_number_of_lines|pdg_info")')
print("time to open pdf")
start = time.time()
pdf_info = PyPDF2.PdfFileReader(pdf_info)
end = time.time()
print(end-start)
print("time to execute tast")
start = time.time()
for page in range(pdf_info.getNumPages()):
file_info = pdf_info.getPage(page)
content = file_info.extractText()
count_escape_char = content.count('\n')
number_of_lines = number_of_lines + count_escape_char
end = time.time()
print(end - start)
return number_of_lines
if __name__ == '__main__':
print("App starts")
app_start = time.time()
index = 0
for link in PDF_LINKS:
with io.BytesIO(download_file(link).content) as response:
SIZE_LIST.append(get_number_of_lines(response))
NAME_LIST.append(PDF_NAMES[index])
index += 1
print(SIZE_LIST)
print(NAME_LIST)
SIZE_LIST, NAME_LIST = (list(t) for t in zip(*sorted(zip(SIZE_LIST, NAME_LIST))))
SIZE_LIST.reverse()
NAME_LIST.reverse()
print(SIZE_LIST)
print(NAME_LIST)
end = time.time()
print("app ends")
app_end = time.time()
print(app_end-app_start)
print(SIZE_LIST)