mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2026-02-16 14:29:26 +00:00
Shift completely to urllib2 and mutagen
This commit is contained in:
@@ -68,11 +68,6 @@ def fixEncoding(query):
|
|||||||
else:
|
else:
|
||||||
return query.encode('utf-8')
|
return query.encode('utf-8')
|
||||||
|
|
||||||
def cleanTemp():
|
|
||||||
for temp in os.listdir('Music/'):
|
|
||||||
if temp.endswith('.m4a.temp'):
|
|
||||||
os.remove('Music/' + temp)
|
|
||||||
|
|
||||||
def graceQuit():
|
def graceQuit():
|
||||||
print('')
|
print('')
|
||||||
print('')
|
print('')
|
||||||
|
|||||||
116
spotdl.py
116
spotdl.py
@@ -7,18 +7,18 @@ from core.misc import getArgs
|
|||||||
from core.misc import isSpotify
|
from core.misc import isSpotify
|
||||||
from core.misc import generateSearchURL
|
from core.misc import generateSearchURL
|
||||||
from core.misc import fixEncoding
|
from core.misc import fixEncoding
|
||||||
from core.misc import cleanTemp
|
|
||||||
from core.misc import graceQuit
|
from core.misc import graceQuit
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from shutil import copyfileobj
|
from shutil import copyfileobj
|
||||||
import sys
|
import sys
|
||||||
from slugify import slugify
|
from slugify import slugify
|
||||||
from titlecase import titlecase
|
from titlecase import titlecase
|
||||||
|
from mutagen.id3 import ID3, APIC
|
||||||
|
from mutagen.easyid3 import EasyID3
|
||||||
from mutagen.mp4 import MP4, MP4Cover
|
from mutagen.mp4 import MP4, MP4Cover
|
||||||
import spotipy
|
import spotipy
|
||||||
import spotipy.oauth2 as oauth2
|
import spotipy.oauth2 as oauth2
|
||||||
import eyed3
|
import urllib2
|
||||||
import requests
|
|
||||||
import pafy
|
import pafy
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@@ -28,7 +28,6 @@ def generateSongName(raw_song):
|
|||||||
raw_song = tags['artists'][0]['name'] + ' - ' + tags['name']
|
raw_song = tags['artists'][0]['name'] + ' - ' + tags['name']
|
||||||
return raw_song
|
return raw_song
|
||||||
|
|
||||||
|
|
||||||
def generateMetaTags(raw_song):
|
def generateMetaTags(raw_song):
|
||||||
try:
|
try:
|
||||||
if isSpotify(raw_song):
|
if isSpotify(raw_song):
|
||||||
@@ -48,11 +47,10 @@ def generateMetaTags(raw_song):
|
|||||||
except BaseException:
|
except BaseException:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def generateYouTubeURL(raw_song):
|
def generateYouTubeURL(raw_song):
|
||||||
song = generateSongName(raw_song)
|
song = generateSongName(raw_song)
|
||||||
searchURL = generateSearchURL(song)
|
searchURL = generateSearchURL(song)
|
||||||
item = requests.get(searchURL).text
|
item = urllib2.urlopen(searchURL).read()
|
||||||
items_parse = BeautifulSoup(item, "html.parser")
|
items_parse = BeautifulSoup(item, "html.parser")
|
||||||
check = 1
|
check = 1
|
||||||
if args.manual:
|
if args.manual:
|
||||||
@@ -80,7 +78,6 @@ def generateYouTubeURL(raw_song):
|
|||||||
full_link = "youtube.com" + result
|
full_link = "youtube.com" + result
|
||||||
return full_link
|
return full_link
|
||||||
|
|
||||||
|
|
||||||
def goPafy(raw_song):
|
def goPafy(raw_song):
|
||||||
trackURL = generateYouTubeURL(raw_song)
|
trackURL = generateYouTubeURL(raw_song)
|
||||||
if trackURL is None:
|
if trackURL is None:
|
||||||
@@ -88,7 +85,6 @@ def goPafy(raw_song):
|
|||||||
else:
|
else:
|
||||||
return pafy.new(trackURL)
|
return pafy.new(trackURL)
|
||||||
|
|
||||||
|
|
||||||
def getYouTubeTitle(content, number):
|
def getYouTubeTitle(content, number):
|
||||||
title = content.title
|
title = content.title
|
||||||
if number is None:
|
if number is None:
|
||||||
@@ -96,7 +92,6 @@ def getYouTubeTitle(content, number):
|
|||||||
else:
|
else:
|
||||||
return str(number) + '. ' + title
|
return str(number) + '. ' + title
|
||||||
|
|
||||||
|
|
||||||
def feedTracks(file, tracks):
|
def feedTracks(file, tracks):
|
||||||
with open(file, 'a') as fout:
|
with open(file, 'a') as fout:
|
||||||
for item in tracks['items']:
|
for item in tracks['items']:
|
||||||
@@ -106,7 +101,6 @@ def feedTracks(file, tracks):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def feedPlaylist(username):
|
def feedPlaylist(username):
|
||||||
playlists = spotify.user_playlists(username)
|
playlists = spotify.user_playlists(username)
|
||||||
links = []
|
links = []
|
||||||
@@ -128,26 +122,22 @@ def feedPlaylist(username):
|
|||||||
feedTracks(file, tracks)
|
feedTracks(file, tracks)
|
||||||
|
|
||||||
# Generate name for the song to be downloaded
|
# Generate name for the song to be downloaded
|
||||||
|
|
||||||
|
|
||||||
def generateFileName(content):
|
def generateFileName(content):
|
||||||
title = (content.title).replace(' ', '_')
|
title = (content.title).replace(' ', '_')
|
||||||
title = slugify(title, ok='-_()[]{}', lower=False)
|
title = slugify(title, ok='-_()[]{}', lower=False)
|
||||||
return fixEncoding(title)
|
return fixEncoding(title)
|
||||||
|
|
||||||
|
def downloadSong(content, input_ext):
|
||||||
def downloadSong(content):
|
|
||||||
music_file = generateFileName(content)
|
music_file = generateFileName(content)
|
||||||
if input_ext == '.webm':
|
if input_ext == '.webm':
|
||||||
link = content.getbestaudio(preftype='webm')
|
link = content.getbestaudio(preftype='webm')
|
||||||
if link is not None:
|
if link is not None:
|
||||||
link.download(filepath='Music/' + music_file + input_ext)
|
link.download(filepath='Music/' + music_file + input_ext)
|
||||||
else:
|
else:
|
||||||
link = content.getbestaudio(preftype="m4a")
|
link = content.getbestaudio(preftype='m4a')
|
||||||
if link is not None:
|
if link is not None:
|
||||||
link.download(filepath='Music/' + music_file + input_ext)
|
link.download(filepath='Music/' + music_file + input_ext)
|
||||||
|
|
||||||
|
|
||||||
def convertWithAvconv(music_file, input_ext, output_ext, verbose):
|
def convertWithAvconv(music_file, input_ext, output_ext, verbose):
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
avconv_path = 'Scripts\\avconv.exe'
|
avconv_path = 'Scripts\\avconv.exe'
|
||||||
@@ -163,7 +153,6 @@ def convertWithAvconv(music_file, input_ext, output_ext, verbose):
|
|||||||
output_ext + '"')
|
output_ext + '"')
|
||||||
os.remove('Music/' + music_file + input_ext)
|
os.remove('Music/' + music_file + input_ext)
|
||||||
|
|
||||||
|
|
||||||
def convertWithFfmpeg(music_file, input_ext, output_ext, verbose):
|
def convertWithFfmpeg(music_file, input_ext, output_ext, verbose):
|
||||||
# What are the differences and similarities between ffmpeg, libav, and avconv?
|
# What are the differences and similarities between ffmpeg, libav, and avconv?
|
||||||
# https://stackoverflow.com/questions/9477115
|
# https://stackoverflow.com/questions/9477115
|
||||||
@@ -210,20 +199,20 @@ def convertWithFfmpeg(music_file, input_ext, output_ext, verbose):
|
|||||||
'"Music/' + music_file + output_ext + '" ')
|
'"Music/' + music_file + output_ext + '" ')
|
||||||
os.remove('Music/' + music_file + input_ext)
|
os.remove('Music/' + music_file + input_ext)
|
||||||
|
|
||||||
|
|
||||||
def checkExists(music_file, raw_song, islist):
|
def checkExists(music_file, raw_song, islist):
|
||||||
if os.path.exists("Music/" + music_file + input_ext + ".temp"):
|
files = os.listdir("Music")
|
||||||
os.remove("Music/" + music_file + input_ext + ".temp")
|
for file in files:
|
||||||
if args.no_convert:
|
if file.endswith(".temp"):
|
||||||
extension = input_ext
|
os.remove("Music/" + file)
|
||||||
else:
|
continue
|
||||||
extension = output_ext
|
|
||||||
if os.path.isfile("Music/" + music_file + extension):
|
if file.startswith(music_file):
|
||||||
if extension == '.mp3':
|
#audiofile = eyed3.load("Music/" + music_file + output_ext)
|
||||||
audiofile = eyed3.load("Music/" + music_file + extension)
|
#if isSpotify(raw_song) and not audiofile.tag.title == (
|
||||||
if isSpotify(raw_song) and not audiofile.tag.title == (
|
# generateMetaTags(raw_song))['name']:
|
||||||
generateMetaTags(raw_song))['name']:
|
# os.remove("Music/" + music_file + output_ext)
|
||||||
os.remove("Music/" + music_file + extension)
|
# return False
|
||||||
|
os.remove("Music/" + file)
|
||||||
return False
|
return False
|
||||||
if islist:
|
if islist:
|
||||||
return True
|
return True
|
||||||
@@ -231,7 +220,7 @@ def checkExists(music_file, raw_song, islist):
|
|||||||
prompt = raw_input(
|
prompt = raw_input(
|
||||||
'Song with same name has already been downloaded. Re-download? (y/n): ').lower()
|
'Song with same name has already been downloaded. Re-download? (y/n): ').lower()
|
||||||
if prompt == "y":
|
if prompt == "y":
|
||||||
os.remove("Music/" + music_file + extension)
|
os.remove("Music/" + file)
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
@@ -245,28 +234,27 @@ def fixSong(music_file, meta_tags, output_ext):
|
|||||||
fixSongM4A(music_file, meta_tags, output_ext)
|
fixSongM4A(music_file, meta_tags, output_ext)
|
||||||
elif output_ext == '.mp3':
|
elif output_ext == '.mp3':
|
||||||
print('Fixing meta-tags')
|
print('Fixing meta-tags')
|
||||||
fixSongMP3(music_file, meta_tags)
|
fixSongMP3(music_file, meta_tags, output_ext)
|
||||||
else:
|
else:
|
||||||
print('Cannot embed meta-tags into given output extension')
|
print('Cannot embed meta-tags into given output extension')
|
||||||
|
|
||||||
def fixSongMP3(music_file, meta_tags):
|
def fixSongMP3(music_file, meta_tags, output_ext):
|
||||||
audiofile = eyed3.load("Music/" + music_file + '.mp3')
|
audiofile = EasyID3('Music/' + music_file + output_ext)
|
||||||
audiofile.tag.artist = meta_tags['artists'][0]['name']
|
#audiofile = ID3('Music/' + music_file + output_ext)
|
||||||
audiofile.tag.album_artist = meta_tags['artists'][0]['name']
|
audiofile['artist'] = "Artist"
|
||||||
audiofile.tag.album = meta_tags['album']['name']
|
audiofile['albumartist'] = "Artist"
|
||||||
audiofile.tag.title = meta_tags['name']
|
audiofile['album'] = "Album"
|
||||||
if meta_tags['genre']:
|
audiofile['title'] = "Title"
|
||||||
audiofile.tag.genre = meta_tags['genre']
|
audiofile['genre'] = "genre"
|
||||||
audiofile.tag.track_num = meta_tags['track_number']
|
audiofile['tracknumber'] = "1"
|
||||||
audiofile.tag.disc_num = meta_tags['disc_number']
|
audiofile['discnumber'] = "2"
|
||||||
audiofile.tag.release_date = meta_tags['release_date']
|
audiofile['date'] = "2000"
|
||||||
albumart = requests.get(meta_tags['album']['images'][0]['url'], stream=True).raw
|
audiofile.save(v2_version=3)
|
||||||
with open('last_albumart.jpg', 'wb') as out_file:
|
audiofile = ID3('Music/' + music_file + output_ext)
|
||||||
copyfileobj(albumart, out_file)
|
albumart = urllib2.urlopen(meta_tags['album']['images'][0]['url']).read()
|
||||||
with open('last_albumart.jpg', 'rb') as albumart:
|
audiofile["APIC"] = APIC(encoding=3, mime='image/jpeg', type=3, desc=u'Cover', data=albumart)
|
||||||
audiofile.tag.images.set(3, albumart.read(), 'image/jpeg')
|
albumart.close()
|
||||||
audiofile.tag.save(version=(2, 3, 0))
|
audiofile.save(v2_version=3)
|
||||||
|
|
||||||
|
|
||||||
def fixSongM4A(music_file, meta_tags, output_ext):
|
def fixSongM4A(music_file, meta_tags, output_ext):
|
||||||
# eyed serves only mp3 not aac so using mutagen
|
# eyed serves only mp3 not aac so using mutagen
|
||||||
@@ -294,11 +282,9 @@ def fixSongM4A(music_file, meta_tags, output_ext):
|
|||||||
audiofile[tags['year']] = meta_tags['release_date']
|
audiofile[tags['year']] = meta_tags['release_date']
|
||||||
audiofile[tags['track']] = [(meta_tags['track_number'], 0)]
|
audiofile[tags['track']] = [(meta_tags['track_number'], 0)]
|
||||||
audiofile[tags['disk']] = [(meta_tags['disc_number'], 0)]
|
audiofile[tags['disk']] = [(meta_tags['disc_number'], 0)]
|
||||||
albumart = requests.get(meta_tags['album']['images'][0]['url'], stream=True).raw
|
albumart = urllib2.urlopen(meta_tags['album']['images'][0]['url']).read()
|
||||||
with open('last_albumart.jpg', 'wb') as out_file:
|
audiofile["covr"] = [ MP4Cover(albumart, imageformat=MP4Cover.FORMAT_JPEG) ]
|
||||||
copyfileobj(albumart, out_file)
|
albumart.close()
|
||||||
with open('last_albumart.jpg', 'rb') as albumart:
|
|
||||||
audiofile["covr"] = [ MP4Cover(albumart.read(), imageformat=MP4Cover.FORMAT_JPEG) ]
|
|
||||||
audiofile.save()
|
audiofile.save()
|
||||||
|
|
||||||
def convertSong(music_file, input_ext, output_ext, ffmpeg, verbose):
|
def convertSong(music_file, input_ext, output_ext, ffmpeg, verbose):
|
||||||
@@ -314,8 +300,6 @@ def convertSong(music_file, input_ext, output_ext, ffmpeg, verbose):
|
|||||||
|
|
||||||
|
|
||||||
# Logic behind preparing the song to download to finishing meta-tags
|
# Logic behind preparing the song to download to finishing meta-tags
|
||||||
|
|
||||||
|
|
||||||
def grabSingle(raw_song, number=None):
|
def grabSingle(raw_song, number=None):
|
||||||
if number:
|
if number:
|
||||||
islist = True
|
islist = True
|
||||||
@@ -327,7 +311,7 @@ def grabSingle(raw_song, number=None):
|
|||||||
print(getYouTubeTitle(content, number))
|
print(getYouTubeTitle(content, number))
|
||||||
music_file = generateFileName(content)
|
music_file = generateFileName(content)
|
||||||
if not checkExists(music_file, raw_song, islist=islist):
|
if not checkExists(music_file, raw_song, islist=islist):
|
||||||
downloadSong(content)
|
downloadSong(content, args.input_ext)
|
||||||
print('')
|
print('')
|
||||||
if not args.no_convert:
|
if not args.no_convert:
|
||||||
convertSong(music_file, args.input_ext, args.output_ext, args.ffmpeg, args.verbose)
|
convertSong(music_file, args.input_ext, args.output_ext, args.ffmpeg, args.verbose)
|
||||||
@@ -335,8 +319,6 @@ def grabSingle(raw_song, number=None):
|
|||||||
fixSong(music_file, meta_tags, args.output_ext)
|
fixSong(music_file, meta_tags, args.output_ext)
|
||||||
|
|
||||||
# Fix python2 encoding issues
|
# Fix python2 encoding issues
|
||||||
|
|
||||||
|
|
||||||
def grabList(file):
|
def grabList(file):
|
||||||
lines = open(file, 'r').read()
|
lines = open(file, 'r').read()
|
||||||
lines = lines.splitlines()
|
lines = lines.splitlines()
|
||||||
@@ -375,8 +357,6 @@ if __name__ == '__main__':
|
|||||||
if not os.path.exists("Music"):
|
if not os.path.exists("Music"):
|
||||||
os.makedirs("Music")
|
os.makedirs("Music")
|
||||||
|
|
||||||
cleanTemp()
|
|
||||||
|
|
||||||
# Please respect this user token :)
|
# Please respect this user token :)
|
||||||
oauth2 = oauth2.SpotifyClientCredentials(
|
oauth2 = oauth2.SpotifyClientCredentials(
|
||||||
client_id='4fe3fecfe5334023a1472516cc99d805',
|
client_id='4fe3fecfe5334023a1472516cc99d805',
|
||||||
@@ -390,12 +370,12 @@ if __name__ == '__main__':
|
|||||||
if not args.verbose:
|
if not args.verbose:
|
||||||
eyed3.log.setLevel("ERROR")
|
eyed3.log.setLevel("ERROR")
|
||||||
|
|
||||||
if args.ffmpeg:
|
#if args.ffmpeg:
|
||||||
input_ext = args.input_ext
|
# input_ext = args.input_ext
|
||||||
output_ext = args.output_ext
|
# output_ext = args.output_ext
|
||||||
else:
|
#else:
|
||||||
input_ext = '.m4a'
|
# input_ext = '.m4a'
|
||||||
output_ext = '.mp3'
|
# output_ext = '.mp3'
|
||||||
|
|
||||||
if args.song:
|
if args.song:
|
||||||
grabSingle(raw_song=args.song)
|
grabSingle(raw_song=args.song)
|
||||||
|
|||||||
Reference in New Issue
Block a user