11#! /usr/bin/env python
22from __future__ import unicode_literals
33
4+ import unicodedata
45import argparse
56import demjson
67import os
1011import sys
1112import urllib
1213
14+ from glob import glob
1315from clint .textui import colored , puts , progress
1416from datetime import datetime
1517from mutagen .mp3 import MP3 , EasyMP3
@@ -77,6 +79,8 @@ def main():
7779 help = 'Open downloaded files after downloading.' )
7880 parser .add_argument ('-k' , '--keep' , action = 'store_true' ,
7981 help = 'Keep 30-second preview tracks' )
82+ parser .add_argument ('-s' , '--plays' , action = 'store_true' ,
83+ help = 'Add number of plays to the file name (SoundCloud playlists only)' )
8084 parser .add_argument ('-v' , '--version' , action = 'store_true' , default = False ,
8185 help = 'Display the current version of SoundScrape' )
8286
@@ -133,6 +137,7 @@ def process_soundcloud(vargs):
133137 track_permalink = vargs ['track' ]
134138 keep_previews = vargs ['keep' ]
135139 folders = vargs ['folders' ]
140+ add_play_count = vargs ['plays' ]
136141
137142 id3_extras = {}
138143 one_track = False
@@ -289,7 +294,8 @@ def process_soundcloud(vargs):
289294
290295 if not aggressive :
291296 filenames = download_tracks (client , tracks , num_tracks , vargs ['downloadable' ], vargs ['folders' ], vargs ['path' ],
292- id3_extras = id3_extras )
297+ id3_extras = id3_extras ,
298+ add_play_count = add_play_count )
293299
294300 if vargs ['open' ]:
295301 open_files (filenames )
@@ -363,7 +369,17 @@ def download_track(track, album_name=u'', keep_previews=False, folders=False, fi
363369
364370 return filename
365371
366- def download_tracks (client , tracks , num_tracks = sys .maxsize , downloadable = False , folders = False , custom_path = '' , id3_extras = {}):
372+ def escape_glob (path ):
373+ transdict = {
374+ '[' : '[[]' ,
375+ ']' : '[]]' ,
376+ '*' : '[*]' ,
377+ '?' : '[?]' ,
378+ }
379+ rc = re .compile ('|' .join (map (re .escape , transdict )))
380+ return rc .sub (lambda m : transdict [m .group (0 )], path )
381+
382+ def download_tracks (client , tracks , num_tracks = sys .maxsize , downloadable = False , folders = False , custom_path = '' , id3_extras = {}, add_play_count = False ):
367383 """
368384 Given a list of tracks, iteratively download all of them.
369385
@@ -372,6 +388,10 @@ def download_tracks(client, tracks, num_tracks=sys.maxsize, downloadable=False,
372388 filenames = []
373389
374390 for i , track in enumerate (tracks ):
391+ plays = ""
392+
393+ if add_play_count and track .has_key ('playback_count' ):
394+ plays = str (track ['playback_count' ]) + " - "
375395
376396 # "Track" and "Resource" objects are actually different,
377397 # even though they're the same.
@@ -416,7 +436,8 @@ def download_tracks(client, tracks, num_tracks=sys.maxsize, downloadable=False,
416436 else :
417437 track_artist = sanitize_filename (track ['user' ]['username' ])
418438 track_title = sanitize_filename (track ['title' ])
419- track_filename = track_artist + ' - ' + track_title + '.mp3'
439+ actual_track_filename = unicodedata .normalize ('NFD' , track_artist + ' - ' + track_title + '.mp3' )
440+ track_filename = plays + actual_track_filename
420441
421442 if folders :
422443 track_artist_path = join (custom_path , track_artist )
@@ -426,7 +447,13 @@ def download_tracks(client, tracks, num_tracks=sys.maxsize, downloadable=False,
426447 else :
427448 track_filename = join (custom_path , track_filename )
428449
429- if exists (track_filename ):
450+ if add_play_count :
451+ glob_pattern = "*" + escape_glob (actual_track_filename )
452+ file_exists = len (glob (glob_pattern )) > 0
453+ else :
454+ file_exists = exists (actual_track_filename )
455+
456+ if file_exists :
430457 puts_safe (colored .yellow ("Track already downloaded: " ) + colored .white (track_title ))
431458 continue
432459
0 commit comments