remove telemetry

+ add docstrings to lyrics functions
This commit is contained in:
mungai-njoroge
2023-11-10 16:18:52 +03:00
parent 89b05ff80c
commit 8b6d10c832
9 changed files with 38 additions and 170 deletions
+1 -16
View File
@@ -1,8 +1,8 @@
"""
Contains all the artist(s) routes.
"""
import random
import math
import random
from datetime import datetime
from flask import Blueprint, request
@@ -15,29 +15,15 @@ from app.serializers.track import serialize_tracks
from app.store.albums import AlbumStore
from app.store.artists import ArtistStore
from app.store.tracks import TrackStore
from app.telemetry import Telemetry
from app.utils.threading import background
api = Blueprint("artist", __name__, url_prefix="/")
ARTIST_VISIT_COUNT = 0
@background
def send_event():
global ARTIST_VISIT_COUNT
ARTIST_VISIT_COUNT += 1
if ARTIST_VISIT_COUNT % 5 == 0:
Telemetry.send_artist_visited()
@api.route("/artist/<artisthash>", methods=["GET"])
def get_artist(artisthash: str):
"""
Get artist data.
"""
send_event()
limit = request.args.get("limit")
if limit is None:
@@ -211,7 +197,6 @@ def get_similar_artists(artisthash: str):
if artist is None:
return {"error": "Artist not found"}, 404
# result = LastFMStore.get_similar_artists_for(artist.artisthash)
result = fmdb.get_similar_artists_for(artist.artisthash)
if result is None:
+11 -3
View File
@@ -1,4 +1,5 @@
from flask import Blueprint, request
from app.lib.lyrics import format_synced_lyrics
from app.plugins.lyrics import Lyrics
from app.utils.hashing import create_hash
@@ -10,6 +11,7 @@ api = Blueprint("lyricsplugin", __name__, url_prefix="/plugins/lyrics")
def search_lyrics():
data = request.get_json()
trackhash = data.get("trackhash", "")
title = data.get("title", "")
artist = data.get("artist", "")
album = data.get("album", "")
@@ -20,7 +22,7 @@ def search_lyrics():
data = finder.search_lyrics_by_title_and_artist(title, artist)
if not data:
return {"downloaded": False}
return {"trackhash": trackhash, "lyrics": None}
perfect_match = data[0]
@@ -34,6 +36,12 @@ def search_lyrics():
perfect_match = track
track_id = perfect_match["track_id"]
downloaded = finder.download_lyrics(track_id, filepath)
lrc = finder.download_lyrics(track_id, filepath)
return {"downloaded": downloaded}, 200
if lrc is not None:
lines = lrc.split("\n")
lyrics = format_synced_lyrics(lines)
return {"trackhash": trackhash, "lyrics": lyrics}, 200
return {"trackhash": trackhash, "lyrics": lrc}, 200
+4 -1
View File
@@ -3,7 +3,7 @@ from enum import Enum
class AlbumVersionEnum(Enum):
"""
Enum for album versions.
Enum that registers supported album versions.
"""
Explicit = ("explicit",)
@@ -59,4 +59,7 @@ class AlbumVersionEnum(Enum):
def get_all_keywords():
"""
Returns a joint string of all album versions.
"""
return "|".join("|".join(i.value) for i in AlbumVersionEnum)
+12 -7
View File
@@ -3,12 +3,11 @@ from tinytag import TinyTag
from app.store.tracks import TrackStore
filepath = "/home/cwilvx/Music/Editor's Pick/Bad Day 😢/6 Dogs - Crying in the Rarri.m4a"
# filepath = "/home/cwilvx/Music/90s/Ballads/All-4-One - I Swear.mp3"
filepath = "/home/cwilvx/Music/Afrobeats/Kabusa Oriental Choir/Kabusa Oriental Choir - Bandana.m4a"
def split_line(line: str):
"""
Split a lyrics line into time and lyrics
"""
items = line.split("]")
time = items[0].removeprefix("[")
lyric = items[1] if len(items) > 1 else ""
@@ -17,6 +16,9 @@ def split_line(line: str):
def convert_to_milliseconds(time: str):
"""
Converts a lyrics time string into milliseconds.
"""
try:
minutes, seconds = time.split(":")
except ValueError:
@@ -124,9 +126,9 @@ def check_lyrics_file(filepath: str, trackhash: str):
def test_is_synced(lyrics: list[str]):
# try to split lines and get milliseconds
# if any passes, return True
"""
Tests if the lyric lines passed are synced.
"""
for line in lyrics:
time, _ = split_line(line)
milliseconds = convert_to_milliseconds(time)
@@ -138,6 +140,9 @@ def test_is_synced(lyrics: list[str]):
def get_extras(filepath: str, keys: list[str]):
"""
Get extra tags from an audio file.
"""
tags = TinyTag.get(filepath)
extras = tags.extra
+7 -9
View File
@@ -10,9 +10,6 @@ from app.db.sqlite.plugins import PluginsMethods
from app.plugins import Plugin, plugin_method
from app.settings import Keys, Paths
# from .base import LRCProvider
# from ..utils import get_best_match
class LRCProvider:
"""
@@ -202,12 +199,13 @@ class Lyrics(Plugin):
is_valid = lrc is not None and lrc.replace("\n", "").strip() != ""
if not is_valid:
return False
return None
if is_valid:
path = Path(path).with_suffix(".lrc")
path = Path(path).with_suffix(".lrc")
try:
with open(path, "w", encoding="utf-8") as f:
f.write(lrc)
return True
return False
return lrc
except:
return lrc
-76
View File
@@ -1,76 +0,0 @@
import uuid as UUID
from posthog import Posthog
from app.logger import log
from app.settings import Keys, Paths, Release
from app.utils.hashing import create_hash
from app.utils.network import has_connection
class Telemetry:
"""
Handles sending telemetry data to posthog.
"""
user_id = ""
off = False
@classmethod
def init(cls) -> None:
try:
cls.posthog = Posthog(
project_api_key=Keys.POSTHOG_API_KEY,
host="https://app.posthog.com",
disable_geoip=False,
)
cls.create_userid()
except AssertionError:
cls.disable_telemetry()
@classmethod
def create_userid(cls):
"""
Creates a unique user id for the user and saves it to a file.
"""
uuid_path = Paths.get_app_dir() + "/userid.txt"
try:
with open(uuid_path, "r") as f:
cls.user_id = f.read().strip()
except FileNotFoundError:
uuid = str(UUID.uuid4())
cls.user_id = "user_" + create_hash(uuid, limit=15)
with open(uuid_path, "w") as f:
f.write(cls.user_id)
@classmethod
def disable_telemetry(cls):
cls.off = True
@classmethod
def send_event(cls, event: str):
"""
Sends an event to posthog.
"""
if cls.off:
return
if has_connection():
cls.posthog.capture(cls.user_id, event=f"v{Release.APP_VERSION}-{event}")
@classmethod
def send_app_installed(cls):
"""
Sends an event to posthog when the app is installed.
"""
cls.send_event("app-installed")
@classmethod
def send_artist_visited(cls):
"""
Sends an event to posthog when an artist page is visited.
"""
cls.send_event("artist-page-visited")