Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Monkeypatch tests #448

Merged
merged 9 commits into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Ability to pass multiple tracks with `-s` option ([@ritiek](https://github.com/ritiek)) (#442)

### Changed
- Change FFmpeg to use the built-in encoder `aac` instead of 3rd party `libfdk-aac` which does not
ship with the apt package ([@ritiek](https://github.com/ritiek)) (#448)
- Monkeypatch ever-changing network-relying tests ([@ritiek](https://github.com/ritiek)) (#448)
- Correct `.m4a` container before writing metadata so metadata fields shows up properly in
media players (especially iTunes) ([@ritiek](https://github.com/ritiek) with thanks to [@Amit-L](https://github.com/Amit-L)!) (#453)
- Refactored core downloading module ([@ritiek](https://github.com/ritiek)) (#410)
Expand Down
15 changes: 8 additions & 7 deletions spotdl/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from logzero import logger as log


"""What are the differences and similarities between ffmpeg, libav, and avconv?
"""
What are the differences and similarities between ffmpeg, libav, and avconv?
https://stackoverflow.com/questions/9477115

ffmeg encoders high to lower quality
Expand All @@ -25,10 +26,10 @@ def song(input_song, output_song, folder, avconv=False, trim_silence=False):
else:
return 0
if avconv:
exit_code = convert.with_avconv()
exit_code, command = convert.with_avconv()
else:
exit_code = convert.with_ffmpeg()
return exit_code
exit_code, command = convert.with_ffmpeg()
return exit_code, command


class Converter:
Expand Down Expand Up @@ -59,7 +60,7 @@ def with_avconv(self):
log.warning("--trim-silence not supported with avconv")

log.debug(command)
return subprocess.call(command)
return subprocess.call(command), command

def with_ffmpeg(self):
ffmpeg_pre = "ffmpeg -y "
Expand All @@ -84,7 +85,7 @@ def with_ffmpeg(self):
if output_ext == ".mp3":
ffmpeg_params = "-codec:a libmp3lame -ar 44100 "
elif output_ext == ".m4a":
ffmpeg_params = "-cutoff 20000 -codec:a libfdk_aac -ar 44100 "
ffmpeg_params = "-cutoff 20000 -codec:a aac -ar 44100 "

if output_ext == ".flac":
ffmpeg_params = "-codec:a flac -ar 44100 "
Expand All @@ -104,4 +105,4 @@ def with_ffmpeg(self):
)

log.debug(command)
return subprocess.call(command)
return subprocess.call(command), command
9 changes: 5 additions & 4 deletions spotdl/downloader.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import spotipy
import urllib
import os
from logzero import logger as log

from spotdl import const
from spotdl import metadata
from spotdl import convert
from spotdl import internals
from spotdl import spotify_tools
from spotdl import youtube_tools

import spotipy
from logzero import logger as log
import os


class CheckExists:
def __init__(self, music_file, meta_tags=None):
Expand Down
6 changes: 3 additions & 3 deletions spotdl/handle.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import appdirs
from spotdl import internals
from logzero import logger as log
import appdirs

import logging
import yaml
import argparse
import mimetypes

import os

from spotdl import internals


_LOG_LEVELS_STR = ["INFO", "WARNING", "ERROR", "DEBUG"]

Expand Down
2 changes: 1 addition & 1 deletion spotdl/internals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from logzero import logger as log
import os
import sys
from logzero import logger as log

from spotdl import const

Expand Down
5 changes: 3 additions & 2 deletions spotdl/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from mutagen.id3 import ID3, TORY, TYER, TPUB, APIC, USLT, COMM
from mutagen.mp4 import MP4, MP4Cover
from mutagen.flac import Picture, FLAC
from logzero import logger as log
from spotdl.const import TAG_PRESET, M4A_TAG_PRESET

import urllib.request
from logzero import logger as log

from spotdl.const import TAG_PRESET, M4A_TAG_PRESET


def compare(music_file, metadata):
Expand Down
11 changes: 6 additions & 5 deletions spotdl/spotdl.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/usr/bin/env python3

import sys
import platform
import pprint
import logzero
from logzero import logger as log

from spotdl import __version__
from spotdl import const
from spotdl import handle
from spotdl import internals
from spotdl import spotify_tools
from spotdl import youtube_tools
from spotdl import downloader
from logzero import logger as log
import logzero
import sys
import platform
import pprint


def debug_sys_info():
Expand Down
28 changes: 14 additions & 14 deletions spotdl/spotify_tools.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import spotipy
import spotipy.oauth2 as oauth2
import lyricwikia
from logzero import logger as log

from spotdl import internals

from slugify import slugify
from titlecase import titlecase
from logzero import logger as log
import pprint
import sys

from spotdl import internals


def generate_token():
""" Generate the token. Please respect these credentials :) """
Expand All @@ -29,8 +29,8 @@ def refresh_token():

# token is mandatory when using Spotify's API
# https://developer.spotify.com/news-stories/2017/01/27/removing-unauthenticated-calls-to-the-web-api/
token = generate_token()
spotify = spotipy.Spotify(auth=token)
_token = generate_token()
spotify = spotipy.Spotify(auth=_token)


def generate_metadata(raw_song):
Expand Down Expand Up @@ -87,12 +87,6 @@ def generate_metadata(raw_song):
return meta_tags


def write_user_playlist(username, text_file=None):
links = get_playlists(username=username)
playlist = internals.input_link(links)
return write_playlist(playlist, text_file)


def get_playlists(username):
""" Fetch user playlists when using the -u option. """
playlists = spotify.user_playlists(username)
Expand Down Expand Up @@ -121,6 +115,12 @@ def get_playlists(username):
return links


def write_user_playlist(username, text_file=None):
links = get_playlists(username=username)
playlist = internals.input_link(links)
return write_playlist(playlist, text_file)


def fetch_playlist(playlist):
try:
playlist_id = internals.extract_spotify_id(playlist)
Expand Down Expand Up @@ -154,7 +154,7 @@ def fetch_album(album):
return album


def fetch_album_from_artist(artist_url, album_type="album"):
def fetch_albums_from_artist(artist_url, album_type="album"):
"""
This funcction returns all the albums from a give artist_url using the US
market
Expand Down Expand Up @@ -191,7 +191,7 @@ def write_all_albums_from_artist(artist_url, text_file=None):
album_base_url = "https://open.spotify.com/album/"

# fetching all default albums
albums = fetch_album_from_artist(artist_url)
albums = fetch_albums_from_artist(artist_url)

# if no file if given, the default save file is in the current working
# directory with the name of the artist
Expand All @@ -204,7 +204,7 @@ def write_all_albums_from_artist(artist_url, text_file=None):
write_album(album_base_url + album["id"], text_file=text_file)

# fetching all single albums
singles = fetch_album_from_artist(artist_url, album_type="single")
singles = fetch_albums_from_artist(artist_url, album_type="single")

for single in singles:
log.info("Fetching single: " + single["name"])
Expand Down
19 changes: 16 additions & 3 deletions spotdl/youtube_tools.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from bs4 import BeautifulSoup
import urllib
import pafy

from slugify import slugify
from logzero import logger as log
import os

from spotdl import spotify_tools
from spotdl import internals
from spotdl import const

import os

# Fix download speed throttle on short duration tracks
# Read more on mps-youtube/pafy#199
pafy.g.opener.addheaders.append(("Range", "bytes=0-"))
Expand Down Expand Up @@ -75,6 +75,8 @@ def generate_m3u(track_file):
log.info("Generating {0} from {1} YouTube URLs".format(target_file, total_tracks))
with open(target_file, "w") as output_file:
output_file.write("#EXTM3U\n\n")

videos = []
for n, track in enumerate(tracks, 1):
content, _ = match_video_and_metadata(track)
if content is None:
Expand All @@ -94,6 +96,9 @@ def generate_m3u(track_file):
log.debug(m3u_key)
with open(target_file, "a") as output_file:
output_file.write(m3u_key)
videos.append(content.watchv_url)

return videos


def download_song(file_name, content):
Expand Down Expand Up @@ -240,7 +245,7 @@ def scrape(self, bestmatch=True, tries_remaining=5):
search_url = generate_search_url(self.search_query)
log.debug("Opening URL: {0}".format(search_url))

item = urllib.request.urlopen(search_url).read()
item = self._fetch_response(search_url).read()
items_parse = BeautifulSoup(item, "html.parser")

videos = []
Expand Down Expand Up @@ -319,3 +324,11 @@ def api(self, bestmatch=True):
return self._best_match(videos)

return videos

@staticmethod
def _fetch_response(url):
# XXX: This method exists only because it helps us indirectly
# monkey patch `urllib.request.open`, directly monkey patching
# `urllib.request.open` causes us to end up in an infinite recursion
# during the test since `urllib.request.open` would monkeypatch itself.
return urllib.request.urlopen(url)
Loading