Selaa lähdekoodia

[locations] Try to fix geolocs finally

Colin Powell 1 vuosi sitten
vanhempi
commit
949416059d
2 muutettua tiedostoa jossa 21 lisäystä ja 19 poistoa
  1. 3 6
      vrobbler/apps/locations/models.py
  2. 18 13
      vrobbler/apps/scrobbles/models.py

+ 3 - 6
vrobbler/apps/locations/models.py

@@ -98,15 +98,12 @@ class GeoLocation(ScrobblableMixin):
         for point in past_points:
             loc_diff = self.loc_diff((point.lat, point.lon))
             if (
-                loc_diff
-                and loc_diff[0] < GEOLOC_PROXIMITY
-                and loc_diff[1] < GEOLOC_PROXIMITY
+                loc_diff[0] > GEOLOC_PROXIMITY
+                or loc_diff[1] > GEOLOC_PROXIMITY
             ):
                 all_moves.append(True)
-            else:
-                all_moves.append(False)
 
-        if not False in all_moves:
+        if True in all_moves:
             has_moved = True
 
         return has_moved

+ 18 - 13
vrobbler/apps/scrobbles/models.py

@@ -687,6 +687,7 @@ class Scrobble(TimeStampedModel):
 
         return percent
 
+    @property
     def can_be_updated(self, media, user_id) -> bool:
         updatable = True
 
@@ -699,17 +700,13 @@ class Scrobble(TimeStampedModel):
         if self.is_stale:
             logger.info(f"No - stale - {self.id} - {self.source}")
             updatable = False
-        if self.media_obj.__class__.__name__ in [
-            "GeoLocation"
-        ] and not self.has_moved(media, user_id):
-            logger.info(f"Yes - in the same place - {self.id} - {self.source}")
-            updatable = True
 
         return updatable
 
     @classmethod
     def has_moved(cls, new_location: GeoLocation, user_id: int) -> bool:
-        """Given a new location, let us know if we've moved from there"""
+        """Given a new location and a user, let us know if we've moved from there"""
+        # TODO This can be moved to a utility function, no reason it's a classmethod
         has_moved = False
 
         past_scrobbles = Scrobble.objects.filter(
@@ -779,7 +776,21 @@ class Scrobble(TimeStampedModel):
             .first()
         )
 
-        if scrobble and scrobble.can_be_updated(media, user_id):
+        geo_loc_has_not_moved = False
+        if key == "geo_location" and not cls.has_moved(media, user_id):
+            # We have a new location, but have not moved from it,
+            # don't proceed with scrobbling, but update the last GeoLoc
+            geo_loc_has_not_moved = True
+            if not scrobble:
+                scrobble = (
+                    cls.objects.filter(
+                        media_type=cls.MediaType.GEO_LOCATION, user_id=user_id
+                    )
+                    .order_by("-timestamp")
+                    .first()
+                )
+
+        if scrobble and (scrobble.can_be_updated or geo_loc_has_not_moved):
             source = scrobble_data["source"]
             mtype = media.__class__.__name__
             logger.info(
@@ -788,12 +799,6 @@ 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)