rippify/lib.py

64 lines
1.8 KiB
Python

import threading
import time
import sys
import os
from spotify import SessionConfig, Session, SessionCallbacks, Link, LinkType
class Callbacks(SessionCallbacks):
frames = ''
def logged_in(self, session, error):
if error.error_type: sys.exit(error)
print "Hi {}".format(session.user.display_name)
link = Link(self.URI)
print "Processing link: {}...".format(link)
if link.type == LinkType.TRACK:
self.tracks = [link.as_track()]
elif link.type == LinkType.PLAYLIST:
print 'playlist'
playlist = link.as_playlist()
self.tracks = playlist.tracks
self.tracks_loaded.set()
print "Number of Tracks Found: {}".format(len(self.tracks))
def metadata_updated(self, session):
session.flush_caches()
def music_delivery(self, session, audio_format, frames, num_frames):
print "Getting {} frames..".format(num_frames)
self.frames += frames # int16
self.delivery.set()
return num_frames
def end_of_track(self, session):
print 'Reached end of track.'
session.player.unload()
self.track_ended.set()
def format_path(track):
san_artist_name = track.artists[0].name.replace('.', '')
san_album_name = track.album.name.replace('.', '')
return "{}/Music/{}/{}/".format(os.getcwd(),
san_artist_name, san_album_name)
def create_path(path):
if not os.path.isdir(path):
os.makedirs(path)
def formatted_track_name(track):
main_artist = track.artists[0]
return "{} - {}, {}".format(main_artist.name, track.name, track.album.name)
def get_id3(track):
return '--tt "{}" --ta "{}" --tl "{}" --ty {} --tn {}'.format(
track.name, track.artists[0].name, track.album.name,
track.album.year, track.index)