Add docstrings; Remove verbose comments; Fix errors introduced with cleanup

This comment will:
- Transform docstrings above functions into docstrings
- Remove some way too verbose comments
- Apply some more recommendations from PEP8 forgotten last time
- Fix some errors introduced with the first code cleanup

Work left to do:
- Add params to docstrings
- Rename file variables
This commit is contained in:
Linus
2017-06-28 16:11:28 +02:00
parent 587f907ed8
commit 0f384bb5ee
5 changed files with 45 additions and 44 deletions

View File

@@ -4,11 +4,13 @@ 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 ' + input_song + ' to ' + output_song.split('.')[-1])
print('Converting {0} to {1}'.format(
input_song, output_song.split('.')[-1]))
if avconv:
exit_code = convert_with_avconv(input_song, output_song, verbose)
else:
@@ -18,7 +20,7 @@ def song(input_song, output_song, avconv=False, verbose=False):
def convert_with_avconv(input_song, output_song, verbose):
# different path for windows
"""Convert the audio file using avconv."""
if os.name == 'nt':
avconv_path = 'Scripts\\avconv.exe'
else:
@@ -39,13 +41,16 @@ def convert_with_avconv(input_song, output_song, verbose):
def convert_with_ffmpeg(input_song, output_song, verbose):
# What are the differences and similarities between ffmpeg, libav, and avconv?
# https://stackoverflow.com/questions/9477115
# ffmeg encoders high to lower quality
# libopus > libvorbis >= libfdk_aac > aac > libmp3lame
# libfdk_aac due to copyrights needs to be compiled by end user
# on MacOS brew install ffmpeg --with-fdk-aac will do just that. Other OS?
# https://trac.ffmpeg.org/wiki/Encode/AAC
"""Convert the audio file using FFMpeg.
What are the differences and similarities between ffmpeg, libav, and avconv?
https://stackoverflow.com/questions/9477115
ffmeg encoders high to lower quality
libopus > libvorbis >= libfdk_aac > aac > libmp3lame
libfdk_aac due to copyrights needs to be compiled by end user
on MacOS brew install ffmpeg --with-fdk-aac will do just that. Other OS?
https://trac.ffmpeg.org/wiki/Encode/AAC
"""
if os.name == "nt":
ffmpeg_pre = 'Scripts\\ffmpeg.exe '

View File

@@ -10,8 +10,8 @@ except ImportError:
import urllib.request as urllib2
# check if input file title matches with expected title
def compare(file, metadata):
"""Check if the input file title matches the expected title."""
already_tagged = False
try:
if file.endswith('.mp3'):
@@ -29,6 +29,7 @@ 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:
@@ -46,6 +47,7 @@ def embed(music_file, meta_tags):
def embed_mp3(music_file, meta_tags):
"""Embed metadata to MP3 files."""
# EasyID3 is fun to use ;)
audiofile = EasyID3('Music/' + music_file)
audiofile['artist'] = meta_tags['artists'][0]['name']
@@ -81,6 +83,7 @@ def embed_mp3(music_file, meta_tags):
def embed_m4a(music_file, meta_tags):
"""Embed metadata to M4A files."""
# Apple has specific tags - see mutagen docs -
# http://mutagen.readthedocs.io/en/latest/api/mp4.html
tags = {'album': '\xa9alb',

View File

@@ -10,8 +10,8 @@ except ImportError:
from urllib.request import quote
# method to input (user playlists) and (track when using manual mode)
def input_link(links):
"""Let the user input a number."""
while True:
try:
the_chosen_one = int(user_input('>> Choose your number: '))
@@ -25,16 +25,16 @@ def input_link(links):
print('Choose a valid number!')
# take input correctly for both python2 & 3
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)
# remove first song from .txt
def trim_song(file):
"""Remove the first song from file."""
with open(file, 'r') as file_in:
data = file_in.read().splitlines(True)
with open(file, 'w') as file_out:
@@ -77,27 +77,29 @@ def get_arguments():
return parser.parse_args()
# check if input song is spotify link
def is_spotify(raw_song):
if (len(raw_song) == 22 and raw_song.replace(" ", "%20") == raw_song) or (raw_song.find('spotify') > -1):
"""Check if the input song is a Spotify link."""
if (len(raw_song) == 22 and raw_song.replace(" ", "%20") == raw_song) or \
(raw_song.find('spotify') > -1):
return True
else:
return False
# generate filename of the song to be downloaded
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)
# please respect these credentials :)
def generate_token():
"""Generate the token. Please respect these credentials :)"""
credentials = oauth2.SpotifyClientCredentials(
client_id='4fe3fecfe5334023a1472516cc99d805',
client_secret='0f02b7c483c04257984695007a4a8d5c')
@@ -106,20 +108,22 @@ def generate_token():
def generate_search_url(song):
"""Generate YouTube search URL for the given song."""
# urllib2.quote() encodes URL with special characters
url = "https://www.youtube.com/results?sp=EgIQAQ%253D%253D&q={0}".format(
quote(song))
return url
# fix encoding issues in python2
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