Drop python 2 compatibility (#107)

This commit is contained in:
Ritiek Malhotra
2017-07-11 20:32:26 +05:30
committed by GitHub
parent b73d0b1049
commit 5add1fd0f8
8 changed files with 47 additions and 101 deletions

View File

@@ -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):