[locations] Turns out we were looking them up wrong

This commit is contained in:
2024-02-10 15:06:16 -05:00
parent e5c6b5e8d9
commit bfd210d280
3 changed files with 36 additions and 2 deletions

View File

@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
BNULL = {"blank": True, "null": True} BNULL = {"blank": True, "null": True}
User = get_user_model() User = get_user_model()
GEOLOC_ACCURACY = getattr(settings, "GEOLOC_ACCURACY", 3) GEOLOC_ACCURACY = getattr(settings, "GEOLOC_ACCURACY", 4)
class GeoLocation(ScrobblableMixin): class GeoLocation(ScrobblableMixin):
@ -51,6 +51,21 @@ class GeoLocation(ScrobblableMixin):
logger.error("No lat or lon keys in data dict") logger.error("No lat or lon keys in data dict")
return 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(".") int_alt, r_alt = str(data_dict.get("alt", "")).split(".")
data_dict["altitude"] = float(int_alt) data_dict["altitude"] = float(int_alt)

View File

@ -46,6 +46,10 @@ logger = logging.getLogger(__name__)
User = get_user_model() User = get_user_model()
BNULL = {"blank": True, "null": True} BNULL = {"blank": True, "null": True}
POINTS_FOR_MOVEMENT_HISTORY = getattr(
settings, "POINTS_FOR_MOVEMENT_HISTORY", 3
)
class BaseFileImportMixin(TimeStampedModel): class BaseFileImportMixin(TimeStampedModel):
user = models.ForeignKey(User, on_delete=models.DO_NOTHING, **BNULL) 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}") logger.info(f"Yes - in the same place - {self.id} - {self.source}")
updatable = True updatable = True
return updatable return updatable
@property @property
@ -739,7 +744,7 @@ class Scrobble(TimeStampedModel):
scrobble = self scrobble = self
all_moves = [] all_moves = []
for i in range(3): for i in range(NUM_HISTORY_FOR_MOVEMENT):
loc_diff = self.loc_diff loc_diff = self.loc_diff
if loc_diff and loc_diff[0] < 0.001 and loc_diff[1] > 0.001: if loc_diff and loc_diff[0] < 0.001 and loc_diff[1] > 0.001:
all_moves.append(True) all_moves.append(True)
@ -802,6 +807,11 @@ class Scrobble(TimeStampedModel):
media_query = models.Q(**{key: media}) media_query = models.Q(**{key: media})
scrobble_data[key + "_id"] = media.id 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 = ( scrobble = (
cls.objects.filter( cls.objects.filter(
media_query, media_query,
@ -820,6 +830,12 @@ class Scrobble(TimeStampedModel):
) )
return scrobble.update(scrobble_data) 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 # Discard status before creating
scrobble_data.pop("mopidy_status", None) scrobble_data.pop("mopidy_status", None)
scrobble_data.pop("jellyfin_status", None) scrobble_data.pop("jellyfin_status", None)

View File

@ -67,6 +67,9 @@ IGDB_CLIENT_ID = os.getenv("VROBBLER_IGDB_CLIENT_ID")
IGDB_CLIENT_SECRET = os.getenv("VROBBLER_IGDB_CLIENT_SECRET") IGDB_CLIENT_SECRET = os.getenv("VROBBLER_IGDB_CLIENT_SECRET")
COMICVINE_API_KEY = os.getenv("VROBBLER_COMICVINE_API_KEY") COMICVINE_API_KEY = os.getenv("VROBBLER_COMICVINE_API_KEY")
GEOLOC_ACCURACY = os.getenv("VROBBLER_GEOLOC_ACCURACY", 3) 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" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"