Check in four year old stuff that probably doesn't work anymore. Like, at all.
This commit is contained in:
commit
e8032adca6
64
lib.py
Normal file
64
lib.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
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)
|
||||
|
5
other_requirements.txt
Normal file
5
other_requirements.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
Non Python Requirements:
|
||||
|
||||
lame
|
||||
libspotify.c
|
||||
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
pyspotify
|
90
rip.py
Normal file
90
rip.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
#!/usr/bin/env
|
||||
|
||||
desc = '''
|
||||
This is a Python application which gets music from Spotify and stores it in
|
||||
MP3 files, in a directory structure like "./Artist/Album/Song.mp3".
|
||||
It takes Spotify URIs for individual songs, as well as playlists.
|
||||
It also creates the appropriate ID3 tags.
|
||||
'''
|
||||
import argparse
|
||||
import threading
|
||||
from subprocess import Popen, PIPE, call
|
||||
|
||||
from spotify import Session, SessionConfig, Bitrate
|
||||
|
||||
from lib import (
|
||||
Callbacks, create_path, formatted_track_name, format_path, get_id3
|
||||
)
|
||||
from settings import username, password
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=desc)
|
||||
parser.add_argument("--URI", help="Valid Spotify URI")
|
||||
args = parser.parse_args()
|
||||
|
||||
session = Session(config=SessionConfig(), callbacks=Callbacks())
|
||||
session.preferred_bitrate(Bitrate.BITRATE_320k)
|
||||
session.callbacks.URI = args.URI
|
||||
session.callbacks.tracks_loaded = threading.Event()
|
||||
|
||||
session.login(username, password)
|
||||
session.process_events()
|
||||
|
||||
session.callbacks.tracks_loaded.wait()
|
||||
|
||||
for track in session.callbacks.tracks:
|
||||
track.load()
|
||||
track_name = formatted_track_name(track)
|
||||
print "Ripping track: {}.".format(track_name)
|
||||
|
||||
if track.is_autolinked:
|
||||
track = track.playable
|
||||
|
||||
session.callbacks.delivery = threading.Event()
|
||||
session.callbacks.track_ended = threading.Event()
|
||||
|
||||
session.player.load(track)
|
||||
session.process_events()
|
||||
session.player.play(play=True)
|
||||
|
||||
while True:
|
||||
if session.callbacks.track_ended.is_set():
|
||||
print "Done receiving audio data for {}.".format(track_name)
|
||||
session.callbacks.track_ended.clear()
|
||||
break
|
||||
|
||||
session.callbacks.delivery.wait()
|
||||
session.process_events()
|
||||
print "........"
|
||||
session.callbacks.delivery.clear()
|
||||
|
||||
path = format_path(track)
|
||||
create_path(path)
|
||||
|
||||
frames = session.callbacks.frames
|
||||
print "Processing {} frames...".format(len(frames))
|
||||
|
||||
id3_args = get_id3(track)
|
||||
song_path = "{}{}.mp3".format(path, track.name)
|
||||
|
||||
cover = track.album.cover(); cover.load()
|
||||
if cover.is_loaded:
|
||||
cover_path = "{}cover.jpg".format(path)
|
||||
with open(cover_path, 'wb') as cover_file:
|
||||
cover_file.write(cover.data)
|
||||
print "Album cover saved at {}".format(cover_path)
|
||||
|
||||
id3_args += ' --ti "{}"'.format(cover_path)
|
||||
|
||||
proc = Popen("lame --silent {} -V2 -h -r - \"{}\"".format(
|
||||
id3_args, song_path), stdin=PIPE, shell=True)
|
||||
|
||||
proc.stdin.write(frames)
|
||||
proc.stdin.close()
|
||||
|
||||
print "Done writing audio data at {}".format(song_path)
|
||||
|
||||
session.logout()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
3
settings.py
Normal file
3
settings.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
# Login details
|
||||
username = ''
|
||||
password = ''
|
Loading…
Reference in a new issue