-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenignFreewareDownloader.py
186 lines (151 loc) · 6.55 KB
/
benignFreewareDownloader.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
__author__ = "Laurence Elliott"
from urllib.request import urlopen as uOpen
from bs4 import BeautifulSoup as soup
import re
import os
myUrl = "https://www.freewarefiles.com/search.php?categoryid=1&query=&boolean=exact"
# connecting to and downloading page
uClient = uOpen(myUrl)
page_html = uClient.read()
uClient.close()
# instatiating BeautifulSoup parsing of first page
page_soup = soup(page_html, "html.parser")
# gets page numbers from list above program listings
numPagesA = page_soup.findAll("li",{"class":"page-item"})
numPagesArr = []
for numPageA in numPagesA:
numPage = numPageA.findAll("a",{"class":"page-link"})[0]
try:
numPage = re.search('(?<=>)[0-9]+(?=<\/a>)',str(numPage)).group(0)
numPagesArr.append(numPage)
except:
pass
# the last of the list of page numbers is stored for reference as the last
# page of the search
maxPage = numPagesArr[-1]
print("Total pages: " + str(maxPage) + "\n")
# get next page link
nextA = page_soup.findAll("a",{"aria-label":"Next"})[0]
print(nextA["href"])
# get links to individual program download pages
downloadPageHeaders = page_soup.findAll("h3",{"class":"search-head"})
downloadPageLinks = []
for pageHeader in downloadPageHeaders:
pageHeader = pageHeader.findAll("a")[0]
downloadPageLink = pageHeader["href"]
print(downloadPageLink)
downloadPageLinks.append(downloadPageLink)
# main
if __name__ == "__main__":
myUrl = "https://www.freewarefiles.com/search.php?categoryid=1&query=&boolean=exact"
count = 1
# connecting to and downloading first page
uClient = uOpen(myUrl)
page_html = uClient.read()
uClient.close()
# instatiating BeautifulSoup parsing of first page
page_soup = soup(page_html, "html.parser")
# gets page numbers from list above program listings
numPagesA = page_soup.findAll("li", {"class": "page-item"})
numPagesArr = []
for numPageA in numPagesA:
numPage = numPageA.findAll("a", {"class": "page-link"})[0]
try:
numPage = re.search('(?<=>)[0-9]+(?=<\/a>)', str(numPage)).group(0)
numPagesArr.append(numPage)
except:
pass
# the last of the list of page numbers is stored for reference as the last
# page of the search
maxPage = int(numPagesArr[-1])
print("Total pages: " + str(maxPage) + "\n")
# get next page link
nextPage = page_soup.findAll("a", {"aria-label": "Next"})[0]["href"]
# get links to individual program download pages
downloadPageHeaders = page_soup.findAll("h3", {"class": "search-head"})
downloadPageLinks = []
for pageHeader in downloadPageHeaders:
pageHeader = pageHeader.findAll("a")[0]
downloadPageLink = pageHeader["href"]
print(downloadPageLink)
downloadPageLinks.append(downloadPageLink)
# load the page's linked download pages and download exe files
for dlPage in downloadPageLinks:
myUrl = dlPage
myUrl = myUrl.replace("_program_", "-Download-Page-")
# connecting to and downloading page
uClient = uOpen(myUrl)
page_html = uClient.read()
uClient.close()
# instatiating BeautifulSoup parsing of page
dlPageSoup = soup(page_html, "html.parser")
downLinks = dlPageSoup.findAll("a", {"class": "dwnlocations"})
for link in downLinks:
link = link["href"]
try:
file = uOpen(link)
if int(file.info()['Content-Length']) <= 27000000:
print(str(count) + ": " + link)
os.system("sudo wget -O /media/lozmund/de9f2ab8-20e0-4d32-82a0-9564591262d0/home/freewareBenignFiles/" + str(count) + " " + link + " --read-timeout=1")
count += 1
except:
pass
for pageNum in range(2,maxPage+1):
print("Page " + str(pageNum) + ": ")
myUrl = nextPage
# connecting to and downloading page
# last 8 characters of url is removed as it
# didn't seem to effect loading of page, and
# could not be parsed by 'urlopen' due to utf-8 encoding
myUrl = myUrl[:-8]
print("\n" + myUrl + "\n")
uClient = uOpen(myUrl)
page_html = uClient.read()
uClient.close()
# instatiating BeautifulSoup parsing of first page
page_soup = soup(page_html, "html.parser")
# get links to individual program download pages
downloadPageHeaders = page_soup.findAll("h3", {"class": "search-head"})
downloadPageLinks = []
for pageHeader in downloadPageHeaders:
pageHeader = pageHeader.findAll("a")[0]
downloadPageLink = pageHeader["href"]
print(downloadPageLink)
downloadPageLinks.append(downloadPageLink)
# load the page's linked download pages and download exe files
for dlPage in downloadPageLinks:
myUrl = dlPage
myUrl = myUrl.replace("_program_","-Download-Page-")
# connecting to and downloading page
uClient = uOpen(myUrl)
page_html = uClient.read()
uClient.close()
# instatiating BeautifulSoup parsing of page
dlPageSoup = soup(page_html, "html.parser")
downLinks = dlPageSoup.findAll("a", {"class": "dwnlocations"})
for link in downLinks:
link = link["href"]
try:
file = uOpen(link)
if int(file.info()['Content-Length']) <= 27000000:
print(str(count) + ": " + link)
os.system(
"sudo wget -O /media/lozmund/de9f2ab8-20e0-4d32-82a0-9564591262d0/home/freewareBenignFiles/" + str(
count) + " " + link + " --read-timeout=1")
count += 1
except:
pass
# downLinks = dlPageSoup.findAll("a",{"class":"dwnlocations"})
# for link in downLinks:
# link = link["href"]
# try:
# file = uOpen(link)
# print(file.info()['Content-Length'])
# if int(file.info()['Content-Length']) <= 27000000:
# print(str(count) + ": " + link)
# os.system("sudo wget -O ~/media/lozmund/de9f2ab8-20e0-4d32-82a0-9564591262d0/home/freewareBenignFiles/" + str(count) + " " + link)
# count += 1
# except:
# pass
# get next page link
nextPage = page_soup.findAll("a", {"aria-label": "Next"})[0]["href"]