From bfd210d28006bf9fafb4ce02e6c3d73bc223b6e8 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Sat, 10 Feb 2024 15:06:16 -0500 Subject: [PATCH] [locations] Turns out we were looking them up wrong --- vrobbler/apps/locations/models.py | 17 ++++++++++++++++- vrobbler/apps/scrobbles/models.py | 18 +++++++++++++++++- vrobbler/settings.py | 3 +++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/vrobbler/apps/locations/models.py b/vrobbler/apps/locations/models.py index 8416cca..dbe531b 100644 --- a/vrobbler/apps/locations/models.py +++ b/vrobbler/apps/locations/models.py @@ -13,7 +13,7 @@ logger = logging.getLogger(__name__) BNULL = {"blank": True, "null": True} User = get_user_model() -GEOLOC_ACCURACY = getattr(settings, "GEOLOC_ACCURACY", 3) +GEOLOC_ACCURACY = getattr(settings, "GEOLOC_ACCURACY", 4) class GeoLocation(ScrobblableMixin): @@ -51,6 +51,21 @@ class GeoLocation(ScrobblableMixin): logger.error("No lat or lon keys in data dict") return + int_lat, r_lat = str(data_dict.get("lat", "")).split(".") + int_lon, r_lon = str(data_dict.get("lon", "")).split(".") + + try: + trunc_lat = r_lat[0:GEOLOC_ACCURACY] + except IndexError: + trunc_lat = r_lat + try: + trunc_lon = r_lon[0:GEOLOC_ACCURACY] + except IndexError: + trunc_lon = r_lon + + data_dict["lat"] = float(f"{int_lat}.{trunc_lat}") + data_dict["lon"] = float(f"{int_lon}.{trunc_lon}") + int_alt, r_alt = str(data_dict.get("alt", "")).split(".") data_dict["altitude"] = float(int_alt) diff --git a/vrobbler/apps/scrobbles/models.py b/vrobbler/apps/scrobbles/models.py index 51e03bb..a92a1a6 100644 --- a/vrobbler/apps/scrobbles/models.py +++ b/vrobbler/apps/scrobbles/models.py @@ -46,6 +46,10 @@ logger = logging.getLogger(__name__) User = get_user_model() BNULL = {"blank": True, "null": True} +POINTS_FOR_MOVEMENT_HISTORY = getattr( + settings, "POINTS_FOR_MOVEMENT_HISTORY", 3 +) + class BaseFileImportMixin(TimeStampedModel): user = models.ForeignKey(User, on_delete=models.DO_NOTHING, **BNULL) @@ -702,6 +706,7 @@ class Scrobble(TimeStampedModel): ): logger.info(f"Yes - in the same place - {self.id} - {self.source}") updatable = True + return updatable @property @@ -739,7 +744,7 @@ class Scrobble(TimeStampedModel): scrobble = self all_moves = [] - for i in range(3): + for i in range(NUM_HISTORY_FOR_MOVEMENT): loc_diff = self.loc_diff if loc_diff and loc_diff[0] < 0.001 and loc_diff[1] > 0.001: all_moves.append(True) @@ -802,6 +807,11 @@ class Scrobble(TimeStampedModel): media_query = models.Q(**{key: media}) scrobble_data[key + "_id"] = media.id + if key == "geo_location": + # For geo locations, it's a time sequence, not per location, so + # just get the last location we know of + media_query = models.Q(media_type="GeoLocation") + scrobble = ( cls.objects.filter( media_query, @@ -820,6 +830,12 @@ class Scrobble(TimeStampedModel): ) return scrobble.update(scrobble_data) + if scrobble: + logger.info( + f"[scrobbling] stopping existing scrobble {scrobble.id} before creating new one" + ) + scrobble.stop() + # Discard status before creating scrobble_data.pop("mopidy_status", None) scrobble_data.pop("jellyfin_status", None) diff --git a/vrobbler/settings.py b/vrobbler/settings.py index f455cf3..b1d7b8c 100644 --- a/vrobbler/settings.py +++ b/vrobbler/settings.py @@ -67,6 +67,9 @@ IGDB_CLIENT_ID = os.getenv("VROBBLER_IGDB_CLIENT_ID") IGDB_CLIENT_SECRET = os.getenv("VROBBLER_IGDB_CLIENT_SECRET") COMICVINE_API_KEY = os.getenv("VROBBLER_COMICVINE_API_KEY") GEOLOC_ACCURACY = os.getenv("VROBBLER_GEOLOC_ACCURACY", 3) +POINTS_FOR_MOVEMENT_HISTORY = os.getenv( + "VROBBLER_POINTS_FOR_MOVEMENT_HISTORY", 3 +) DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"