-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleDownloader.py
39 lines (30 loc) · 1.24 KB
/
SimpleDownloader.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
from multiprocessing import Semaphore
from multiprocessing.process import Process
from urllib.parse import urljoin, quote
from urllib.request import urlopen
from bs4 import BeautifulSoup
from DownloadProcess import DownloadProcess
__author__ = 'Maxi'
accepted_files = ('.pdf', '.class', '.java', '.jar', '.zip', '.doc')
def validDocument(link):
href = link.get('href')
return (href is not None) and href.endswith(accepted_files)
class SimpleDownloader(Process):
def __init__(self, url, username, password, encoding = 'utf-8'):
super(SimpleDownloader,self).__init__()
self.url = url
self.username = username
self.password = password
self.encoding = encoding
self.downloadSemaphore = Semaphore(4)
def run(self):
raw_request = urlopen(self.url)
dat = raw_request.read().decode(self.encoding)
soup = BeautifulSoup(dat)
for link in soup.find_all('a'):
if validDocument(link):
DownloadProcess(self.generateUrl(link), self.username, self.password, self.downloadSemaphore).start()
def generateUrl(self,link):
return str(quote(urljoin(self.url, link.get('href')), safe="%/:=&?~#+!$,;'@()*[]"))
def getLinks(self):
return None