Skip to content

Commit e5747f9

Browse files
author
Péter Volf
committed
fixed the neighbor count limiting feature of SpotifyArtistGraph and made it configurable, it's also an API breaking change...
1 parent b750a84 commit e5747f9

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setup(
1010
name="graphscraper",
11-
version="0.2.0",
11+
version="0.3.0",
1212
description="Graph implementation that loads graph data (nodes and edges) from external sources "
1313
"and caches the loaded data in a database using sqlalchemy or flask-sqlalchemy.",
1414
long_description=long_description,

src/graphscraper/spotifyartist.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class SpotifyArtistGraph(Graph):
4444
def __init__(self,
4545
client_id: str,
4646
client_key: str,
47+
neighbor_count: int = 6,
4748
database: Optional[GraphDatabaseInterface] = None):
4849
"""
4950
Initialization.
@@ -53,17 +54,22 @@ def __init__(self,
5354
Arguments:
5455
client_id (str): The Spotify API client ID to use.
5556
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.
5658
database (Optional[GraphDatabaseInterface]): The database interface the graph is using.
5759
"""
5860
if database is None:
59-
database = SpotifyArtistGraph._create_default_database()
61+
database = SpotifyArtistGraph.create_default_database()
6062

6163
super(SpotifyArtistGraph, self).__init__(database)
6264

6365
self._client: SpotifyClient = SpotifyClient(client_id, client_key)
6466
"""
6567
The Spotify web API client to use to request data.
6668
"""
69+
self._neighbor_count: int = neighbor_count
70+
"""
71+
The number of neighbors to load for any given node.
72+
"""
6773

6874
# Static methods
6975
# ------------------------------------------------------------
@@ -75,11 +81,18 @@ def client(self) -> "SpotifyClient":
7581
"""
7682
return self._client
7783

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+
7891
# Static methods
7992
# ------------------------------------------------------------
8093

8194
@staticmethod
82-
def _create_default_database(reset: bool = False) -> GraphDatabaseInterface:
95+
def create_default_database(reset: bool = False) -> GraphDatabaseInterface:
8396
"""
8497
Creates and returns a default SQLAlchemy database interface to use.
8598
@@ -145,7 +158,7 @@ class SpotifyArtistNode(Node):
145158
`Node` extension that loads its neighbors from Spotify using the Artist API.
146159
"""
147160

148-
_NEIGHBORS_TO_LOAD: int = 7
161+
_NEIGHBORS_TO_LOAD: int = 6
149162
"""
150163
The number of neighbors to load from the Spotify web API for a node.
151164
"""
@@ -178,8 +191,11 @@ def _load_neighbors_from_external_source(self) -> None:
178191
wrapped by the graph that has this node.
179192
"""
180193
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:]
183199

184200
for item in items:
185201
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
358374

359375
return result
360376

361-
def similar_artists(self, artist_id: str, limit: int = 50) -> List[NameExternalIDPair]:
377+
def similar_artists(self, artist_id: str) -> List[NameExternalIDPair]:
362378
"""
363379
Returns zero or more similar artists (in the form of artist name - external ID pairs)
364380
to the one corresponding to the given artist ID.
365381
366382
Arguments:
367383
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.
369384
370385
Returns:
371386
Zero or more artist name - external ID pairs.
@@ -376,8 +391,7 @@ def similar_artists(self, artist_id: str, limit: int = 50) -> List[NameExternalI
376391
"""
377392
response: requests.Response = requests.get(
378393
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)}
381395
)
382396

383397
# TODO: handle API rate limiting

0 commit comments

Comments
 (0)