mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-12-08 20:39:08 +00:00
Drop python 2 compatibility (#107)
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
import subprocess
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def song(input_song, output_song, avconv=False, verbose=False):
|
||||
"""Do the audio format conversion."""
|
||||
if not input_song == output_song:
|
||||
if sys.version_info < (3, 0):
|
||||
input_song = input_song.encode('utf-8')
|
||||
output_song = output_song.encode('utf-8')
|
||||
print('Converting {0} to {1}'.format(
|
||||
input_song, output_song.split('.')[-1]))
|
||||
if avconv:
|
||||
|
||||
@@ -1,26 +1,21 @@
|
||||
from mutagen.easyid3 import EasyID3
|
||||
from mutagen.id3 import ID3, APIC
|
||||
from mutagen.mp4 import MP4, MP4Cover
|
||||
import sys
|
||||
|
||||
# urllib2 is urllib.request in python3
|
||||
try:
|
||||
import urllib2
|
||||
except ImportError:
|
||||
import urllib.request as urllib2
|
||||
import urllib.request
|
||||
|
||||
|
||||
def compare(file, metadata):
|
||||
"""Check if the input file title matches the expected title."""
|
||||
def compare(music_file, metadata):
|
||||
"""Check if the input music file title matches the expected title."""
|
||||
already_tagged = False
|
||||
try:
|
||||
if file.endswith('.mp3'):
|
||||
audiofile = EasyID3('Music/' + file)
|
||||
if music_file.endswith('.mp3'):
|
||||
audiofile = EasyID3('Music/' + music_file)
|
||||
# fetch track title metadata
|
||||
already_tagged = audiofile['title'][0] == metadata['name']
|
||||
elif file.endswith('.m4a'):
|
||||
elif music_file.endswith('.m4a'):
|
||||
tags = {'title': '\xa9nam'}
|
||||
audiofile = MP4('Music/' + file)
|
||||
audiofile = MP4('Music/' + music_file)
|
||||
# fetch track title metadata
|
||||
already_tagged = audiofile[tags['title']] == metadata['name']
|
||||
except (KeyError, TypeError):
|
||||
@@ -30,8 +25,6 @@ def compare(file, metadata):
|
||||
|
||||
def embed(music_file, meta_tags):
|
||||
"""Embed metadata."""
|
||||
if sys.version_info < (3, 0):
|
||||
music_file = music_file.encode('utf-8')
|
||||
if meta_tags is None:
|
||||
print('Could not find meta-tags')
|
||||
return None
|
||||
@@ -74,7 +67,7 @@ def embed_mp3(music_file, meta_tags):
|
||||
audiofile['copyright'] = meta_tags['copyright']
|
||||
audiofile.save(v2_version=3)
|
||||
audiofile = ID3('Music/' + music_file)
|
||||
albumart = urllib2.urlopen(meta_tags['album']['images'][0]['url'])
|
||||
albumart = urllib.request.urlopen(meta_tags['album']['images'][0]['url'])
|
||||
audiofile["APIC"] = APIC(encoding=3, mime='image/jpeg', type=3,
|
||||
desc=u'Cover', data=albumart.read())
|
||||
albumart.close()
|
||||
@@ -117,7 +110,7 @@ def embed_m4a(music_file, meta_tags):
|
||||
audiofile[tags['genre']] = meta_tags['genre']
|
||||
if meta_tags['copyright']:
|
||||
audiofile[tags['copyright']] = meta_tags['copyright']
|
||||
albumart = urllib2.urlopen(meta_tags['album']['images'][0]['url'])
|
||||
albumart = urllib.request.urlopen(meta_tags['album']['images'][0]['url'])
|
||||
audiofile[tags['albumart']] = [MP4Cover(
|
||||
albumart.read(), imageformat=MP4Cover.FORMAT_JPEG)]
|
||||
albumart.close()
|
||||
|
||||
42
core/misc.py
42
core/misc.py
@@ -1,20 +1,15 @@
|
||||
import argparse
|
||||
import sys
|
||||
import os
|
||||
from slugify import slugify
|
||||
import argparse
|
||||
import spotipy.oauth2 as oauth2
|
||||
|
||||
try:
|
||||
from urllib2 import quote
|
||||
except ImportError:
|
||||
from urllib.request import quote
|
||||
|
||||
from urllib.request import quote
|
||||
from slugify import slugify
|
||||
|
||||
def input_link(links):
|
||||
"""Let the user input a number."""
|
||||
while True:
|
||||
try:
|
||||
the_chosen_one = int(user_input('>> Choose your number: '))
|
||||
the_chosen_one = int(input('>> Choose your number: '))
|
||||
if 1 <= the_chosen_one <= len(links):
|
||||
return links[the_chosen_one - 1]
|
||||
elif the_chosen_one == 0:
|
||||
@@ -25,14 +20,6 @@ def input_link(links):
|
||||
print('Choose a valid number!')
|
||||
|
||||
|
||||
def user_input(string=''):
|
||||
"""Take input correctly for both Python 2 & 3."""
|
||||
if sys.version_info > (3, 0):
|
||||
return input(string)
|
||||
else:
|
||||
return raw_input(string)
|
||||
|
||||
|
||||
def trim_song(file):
|
||||
"""Remove the first song from file."""
|
||||
with open(file, 'r') as file_in:
|
||||
@@ -88,14 +75,11 @@ def is_spotify(raw_song):
|
||||
|
||||
def generate_filename(title):
|
||||
"""Generate filename of the song to be downloaded."""
|
||||
# IMO python2 sucks dealing with unicode
|
||||
title = fix_encoding(title)
|
||||
title = fix_decoding(title)
|
||||
title = title.replace(' ', '_')
|
||||
|
||||
# slugify removes any special characters
|
||||
filename = slugify(title, ok='-_()[]{}', lower=False)
|
||||
return fix_encoding(filename)
|
||||
return filename
|
||||
|
||||
|
||||
def generate_token():
|
||||
@@ -109,26 +93,12 @@ def generate_token():
|
||||
|
||||
def generate_search_url(song):
|
||||
"""Generate YouTube search URL for the given song."""
|
||||
# urllib2.quote() encodes URL with special characters
|
||||
# urllib.request.quote() encodes URL with special characters
|
||||
url = u"https://www.youtube.com/results?sp=EgIQAQ%253D%253D&q={0}".format(
|
||||
quote(song))
|
||||
return url
|
||||
|
||||
|
||||
def fix_encoding(query):
|
||||
"""Fix encoding issues in Python 2."""
|
||||
if sys.version_info < (3, 0):
|
||||
query = query.encode('utf-8')
|
||||
return query
|
||||
|
||||
|
||||
def fix_decoding(query):
|
||||
"""Fix decoding issues in Python 2."""
|
||||
if sys.version_info < (3, 0):
|
||||
query = query.decode('utf-8')
|
||||
return query
|
||||
|
||||
|
||||
def filter_path(path):
|
||||
os.chdir(sys.path[0])
|
||||
if not os.path.exists(path):
|
||||
|
||||
Reference in New Issue
Block a user