Skip to content

Commit c8b0e5f

Browse files
authored
Merge pull request #165 from metabrainz/yet-more-rate-limiting-yeaaa-haaawwww
Yet more rate limiting improvements by passing the troi auth token
2 parents d13b47e + 32d7bf9 commit c8b0e5f

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

tests/musicbrainz/test_recording_lookup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ def test_read(self, req):
322322
'recording_mbids':
323323
['1234a7ae-2af2-4291-aa84-bd0bafe291a1', 'ec5b8aa9-7483-4791-a185-1f599a0cdc35'],
324324
'inc': 'artist release tag'
325-
})
325+
},
326+
headers={})
326327

327328
assert len(entities) == 2
328329

troi/listenbrainz/recs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ class UserRecordingRecommendationsElement(Element):
1919

2020
MAX_RECORDINGS_TO_FETCH = 2000
2121

22-
def __init__(self, user_name, artist_type, count=25, offset=0):
22+
def __init__(self, user_name, artist_type, count=25, offset=0, auth_token=None):
2323
super().__init__()
2424
self.client = liblistenbrainz.ListenBrainz()
25+
if auth_token is not None:
26+
self.client.set_auth_token(auth_token)
2527
self.user_name = user_name
2628
self.count = count
2729
self.offset = offset

troi/musicbrainz/recording_lookup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ class RecordingLookupElement(Element):
1717
SERVER_URL = "https://api.listenbrainz.org/1/metadata/recording"
1818
MAX_RECORDINGS = 1000
1919

20-
def __init__(self, skip_not_found=True, lookup_tags=False, tag_threshold=None):
20+
def __init__(self, skip_not_found=True, lookup_tags=False, tag_threshold=None, auth_token=None):
2121
Element.__init__(self)
2222
self.skip_not_found = skip_not_found
2323
self.lookup_tags = lookup_tags
2424
self.tag_threshold = tag_threshold
25+
self.auth_token = auth_token
2526

2627
@staticmethod
2728
def inputs():
@@ -52,7 +53,8 @@ def read(self, inputs):
5253
inc += " tag"
5354

5455
while True:
55-
r = requests.post(self.SERVER_URL, json={"recording_mbids": recording_mbids, "inc": inc})
56+
headers = {"Authorization": f"Token {self.auth_token}"} if self.auth_token else {}
57+
r = requests.post(self.SERVER_URL, json={"recording_mbids": recording_mbids, "inc": inc}, headers=headers)
5658
if r.status_code == 429:
5759
sleep(2)
5860
continue

troi/patches/periodic_jams.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def create(self, inputs):
9898

9999
recs = troi.listenbrainz.recs.UserRecordingRecommendationsElement(user_name,
100100
"raw",
101-
count=1000)
101+
count=1000,
102+
auth_token=inputs.get("token"))
102103

103104
recent_listens_lookup = troi.listenbrainz.listens.RecentListensTimestampLookup(user_name,
104105
days=2,
@@ -129,7 +130,7 @@ def create(self, inputs):
129130
feedback_lookup = troi.listenbrainz.feedback.ListensFeedbackLookup(user_name, auth_token=inputs.get("token"))
130131
feedback_lookup.set_sources(latest_filter)
131132

132-
recs_lookup = troi.musicbrainz.recording_lookup.RecordingLookupElement()
133+
recs_lookup = troi.musicbrainz.recording_lookup.RecordingLookupElement(auth_token=inputs.get("token"))
133134
recs_lookup.set_sources(feedback_lookup)
134135

135136
hate_filter = troi.filters.HatedRecordingsFilterElement()

0 commit comments

Comments
 (0)