File size: 4,297 Bytes
5a30a94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""Refresh Spotify popularity snapshots for existing Spotify tracks."""

import os
import sys
from datetime import datetime

from dotenv import load_dotenv

sys.path.insert(0, os.path.dirname(__file__))

from db import (  # noqa: E402
    get_connection,
    generate_cycle_id,
    get_previous_snapshot,
    init_db,
    insert_snapshot,
    insert_viral_alert,
)
from collect_spotify import (  # noqa: E402
    SPOTIFY_VIRAL_POPULARITY_JUMP,
    normalize_release_date,
)
from spotify_client import SpotifyClient  # noqa: E402

load_dotenv(os.path.join(os.path.dirname(__file__), "..", ".env"))


def enrich_spotify_data():
    """Refresh Spotify track popularity and insert new snapshots."""
    print(f"\n{'='*64}")
    print(f"Spotify Enrichment — {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print(f"Viral jump threshold: +{SPOTIFY_VIRAL_POPULARITY_JUMP}")
    print(f"{'='*64}")

    init_db()
    conn = get_connection()
    client = SpotifyClient()
    cycle_id = generate_cycle_id("spotify")
    print(f"Cycle ID: {cycle_id}")

    songs = conn.execute(
        """
        SELECT s.id, s.platform_id, s.artist_id, a.name AS artist_name
        FROM songs s
        JOIN artists a ON s.artist_id = a.id
        WHERE s.platform = 'spotify'
          AND s.platform_id IS NOT NULL
        ORDER BY a.name, s.title
        """
    ).fetchall()

    if not songs:
        conn.close()
        print("\nNo Spotify songs found to enrich.\n")
        return {"songs_updated": 0, "viral_alerts": 0}

    total_updated = 0
    total_viral = 0

    for i in range(0, len(songs), 50):
        batch = songs[i : i + 50]
        track_ids = [row["platform_id"] for row in batch if row["platform_id"]]
        try:
            tracks = client.get_tracks(track_ids)
        except Exception as exc:
            print(f"  ❌ failed batch {i // 50 + 1}: {exc}")
            continue

        tracks_by_id = {track.get("id"): track for track in tracks if track and track.get("id")}
        for row in batch:
            track = tracks_by_id.get(row["platform_id"])
            if not track:
                continue

            popularity = int(track.get("popularity") or 0)
            prev = get_previous_snapshot(conn, row["id"], "spotify")
            insert_snapshot(conn, row["id"], popularity, 0, 0, "spotify", cycle_id)

            album = track.get("album") or {}
            images = album.get("images") or []
            conn.execute(
                """
                UPDATE songs
                SET title = ?,
                    album_name = ?,
                    release_date = ?,
                    thumbnail_url = ?
                WHERE id = ?
                """,
                (
                    track.get("name", "Unknown"),
                    album.get("name"),
                    normalize_release_date(album.get("release_date", "")),
                    images[0].get("url", "") if images else "",
                    row["id"],
                ),
            )

            total_updated += 1

            if prev and prev.get("play_count") is not None:
                delta = popularity - int(prev["play_count"])
                if delta >= SPOTIFY_VIRAL_POPULARITY_JUMP:
                    insert_viral_alert(
                        conn,
                        {
                            "song_id": row["id"],
                            "previous_count": int(prev["play_count"]),
                            "current_count": popularity,
                            "growth_factor": float(delta),
                            "platform": "spotify",
                        },
                    )
                    total_viral += 1

        conn.commit()
        print(
            f"  ✅ batch {i // 50 + 1}: {len(batch)} tracks processed "
            f"(running total: {total_updated})"
        )

    conn.close()
    print(f"\n{'='*64}")
    print("Spotify enrichment complete")
    print(f"  Songs updated: {total_updated}")
    print(f"  Viral alerts: {total_viral}")
    print(f"{'='*64}\n")
    return {"songs_updated": total_updated, "viral_alerts": total_viral}


if __name__ == "__main__":
    try:
        enrich_spotify_data()
    except Exception as exc:
        print(f"Spotify enrichment failed: {exc}")
        sys.exit(1)