@@ -102,27 +102,24 @@ impl Api {
102102 unfiltered : Option < DownloadsMap > ,
103103 items : & ' a Vec < & ' a Item > ,
104104 album : Option < & String > ,
105- artist : Option < & String >
105+ artist : Option < & String > ,
106106 ) -> DownloadsMap {
107107 unfiltered
108108 . iter ( )
109109 . flatten ( )
110110 . filter_map ( |( id, url) | {
111- items. iter ( ) . find ( |v| & format ! ( "{}{}" , v. sale_item_type, v. sale_item_id) == id)
112- . filter ( |item| {
113- artist. is_none_or ( |v| item. band_name . eq_ignore_ascii_case ( v) )
114- } )
115- . filter ( |item| {
116- album. is_none_or ( |v| item. item_title . eq_ignore_ascii_case ( v) )
117- } )
111+ items
112+ . iter ( )
113+ . find ( |v| & format ! ( "{}{}" , v. sale_item_type, v. sale_item_id) == id)
114+ . filter ( |item| artist. is_none_or ( |v| item. band_name . eq_ignore_ascii_case ( v) ) )
115+ . filter ( |item| album. is_none_or ( |v| item. item_title . eq_ignore_ascii_case ( v) ) )
118116 . map ( |_| ( id. clone ( ) , url. clone ( ) ) )
119117 } )
120118 . collect :: < DownloadsMap > ( )
121119 }
122120
123- /// Scrape a user's Bandcamp page to find download urls
124- pub fn get_download_urls ( & self , name : & str , artist : Option < & String > , album : Option < & String > ) -> Result < BandcampPage , Box < dyn Error > > {
125- debug ! ( "`get_download_urls` for Bandcamp page '{name}'" ) ;
121+ fn download_fanpage_data ( & self , name : & str ) -> Result < ParsedFanpageData , Box < dyn Error > > {
122+ debug ! ( "`download_fanpage_data` for Bandcamp page '{name}'" ) ;
126123
127124 let body = self . request ( Method :: GET , & Self :: bc_path ( name) ) ?. text ( ) ?;
128125 let soup = Soup :: new ( & body) ;
@@ -138,7 +135,24 @@ impl Api {
138135 . expect ( "Failed to deserialise collection page data blob." ) ;
139136 debug ! ( "Successfully fetched Bandcamp page, and found + deserialised data blob" ) ;
140137
141- let items = fanpage_data. item_cache . collection . values ( ) . collect :: < Vec < & Item > > ( ) ;
138+ Ok ( fanpage_data)
139+ }
140+
141+ /// Scrape a user's Bandcamp page to find download urls
142+ pub fn get_download_urls (
143+ & self ,
144+ name : & str ,
145+ artist : Option < & String > ,
146+ album : Option < & String > ,
147+ ) -> Result < BandcampPage , Box < dyn Error > > {
148+ debug ! ( "`get_download_urls` for Bandcamp page '{name}'" ) ;
149+
150+ let fanpage_data = self . download_fanpage_data ( & name) ?;
151+ let items = fanpage_data
152+ . item_cache
153+ . collection
154+ . values ( )
155+ . collect :: < Vec < & Item > > ( ) ;
142156
143157 match fanpage_data. fan_data . is_own_page {
144158 Some ( true ) => ( ) ,
@@ -147,8 +161,12 @@ impl Api {
147161 ) ) ,
148162 }
149163
150- // TODO: make sure this exists
151- let mut collection = Self :: filter_download_map ( fanpage_data. collection_data . redownload_urls . clone ( ) , & items, album, artist) ;
164+ let mut collection = Self :: filter_download_map (
165+ fanpage_data. collection_data . redownload_urls . clone ( ) ,
166+ & items,
167+ album,
168+ artist,
169+ ) ;
152170
153171 let skip_hidden_items = true ;
154172 if skip_hidden_items {
@@ -163,7 +181,12 @@ impl Api {
163181 // This should never be `None` thanks to the comparison above.
164182 fanpage_data. collection_data. item_count. unwrap( )
165183 ) ;
166- let rest = self . get_rest_downloads_in_collection ( & fanpage_data, "collection_items" , album, artist) ?;
184+ let rest = self . get_rest_downloads_in_collection (
185+ & fanpage_data,
186+ "collection_items" ,
187+ album,
188+ artist,
189+ ) ?;
167190 collection. extend ( rest) ;
168191 }
169192
@@ -174,12 +197,15 @@ impl Api {
174197 "Too many in `hidden_data`, and we're told not to skip, so we need to paginate ({} total)" ,
175198 fanpage_data. hidden_data. item_count. unwrap( )
176199 ) ;
177- let rest = self . get_rest_downloads_in_collection ( & fanpage_data, "hidden_items" , album, artist) ?;
200+ let rest = self . get_rest_downloads_in_collection (
201+ & fanpage_data,
202+ "hidden_items" ,
203+ album,
204+ artist,
205+ ) ?;
178206 collection. extend ( rest) ;
179207 }
180208
181- // let title = soup.tag("title").find().unwrap().text();
182-
183209 debug ! ( "Successfully retrieved all download URLs" ) ;
184210 Ok ( BandcampPage {
185211 // page_name: title,
@@ -223,10 +249,10 @@ impl Api {
223249 . json :: < ParsedCollectionItems > ( ) ?;
224250
225251 let items = body. items . iter ( ) . by_ref ( ) . collect :: < Vec < _ > > ( ) ;
226- let redownload_urls = Self :: filter_download_map ( Some ( body. redownload_urls ) , & items, album, artist) ;
252+ let redownload_urls =
253+ Self :: filter_download_map ( Some ( body. redownload_urls ) , & items, album, artist) ;
227254 trace ! ( "Collected {} items" , redownload_urls. len( ) ) ;
228255
229-
230256 collection. extend ( redownload_urls) ;
231257 more_available = body. more_available ;
232258 last_token = body. last_token ;
0 commit comments