mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2026-02-10 03:19:29 +00:00
Fix python3 compatibility
This commit is contained in:
29
spotdl.py
29
spotdl.py
@@ -1,9 +1,8 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from random import choice
|
|
||||||
from shutil import copyfileobj
|
from shutil import copyfileobj
|
||||||
from sys import path
|
from sys import path, version_info
|
||||||
import spotipy
|
import spotipy
|
||||||
import eyed3
|
import eyed3
|
||||||
import requests
|
import requests
|
||||||
@@ -12,6 +11,9 @@ import os
|
|||||||
import argparse
|
import argparse
|
||||||
#import spotipy.util as util
|
#import spotipy.util as util
|
||||||
|
|
||||||
|
if version_info > (3,0):
|
||||||
|
raw_input = input
|
||||||
|
|
||||||
eyed3.log.setLevel("ERROR")
|
eyed3.log.setLevel("ERROR")
|
||||||
|
|
||||||
os.chdir(path[0])
|
os.chdir(path[0])
|
||||||
@@ -61,7 +63,7 @@ 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)
|
||||||
raw_song = (tags['artists'][0]['name'] + ' - ' + tags['name']).encode('utf-8')
|
raw_song = fixEncoding(tags['artists'][0]['name'] + ' - ' + tags['name'])
|
||||||
return raw_song
|
return raw_song
|
||||||
|
|
||||||
def generateMetaTags(raw_song):
|
def generateMetaTags(raw_song):
|
||||||
@@ -71,7 +73,7 @@ def generateMetaTags(raw_song):
|
|||||||
return spotify.search(raw_song, limit=1)['tracks']['items'][0]
|
return spotify.search(raw_song, limit=1)['tracks']['items'][0]
|
||||||
|
|
||||||
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")
|
||||||
return URL
|
return URL
|
||||||
|
|
||||||
def generateYouTubeURL(raw_song):
|
def generateYouTubeURL(raw_song):
|
||||||
@@ -86,7 +88,7 @@ def generateYouTubeURL(raw_song):
|
|||||||
print('')
|
print('')
|
||||||
for x in items_parse.find_all('h3', {'class':'yt-lockup-title'}):
|
for x in items_parse.find_all('h3', {'class':'yt-lockup-title'}):
|
||||||
if not x.find('channel') == -1 or not x.find('googleads') == -1:
|
if not x.find('channel') == -1 or not x.find('googleads') == -1:
|
||||||
print((str(check) + '. ' + x.get_text()).encode('utf-8'))
|
print(fixEncoding(str(check) + '. ' + x.get_text()))
|
||||||
links.append(x.find('a')['href'])
|
links.append(x.find('a')['href'])
|
||||||
check += 1
|
check += 1
|
||||||
result = getInputLink(links)
|
result = getInputLink(links)
|
||||||
@@ -103,14 +105,14 @@ def goPafy(raw_song):
|
|||||||
return pafy.new(trackURL)
|
return pafy.new(trackURL)
|
||||||
|
|
||||||
def getYouTubeTitle(content, number):
|
def getYouTubeTitle(content, number):
|
||||||
title = (content.title).encode("utf-8")
|
title = fixEncoding(content.title)
|
||||||
if number == None:
|
if number == None:
|
||||||
return title
|
return title
|
||||||
else:
|
else:
|
||||||
return str(number) + '. ' + title
|
return str(number) + '. ' + title
|
||||||
|
|
||||||
def generateFileName(content):
|
def generateFileName(content):
|
||||||
return ((content.title).replace("\\", "_").replace("/", "_").replace(":", "_").replace("*", "_").replace("?", "_").replace('"', "_").replace("<", "_").replace(">", "_").replace("|", "_").replace(" ", "_")).encode('utf-8')
|
return fixEncoding((content.title).replace("\\", "_").replace("/", "_").replace(":", "_").replace("*", "_").replace("?", "_").replace('"', "_").replace("<", "_").replace(">", "_").replace("|", "_").replace(" ", "_"))
|
||||||
|
|
||||||
def downloadSong(content):
|
def downloadSong(content):
|
||||||
music_file = generateFileName(content)
|
music_file = generateFileName(content)
|
||||||
@@ -173,7 +175,7 @@ def grabSingle(raw_song, number):
|
|||||||
else:
|
else:
|
||||||
islist = False
|
islist = False
|
||||||
content = goPafy(raw_song)
|
content = goPafy(raw_song)
|
||||||
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)
|
||||||
@@ -202,14 +204,19 @@ def grabList(file):
|
|||||||
print('')
|
print('')
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
graceQuit()
|
graceQuit()
|
||||||
except Exception as e:
|
except:
|
||||||
print e
|
|
||||||
lines.append(raw_song)
|
lines.append(raw_song)
|
||||||
trimSong(file)
|
trimSong(file)
|
||||||
with open(file, 'a') as myfile:
|
with open(file, 'a') as myfile:
|
||||||
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 fixEncoding(query):
|
||||||
|
if version_info > (3,0):
|
||||||
|
return query
|
||||||
|
else:
|
||||||
|
return query.decode('utf-8').encode('utf-8')
|
||||||
|
|
||||||
def graceQuit():
|
def graceQuit():
|
||||||
print('')
|
print('')
|
||||||
print('')
|
print('')
|
||||||
@@ -221,6 +228,6 @@ while True:
|
|||||||
if m.endswith('.m4a.temp'):
|
if m.endswith('.m4a.temp'):
|
||||||
os.remove('Music/' + m)
|
os.remove('Music/' + m)
|
||||||
print('')
|
print('')
|
||||||
command = raw_input('>> Enter a song/cmd: ').decode('utf-8').encode('utf-8')
|
command = fixEncoding(raw_input('>> Enter a song/cmd: '))
|
||||||
print('')
|
print('')
|
||||||
initializeInput(command)
|
initializeInput(command)
|
||||||
|
|||||||
Reference in New Issue
Block a user