@@ -44,6 +44,7 @@ class SpotifyArtistGraph(Graph):
44
44
def __init__ (self ,
45
45
client_id : str ,
46
46
client_key : str ,
47
+ neighbor_count : int = 6 ,
47
48
database : Optional [GraphDatabaseInterface ] = None ):
48
49
"""
49
50
Initialization.
@@ -53,17 +54,22 @@ def __init__(self,
53
54
Arguments:
54
55
client_id (str): The Spotify API client ID to use.
55
56
client_key (str): The Spotify API cliend secret key corresponding to the client ID.
57
+ neighbor_count (int): The number of neighbors to load for any given node.
56
58
database (Optional[GraphDatabaseInterface]): The database interface the graph is using.
57
59
"""
58
60
if database is None :
59
- database = SpotifyArtistGraph ._create_default_database ()
61
+ database = SpotifyArtistGraph .create_default_database ()
60
62
61
63
super (SpotifyArtistGraph , self ).__init__ (database )
62
64
63
65
self ._client : SpotifyClient = SpotifyClient (client_id , client_key )
64
66
"""
65
67
The Spotify web API client to use to request data.
66
68
"""
69
+ self ._neighbor_count : int = neighbor_count
70
+ """
71
+ The number of neighbors to load for any given node.
72
+ """
67
73
68
74
# Static methods
69
75
# ------------------------------------------------------------
@@ -75,11 +81,18 @@ def client(self) -> "SpotifyClient":
75
81
"""
76
82
return self ._client
77
83
84
+ @property
85
+ def neighbor_count (self ) -> int :
86
+ """
87
+ The number of neighbors to load for any given node.
88
+ """
89
+ return self ._neighbor_count
90
+
78
91
# Static methods
79
92
# ------------------------------------------------------------
80
93
81
94
@staticmethod
82
- def _create_default_database (reset : bool = False ) -> GraphDatabaseInterface :
95
+ def create_default_database (reset : bool = False ) -> GraphDatabaseInterface :
83
96
"""
84
97
Creates and returns a default SQLAlchemy database interface to use.
85
98
@@ -145,7 +158,7 @@ class SpotifyArtistNode(Node):
145
158
`Node` extension that loads its neighbors from Spotify using the Artist API.
146
159
"""
147
160
148
- _NEIGHBORS_TO_LOAD : int = 7
161
+ _NEIGHBORS_TO_LOAD : int = 6
149
162
"""
150
163
The number of neighbors to load from the Spotify web API for a node.
151
164
"""
@@ -178,8 +191,11 @@ def _load_neighbors_from_external_source(self) -> None:
178
191
wrapped by the graph that has this node.
179
192
"""
180
193
graph : SpotifyArtistGraph = self ._graph
181
- items : List [NameExternalIDPair ] = \
182
- graph .client .similar_artists (self .external_id , self ._NEIGHBORS_TO_LOAD )
194
+ items : List [NameExternalIDPair ] = graph .client .similar_artists (self .external_id )
195
+
196
+ limit : int = graph .neighbor_count if graph .neighbor_count > 0 else self ._NEIGHBORS_TO_LOAD
197
+ if len (items ) > limit :
198
+ del items [limit :]
183
199
184
200
for item in items :
185
201
neighbor : SpotifyArtistNode = graph .nodes .get_node_by_name (item .name ,
@@ -358,14 +374,13 @@ def search_artists_by_name(self, artist_name: str, limit: int = 5) -> List[NameE
358
374
359
375
return result
360
376
361
- def similar_artists (self , artist_id : str , limit : int = 50 ) -> List [NameExternalIDPair ]:
377
+ def similar_artists (self , artist_id : str ) -> List [NameExternalIDPair ]:
362
378
"""
363
379
Returns zero or more similar artists (in the form of artist name - external ID pairs)
364
380
to the one corresponding to the given artist ID.
365
381
366
382
Arguments:
367
383
artist_id ([str]): The Spotify ID of the artist for whom similar artists are requested.
368
- limit (int): The maximum number of results to return.
369
384
370
385
Returns:
371
386
Zero or more artist name - external ID pairs.
@@ -376,8 +391,7 @@ def similar_artists(self, artist_id: str, limit: int = 50) -> List[NameExternalI
376
391
"""
377
392
response : requests .Response = requests .get (
378
393
self ._API_URL_TEMPLATE .format ("artists/{}/related-artists" .format (artist_id )),
379
- headers = {"Authorization" : "Bearer {}" .format (self ._token .access_token )},
380
- params = {"limit" : limit }
394
+ headers = {"Authorization" : "Bearer {}" .format (self ._token .access_token )}
381
395
)
382
396
383
397
# TODO: handle API rate limiting
0 commit comments