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

Adds --plays option to add play count #183

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 31 additions & 4 deletions soundscrape/soundscrape.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#! /usr/bin/env python
from __future__ import unicode_literals

import unicodedata
import argparse
import demjson
import os
Expand All @@ -10,6 +11,7 @@
import sys
import urllib

from glob import glob
from clint.textui import colored, puts, progress
from datetime import datetime
from mutagen.mp3 import MP3, EasyMP3
Expand Down Expand Up @@ -77,6 +79,8 @@ def main():
help='Open downloaded files after downloading.')
parser.add_argument('-k', '--keep', action='store_true',
help='Keep 30-second preview tracks')
parser.add_argument('-s', '--plays', action='store_true',
help='Add number of plays to the file name (SoundCloud playlists only)')
parser.add_argument('-v', '--version', action='store_true', default=False,
help='Display the current version of SoundScrape')

Expand Down Expand Up @@ -133,6 +137,7 @@ def process_soundcloud(vargs):
track_permalink = vargs['track']
keep_previews = vargs['keep']
folders = vargs['folders']
add_play_count = vargs['plays']

id3_extras = {}
one_track = False
Expand Down Expand Up @@ -289,7 +294,8 @@ def process_soundcloud(vargs):

if not aggressive:
filenames = download_tracks(client, tracks, num_tracks, vargs['downloadable'], vargs['folders'], vargs['path'],
id3_extras=id3_extras)
id3_extras=id3_extras,
add_play_count=add_play_count)

if vargs['open']:
open_files(filenames)
Expand Down Expand Up @@ -363,7 +369,17 @@ def download_track(track, album_name=u'', keep_previews=False, folders=False, fi

return filename

def download_tracks(client, tracks, num_tracks=sys.maxsize, downloadable=False, folders=False, custom_path='', id3_extras={}):
def escape_glob(path):
transdict = {
'[': '[[]',
']': '[]]',
'*': '[*]',
'?': '[?]',
}
rc = re.compile('|'.join(map(re.escape, transdict)))
return rc.sub(lambda m: transdict[m.group(0)], path)

def download_tracks(client, tracks, num_tracks=sys.maxsize, downloadable=False, folders=False, custom_path='', id3_extras={}, add_play_count=False):
"""
Given a list of tracks, iteratively download all of them.

Expand All @@ -372,6 +388,10 @@ def download_tracks(client, tracks, num_tracks=sys.maxsize, downloadable=False,
filenames = []

for i, track in enumerate(tracks):
plays = ""

if add_play_count and track.has_key('playback_count'):
plays = str(track['playback_count']) + " - "

# "Track" and "Resource" objects are actually different,
# even though they're the same.
Expand Down Expand Up @@ -416,7 +436,8 @@ def download_tracks(client, tracks, num_tracks=sys.maxsize, downloadable=False,
else:
track_artist = sanitize_filename(track['user']['username'])
track_title = sanitize_filename(track['title'])
track_filename = track_artist + ' - ' + track_title + '.mp3'
actual_track_filename = unicodedata.normalize('NFD', track_artist + ' - ' + track_title + '.mp3')
track_filename = plays + actual_track_filename

if folders:
track_artist_path = join(custom_path, track_artist)
Expand All @@ -426,7 +447,13 @@ def download_tracks(client, tracks, num_tracks=sys.maxsize, downloadable=False,
else:
track_filename = join(custom_path, track_filename)

if exists(track_filename):
if add_play_count:
glob_pattern = "*" + escape_glob(actual_track_filename)
file_exists = len(glob(glob_pattern)) > 0
else:
file_exists = exists(actual_track_filename)

if file_exists:
puts_safe(colored.yellow("Track already downloaded: ") + colored.white(track_title))
continue

Expand Down
10 changes: 5 additions & 5 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_soundcloud(self):
os.unlink(f)

mp3_count = len(glob.glob1('', "*.mp3"))
vargs = {'path':'', 'folders': False, 'group': False, 'track': '', 'num_tracks': 9223372036854775807, 'bandcamp': False, 'downloadable': False, 'likes': False, 'open': False, 'artist_url': 'https://soundcloud.com/fzpz/revised', 'keep': True}
vargs = {'path':'', 'folders': False, 'group': False, 'track': '', 'num_tracks': 9223372036854775807, 'bandcamp': False, 'downloadable': False, 'likes': False, 'open': False, 'artist_url': 'https://soundcloud.com/fzpz/revised', 'keep': True, 'plays': False}
process_soundcloud(vargs)
new_mp3_count = len(glob.glob1('', "*.mp3"))
self.assertTrue(new_mp3_count > mp3_count)
Expand All @@ -48,7 +48,7 @@ def test_soundcloud_hard(self):
os.unlink(f)

mp3_count = len(glob.glob1('', "*.mp3"))
vargs = {'path':'', 'folders': False, 'group': False, 'track': '', 'num_tracks': 1, 'bandcamp': False, 'downloadable': False, 'likes': False, 'open': False, 'artist_url': 'puptheband', 'keep': False}
vargs = {'path':'', 'folders': False, 'group': False, 'track': '', 'num_tracks': 1, 'bandcamp': False, 'downloadable': False, 'likes': False, 'open': False, 'artist_url': 'puptheband', 'keep': False, 'plays': False}
process_soundcloud(vargs)
new_mp3_count = len(glob.glob1('', "*.mp3"))
self.assertTrue(new_mp3_count > mp3_count)
Expand All @@ -62,7 +62,7 @@ def test_soundcloud_hard_2(self):
os.unlink(f)

mp3_count = len(glob.glob1('', "*.mp3"))
vargs = {'path':'', 'folders': False, 'group': False, 'track': '', 'num_tracks': 1, 'bandcamp': False, 'downloadable': False, 'likes': False, 'open': False, 'artist_url': 'https://soundcloud.com/lostdogz/snuggles-chapstick', 'keep': False}
vargs = {'path':'', 'folders': False, 'group': False, 'track': '', 'num_tracks': 1, 'bandcamp': False, 'downloadable': False, 'likes': False, 'open': False, 'artist_url': 'https://soundcloud.com/lostdogz/snuggles-chapstick', 'keep': False, 'plays': False}
process_soundcloud(vargs)
new_mp3_count = len(glob.glob1('', "*.mp3"))
self.assertTrue(new_mp3_count > mp3_count)
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_bandcamp(self):
os.unlink(f)

mp3_count = len(glob.glob1('', "*.mp3"))
vargs = {'path':'', 'folders': False, 'group': False, 'track': '', 'num_tracks': 9223372036854775807, 'bandcamp': False, 'downloadable': False, 'likes': False, 'open': False, 'artist_url': 'https://atenrays.bandcamp.com/track/who-u-think'}
vargs = {'path':'', 'folders': False, 'group': False, 'track': '', 'num_tracks': 9223372036854775807, 'bandcamp': False, 'downloadable': False, 'likes': False, 'open': False, 'artist_url': 'https://atenrays.bandcamp.com/track/who-u-think', 'plays': False}
process_bandcamp(vargs)
new_mp3_count = len(glob.glob1('', "*.mp3"))
self.assertTrue(new_mp3_count > mp3_count)
Expand All @@ -105,7 +105,7 @@ def test_bandcamp_slashes(self):
os.unlink(f)

mp3_count = len(glob.glob1('', "*.mp3"))
vargs = {'path':'', 'folders': False, 'group': False, 'track': '', 'num_tracks': 9223372036854775807, 'bandcamp': False, 'downloadable': False, 'likes': False, 'open': False, 'artist_url': 'https://defill.bandcamp.com/track/amnesia-chamber-harvest-skit'}
vargs = {'path':'', 'folders': False, 'group': False, 'track': '', 'num_tracks': 9223372036854775807, 'bandcamp': False, 'downloadable': False, 'likes': False, 'open': False, 'artist_url': 'https://defill.bandcamp.com/track/amnesia-chamber-harvest-skit', 'plays': False}
process_bandcamp(vargs)
new_mp3_count = len(glob.glob1('', "*.mp3"))
self.assertTrue(new_mp3_count > mp3_count)
Expand Down