Skip to content

Commit e817a78

Browse files
committed
Move musicbrainz config to env
1 parent 912fd96 commit e817a78

File tree

6 files changed

+25
-6
lines changed

6 files changed

+25
-6
lines changed

ansible/.ansible-secrets.template.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ spotify_client_id: spotify_client_id
22
spotify_secret: spotify_secret
33
debug_mode: false
44
flask_secret_key: flask_secret_key
5-
db_connection_string: postgresql://username:password@hostname:port/database
5+
musicbrainz_url: musicbrainz_url
6+
musicbrainz_user_agent: musicbrainz_user_agent
7+
db_connection_string: db_connection_string

backend/.env.j2

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ SPOTIFY_CLIENT_ID={{spotify_client_id}}
1515
SPOTIFY_SECRET={{spotify_secret}}
1616
SPOTIFY_REDIRECT_URI="{{backend_url}}/auth/get-user-code"
1717

18+
# Musicbrainz Api
19+
MUSICBRAINZ_URL={{musicbrainz_url}}
20+
MUSICBRAINZ_USER_AGENT={{musicbrainz_user_agent}}
21+
1822
# Database Connection String
19-
DB_CONNECTION_STRING=postgresql://username:password@hostname:port/database
23+
DB_CONNECTION_STRING={{db_connection_string}}

backend/.env.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ SPOTIFY_CLIENT_ID="https://developer.spotify.com/dashboard"
1515
SPOTIFY_SECRET="https://developer.spotify.com/dashboard"
1616
SPOTIFY_REDIRECT_URI="http://localhost:8080/spotify-redirect"
1717

18+
# Musicbrainz Api
19+
MUSICBRAINZ_URL=https://musicbrainz.org/ws/2
20+
MUSICBRAINZ_USER_AGENT="Application PlaylistManager/1.0 - Hobby project (put maintainers email here)"
21+
1822
# Database Connection String
1923
DB_CONNECTION_STRING=postgresql://username:password@hostname:port/database

backend/.env.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ SPOTIFY_CLIENT_ID="https://developer.spotify.com/dashboard"
1515
SPOTIFY_SECRET="https://developer.spotify.com/dashboard"
1616
SPOTIFY_REDIRECT_URI="http://localhost:8080/spotify-redirect"
1717

18+
# Musicbrainz Api
19+
MUSICBRAINZ_URL=https://musicbrainz.org/ws/2
20+
MUSICBRAINZ_USER_AGENT="Application PlaylistManager/1.0 - Hobby project (put maintainers email here)"
21+
1822
# Database Connection String
1923
DB_CONNECTION_STRING=postgresql://username:password@hostname:port/database

backend/src/flask_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def __init__(self):
88
self.BACKEND_URL = os.environ.get("BACKEND_URL")
99
self.FRONTEND_URL = os.environ.get("FRONTEND_URL")
1010
self.DB_CONNECTION_STRING = os.environ.get("DB_CONNECTION_STRING")
11+
self.MUSICBRAINZ_URL = os.environ.get("MUSICBRAINZ_URL")
12+
self.MUSICBRAINZ_USER_AGENT = os.environ.get("MUSICBRAINZ_USER_AGENT")
1113
if not self.SECRET_KEY:
1214
raise ValueError(
1315
"No SECRET_KEY set for Flask application. Did you follow the setup instructions?"

backend/src/musicbrainz.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
from src.exceptions.Unauthorized import UnauthorizedException
66
from time import sleep
77

8+
from src.flask_config import Config
89

10+
11+
### Musicbrainz requests must be followed by a 1 second sleep if being sent in bulk to avoid rate limiting
912
class MusicbrainzClient:
1013
request_headers = {
1114
"Accept": "application/json",
12-
"User-Agent": "Application PlaylistManager/1.0 - Hobby project ([email protected])",
15+
"User-Agent": Config().MUSICBRAINZ_USER_AGENT,
1316
}
1417

1518
def response_handler(self, response: requests.Response, jsonify=True):
@@ -30,23 +33,23 @@ def get_genre_list(self, limit=100, offset=0) -> List[str]:
3033
else:
3134
query = f"?limit={limit}&offset={offset}"
3235
response = requests.get(
33-
url="https://musicbrainz.org/ws/2/genre/all" + query,
36+
url=Config().MUSICBRAINZ_URL + "/genre/all" + query,
3437
headers=self.request_headers,
3538
)
3639
data = response.json()
3740
genre_list = [genre["name"] for genre in data["genres"]]
3841
if genre_list == []:
3942
return []
4043
else:
41-
sleep(2) # Needed to avoid rate limiting
44+
sleep(1)
4245
return genre_list + self.get_genre_list(limit, offset + limit)
4346

4447
def get_album_genres(self, artist_name: str, album_title: str) -> List[str]:
4548
query = quote_plus(
4649
f'artistname:"{artist_name}" AND releasegroup:"{album_title}"'
4750
)
4851
response = requests.get(
49-
url="https://musicbrainz.org/ws/2/release-group?query=" + query,
52+
url=Config().MUSICBRAINZ_URL + "/release-group?query=" + query,
5053
headers=self.request_headers,
5154
)
5255
data = response.json()

0 commit comments

Comments
 (0)