33
44from py1337x import models
55
6+
67def torrent_parser (response : Response , base_url : str , page : int = 1 ) -> models .TorrentResult :
78 """
89 Parse the response from a torrent result page.
@@ -15,22 +16,22 @@ def torrent_parser(response: Response, base_url: str, page: int = 1) -> models.T
1516 Returns:
1617 The parsed search results.
1718 """
18- soup = BeautifulSoup (response .content , ' html.parser' )
19+ soup = BeautifulSoup (response .content , " html.parser" )
1920
2021 torrent_list = soup .select ('a[href*="/torrent/"]' )
21- seeders_list = soup .select (' td.coll-2' )
22- leechers_list = soup .select (' td.coll-3' )
23- size_list = soup .select (' td.coll-4' )
24- time_list = soup .select (' td.coll-date' )
25- uploader_list = soup .select (' td.coll-5' )
22+ seeders_list = soup .select (" td.coll-2" )
23+ leechers_list = soup .select (" td.coll-3" )
24+ size_list = soup .select (" td.coll-4" )
25+ time_list = soup .select (" td.coll-date" )
26+ uploader_list = soup .select (" td.coll-5" )
2627
27- last_page = soup .find (' div' , {' class' : ' pagination' })
28+ last_page = soup .find (" div" , {" class" : " pagination" })
2829
2930 if not last_page :
3031 page_count = page
3132 else :
3233 try :
33- page_count = int (last_page .findAll ('a' )[- 1 ][' href' ].split ('/' )[- 2 ])
34+ page_count = int (last_page .findAll ("a" )[- 1 ][" href" ].split ("/" )[- 2 ])
3435 except Exception :
3536 page_count = page
3637
@@ -39,34 +40,34 @@ def torrent_parser(response: Response, base_url: str, page: int = 1) -> models.T
3940 if torrent_list :
4041 for count , torrent in enumerate (torrent_list ):
4142 name = torrent .getText ().strip ()
42- torrent_id = torrent [' href' ].split ('/' )[2 ]
43- link = base_url + torrent [' href' ]
43+ torrent_id = torrent [" href" ].split ("/" )[2 ]
44+ link = base_url + torrent [" href" ]
4445 seeders = seeders_list [count ].getText ()
4546 leechers = leechers_list [count ].getText ()
4647 size = size_list [count ].contents [0 ]
4748 time = time_list [count ].getText ()
4849 uploader = uploader_list [count ].getText ().strip ()
49- uploader_link = base_url + '/' + uploader + '/'
50-
51- items .append (models .TorrentItem (
52- name = name ,
53- torrent_id = torrent_id ,
54- url = link ,
55- seeders = seeders ,
56- leechers = leechers ,
57- size = size ,
58- time = time ,
59- uploader = uploader ,
60- uploader_link = uploader_link
61- ))
50+ uploader_link = base_url + "/" + uploader + "/"
51+
52+ items .append (
53+ models .TorrentItem (
54+ name = name ,
55+ torrent_id = torrent_id ,
56+ url = link ,
57+ seeders = seeders ,
58+ leechers = leechers ,
59+ size = size ,
60+ time = time ,
61+ uploader = uploader ,
62+ uploader_link = uploader_link ,
63+ )
64+ )
6265
6366 return models .TorrentResult (
64- items = items ,
65- current_page = page or 1 ,
66- item_count = len (torrent_list ),
67- page_count = page_count
67+ items = items , current_page = page or 1 , item_count = len (torrent_list ), page_count = page_count
6868 )
6969
70+
7071def info_parser (response : Response , base_url : str ) -> models .TorrentInfo :
7172 """
7273 Parse the response from a torrent information page.
@@ -78,58 +79,60 @@ def info_parser(response: Response, base_url: str) -> models.TorrentInfo:
7879 Returns:
7980 The parsed torrent information.
8081 """
81- soup = BeautifulSoup (response .content , ' html.parser' )
82+ soup = BeautifulSoup (response .content , " html.parser" )
8283
83- name = soup .find (' div' , {' class' : ' box-info-heading clearfix' })
84+ name = soup .find (" div" , {" class" : " box-info-heading clearfix" })
8485 name = name .text .strip () if name else None
8586
86- short_name = soup .find (' div' , {' class' : ' torrent-detail-info' })
87- short_name = short_name .find ('h3' ).getText ().strip () if short_name else None
87+ short_name = soup .find (" div" , {" class" : " torrent-detail-info" })
88+ short_name = short_name .find ("h3" ).getText ().strip () if short_name else None
8889
89- description = soup .find (' div' , {' class' : ' torrent-detail-info' })
90- description = description .find ('p' ).getText ().strip () if description else None
90+ description = soup .find (" div" , {" class" : " torrent-detail-info" })
91+ description = description .find ("p" ).getText ().strip () if description else None
9192
92- genre = soup .find (' div' , {' class' : ' torrent-category clearfix' })
93- genre = [i .text .strip () for i in genre .find_all (' span' )] if genre else None
93+ genre = soup .find (" div" , {" class" : " torrent-category clearfix" })
94+ genre = [i .text .strip () for i in genre .find_all (" span" )] if genre else None
9495
95- thumbnail = soup .find (' div' , {' class' : ' torrent-image' })
96- thumbnail = thumbnail .find (' img' )[ ' src' ] if thumbnail else None
96+ thumbnail = soup .find (" div" , {" class" : " torrent-image" })
97+ thumbnail = thumbnail .find (" img" )[ " src" ] if thumbnail else None
9798
98- if thumbnail and not thumbnail .startswith (' http' ):
99- if thumbnail .startswith ('//' ):
100- thumbnail = ' https:' + thumbnail
99+ if thumbnail and not thumbnail .startswith (" http" ):
100+ if thumbnail .startswith ("//" ):
101+ thumbnail = " https:" + thumbnail
101102 else :
102103 thumbnail = base_url + thumbnail
103104
104105 magnet_link = soup .select ('a[href^="magnet"]' )
105- magnet_link = magnet_link [0 ][' href' ] if magnet_link else None
106+ magnet_link = magnet_link [0 ][" href" ] if magnet_link else None
106107
107- info_hash = soup .find (' div' , {' class' : ' infohash-box' })
108- info_hash = info_hash .find (' span' ).getText () if info_hash else None
108+ info_hash = soup .find (" div" , {" class" : " infohash-box" })
109+ info_hash = info_hash .find (" span" ).getText () if info_hash else None
109110
110- images = soup .find (' div' , {' class' : ' tab-pane active' })
111- images = [i [' src' ] for i in images .find_all (' img' )] if images else None
111+ images = soup .find (" div" , {" class" : " tab-pane active" })
112+ images = [i [" src" ] for i in images .find_all (" img" )] if images else None
112113
113- description_list = soup .find_all ('ul' , {' class' : ' list' })
114+ description_list = soup .find_all ("ul" , {" class" : " list" })
114115
115116 if len (description_list ) > 2 :
116- first_list = description_list [1 ].find_all ('li' )
117- second_list = description_list [2 ].find_all ('li' )
118-
119- category = first_list [0 ].find (' span' ).getText ()
120- species = first_list [1 ].find (' span' ).getText ()
121- language = first_list [2 ].find (' span' ).getText ()
122- size = first_list [3 ].find (' span' ).getText ()
123- uploader = first_list [4 ].find (' span' ).getText ().strip ()
124- uploader_link = base_url + '/' + uploader + '/'
125-
126- downloads = second_list [0 ].find (' span' ).getText ()
127- last_checked = second_list [1 ].find (' span' ).getText ()
128- date_uploaded = second_list [2 ].find (' span' ).getText ()
129- seeders = second_list [3 ].find (' span' ).getText ()
130- leechers = second_list [4 ].find (' span' ).getText ()
117+ first_list = description_list [1 ].find_all ("li" )
118+ second_list = description_list [2 ].find_all ("li" )
119+
120+ category = first_list [0 ].find (" span" ).getText ()
121+ species = first_list [1 ].find (" span" ).getText ()
122+ language = first_list [2 ].find (" span" ).getText ()
123+ size = first_list [3 ].find (" span" ).getText ()
124+ uploader = first_list [4 ].find (" span" ).getText ().strip ()
125+ uploader_link = base_url + "/" + uploader + "/"
126+
127+ downloads = second_list [0 ].find (" span" ).getText ()
128+ last_checked = second_list [1 ].find (" span" ).getText ()
129+ date_uploaded = second_list [2 ].find (" span" ).getText ()
130+ seeders = second_list [3 ].find (" span" ).getText ()
131+ leechers = second_list [4 ].find (" span" ).getText ()
131132 else :
132- category = species = language = size = uploader = uploader_link = downloads = last_checked = date_uploaded = seeders = leechers = None
133+ category = species = language = size = uploader = uploader_link = downloads = last_checked = date_uploaded = (
134+ seeders
135+ ) = leechers = None
133136
134137 return models .TorrentInfo (
135138 name = name ,
@@ -150,5 +153,5 @@ def info_parser(response: Response, base_url: str) -> models.TorrentInfo:
150153 seeders = seeders ,
151154 leechers = leechers ,
152155 magnet_link = magnet_link ,
153- info_hash = info_hash .strip () if info_hash else None
156+ info_hash = info_hash .strip () if info_hash else None ,
154157 )
0 commit comments