@@ -14,7 +14,7 @@ async def fetch(session, url):
14
14
15
15
async def download_image (session , url , path_to_file ):
16
16
try :
17
- print (f"Downloading image from URL: { url } " )
17
+ print (f"\t Downloading image from URL: { url } " )
18
18
async with session .get (url ) as response :
19
19
if response .status == 200 :
20
20
with open (path_to_file , 'wb' ) as f :
@@ -23,78 +23,91 @@ async def download_image(session, url, path_to_file):
23
23
if not chunk :
24
24
break
25
25
f .write (chunk )
26
- print (f"Image saved to: { path_to_file } " )
26
+ print (f"\t \t Image saved to: { path_to_file } " )
27
27
else :
28
- print (f"Failed to download { url } . Status code: { response .status } " )
28
+ print (f"\t \t Failed to download { url } . Status code: { response .status } " )
29
29
except Exception as e :
30
- print (f"An error occurred while downloading { url } : { e } " )
30
+ print (f"\t \t An error occurred while downloading { url } : { e } " )
31
31
32
32
async def parse_navbar (session , url , skins_dir ):
33
- print (f"Parsing navbar: { url } " )
33
+ print (f"\t Parsing navbar: { url } " )
34
34
html = await fetch (session , url )
35
35
soup = BeautifulSoup (html , 'lxml' )
36
36
37
37
navbar = soup .find ('nav' , class_ = 'main' )
38
- sections = navbar .find_all ('li' )[1 :- 1 ] # Skip the first and last item
38
+ sections = navbar .find_all ('li' )[:- 1 ] # Skip the last item
39
39
40
40
tasks = [parse_section (session , url , li .a ['href' ], skins_dir ) for li in sections ]
41
41
await asyncio .gather (* tasks )
42
42
43
+ async def get_num_pages (session , section_url ):
44
+ html = await fetch (session , section_url )
45
+ soup = BeautifulSoup (html , 'lxml' )
46
+
47
+ # Get the page counter
48
+ page_counter = soup .find ('span' , class_ = 'count' )
49
+ # Get the page counter's string
50
+ page_counter_span = page_counter .find ('span' )
51
+ page_counter_str = page_counter_span .text
52
+ # Get the start of the number representing the number of pages
53
+ page_count_start = page_counter_str .rfind (' ' ) + 1
54
+ # Return the number of pages
55
+ return int (page_counter_str [page_count_start :])
56
+
43
57
async def parse_section (session , base_url , section , skins_dir ):
44
58
section_dir = os .path .join (skins_dir , os .path .basename (section ))
45
59
safe_mkdir (section_dir )
46
60
47
61
section_url = base_url + section
48
- html = await fetch (session , section_url )
49
- soup = BeautifulSoup (html , 'lxml' )
50
-
51
- # Find all the skin blocks in the section.
52
- skin_blocks = soup .find_all ('div' , class_ = 'card' )
53
-
54
- # Loop over each skin block
55
- for block in skin_blocks :
56
- # Extract the relative URL of the skin
57
- skin = block .find ('a' )['href' ]
58
-
59
- # Create the section directories if they don't exist
60
- skinDir = section_dir + skin
61
- safe_mkdir (skinDir )
62
-
63
- # Get the URL to the skin
64
- skinURL = base_url + skin
65
- skinResult = await fetch (session , skinURL )
66
-
67
- # Get the name of the skin
68
- skinName = skinResult [skinResult .find ("<h2 class=\" card-title\" >" ) + 23 :]
69
- skinName = skinName [:skinName .find ('<' )]
70
-
71
- # Get the description for the skin
72
- skinDescription = skinResult [skinResult .find ("<p class=\" card-description\" >" ) + 28 :]
73
- skinDescription = skinDescription [:skinDescription .find ('<' )]
74
-
75
- # Create a text file containing the skin's name and description
76
- with open (skinDir + "/meta.txt" , 'w' ) as f :
77
- f .write (f"Name: { skinName } \n Description: { skinDescription } " )
78
-
79
- # Get the URL to the skin img
80
- skinImgURL = skinURL + "/download"
81
- path_to_file = skinDir + "/skin.png"
82
- await download_image (session , skinImgURL , path_to_file )
83
-
84
- # Create a text file containing the skin's name and description
85
- with open (os .path .join (skinDir , "meta.txt" ), 'w' ) as f :
86
- f .write (f"Name: { skinName } \n Description: { skinDescription } " )
87
-
88
- # Get the URL to the skin img
89
- skinImgURL = skinURL + "/download"
90
- path_to_file = os .path .join (skinDir , "skin.png" )
91
- await download_image (session , skinImgURL , path_to_file )
92
-
93
- # Pagination: Continue to the next page if a 'next' button is present.
94
- next_button = soup .find ('a' , string = 'Next' )
95
- if next_button and next_button .has_attr ('href' ):
96
- next_page_url = base_url + next_button ['href' ]
97
- await parse_section (session , base_url , next_page_url , skins_dir )
62
+ num_pages = await get_num_pages (session , section_url )
63
+
64
+ # Loop over all pages
65
+ for i in range (1 , num_pages + 1 ):
66
+ section_page_url = f"{ section_url } /{ i } "
67
+ html = await fetch (session , section_page_url )
68
+ soup = BeautifulSoup (html , 'lxml' )
69
+
70
+ # Find all the skin blocks in the section.
71
+ skin_blocks = soup .find_all ('div' , class_ = 'card' )
72
+
73
+ # Loop over each skin block
74
+ for block in skin_blocks :
75
+ # Extract the relative URL of the skin
76
+ skin = block .find ('a' )['href' ]
77
+
78
+ # Create the section directories if they don't exist
79
+ skinDir = section_dir + skin
80
+ safe_mkdir (skinDir )
81
+
82
+ # Get the URL to the skin
83
+ skinURL = base_url + skin
84
+ skinResult = await fetch (session , skinURL )
85
+
86
+ # Get the name of the skin
87
+ skinName = skinResult [skinResult .find ("<h2 class=\" card-title\" >" ) + 23 :]
88
+ skinName = skinName [:skinName .find ('<' )]
89
+
90
+ # Get the description for the skin
91
+ skinDescription = skinResult [skinResult .find ("<p class=\" card-description\" >" ) + 28 :]
92
+ skinDescription = skinDescription [:skinDescription .find ('<' )]
93
+
94
+ # Create a text file containing the skin's name and description
95
+ with open (skinDir + "/meta.txt" , 'w' ) as f :
96
+ f .write (f"Name: { skinName } \n Description: { skinDescription } " )
97
+
98
+ # Get the URL to the skin img
99
+ skinImgURL = skinURL + "/download"
100
+ path_to_file = skinDir + "/skin.png"
101
+ await download_image (session , skinImgURL , path_to_file )
102
+
103
+ # Create a text file containing the skin's name and description
104
+ with open (os .path .join (skinDir , "meta.txt" ), 'w' ) as f :
105
+ f .write (f"Name: { skinName } \n Description: { skinDescription } " )
106
+
107
+ # Get the URL to the skin img
108
+ skinImgURL = skinURL + "/download"
109
+ path_to_file = os .path .join (skinDir , "skin.png" )
110
+ await download_image (session , skinImgURL , path_to_file )
98
111
99
112
async def main ():
100
113
print ("Starting the script." )
@@ -110,4 +123,4 @@ async def main():
110
123
try :
111
124
asyncio .run (main ())
112
125
except Exception as e :
113
- print (f"An error occurred: { e } " )
126
+ print (f"An error occurred: { e } " )
0 commit comments