@@ -238,13 +238,14 @@ def _get_generic_download(soup):
238238
239239
240240def _get_trlevel_download_info (trle_info ):
241- zip_file = data_factory . make_zip_file ()
241+ zip_file = []
242242 url = "https://www.trlevel.de/filebase/index.php?category-file-list/558-trle-custom-levels/"
243243 page_prefix = "&sortField=time&sortOrder=DESC&pageNo="
244244 number = 1
245245 max_pages = 20
246246
247247 while True :
248+
248249 trlevel_soup = scrape_common .get_soup (url + f"{ page_prefix } { number } " )
249250 if not trlevel_soup :
250251 print (f"Failed to fetch page { number } " )
@@ -253,16 +254,23 @@ def _get_trlevel_download_info(trle_info):
253254 card_links = trlevel_soup .find_all ("a" , class_ = "filebaseFileCardLink" )
254255 for link in card_links :
255256 if trle_info [0 ].lower () in link .get_text (strip = True ).lower ():
256- level = link ['href' ].split ("https://www.trlevel.de/filebase/index.php?file/" )[1 ]
257- level_id = level .split ('-' )[0 ]
258- download_url = f"https://www.trlevel.de/index.php?file-download/{ level_id } /"
257+ trlevel_level_page_soup = scrape_common .get_soup (link ['href' ])
258+ download_tag = trlevel_level_page_soup .find (
259+ "a" , class_ = "button buttonPrimary" , itemprop = "downloadUrl" )
260+
261+ if not isinstance (download_tag , Tag ):
262+ print ("Did not get download url from trlevel.de page" )
263+ sys .exit (1 )
264+
265+ download_url = download_tag .get ('href' )
259266 head = scrape_common .https .get (f"{ download_url } " , "head" )
260267 match = re .search (r'filename\*?=(?:UTF-8\'\')?["\']?([^"\']+)["\']?' , head )
268+ zip_file = data_factory .make_zip_file ()
261269 zip_file ['name' ] = match .group (1 ) if match else ''
262270 zip_file ['size' ] = trle_info [2 ]
263271 zip_file ['md5' ] = "MISSING"
264272 zip_file ['url' ] = download_url
265- break
273+ return [ zip_file ]
266274
267275 if len (card_links ) < 20 :
268276 break # Stop if less than 20 links (end of pages)
@@ -276,6 +284,7 @@ def _get_trlevel_download_info(trle_info):
276284
277285
278286def _search_trcustoms (trle_info ):
287+ zip_file = []
279288 title = quote (trle_info [0 ])
280289 release = trle_info [1 ]
281290 trcustoms = scrape_common .get_json (f"https://trcustoms.org/api/levels/?search={ title } " )
@@ -288,8 +297,8 @@ def _search_trcustoms(trle_info):
288297 # check if it has the files attribute
289298 last_file = level .get ('last_file' , {})
290299 if last_file :
291- return _get_trcustoms_download_info (level )
292- return []
300+ zip_file = [ _get_trcustoms_download_info (level )]
301+ return zip_file
293302
294303
295304def _check_lid (lid ):
@@ -300,6 +309,7 @@ def _check_lid(lid):
300309 logging .error ("Error: lid not a digit" )
301310 sys .exit (1 )
302311
312+
303313def _get_download_info (lid , url ):
304314 zip_file = data_factory .make_zip_file ()
305315 trle_info = _get_trle_info (lid )
@@ -314,39 +324,40 @@ def get_zip_file_info(lid):
314324 """Entery function for trle download module."""
315325 _check_lid (lid )
316326 head = scrape_common .https .get (f"https://www.trle.net/scadm/trle_dl.php?lid={ lid } " , 'head' )
327+ files = []
317328 if head :
318329 print (f"head: { head } " )
319330 redirect_url = head .partition ("Location: " )[2 ].split ("\r \n " , 1 )[0 ]
320331 print (f"Location: { redirect_url } " )
321332 if redirect_url :
322333 if redirect_url .endswith (".zip" ) and \
323334 redirect_url .startswith ("https://www.trle.net/levels/levels/" ):
324- return _get_download_info (lid , redirect_url )
335+ files = [ _get_download_info (lid , redirect_url )]
325336
326337 if redirect_url .startswith ("https://www.trle.net/sc/levelfeatures.php?lid=" ):
327338 trle_info = _get_trle_info (lid )
328- return _search_trcustoms (trle_info )
339+ files . extend ( _search_trcustoms (trle_info ) )
329340
330341 if redirect_url .lower ().endswith ("/btb/web/index.html" ) and \
331342 redirect_url .startswith ("https://www.trle.net/levels/levels" ):
332- return _get_trle_btb_download_info (redirect_url , lid )
343+ files = [ _get_trle_btb_download_info (redirect_url , lid )]
333344
334345 if redirect_url .endswith (".htm" ) and \
335346 redirect_url .startswith ("https://www.trle.net/levels/levels/" ):
336347 url = _get_generic_download (scrape_common .get_soup (redirect_url ))
337- return _get_download_info (lid , url )
348+ files = [ _get_download_info (lid , url )]
338349
339350 if redirect_url .startswith ("https://trcustoms.org/levels/" ) and \
340351 redirect_url .split ("/" )[- 1 ].isdigit ():
341352 api_url = f"https://trcustoms.org/api/levels/{ redirect_url .split ("/" )[- 1 ]} /"
342353 trcustoms_level_dict = scrape_common .get_json (api_url )
343- return _get_trcustoms_download_info (trcustoms_level_dict )
354+ files = [ _get_trcustoms_download_info (trcustoms_level_dict )]
344355
345356 if redirect_url == "https://www.trlevel.de" :
346357 trle_info = _get_trle_info (lid )
347- return _get_trlevel_download_info (trle_info )
358+ files . extend ( _get_trlevel_download_info (trle_info ) )
348359
349- return []
360+ return files
350361
351362
352363if __name__ == '__main__' :
@@ -357,7 +368,5 @@ def get_zip_file_info(lid):
357368 sys .exit (1 )
358369 else :
359370 LID = sys .argv [1 ]
360- if LID == "" :
361- LID = "1978"
362371 ZIP_DATA = get_zip_file_info (LID )
363372 print (f"{ ZIP_DATA } " )
0 commit comments