Skip to content

Commit

Permalink
change to LF, explicitly mention python3 (#194)
Browse files Browse the repository at this point in the history
fixes #187
  • Loading branch information
SathyaBhat authored Jul 16, 2021
1 parent 5ffde9b commit 676bf40
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 162 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

from setuptools import setup, find_packages
from spotify_dl.constants import VERSION
Expand Down
62 changes: 31 additions & 31 deletions spotify_dl/cache.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
from spotify_dl.models import Song
from peewee import DoesNotExist
from spotify_dl.scaffold import log


def check_if_in_cache(search_term):
"""
Checks if the specified search term is in the local database cache.
and returns the video id if it exists.
:param search_term: String to be searched for in the cache
:return A tuple with Boolean and video id if it exists
"""
try:
song = Song.get(search_term=search_term)
log.debug(f"Found id {song.video_id} for {search_term} in cache")
return True, song.video_id
except DoesNotExist:
log.debug(f"Couldn't find id for {search_term} in cache")
return False, None


def save_to_cache(search_term, video_id):
"""
Saves the search term and video id to the database cache so it can be looked up later.
:param search_term: Search term to be saved to in the cache
:param video_id: Video id to be saved to in the cache
:return Video id saved in the cache
"""
song_info, saved = Song.get_or_create(search_term=search_term, video_id=video_id)
log.debug(f"Saved: {saved} video id {song_info.video_id} in cache")
return song_info.video_id
from spotify_dl.models import Song
from peewee import DoesNotExist
from spotify_dl.scaffold import log


def check_if_in_cache(search_term):
"""
Checks if the specified search term is in the local database cache.
and returns the video id if it exists.
:param search_term: String to be searched for in the cache
:return A tuple with Boolean and video id if it exists
"""
try:
song = Song.get(search_term=search_term)
log.debug(f"Found id {song.video_id} for {search_term} in cache")
return True, song.video_id
except DoesNotExist:
log.debug(f"Couldn't find id for {search_term} in cache")
return False, None


def save_to_cache(search_term, video_id):
"""
Saves the search term and video id to the database cache so it can be looked up later.
:param search_term: Search term to be saved to in the cache
:param video_id: Video id to be saved to in the cache
:return Video id saved in the cache
"""
song_info, saved = Song.get_or_create(search_term=search_term, video_id=video_id)
log.debug(f"Saved: {saved} video id {song_info.video_id} in cache")
return song_info.video_id
32 changes: 16 additions & 16 deletions spotify_dl/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from peewee import SqliteDatabase
from peewee import Model, TextField
from os import path
from pathlib import Path
from spotify_dl.constants import SAVE_PATH

Path(path.expanduser(SAVE_PATH)).mkdir(exist_ok=True)
db = SqliteDatabase(path.expanduser(f"{SAVE_PATH}/songs.db"))


class Song(Model):
search_term = TextField()
video_id = TextField()

class Meta:
database = db
from peewee import SqliteDatabase
from peewee import Model, TextField
from os import path
from pathlib import Path
from spotify_dl.constants import SAVE_PATH

Path(path.expanduser(SAVE_PATH)).mkdir(exist_ok=True)
db = SqliteDatabase(path.expanduser(f"{SAVE_PATH}/songs.db"))


class Song(Model):
search_term = TextField()
video_id = TextField()

class Meta:
database = db
190 changes: 95 additions & 95 deletions spotify_dl/spotify_dl.py
Original file line number Diff line number Diff line change
@@ -1,95 +1,95 @@
#!/usr/bin/env python
import argparse
import json
import os
import sys
from logging import DEBUG
from pathlib import Path, PurePath

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

from spotify_dl.constants import VERSION
from spotify_dl.models import db, Song
from spotify_dl.scaffold import log, check_for_tokens
from spotify_dl.spotify import fetch_tracks, parse_spotify_url, validate_spotify_url, get_item_name
from spotify_dl.youtube import download_songs, default_filename, playlist_num_filename


def spotify_dl():
"""Main entry point of the script."""
parser = argparse.ArgumentParser(prog='spotify_dl')
parser.add_argument('-l', '--url', action="store",
help="Spotify Playlist link URL", type=str, required=True)
parser.add_argument('-o', '--output', type=str, action='store',
help='Specify download directory.', required=True)
parser.add_argument('-d', '--download', action='store_true',
help='Download using youtube-dl', default=True)
parser.add_argument('-f', '--format_str', type=str, action='store',
help='Specify youtube-dl format string.',
default='bestaudio/best')
parser.add_argument('-k', '--keep_playlist_order', default=False,
action='store_true',
help='Whether to keep original playlist ordering or not.')
parser.add_argument('-m', '--skip_mp3', action='store_true',
help='Don\'t convert downloaded songs to mp3')
parser.add_argument('-s', '--scrape', action="store",
help="Use HTML Scraper for YouTube Search", default=True)
parser.add_argument('-V', '--verbose', action='store_true',
help='Show more information on what''s happening.')
parser.add_argument('-v', '--version', action='store_true',
help='Shows current version of the program')
args = parser.parse_args()

if args.version:
print("spotify_dl v{}".format(VERSION))
exit(0)

db.connect()
db.create_tables([Song])
if os.path.isfile(os.path.expanduser('~/.spotify_dl_settings')):
with open(os.path.expanduser('~/.spotify_dl_settings')) as file:
config = json.loads(file.read())

for key, value in config.items():
if value and (value.lower() == 'true' or value.lower() == 't'):
setattr(args, key, True)
else:
setattr(args, key, value)

if args.verbose:
log.setLevel(DEBUG)

log.info('Starting spotify_dl')
log.debug('Setting debug mode on spotify_dl')

if not check_for_tokens():
exit(1)

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
log.debug('Arguments: {}'.format(args))

if args.url:
valid_item = validate_spotify_url(args.url)

if not valid_item:
sys.exit(1)

if args.output:
item_type, item_id = parse_spotify_url(args.url)
directory_name = get_item_name(sp, item_type, item_id)
save_path = Path(PurePath.joinpath(Path(args.output), Path(directory_name)))
save_path.mkdir(parents=True, exist_ok=True)
log.info("Saving songs to: {}".format(directory_name))

songs = fetch_tracks(sp, item_type, args.url)
if args.download is True:
file_name_f = default_filename
if args.keep_playlist_order:
file_name_f = playlist_num_filename

download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order, file_name_f)


if __name__ == '__main__':
spotify_dl()
#!/usr/bin/env python3
import argparse
import json
import os
import sys
from logging import DEBUG
from pathlib import Path, PurePath

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

from spotify_dl.constants import VERSION
from spotify_dl.models import db, Song
from spotify_dl.scaffold import log, check_for_tokens
from spotify_dl.spotify import fetch_tracks, parse_spotify_url, validate_spotify_url, get_item_name
from spotify_dl.youtube import download_songs, default_filename, playlist_num_filename


def spotify_dl():
"""Main entry point of the script."""
parser = argparse.ArgumentParser(prog='spotify_dl')
parser.add_argument('-l', '--url', action="store",
help="Spotify Playlist link URL", type=str, required=True)
parser.add_argument('-o', '--output', type=str, action='store',
help='Specify download directory.', required=True)
parser.add_argument('-d', '--download', action='store_true',
help='Download using youtube-dl', default=True)
parser.add_argument('-f', '--format_str', type=str, action='store',
help='Specify youtube-dl format string.',
default='bestaudio/best')
parser.add_argument('-k', '--keep_playlist_order', default=False,
action='store_true',
help='Whether to keep original playlist ordering or not.')
parser.add_argument('-m', '--skip_mp3', action='store_true',
help='Don\'t convert downloaded songs to mp3')
parser.add_argument('-s', '--scrape', action="store",
help="Use HTML Scraper for YouTube Search", default=True)
parser.add_argument('-V', '--verbose', action='store_true',
help='Show more information on what''s happening.')
parser.add_argument('-v', '--version', action='store_true',
help='Shows current version of the program')
args = parser.parse_args()

if args.version:
print("spotify_dl v{}".format(VERSION))
exit(0)

db.connect()
db.create_tables([Song])
if os.path.isfile(os.path.expanduser('~/.spotify_dl_settings')):
with open(os.path.expanduser('~/.spotify_dl_settings')) as file:
config = json.loads(file.read())

for key, value in config.items():
if value and (value.lower() == 'true' or value.lower() == 't'):
setattr(args, key, True)
else:
setattr(args, key, value)

if args.verbose:
log.setLevel(DEBUG)

log.info('Starting spotify_dl')
log.debug('Setting debug mode on spotify_dl')

if not check_for_tokens():
exit(1)

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials())
log.debug('Arguments: {}'.format(args))

if args.url:
valid_item = validate_spotify_url(args.url)

if not valid_item:
sys.exit(1)

if args.output:
item_type, item_id = parse_spotify_url(args.url)
directory_name = get_item_name(sp, item_type, item_id)
save_path = Path(PurePath.joinpath(Path(args.output), Path(directory_name)))
save_path.mkdir(parents=True, exist_ok=True)
log.info("Saving songs to: {}".format(directory_name))

songs = fetch_tracks(sp, item_type, args.url)
if args.download is True:
file_name_f = default_filename
if args.keep_playlist_order:
file_name_f = playlist_num_filename

download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order, file_name_f)


if __name__ == '__main__':
spotify_dl()
38 changes: 19 additions & 19 deletions tests/test_cache_get.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from spotify_dl.cache import check_if_in_cache, save_to_cache
from spotify_dl.models import db, Song

search_term_wrong = 'bleh'
search_term = "Red Hot Chili Peppers - Dani California [Official Music Video]"

video_id = "Sb5aq5HcS1A"
db.connect()
db.create_tables([Song])

def test_check_for_cache_miss():
exists, song_info = check_if_in_cache(search_term=search_term_wrong)
assert exists is False
assert song_info is None

def test_check_for_cache_hit():
_ = save_to_cache(search_term=search_term, video_id='Sb5aq5HcS1A')
exists, cache_video_id = check_if_in_cache(search_term=search_term)
assert exists is True
from spotify_dl.cache import check_if_in_cache, save_to_cache
from spotify_dl.models import db, Song

search_term_wrong = 'bleh'
search_term = "Red Hot Chili Peppers - Dani California [Official Music Video]"

video_id = "Sb5aq5HcS1A"
db.connect()
db.create_tables([Song])

def test_check_for_cache_miss():
exists, song_info = check_if_in_cache(search_term=search_term_wrong)
assert exists is False
assert song_info is None

def test_check_for_cache_hit():
_ = save_to_cache(search_term=search_term, video_id='Sb5aq5HcS1A')
exists, cache_video_id = check_if_in_cache(search_term=search_term)
assert exists is True
assert cache_video_id == video_id

0 comments on commit 676bf40

Please sign in to comment.