예제 #1
0
    def _download_list_songs_to_file(self,
                                     song_urls,
                                     save_path_list,
                                     total=None):
        '''
		批量通过歌曲的url list 下载歌曲到本地
		:param song_urls: 歌曲 download url list
		:param save_path_list: 歌曲保存地址list
		:return:
		'''
        n = len(song_urls)
        if n != len(save_path_list):
            raise ParamsError(
                "len(song_urls) must be equal to len(save_path_list)!")
        for i in range(n):
            Helper.download_network_resource(song_urls[i], save_path_list[i])
            if total is None:
                self.logger.info("Download %d/%d %s to %s!" %
                                 (i + 1, n, song_urls[i], save_path_list[i]))
            else:
                # 加锁,更新计数器
                if self.lock.acquire():
                    self.no_counter += 1
                    self.logger.info("Download %d/%d %s to %s!" %
                                     (self.no_counter, total, song_urls[i],
                                      save_path_list[i]))
                    self.lock.release()
예제 #2
0
    def download_play_list_songs(self, play_list_id, limit=1000):
        '''
		下载歌单中的全部歌曲,单线程
		:param play_list_id: 歌单id
		:param limit: 下载的最大数量
		:return:
		'''
        start_time = time.time()
        # 获取歌单详情
        res = self.get_play_list_detail(play_list_id, limit).json()
        songs_id_list = []
        # 获取歌单歌曲id list
        for content in res['playlist']["trackIds"]:
            songs_id_list.append(content['id'])
        # 歌单名字
        play_list_name = res['playlist']['name']
        # 歌单下载音乐保存地址
        save_path = os.path.join(Constants.PLAY_LIST_SAVE_DIR, play_list_name)
        Helper.mkdir(save_path)
        # 获取歌曲名+歌手名字符串列表
        songs_name_and_singer_name_str_list = self.get_songs_name_and_singer_name_str_list_by_ids_list(
            songs_id_list)
        # 获取歌曲下载url list
        urls_list = self.get_download_urls_by_ids(songs_id_list)
        # 全部歌曲数目
        total = len(urls_list)
        self.logger.info("play list %s has total %d songs!" %
                         (play_list_name, total))
        self.logger.info(
            "(single thread)Now start download musics of %s(save path is:%s):"
            % (play_list_name, save_path))
        for index, url in enumerate(urls_list, 1):
            try:
                Helper.download_network_resource(
                    url,
                    os.path.join(
                        save_path, "%s.mp3" %
                        songs_name_and_singer_name_str_list[index - 1]))
                self.logger.info(
                    "Successfully download %d/%d(%s)!" %
                    (index, total,
                     songs_name_and_singer_name_str_list[index - 1]))
            except Exception:
                self.logger.info(
                    "Fail download %d/%d(%s)!" %
                    (index, total,
                     songs_name_and_singer_name_str_list[index - 1]))
                continue
        end_time = time.time()
        self.logger.info(
            "It costs %.2f seconds to download play list %s(id=%s)'s %d songs to %s "
            "using single thread!" % ((end_time - start_time), play_list_name,
                                      play_list_id, total, save_path))
예제 #3
0
    def download_singer_hot_songs_by_name(self, singer_name):
        '''
		通过输入歌手名字来下载歌手的全部热门歌曲,单线程实现
		:param singer_name: 歌手名字
		:return:
		'''
        start_time = time.time()
        # 热门歌曲保存地址
        save_path = os.path.join(Constants.SINGER_SAVE_DIR, singer_name,
                                 Constants.HOT_SONGS_SAVE_NAME)
        # 根据名字得到歌手id
        uid = self.get_singer_id_by_name(singer_name)
        # 歌手主页地址
        singer_url = "http://music.163.com/artist?id=%d" % uid
        # 歌手全部热门歌曲id list
        hot_songs_ids = Helper.get_singer_hot_songs_ids(singer_url)
        # 通过歌曲id得到下载url
        urls_list = self.get_download_urls_by_ids(hot_songs_ids)
        # 通过歌曲id获得歌曲名
        songs_name_and_singer_name_str_list = self.get_songs_name_and_singer_name_str_list_by_ids_list(
            hot_songs_ids)
        # 全部热门歌曲数
        total = len(urls_list)
        Helper.mkdir(save_path)
        self.logger.info("%s has total %d hot songs!" % (singer_name, total))
        self.logger.info(
            "(single thread)Now start download hot musics of %s(save path is:%s):"
            % (singer_name, save_path))
        for index, url in enumerate(urls_list, 1):
            try:
                # 下载
                Helper.download_network_resource(
                    url,
                    os.path.join(
                        save_path, "%s.mp3" %
                        songs_name_and_singer_name_str_list[index - 1]))
                self.logger.info(
                    "Successfully download %d/%d(%s)!" %
                    (index, total,
                     songs_name_and_singer_name_str_list[index - 1]))
            except Exception:
                self.logger.info(
                    "Fail download %d/%d(%s)!" %
                    (index, total,
                     songs_name_and_singer_name_str_list[index - 1]))
                continue
        end_time = time.time()
        self.logger.info(
            "It costs %.2f seconds to download singer %s's %d hot songs to %s "
            "using single thread!" %
            ((end_time - start_time), singer_name, total, save_path))