Skip to content

Commit 91552a9

Browse files
committed
Adding docstring and comments
1 parent 276e20d commit 91552a9

9 files changed

+79
-28
lines changed

Download.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ def __init__(self, file_name, path=''):
1313
self.file_to_set()
1414
self.path = path
1515

16-
"""
17-
Load links from file and set to Set()
18-
"""
19-
20-
def file_to_set(self):
16+
def file_to_set(self) -> object:
17+
"""
18+
Load links from file and set to Set()
19+
:return: object
20+
"""
2121
if not os.path.exists(self.file_name):
2222
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), self.file_name)
2323
with open(self.file_name, 'rt') as f:
2424
for line in f:
2525
self.links.add(line.replace('\n', ''))
2626
return sorted(self.links)
2727

28-
"""
29-
Start of the downloading
30-
"""
31-
32-
def start(self):
28+
def start(self) -> object:
29+
"""
30+
Start Downloading file
31+
:rtype: object
32+
"""
3333
for file in self.links:
3434
try:
3535
img = SaveFile.SaveFile(file, self.path)
@@ -39,11 +39,11 @@ def start(self):
3939
self.completed.add(file)
4040
self.set_to_file()
4141

42-
"""
43-
Update links txt file
44-
"""
45-
46-
def set_to_file(self):
42+
def set_to_file(self) -> object:
43+
"""
44+
Update links txt file
45+
:return : None
46+
"""
4747
remaining = self.links.difference(self.completed)
4848
with open(self.file_name, 'w') as f:
4949
if len(remaining) > 0:

ImgFinder.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ def __init__(self, page_url):
1212
self.path = urlres.path
1313
self.src = set()
1414
HTMLParser.__init__(self)
15-
"""
16-
This function called by HTMLParser internally. We modify it to make our work
17-
"""
15+
1816
def handle_starttag(self, tag, attrs):
17+
"""
18+
This function called by HTMLParser internally. We modify it to make our work
19+
20+
:rtype: object
21+
"""
1922
if tag == 'img':
2023
for (attr, value) in attrs:
2124
if attr == 'src':
@@ -24,19 +27,35 @@ def handle_starttag(self, tag, attrs):
2427
else:
2528
continue
2629

27-
def getSrc(self):
30+
def getSrc(self) -> object:
31+
"""
32+
List of image link as set
33+
:return: object
34+
"""
2835
return self.src
2936

30-
def get_base_url(self):
37+
def get_base_url(self) -> object:
38+
"""
39+
Return Base url of the page. Like www.example.com/home.php will be www.example.com
40+
:rtype: object
41+
"""
3142
return self.base_url
3243

33-
def save_to_file(self):
34-
file_name = self.folder_path()+ self.path + '.txt'
44+
def save_to_file(self) -> object:
45+
"""
46+
Save waiting downloadable image to queue. So next time when program run
47+
:rtype: object
48+
"""
49+
file_name = self.folder_path() + self.path + '.txt'
3550
with open(file_name, 'w') as f:
3651
for line in sorted(self.src):
3752
f.write(line + '\n')
3853

3954
return file_name
4055

41-
def folder_path(self):
56+
def folder_path(self) -> object:
57+
"""
58+
Path to folder resource where images and others file will be saved for current domain relative to current folder
59+
:return: object
60+
"""
4261
return "storage/" + self.folder

SaveFile.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@
44

55
class SaveFile:
66
def __init__(self, file_name, path=''):
7+
"""
8+
SaveFile Constructor
9+
:param file_name:
10+
:param path
11+
:rtype: object
12+
"""
713
self.file_name = file_name
814

915
self.base_name = os.path.basename(urllib.request.urlparse(file_name).path)
1016
self.path = path
1117

12-
def save(self):
18+
def save(self) -> object:
19+
"""
20+
Download from web and save it to local folder
21+
:rtype: object
22+
"""
1323
try:
1424
if len(self.path) > 0:
1525
full_file_path = self.path + '/' + self.base_name

__pycache__/Download.cpython-36.pyc

246 Bytes
Binary file not shown.

__pycache__/ImgFinder.cpython-36.pyc

624 Bytes
Binary file not shown.

__pycache__/SaveFile.cpython-36.pyc

221 Bytes
Binary file not shown.

__pycache__/functions.cpython-36.pyc

452 Bytes
Binary file not shown.

functions.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
import os.path
55

66

7-
def gather_img_src(page_url):
7+
def gather_img_src(page_url) -> object:
8+
"""
9+
Parse Html and find img source and return as set
10+
:param page_url:
11+
:return: object
12+
"""
813
try:
914
html = html_string(page_url)
1015
finder = ImgFinder.ImgFinder(page_url)
@@ -16,13 +21,23 @@ def gather_img_src(page_url):
1621
return finder.getSrc()
1722

1823

19-
def create_project_folder(page_url):
24+
def create_project_folder(page_url: object) -> object:
25+
"""
26+
Create a project folder
27+
:param page_url:
28+
:rtype: object
29+
"""
2030
base_url = get_folder_name(urllib.parse.urlparse(page_url).netloc)
2131
if not os.path.exists("storage/" + base_url):
2232
os.makedirs("storage/" + base_url)
2333

2434

25-
def html_string(page_url):
35+
def html_string(page_url: object) -> object:
36+
"""
37+
Fetch html from url and return as Html String
38+
:param page_url:
39+
:rtype: object
40+
"""
2641
html_string = ''
2742
try:
2843
response = urllib.request.urlopen(page_url)
@@ -35,7 +50,12 @@ def html_string(page_url):
3550
return html_string
3651

3752

38-
def get_folder_name(base_url):
53+
def get_folder_name(base_url) -> object:
54+
"""
55+
Get hostname from a base url like www.example.com will return example
56+
:param base_url:
57+
:rtype: object
58+
"""
3959
parts = base_url.split(".")
4060
if len(parts) == 3:
4161
return parts[1]

main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
if __name__ == '__main__':
88
PAGE_URL = 'http://www.fdfashionbd.com/gallarey'
9+
# Create the project folder into storage folder
910
create_project_folder(PAGE_URL)
11+
# Find images source and save it to project folder
1012
finder = ImgFinder.ImgFinder(PAGE_URL)
1113
finder.feed(html_string(PAGE_URL))
1214
file_name = finder.save_to_file()

0 commit comments

Comments
 (0)