Skip to content

Commit

Permalink
Merge branch 'master' into single-page-app
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkeyDo authored Apr 26, 2024
2 parents 1038ca5 + 16d233b commit 0692c4b
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 33 deletions.
34 changes: 25 additions & 9 deletions frontend/js/src/common/brainzplayer/SpotifyPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,16 @@ export default class SpotifyPlayer
retryCount = 0
): Promise<void> => {
const { device_id } = this.state;
const { handleError } = this.props;
const { handleError, onTrackNotFound } = this.props;
if (retryCount > 5) {
handleError("Could not play Spotify track", "Playback error");
onTrackNotFound();
return;
}
if (!this.spotifyPlayer || !device_id) {
this.connectSpotifyPlayer(
this.playSpotifyURI.bind(this, spotifyURI, retryCount + 1)
this.playSpotifyURI.bind(this, spotifyURI, retryCount + 1),
retryCount + 1
);
return;
}
Expand Down Expand Up @@ -340,14 +342,14 @@ export default class SpotifyPlayer
this.handleAccountError();
return;
}
const { onTrackNotFound } = this.props;
const { onInvalidateDataSource } = this.props;
if (this.authenticationRetries > 5) {
const { handleError } = this.props;
handleError(
isString(error) ? error : error?.message,
"Spotify token error"
);
onTrackNotFound();
onInvalidateDataSource();
return;
}
this.authenticationRetries += 1;
Expand Down Expand Up @@ -407,11 +409,22 @@ export default class SpotifyPlayer
);
};

connectSpotifyPlayer = (callbackFunction?: () => void): void => {
connectSpotifyPlayer = (
callbackFunction?: () => void,
retryCount = 0
): void => {
const { handleError, onInvalidateDataSource } = this.props;
this.disconnectSpotifyPlayer();

if (retryCount > 5) {
handleError("Could not connect to Spotify", "Spotify error");
onInvalidateDataSource();
return;
}
if (!window.Spotify) {
setTimeout(this.connectSpotifyPlayer.bind(this, callbackFunction), 1000);
setTimeout(
this.connectSpotifyPlayer.bind(this, callbackFunction, retryCount + 1),
1000
);
return;
}
const { refreshSpotifyToken } = this.props;
Expand All @@ -427,15 +440,18 @@ export default class SpotifyPlayer
} catch (error) {
handleError(error, "Error connecting to Spotify");
setTimeout(
this.connectSpotifyPlayer.bind(this, callbackFunction),
this.connectSpotifyPlayer.bind(
this,
callbackFunction,
retryCount + 1
),
1000
);
}
},
volume: 0.7, // Careful with this, now…
});

const { handleError } = this.props;
// Error handling
this.spotifyPlayer.on(
"initialization_error",
Expand Down
2 changes: 1 addition & 1 deletion frontend/js/src/explore/lb-radio/components/Prompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function Prompt(props: PromptProps) {
id="doc-link"
href="https://troi.readthedocs.io/en/latest/lb_radio.html"
>
How do I write a query?
How do I write a prompt?
</a>
</small>
</h3>
Expand Down
6 changes: 4 additions & 2 deletions listenbrainz/spark/spark_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
)
from listenbrainz.spark.spark_dataset import CouchDbDataset
from listenbrainz.utils import get_fallback_connection_name
from listenbrainz.webserver import create_app
from listenbrainz.webserver import create_app, db_conn, ts_conn


class SparkReader(ConsumerMixin):
Expand Down Expand Up @@ -127,7 +127,9 @@ def process_response(self, response):
self.app.logger.error("Error in the spark reader response handler: data: %s",
json.dumps(response, indent=4), exc_info=True)
sentry_sdk.capture_exception(e)
return
finally:
db_conn.rollback()
ts_conn.rollback()

def callback(self, message: Message):
""" Handle the data received from the queue and
Expand Down
9 changes: 5 additions & 4 deletions listenbrainz/troi/daily_jams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
"""
from flask import current_app
from sqlalchemy import text
from troi.core import generate_playlist
from troi.patches.recs_to_playlist import RecommendationsToPlaylistPatch
from troi.patches.periodic_jams import PeriodicJamsPatch
from troi.patches.recs_to_playlist import RecommendationsToPlaylistPatch

from listenbrainz import db
from listenbrainz.db.playlist import TROI_BOT_USER_ID, TROI_BOT_DEBUG_USER_ID
Expand Down Expand Up @@ -88,7 +87,8 @@ def make_playlist_from_recommendations(user):
# need to copy dict so that test mocks keep working
_args = args.copy()
_args["type"] = recs_type
generate_playlist(RecommendationsToPlaylistPatch(), _args)
patch = RecommendationsToPlaylistPatch(_args)
patch.generate_playlist()


def _get_spotify_details(user_id, service):
Expand Down Expand Up @@ -131,7 +131,8 @@ def run_daily_jams(db_conn, user, existing_url, service):
spotify["existing_urls"] = [existing_url]
args["spotify"] = spotify

playlist = generate_playlist(PeriodicJamsPatch(), args)
playlist = PeriodicJamsPatch(args)
playlist.generate_playlist()

if playlist is not None and len(playlist.playlists) > 0:
url = current_app.config["SERVER_ROOT_URL"] + "/playlist/" + playlist.playlists[0].mbid
Expand Down
4 changes: 2 additions & 2 deletions listenbrainz/troi/export.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import spotipy
from troi.core import generate_playlist
from troi.patches.playlist_from_listenbrainz import TransferPlaylistPatch


Expand All @@ -21,6 +20,7 @@ def export_to_spotify(lb_token, spotify_token, is_public, playlist_mbid=None, js
"echo": False,
"min_recordings": 1
}
playlist = generate_playlist(TransferPlaylistPatch(), args)
patch = TransferPlaylistPatch(args)
playlist = patch.generate_playlist()
metadata = playlist.playlists[0].additional_metadata
return metadata["external_urls"]["spotify"]
2 changes: 0 additions & 2 deletions listenbrainz/troi/spark.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
""" Common code for processing troi playlists generated in spark """

import json
from datetime import datetime, timedelta

from flask import current_app
from psycopg2.extras import execute_values, DictCursor
from psycopg2.sql import SQL, Literal
from spotipy import Spotify
from sqlalchemy import text
from troi import Recording, Playlist
from troi.patches.periodic_jams import WEEKLY_JAMS_DESCRIPTION, WEEKLY_EXPLORATION_DESCRIPTION
from troi.tools.spotify_lookup import submit_to_spotify

from listenbrainz import db
Expand Down
2 changes: 0 additions & 2 deletions listenbrainz/troi/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from sqlalchemy import text

from listenbrainz.db import timescale

SPOTIFY_EXPORT_PREFERENCE = "export_to_spotify"


Expand Down
6 changes: 5 additions & 1 deletion listenbrainz/webserver/views/api_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import bleach
from kombu import Producer
from kombu.entity import PERSISTENT_DELIVERY_MODE
from more_itertools import chunked

import listenbrainz.webserver.rabbitmq_connection as rabbitmq_connection
import listenbrainz.webserver.redis_connection as redis_connection
Expand Down Expand Up @@ -53,6 +54,8 @@

MAX_ITEMS_PER_MESSYBRAINZ_LOOKUP = 10

MAX_LISTENS_PER_RMQ_MESSAGE = 100 # internal limit on number of listens per RMQ message to avoid timeouts in TS writer


# Define the values for types of listens
LISTEN_TYPE_SINGLE = 1
Expand Down Expand Up @@ -117,7 +120,8 @@ def _send_listens_to_queue(listen_type, listens):
else:
exchange = rabbitmq_connection.INCOMING_EXCHANGE

publish_data_to_queue(submit, exchange)
for chunk in chunked(submit, MAX_LISTENS_PER_RMQ_MESSAGE):
publish_data_to_queue(chunk, exchange)


def _raise_error_if_has_unicode_null(value, listen):
Expand Down
11 changes: 3 additions & 8 deletions listenbrainz/webserver/views/explore_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from listenbrainz.webserver.views.api_tools import _parse_int_arg, _parse_bool_arg
from listenbrainz.db.color import get_releases_for_color
from troi.patches.lb_radio import LBRadioPatch
from troi.core import generate_playlist
from troi.patch import Patch

DEFAULT_NUMBER_OF_FRESH_RELEASE_DAYS = 14
MAX_NUMBER_OF_FRESH_RELEASE_DAYS = 90
Expand Down Expand Up @@ -178,14 +178,9 @@ def lb_radio():
raise APIBadRequest(
f"The mode parameter must be one of 'easy', 'medium', 'hard'.")

patch = LBRadioPatch()
try:
playlist = generate_playlist(
patch,
args={
"mode": mode,
"prompt": prompt,
"echo": False})
patch = LBRadioPatch({ "mode": mode, "prompt": prompt, "quiet": True, "min_recordings": 1})
playlist = patch.generate_playlist()
except RuntimeError as err:
raise APIBadRequest(f"LB Radio generation failed: {err}")

Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pika == 1.2.1
brainzutils@git+https://github.com/metabrainz/brainzutils-python.git@upgrade-deps
spotipy>=2.22.1
datasethoster@git+https://github.com/metabrainz/data-set-hoster.git@a1e12968af93d2f296a42a8094ebff83f3ed33f3
troi@git+https://github.com/metabrainz/troi-recommendation-playground.git@v-2023-10-30.0
troi@git+https://github.com/metabrainz/troi-recommendation-playground.git@v2024.04.26.0
PyYAML==6.0
eventlet == 0.33.1
# eventlet fails to patch dnspython >= 2.3 https://github.com/eventlet/eventlet/issues/781
Expand All @@ -42,7 +42,7 @@ SQLAlchemy==2.0.23
mbdata@git+https://github.com/amCap1712/mbdata.git@fix-sqlalchemy-warnings
sqlalchemy-dst==1.0.1
typesense==0.14.0
unidecode==1.3.4
unidecode>=1.3.4
Levenshtein==0.20.8
google_auth_oauthlib==0.4.4
google-auth==1.30.0
Expand Down

0 comments on commit 0692c4b

Please sign in to comment.