From 6ae40e460189dda2a9ff4ff15d3c5092fd3d44ba Mon Sep 17 00:00:00 2001 From: Juan Blanco Date: Sat, 4 Apr 2020 12:06:56 -0300 Subject: [PATCH 1/3] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ccc81fa..8405c9a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ __pycache__ *.ini` venv/* credentials.json +.idea/ \ No newline at end of file From a9383881536829ba5a27756a88ce5e327e37c494 Mon Sep 17 00:00:00 2001 From: johnblanco Date: Sat, 4 Apr 2020 17:18:07 -0300 Subject: [PATCH 2/3] better variable names --- fresh.py | 67 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/fresh.py b/fresh.py index cebbe6b..877ac0d 100644 --- a/fresh.py +++ b/fresh.py @@ -254,7 +254,7 @@ def filter_tags(title): return filtered_title, tags -def extract_track_url(search): +def extract_track_url(search_results): """ Get the first Spotify track url from a given search. @@ -262,7 +262,7 @@ def extract_track_url(search): Parameters ---------- - search : dict + search_results : dict Contains information relating to Spotify API track search request. Returns @@ -271,8 +271,8 @@ def extract_track_url(search): Spotify URL for the first track received from search query. """ - if 'tracks' in search: - tracks = search['tracks'] + if 'tracks' in search_results: + tracks = search_results['tracks'] if 'items' in tracks: items = tracks['items'] # take the first url we can find @@ -345,23 +345,24 @@ def process_choice_input(): def process_fresh(): return cutie.prompt_yes_or_no('Would you like to only add tracks tagged as [FRESH]?') -def process_subreddit(subreddit, choice, l): - if choice.lower() == 'hot': - sub_choice = subreddit.hot(limit=l) - elif choice.lower() == 'new': - sub_choice = subreddit.new(limit=l) - elif choice.lower() == 'rising': - sub_choice = subreddit.rising(limit=l) - elif choice.lower() == 'random_rising': - sub_choice = subreddit.random_rising(limit=l) - elif choice.lower() == 'controversial': - sub_choice = subreddit.controversial(limit=l) - elif choice.lower() == 'top': - sub_choice = subreddit.top(limit=l) + +def process_subreddit(subreddit, subreddit_list_by, limit): + if subreddit_list_by.lower() == 'hot': + submissions = subreddit.hot(limit=limit) + elif subreddit_list_by.lower() == 'new': + submissions = subreddit.new(limit=limit) + elif subreddit_list_by.lower() == 'rising': + submissions = subreddit.rising(limit=limit) + elif subreddit_list_by.lower() == 'random_rising': + submissions = subreddit.random_rising(limit=limit) + elif subreddit_list_by.lower() == 'controversial': + submissions = subreddit.controversial(limit=limit) + elif subreddit_list_by.lower() == 'top': + submissions = subreddit.top(limit=limit) else: print("Unsupported sorting method") sys.exit() - return sub_choice + return submissions def addSpotifyTrack(fresh, threshold, includeAlbums, verbose, sub, tracks): @@ -429,34 +430,36 @@ def main(): spotifyObj.trace = False if args.verbose: print('Welcome to the HipHopHeads Fresh Script') - verbose, l, choice, threshold, includeAlbums, fresh = process_args( + verbose, limit, choice, threshold, includeAlbums, fresh = process_args( args, user) - sub_choice = process_subreddit(subreddit, choice, l) + sub_choice = process_subreddit(subreddit, choice, limit) tracks = [] tracks_array = [] - for sub in sub_choice: - if sub.domain == "open.spotify.com": - addSpotifyTrack(fresh, threshold, includeAlbums, verbose, sub, tracks) + for submission in sub_choice: + if submission.domain == "open.spotify.com": + addSpotifyTrack(fresh, threshold, includeAlbums, verbose, submission, tracks) else: - title, tags = filter_tags(sub.title) + title, tags = filter_tags(submission.title) if 'discussion' not in tags: if 'album' in tags or 'impressions' in tags: # there is a pull request for this feature at the moment # so I will leave it out for now - search = spotifyObj.search(title, type='album') + + #search = spotifyObj.search(title, type='album') #TODO + pass else: - search = spotifyObj.search(title, type='track') - if search: - track_url = extract_track_url(search) + search_result = spotifyObj.search(title, type='track') + if search_result: + track_url = extract_track_url(search_result) if track_url: otherDomainList = ['youtu.be', 'youtube.com', 'soundcloud.com'] # handle non-spotify posts - if sub.domain in otherDomainList and verbose: - print("Post: ", sub.title) - print("URL: ", sub.url) - print("Score: ", sub.score) + if submission.domain in otherDomainList and verbose: + print("Post: ", submission.title) + print("URL: ", submission.url) + print("Score: ", submission.score) print("------------------------\n") tracks.append(track_url) From 09df5562ce678dbb51d6163f2ddafdc36300cd21 Mon Sep 17 00:00:00 2001 From: johnblanco Date: Sat, 4 Apr 2020 17:18:30 -0300 Subject: [PATCH 3/3] test extract_track_url --- tests/test_fresh.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/test_fresh.py diff --git a/tests/test_fresh.py b/tests/test_fresh.py new file mode 100644 index 0000000..4e66a15 --- /dev/null +++ b/tests/test_fresh.py @@ -0,0 +1,32 @@ +import unittest +import fresh + + +class TestFresh(unittest.TestCase): + def test_extract_track_url_if_present(self): + search_result = { + 'tracks': { + 'items': [ + { + 'external_urls': { + 'spotify': 'https://open.spotify.com/track/1TlPpvXcPX8xlD2CiOsds7' + } + } + ] + } + } + + url = fresh.extract_track_url(search_result) + + self.assertEqual(url, 'https://open.spotify.com/track/1TlPpvXcPX8xlD2CiOsds7') + + def test_extract_track_url_none_if_no_tracks_present(self): + search_result = { + 'tracks': { + 'items': [] + } + } + + url = fresh.extract_track_url(search_result) + + self.assertEqual(url, None) \ No newline at end of file