mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Fix bugs
This commit is contained in:
60
spotdl.py
60
spotdl.py
@@ -10,7 +10,7 @@ import requests
|
|||||||
import pafy
|
import pafy
|
||||||
import os
|
import os
|
||||||
import argparse
|
import argparse
|
||||||
#import spotipy.util as util
|
import spotipy.util as util
|
||||||
|
|
||||||
eyed3.log.setLevel("ERROR")
|
eyed3.log.setLevel("ERROR")
|
||||||
|
|
||||||
@@ -32,12 +32,6 @@ if args.no_convert:
|
|||||||
if args.manual:
|
if args.manual:
|
||||||
print("-m, --manual choose the song to download manually")
|
print("-m, --manual choose the song to download manually")
|
||||||
|
|
||||||
def graceQuit():
|
|
||||||
print('')
|
|
||||||
print('')
|
|
||||||
print('Exitting..')
|
|
||||||
exit()
|
|
||||||
|
|
||||||
def initializeInput(command):
|
def initializeInput(command):
|
||||||
if command == 'list':
|
if command == 'list':
|
||||||
grabList(file='list.txt')
|
grabList(file='list.txt')
|
||||||
@@ -67,15 +61,15 @@ def isSpotify(raw_song):
|
|||||||
def generateSongName(raw_song):
|
def generateSongName(raw_song):
|
||||||
if isSpotify(raw_song):
|
if isSpotify(raw_song):
|
||||||
tags = generateMetaTags(raw_song)
|
tags = generateMetaTags(raw_song)
|
||||||
song = (tags['artists'][0]['name'] + ' - ' + tags['name']).encode('utf-8')
|
raw_song = (tags['artists'][0]['name'] + ' - ' + tags['name']).encode('utf-8')
|
||||||
return song
|
return raw_song
|
||||||
|
|
||||||
def generateMetaTags(raw_song):
|
def generateMetaTags(raw_song):
|
||||||
if isSpotify(raw_song):
|
if isSpotify(raw_song):
|
||||||
return spotify.track(raw_song)
|
return spotify.track(raw_song)
|
||||||
else:
|
else:
|
||||||
# Search for song on Spotify and generate tags
|
print spotify.search(raw_song, limit=1)
|
||||||
return
|
return spotify.search(raw_song, limit=1)
|
||||||
|
|
||||||
def generateSearchURL(song):
|
def generateSearchURL(song):
|
||||||
URL = "https://www.youtube.com/results?sp=EgIQAQ%253D%253D&q=" + song.replace(" ", "%20").encode('utf-8')
|
URL = "https://www.youtube.com/results?sp=EgIQAQ%253D%253D&q=" + song.replace(" ", "%20").encode('utf-8')
|
||||||
@@ -116,14 +110,14 @@ def getYouTubeTitle(content, number):
|
|||||||
else:
|
else:
|
||||||
return str(number) + '. ' + title
|
return str(number) + '. ' + title
|
||||||
|
|
||||||
|
def generateFileName(content):
|
||||||
|
return ((content.title).replace("\\", "_").replace("/", "_").replace(":", "_").replace("*", "_").replace("?", "_").replace('"', "_").replace("<", "_").replace(">", "_").replace("|", "_").replace(" ", "_")).encode('utf-8')
|
||||||
|
|
||||||
def downloadSong(content):
|
def downloadSong(content):
|
||||||
music_file = generateFileName(content)
|
music_file = generateFileName(content)
|
||||||
link = content.getbestaudio(preftype="m4a")
|
link = content.getbestaudio(preftype="m4a")
|
||||||
link.download(filepath="Music/" + music_file + ".m4a")
|
link.download(filepath="Music/" + music_file + ".m4a")
|
||||||
|
|
||||||
def generateFileName(content):
|
|
||||||
return ((content.title).replace("\\", "_").replace("/", "_").replace(":", "_").replace("*", "_").replace("?", "_").replace('"', "_").replace("<", "_").replace(">", "_").replace("|", "_").replace(" ", "_")).encode('utf-8')
|
|
||||||
|
|
||||||
def convertToMP3(music_file):
|
def convertToMP3(music_file):
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
os.system('Scripts\\avconv.exe -loglevel 0 -i "' + 'Music/' + music_file + '.m4a" -ab 192k "' + 'Music/' + music_file + '.mp3"')
|
os.system('Scripts\\avconv.exe -loglevel 0 -i "' + 'Music/' + music_file + '.m4a" -ab 192k "' + 'Music/' + music_file + '.mp3"')
|
||||||
@@ -174,6 +168,24 @@ def fixSong(music_file, meta_tags):
|
|||||||
audiofile.tag.images.set(3,albumart,"image/jpeg")
|
audiofile.tag.images.set(3,albumart,"image/jpeg")
|
||||||
audiofile.tag.save(version=(2,3,0))
|
audiofile.tag.save(version=(2,3,0))
|
||||||
|
|
||||||
|
def grabSingle(raw_song, number):
|
||||||
|
if number:
|
||||||
|
islist = True
|
||||||
|
else:
|
||||||
|
islist = False
|
||||||
|
content = goPafy(raw_song)
|
||||||
|
print getYouTubeTitle(content, number)
|
||||||
|
music_file = generateFileName(content)
|
||||||
|
if not checkExists(music_file, raw_song, islist=islist):
|
||||||
|
downloadSong(content)
|
||||||
|
print('')
|
||||||
|
if not args.no_convert:
|
||||||
|
print('Converting ' + music_file + '.m4a to mp3')
|
||||||
|
convertToMP3(music_file)
|
||||||
|
print('Fixing meta-tags')
|
||||||
|
meta_tags = generateMetaTags(raw_song)
|
||||||
|
fixSong(music_file, meta_tags)
|
||||||
|
|
||||||
def grabList(file):
|
def grabList(file):
|
||||||
lines = open(file, 'r').read()
|
lines = open(file, 'r').read()
|
||||||
lines = lines.splitlines()
|
lines = lines.splitlines()
|
||||||
@@ -199,23 +211,11 @@ def grabList(file):
|
|||||||
myfile.write(raw_song)
|
myfile.write(raw_song)
|
||||||
print('Failed to download song. Will retry after other songs.')
|
print('Failed to download song. Will retry after other songs.')
|
||||||
|
|
||||||
def grabSingle(raw_song, number):
|
def graceQuit():
|
||||||
if number:
|
|
||||||
islist = True
|
|
||||||
else:
|
|
||||||
islist = False
|
|
||||||
content = goPafy(raw_song)
|
|
||||||
print getYouTubeTitle(content, number)
|
|
||||||
music_file = generateFileName(content)
|
|
||||||
if not checkExists(music_file, raw_song, islist=islist):
|
|
||||||
downloadSong(content)
|
|
||||||
print('')
|
print('')
|
||||||
if not args.no_convert:
|
print('')
|
||||||
print('Converting ' + music_file + '.m4a to mp3')
|
print('Exitting..')
|
||||||
convertToMP3(music_file)
|
exit()
|
||||||
print('Fixing meta-tags')
|
|
||||||
meta_tags = generateMetaTags(raw_song)
|
|
||||||
fixSong(music_file, meta_tags)
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
for m in os.listdir('Music/'):
|
for m in os.listdir('Music/'):
|
||||||
|
|||||||
Reference in New Issue
Block a user