The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a playlist rotation analyzer

Building a playlist rotation analyzer involves creating a system that can track how frequently songs are played within a playlist over time and analyze patterns like play counts, rotation speed, and frequency distribution. This is useful for DJs, radio stations, streaming services, or personal music libraries.

Here’s a detailed, step-by-step guide to building a basic playlist rotation analyzer in Python. This will track song plays and analyze rotation metrics.


Features to include:

  • Log each song play with timestamp

  • Calculate total plays per song

  • Calculate play frequency per time period (daily, weekly, monthly)

  • Identify hot tracks (most played recently)

  • Visualize rotation trends (optional)


Step 1: Data Model

We need to store song info and play logs.

python
from datetime import datetime from collections import defaultdict class Song: def __init__(self, title, artist): self.title = title self.artist = artist self.plays = [] # list of datetime objects when played def log_play(self, play_time=None): if play_time is None: play_time = datetime.now() self.plays.append(play_time) def total_plays(self): return len(self.plays) def plays_in_period(self, start_date, end_date): return len([p for p in self.plays if start_date <= p <= end_date])

Step 2: Playlist and Analyzer

Create a Playlist class to hold songs and an Analyzer to summarize rotation.

python
class Playlist: def __init__(self): self.songs = {} def add_song(self, song): key = (song.title, song.artist) self.songs[key] = song def log_play(self, title, artist, play_time=None): key = (title, artist) if key in self.songs: self.songs[key].log_play(play_time) else: new_song = Song(title, artist) new_song.log_play(play_time) self.songs[key] = new_song class RotationAnalyzer: def __init__(self, playlist): self.playlist = playlist def total_plays(self): return {key: song.total_plays() for key, song in self.playlist.songs.items()} def plays_in_last_days(self, days=7): end_date = datetime.now() start_date = end_date - timedelta(days=days) return { key: song.plays_in_period(start_date, end_date) for key, song in self.playlist.songs.items() } def most_played(self, days=7, top_n=5): plays = self.plays_in_last_days(days) sorted_plays = sorted(plays.items(), key=lambda x: x[1], reverse=True) return sorted_plays[:top_n]

Step 3: Usage Example

python
from datetime import timedelta playlist = Playlist() playlist.add_song(Song("Song A", "Artist 1")) playlist.add_song(Song("Song B", "Artist 2")) playlist.add_song(Song("Song C", "Artist 3")) # Log plays with timestamps (simulate some plays) playlist.log_play("Song A", "Artist 1") playlist.log_play("Song A", "Artist 1") playlist.log_play("Song B", "Artist 2", datetime.now() - timedelta(days=3)) playlist.log_play("Song C", "Artist 3", datetime.now() - timedelta(days=10)) analyzer = RotationAnalyzer(playlist) print("Total plays:") print(analyzer.total_plays()) print("nPlays in last 7 days:") print(analyzer.plays_in_last_days(7)) print("nTop 2 most played in last 7 days:") print(analyzer.most_played(7, top_n=2))

Step 4 (Optional): Visualization

You can extend this by using libraries like matplotlib or seaborn to graph plays over time for each track.


Summary

This script tracks each song’s plays with timestamps and lets you analyze play counts over any time period. You can extend it to:

  • Track plays per hour or day

  • Detect rotation speed (time between plays)

  • Generate alerts when rotation drops or spikes

Let me know if you want me to build a more advanced version with databases, web UI, or integration with streaming APIs!

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About