1414from builtins import *
1515from html .parser import HTMLParser
1616
17- USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'
18-
19- def urlopen_ua (url , data = None ):
20- return urlopen (Request (url , data = data , headers = {'User-Agent' : USER_AGENT }), timeout = 5 )
21-
22- def req (url , data = None ):
17+ USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.3'
18+
19+ def urlopen_ua (url , data = None , content_type = None ):
20+ headers = {'User-Agent' : USER_AGENT }
21+ if content_type is not None :
22+ headers ['Content-Type' ] = content_type
23+ return urlopen (Request (url , data = data , headers = headers ), timeout = 15 )
24+
25+ def req (url , data = None , content_type = None ):
26+ if isinstance (data , dict ):
27+ data = json .dumps (data )
28+ if content_type is None :
29+ content_type = 'application/json'
2330 body = data .encode () if data else None
24- return urlopen_ua (url , data = body ).read ().decode ()
31+ return urlopen_ua (url , data = body , content_type = content_type ).read ().decode ()
2532
2633class Band :
2734 def __init__ (self , band_id = None , band_name = "" , band_img = None ):
@@ -127,8 +134,7 @@ def get_subgenres(self):
127134 def get_collection (self , fan_id , count = 1000 , return_albums = False ):
128135 url = "https://bandcamp.com/api/fancollection/1/collection_items"
129136 token = self ._get_token ()
130- body = '{{"fan_id": "{fan_id}", "older_than_token": "{token}", "count":"{count}"}}' \
131- .format (fan_id = fan_id , token = token , count = count )
137+ body = {"fan_id" : fan_id , "older_than_token" : token , "count" : count }
132138 x = req (url , data = body )
133139 items = json .loads (x )['items' ]
134140 bands = {}
@@ -150,8 +156,7 @@ def get_collection(self, fan_id, count=1000, return_albums=False):
150156 def get_wishlist (self , fan_id , count = 1000 , return_albums = False ):
151157 url = "https://bandcamp.com/api/fancollection/1/wishlist_items"
152158 token = self ._get_token ()
153- body = '{{"fan_id": "{fan_id}", "older_than_token": "{token}", "count":"{count}"}}' \
154- .format (fan_id = fan_id , token = token , count = count )
159+ body = {"fan_id" : fan_id , "older_than_token" : token , "count" : count }
155160 x = req (url , data = body )
156161 items = json .loads (x )['items' ]
157162 bands = {}
@@ -224,7 +229,7 @@ def get_album_by_url(self, url):
224229
225230 def get_band (self , band_id ):
226231 url = "https://bandcamp.com/api/mobile/24/band_details"
227- body = '{{ "band_id": "{ band_id}"}}' . format ( band_id = band_id )
232+ body = { "band_id" : band_id }
228233 request = req (url , data = body )
229234 band_details = json .loads (request )
230235 band = Band (band_id = band_details ['id' ], band_name = band_details ['name' ],
@@ -239,8 +244,9 @@ def get_band(self, band_id):
239244 def search (self , query ):
240245 if PY2 :
241246 query = query .decode ('utf-8' )
242- url = "https://bandcamp.com/api/fuzzysearch/1/autocomplete?q={query}" .format (query = quote_plus (query ))
243- request = req (url )
247+ url = "https://bandcamp.com/api/bcsearch_public_api/1/autocomplete_elastic"
248+ body = {"full_page" : False , "search_filter" : "" , "search_text" : query }
249+ request = req (url , data = body )
244250 results = json .loads (request )['auto' ]['results' ]
245251 items = []
246252 for result in results :
0 commit comments