فهرست منبع

[locations] Turns out we were looking them up wrong

Colin Powell 1 سال پیش
والد
کامیت
bfd210d280
3فایلهای تغییر یافته به همراه36 افزوده شده و 2 حذف شده
  1. 16 1
      vrobbler/apps/locations/models.py
  2. 17 1
      vrobbler/apps/scrobbles/models.py
  3. 3 0
      vrobbler/settings.py

+ 16 - 1
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)

+ 17 - 1
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)

+ 3 - 0
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"