video & music playlist scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

87 lines
2.9 KiB

#!/usr/bin/python3
"""
Currents.py will scan new music files in your music directory and create
a playlist of the new Music found, with only the top X songs of each artist.
Currently requires the music library to be organized as:
.../(first character of artist name, excluding 'the' 'a' and 'an')/(artist name)/(album)/[(track number)] - (title).(extension)
"""
import requests
import os
import json
import yaml
import datetime
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE = os.path.join(BASE_DIR, "config.yaml")
with open(CONFIG_FILE, 'r') as c:
try:
config = yaml.safe_load(c)
except yaml.YAMLError:
print("Have you copied over the default config file to 'config.yaml'?")
##make a file of all recently updated music and open it
find_music = "find " + config["global"]["music_dir"] +" -type f -mtime -"+ str(config["currents"]["recent_tracks"]) + " -size +300k -print > /tmp/currents_1"
os.system(find_music)
new_music_file = open ("/tmp/currents_1", "r")
fullpath = [lines.split("/") for lines in new_music_file]
##create unique id for each song, make tuples for artists, tracks, and paths
id = 1
new_songs_dict = {}
for song in fullpath:
d = "/"
track_path = d.join(song)
artist = song[7]
for ignore in config["currents"]["ignore_artists"]:
if ignore in artist:
continue
album = song[8]
track2 = song[9]
track1 = track2.split("] ")[1]
track = track1.split(".")[0]
artist_info = (id, artist, track, track_path, album)
new_songs_dict.update({id : artist_info})
id += 1
##assemble list of artists
data = new_songs_dict.values()
artists = []
for song in data:
artists.append(song[1])
unique_artists = set(artists)
##grab top track info from last.fm, create lists
top_songs = []
for artist in unique_artists:
lastfm_params = {'method': "artist.gettoptracks", "artist": artist, "api_key": config["last_fm"]["api_key"], "format": "json", "limit": config["currents"]["max_artist_tracks"]}
r = requests.get('http://ws.audioscrobbler.com/2.0/', params=lastfm_params)
if "error" in r.text:
continue
tracks_dict = json.loads(r.text)
tracks = tracks_dict['toptracks']['track']
for song in tracks:
song_name = song['name']
playcount = song['playcount']
top_songs.append([artist, song_name, playcount])
#Matching top tracks to songs in our data
id_list = {}
for song in data:
track = song[2]
count = 0
for tracks in top_songs:
top_track = tracks[1]
playcount = tracks[2]
if track == top_track:
id_list.update({song[0] : playcount})
top_songs.pop(count)
count += 1
#creating the playlist
currents_dir = os.path.join(config["global"]["playlist_dir"], "currents")
playlist_name = "currents-" + str(datetime.date.today()) + ".m3u"
if not os.path.exists(currents_dir):
os.mkdir(currents_dir)
with open(os.path.join(currents_dir, playlist_name), 'w') as play:
play.write("#EXTM3U \n")
for id in id_list:
data = new_songs_dict.get(id)
play.write(data[3])