Skip to content

Commit a245479

Browse files
authored
Use an auth token for extended ratelimit (#96)
* Use an auth token for extended ratelimit Daily jams are failing for users in production due to ratelimit errors. The intent is to pass troi's auth token to the patch in prod so that the ratelimit can be bypassed.
1 parent 49cede0 commit a245479

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

troi/core.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
32
import sys
43
from typing import Dict
54

troi/listenbrainz/feedback.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ class ListensFeedbackLookup(Element):
1414
:param user_name: The ListenBrainz user_name for whom to fetch feedback information.
1515
"""
1616

17-
def __init__(self, user_name):
17+
def __init__(self, user_name, auth_token=None):
1818
super().__init__()
1919
self.user_name = user_name
20+
self.auth_token = auth_token
2021

2122
@staticmethod
2223
def inputs():
@@ -36,6 +37,8 @@ def read(self, inputs):
3637
mbids.add(r.mbid)
3738
mbids = list(mbids)
3839

40+
headers = {"Authorization": f"Token {self.auth_token}"} if self.auth_token else {}
41+
3942
feedback_map = {}
4043
batch_size = 50
4144
for idx in range(0, len(mbids), batch_size):
@@ -44,14 +47,14 @@ def read(self, inputs):
4447
while True:
4548
response = requests.get(
4649
f"https://api.listenbrainz.org/1/feedback/user/{self.user_name}/get-feedback-for-recordings",
47-
params={"recording_mbids": ",".join(recording_mbids)}
50+
params={"recording_mbids": ",".join(recording_mbids)},
51+
headers=headers
4852
)
4953
if response.status_code == 429:
5054
sleep(2)
5155
continue
5256

5357
break
54-
5558
response.raise_for_status()
5659
data = response.json()["feedback"]
5760
if len(data) == 0:

troi/listenbrainz/listens.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ class RecentListensTimestampLookup(Element):
1616
Timestamps are stored in the listenbrainz dict, with key name "latest_listened_at".
1717
1818
:param user_name: The ListenBrainz user for whome to fetch recent listen timestamps.
19+
:param auth_token: a ListenBrainz auth token
1920
:param days: The number of days to check.
2021
"""
2122

22-
def __init__(self, user_name, days: int):
23+
def __init__(self, user_name, days: int, auth_token=None):
2324
super().__init__()
2425
self.user_name = user_name
2526
self.days = days
27+
self.auth_token = auth_token
2628
self.index: Optional[dict[str, int]] = None
2729

2830
@staticmethod
@@ -42,9 +44,11 @@ def _fetch_recent_listens_index(self):
4244
min_dt = datetime.now() + timedelta(days=-self.days)
4345
min_ts = int(min_dt.timestamp())
4446
while True:
47+
headers = {"Authorization": f"Token {self.auth_token}"} if self.auth_token else {}
4548
response = requests.get(
4649
f"https://api.listenbrainz.org/1/user/{self.user_name}/listens",
47-
params={"min_ts": min_ts, "count": 100}
50+
params={"min_ts": min_ts, "count": 100},
51+
headers=headers
4852
)
4953
if response.status_code == 429:
5054
sleep(2)

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 = pylistenbrainz.ListenBrainz()
25+
if auth_token:
26+
self.client.set_auth_token(auth_token)
2527
self.user_name = user_name
2628
self.count = count
2729
self.offset = offset

troi/patches/daily_jams.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def create(self, inputs):
5757
if jam_date is None:
5858
jam_date = datetime.utcnow().strftime("%Y-%m-%d %a")
5959

60-
recs = troi.listenbrainz.recs.UserRecordingRecommendationsElement(user_name, "raw", count=1000)
60+
recs = troi.listenbrainz.recs.UserRecordingRecommendationsElement(user_name, "raw", count=1000, auth_token=inputs.get("token"))
6161

62-
recent_listens_lookup = troi.listenbrainz.listens.RecentListensTimestampLookup(user_name, days=2)
62+
recent_listens_lookup = troi.listenbrainz.listens.RecentListensTimestampLookup(user_name, days=2, auth_token=inputs.get("token"))
6363
recent_listens_lookup.set_sources(recs)
6464

6565
# Remove tracks that have not been listened to before.
@@ -69,7 +69,7 @@ def create(self, inputs):
6969
latest_filter = troi.filters.LatestListenedAtFilterElement(DAYS_OF_RECENT_LISTENS_TO_EXCLUDE)
7070
latest_filter.set_sources(never_listened)
7171

72-
feedback_lookup = troi.listenbrainz.feedback.ListensFeedbackLookup(user_name)
72+
feedback_lookup = troi.listenbrainz.feedback.ListensFeedbackLookup(user_name, auth_token=inputs.get("token"))
7373
feedback_lookup.set_sources(latest_filter)
7474

7575
recs_lookup = troi.musicbrainz.recording_lookup.RecordingLookupElement()

0 commit comments

Comments
 (0)