def download(self, localdir, max_retry): """ Download a track in local directory. """ local_file = self.gen_localdir(localdir) + self.gen_filename() if self.track_exists(localdir): print("Track {0} already downloaded, skipping!".format( self.get("id"))) return False if local_file in self.get_ignored_tracks(localdir): print("\033[93mTrack {0} ignored, skipping!!\033[0m".format( self.get("id"))) return False dlurl = self.get_download_link() if not dlurl: raise serror("Can't download track_id:%d|%s" % ( self.get("id"), self.get("title"))) retry = max_retry print("\nDownloading %s (%d).." % (self.get("title"), self.get("id"))) while True: try: urllib.request.urlretrieve(dlurl, local_file, self._progress_hook) break except Exception as e: if os.path.isfile(local_file): os.unlink(local_file) retry -= 1 if retry < 0: raise serror("Can't download track-id %s, max retry " "reached (%d). Error occured: %s" % ( self.get("id"), max_retry, type(e))) else: print("\033[93mError occured for track-id %s (%s). " "Retrying.. (%d/%d) \033[0m" % ( self.get("id"), type(e), max_retry - retry, max_retry)) except KeyboardInterrupt: if os.path.isfile(local_file): os.unlink(local_file) raise serror("KeyBoard Interrupt: Incomplete file removed.") self.filepath = local_file + self.get_file_extension(local_file) os.rename(local_file, self.filepath) print("Downloaded => %s" % self.filepath) self.downloaded = True return True
def send_request(self, url): """ Send a request to given url. """ while True: try: return urllib.request.urlopen(url) except urllib.error.HTTPError as e: raise serror("Request `%s` failed (%s:%s)." % (url, e.__class__.__name__, e.code)) except Exception as e: choice = input( serror("Error occured: %s - Retry? [yN]" % type(e))) if choice.strip().lower() != "y": raise serror(e)
def process_tags(self, tag=None): """Process ID3 Tags for mp3 files.""" if self.downloaded is False: raise serror("Track not downloaded, can't process tags..") filetype = magic.from_file(self.filepath, mime=True) if filetype != "audio/mpeg": raise serror("Cannot process tags for file type %s." % filetype) print("Processing tags for %s.." % self.filepath) if tag is None: tag = stag() tag.load_id3(self) tag.write_id3(self.filepath)
def send_request(self, url): """ Send a request to given url. """ while True: try: return urllib.request.urlopen(url) except urllib.error.HTTPError as e: raise serror( "Request `%s` failed (%s:%s)." % (url, e.__class__.__name__, e.code)) except Exception as e: choice = input(serror( "Error occured: %s - Retry? [yN]" % type(e))) if choice.strip().lower() != "y": raise serror(e)
def download(self, localdir, process_tag=True): """ Download a track in local directory. """ local_file = self.gen_localdir(localdir) + self.gen_filename() if self.track_exists(localdir): print("INFO: Track {0} already downloaded, skipping!".format(self.get("id"))) return False if local_file in self.get_ignored_tracks(localdir): print("\033[93mINFO: Track {0} ignored, skipping!!\033[0m".format(self.get("id"))) return False dlurl = self.get_download_link() if not dlurl: raise serror("Can't download track_id:%d|%s" % (self.get("id"), self.get("title"))) try: print("Start downloading %s (%d).." % (self.get("title"), self.get("id"))) urllib.request.urlretrieve(dlurl, local_file, self._progress_hook) except: os.remove(local_file) raise self._download_artwork(localdir) if process_tag and self.get("original-format") == "mp3": tag = stag() tag.load_id3(self) tag.write_id3(local_file)
def convert(self): """Convert file in mp3 format.""" if self.downloaded is False: raise serror("Track not downloaded, can't convert file..") filetype = magic.from_file(self.filepath, mime=True) if filetype == "audio/mpeg": print("File is already in mp3 format. Skipping convert.") return False rootpath = os.path.dirname(os.path.dirname(self.filepath)) backupdir = rootpath + "/backups/" + self.get("username") if not os.path.exists(backupdir): os.makedirs(backupdir) backupfile = "%s/%s%s" % ( backupdir, self.gen_filename(), self.get_file_extension(self.filepath)) newfile = "%s.mp3" % self.filename_without_extension() os.rename(self.filepath, backupfile) self.filepath = newfile print("Converting to %s.." % newfile) song = AudioSegment.from_file(backupfile) return song.export(newfile, format="mp3")
def send_request(self, url): """ Send a request to given url. """ try: return urllib.request.urlopen(url) except urllib.error.HTTPError as e: raise serror( "Request `%s` failed (%s:%s)." % (url, e.__class__.__name__, e.code))
def get_client_id(self): """ Attempt to get client_id from soundcloud homepage. """ # FIXME: This method doesn't works id = re.search("\"clientID\":\"([a-z0-9]*)\"", self.send_request(self.SC_HOME).read().decode("utf-8")) if not id: raise serror("Cannot retrieve client_id.") return id.group(1)
def get_client_id(self): """ Attempt to get client_id from soundcloud homepage. """ id = re.search( "\"clientID\":\"([a-z0-9]*)\"", self.send_request(self.SC_HOME).read().decode("utf-8")) if not id: raise serror("Cannot retrieve client_id.") return id.group(1)
def process_tags(self, tag=None): """Process ID3 Tags for mp3 files.""" if self.downloaded is False: raise serror("Track not downloaded, can't process tags..") if magic.from_file(self.filepath, mime=True) != b"audio/mpeg": return False print("Processing tags for %s.." % self.filepath) if tag is None: tag = stag() tag.load_id3(self) tag.write_id3(self.filepath)
def download_artwork(self, localdir, max_retry): """ Download track's artwork and return file path. Artwork's path is saved in track's metadata as 'artwork-path' key. """ if self.get("artwork-url") == "None": self.metadata["artwork-path"] = None return None artwork_dir = localdir + "/artworks" if not os.path.isdir(artwork_dir): if os.path.isfile(artwork_dir): os.unlink(artwork_dir) os.mkdir(artwork_dir) artwork_filepath = artwork_dir + "/" + self.gen_artwork_filename() retry = max_retry while True: try: res = urllib.request.urlopen(self.get("artwork-url")) with open(artwork_filepath, "wb") as file: file.write(res.read()) break except Exception as e: retry -= 1 if retry < 0: print(serror("Can't download track's artwork, max retry " "reached (%d). Error occured: %s" % ( max_retry, type(e)))) return False else: print("\033[93mTrack's artwork download failed (%s). " "Retrying.. (%d/%d) \033[0m" % ( type(e), max_retry - retry, max_retry)) self.metadata["artwork-path"] = artwork_filepath
def convert(self): """Convert file in mp3 format.""" if self.downloaded is False: raise serror("Track not downloaded, can't convert file..") if magic.from_file(self.filepath, mime=True) == b"audio/mpeg": return False rootpath = os.path.dirname(os.path.dirname(self.filepath)) backupdir = rootpath + "/backups/" + self.get("username") if not os.path.exists(backupdir): os.makedirs(backupdir) backupfile = "%s/%s%s" % ( backupdir, self.gen_filename(), self.get_file_extension(self.filepath)) newfile = "%s.mp3" % self.filename_without_extension() os.rename(self.filepath, backupfile) self.filepath = newfile print("Converting to %s.." % newfile) song = AudioSegment.from_file(backupfile) return song.export(newfile, format="mp3")
except serror as e: print(e) return False for strack in tracks: try: strack.download(output) except serror as e: print(e) return len(tracks) args = parser.parse_args() if not os.path.exists(args.output_dir): print(serror("Error: output directory `%s` doesn's exists." % args.output_dir)) exit(1) offset = 0 limit = 50 if args.offset: offset = args.offset if args.limit: if args.limit > 200: print(serror("Error: tracks limit limited to 200 tracks..")) exit(2) limit = args.limit try: sclient = sclient(args.client_id) suser = suser(args.user, client=sclient)
def test_to_string(self): """ Test exception string representation. """ serr = serror("bar") self.assertEquals("\033[91mERROR: bar\033[0m", serr.__str__())
def test_init(self): """ Test exception initialization. """ serr = serror("foo") self.assertEquals("foo", serr.message)