Esempio n. 1
0
    def get_search(self, search_keyword="", search_type=config.search_type):
        """
        关键字搜索

        :param search_keyword: 搜索关键字
        :param search_type: 搜索类型
        :return: status:搜索状态
        :return: 搜索结果
        """
        _first_param = first_param().get_first_param_search(
            search_keywords=search_keyword, search_type=search_type)
        content = request_data().get_request_data(first_param=_first_param[1],
                                                  url=config.url_search)
        if content[0]:
            json_data = content[1]
        else:
            return False, None
        logger.debug(
            "get_search success",
            "search_keyword:{},search_type:{}".format(search_keyword,
                                                      search_type))
        return True, content[1]
    def get_user_ranklist_songs(self,
                                user_id=config.user_id,
                                rank_type=config.rank_type,
                                rank_max=config.rank_max):
        """
         获取某用户排行榜歌曲列表

        :param user_id: 用户id
        :param rank_max: 排行榜歌曲获取最大数
        :return: status: 是否获取到歌曲
        :return: 排行榜歌曲列表
        """
        # 请求参数
        _first_param = first_param().get_first_param_ranklist(
            user_id=user_id, rank_type=rank_type)
        # 请求数据 ----------------------------------------------------
        json_data = None
        try:
            content = request_data().get_request_data(
                first_param=_first_param[1], url=config.url_user_rank)
            if content[0]:
                if rank_type == config.rank_type_all:
                    json_data = json.loads(content[1])["allData"]
                elif rank_type == config.rank_type_week:
                    json_data = json.loads(content[1])["weekData"]
            else:
                return False, None
        except KeyError as e:
            logger.warning(
                "get_user_ranklist_songs get failed, Maybe the guy's ranklist is hidden,can you see it in the webpage ?",
                "user_id:{},rank_type:{},ranklist_url:https://music.163.com/#/user/songs/rank?id={} ,error_type:{},error:{}"
                .format(user_id, rank_type, user_id, type(e), e))
            return False, None
        except Exception as e:
            logger.error(
                "get_user_ranklist_songs get failed",
                "user_id:{},rank_type:{},error_type:{},error:{}".format(
                    user_id, rank_type, type(e), e))
            return False, None
        # 解析数据 ----------------------------------------------------
        ranklist_id = str(user_id) + "r" + str(rank_type)
        song_success_count = 0
        rank_list = []
        user_rank_list = []
        song_list = []
        artist_list = []
        artist_song_list = []
        song_rank_list = []
        user_song_list = []
        try:
            # ranklist_id rank_type rank_date
            rank_list.append(
                [ranklist_id, rank_type,
                 int(round(time.time() * 1000))])
            # user_id ranklist_id
            user_rank_list.append([user_id, ranklist_id])
            while song_success_count < rank_max and song_success_count < len(
                    json_data):
                song_id = json_data[song_success_count]["song"]["id"]
                song_score = json_data[song_success_count]["score"]
                print(json_data)
                print('#############################')
                print(json_data[song_success_count])
                print('#############################')
                print(song_score)

                # song_id song_name
                song_list.append(
                    [song_id, json_data[song_success_count]["song"]["name"]])
                # song_id ranklist_id song_score
                song_rank_list.append([song_id, ranklist_id, song_score])
                # 多个歌手
                artist_count = 0
                for artist in json_data[song_success_count]["song"]["ar"]:
                    # artist_id artist_name
                    artist_list.append([artist["id"], artist["name"]])
                    # artist_id song_id sort
                    artist_song_list.append(
                        [artist["id"], song_id, artist_count])
                    artist_count += 1
                # user_id song_id ranklist_all/week_score
                user_song_list.append([user_id, song_id, song_score])
                song_success_count += 1
        except Exception as e:
            logger.error(
                "get_user_ranklist_songs parse failed",
                "user_id:{},rank_type:{},error_type:{},song_success_count:{},error:{}"
                .format(user_id, rank_type, song_success_count, type(e), e))
            return False, None
        # 存储数据 ----------------------------------------------------
        try:
            _database_tool = database_tool()
            _database_tool.insert_many_ranklist(rank_list)
            _database_tool.insert_many_song(song_list)
            _database_tool.insert_many_artist(artist_list)
            _database_tool.commit()
            _database_tool.insert_many_user_ranklist(user_rank_list)
            _database_tool.insert_many_song_ranklist(song_rank_list)
            _database_tool.insert_many_artist_song(artist_song_list)
            _database_tool.insert_many_user_song_column(
                column="rank_all_score"
                if rank_type == config.rank_type_all else "rank_week_score",
                data_list=user_song_list)
            _database_tool.commit()
            _database_tool.close()
        except Exception as e:
            logger.error(
                "get_user_ranklist_songs save failed",
                "user_id:{},rank_type:{},song_count_success,error_type:{},error:{}"
                .format(user_id, rank_type, song_success_count, type(e), e))
            return False, None
        logger.info(
            "get_user_ranklist_songs success",
            "user_id:{},rank_type:{},song_success_count:{}".format(
                user_id, rank_type, song_success_count))
        return True, (rank_list, song_list, artist_list, user_rank_list,
                      song_rank_list, artist_song_list)
Esempio n. 3
0
    def get_user_playlists(
            self,
            user_id=config.user_id,
            created_playlists_max=config.created_playlists_max,
            collected_playlists_max=config.collected_playlists_max,
            is_playlists_default=config.is_playlists_default,
            is_playlists_created=config.is_playlists_created,
            is_playlists_collected=config.is_playlists_collected):
        """
        获取用户歌单列表
        
        :param user_id: 用户id
        :param created_playlists_max: 用户创建的歌单获取最大数
        :param collected_playlists_max: 用户收集的歌单获取最大数
        :param is_playlists_default: 是否爬取”用户喜欢的音乐“歌单
        :param is_playlists_created: 是否爬取用户创建的歌单
        :param is_playlists_collected: 是否爬取用户收藏的歌单
        :return: status: 是否获取到歌单
        :return: 歌单列表
        :return: 用户歌单列表
        """
        # 请求参数 ----------------------------------------------------
        _first_param = first_param().get_first_param_user_playlists(
            user_id=user_id)
        # 请求数据 ----------------------------------------------------
        try:
            content = request_data().get_request_data(
                _first_param[1], url=config.url_user_playlists)
            if content[0]:
                json_playlists_data = json.loads(content[1])["playlist"]
            else:
                return False, None
        except Exception as e:
            logger.error(
                "get_user_playlists get failed",
                "user_id:{},error_type:{},error:{}".format(
                    user_id, type(e), e))
            return False, None
        # 解析数据 ----------------------------------------------------
        playlist_count = 0
        created_playlists_count = 0
        collected_playlists_count = 0
        playlist_list = []
        user_playlists_list = []
        tag_list = []
        playlist_tag_list = []
        try:
            while playlist_count < len(json_playlists_data):
                # ”用户喜欢的音乐“歌单
                if is_playlists_default:
                    parse = self.__parser(
                        user_id=user_id,
                        playlist_count=playlist_count,
                        data=json_playlists_data,
                        playlist_type=config.default_playlist)
                    playlist_list.append(parse["playlist_list"])
                    user_playlists_list.append(parse["user_playlist_list"])
                    if len(parse["tag_list"]) != 0:
                        tag_list.extend(parse["tag_list"])
                        playlist_tag_list.extend(parse["playlist_tag_list"])
                    playlist_count += 1
                    is_playlists_default = False
                    continue

                # 用户创建的歌单
                elif is_playlists_created:
                    # 若此处出现playlist为0的情况,说明没有爬取“用户喜欢的音乐”歌单,跳过此歌单
                    if playlist_count == 0:
                        playlist_count += 1
                        continue
                    # 是否是用户自己创建的歌单
                    elif str(json_playlists_data[playlist_count]["creator"]
                             ['userId']) == str(user_id):
                        if created_playlists_count < created_playlists_max:
                            parse = self.__parser(
                                user_id=user_id,
                                playlist_count=playlist_count,
                                data=json_playlists_data,
                                playlist_type=config.created_playlist)
                            playlist_list.append(parse["playlist_list"])
                            user_playlists_list.append(
                                parse["user_playlist_list"])
                            if len(parse["tag_list"]) != 0:
                                tag_list.extend(parse["tag_list"])
                                playlist_tag_list.extend(
                                    parse["playlist_tag_list"])
                            created_playlists_count += 1
                        playlist_count += 1
                    else:
                        is_playlists_created = False
                    continue

                # 用户收藏的歌单
                elif is_playlists_collected:
                    # 是否是用户收藏的歌单
                    if str(json_playlists_data[playlist_count]["creator"]
                           ['userId']) != str(user_id):
                        if collected_playlists_count < collected_playlists_max:
                            parse = self.__parser(
                                user_id=user_id,
                                playlist_count=playlist_count,
                                data=json_playlists_data,
                                playlist_type=config.collected_playlist)
                            playlist_list.append(parse["playlist_list"])
                            user_playlists_list.append(
                                parse["user_playlist_list"])
                            if len(parse["tag_list"]) != 0:
                                tag_list.extend(parse["tag_list"])
                                playlist_tag_list.extend(
                                    parse["playlist_tag_list"])
                            collected_playlists_count += 1
                    # 若此处出现不是用户收藏的歌单情况,说明用户创建歌单没有完全爬取,继续循环
                    playlist_count += 1
                    continue
                break
        except Exception as e:
            logger.error(
                "get_user_playlists parse failed",
                "user_id:{},created_playlists_count:{},collected_playlists_count:{},error_type:{},error:{}"
                .format(user_id, created_playlists_count,
                        collected_playlists_count, type(e), e))
            return False, None
        # 存储数据 ----------------------------------------------------
        try:
            _database_tool = database_tool()
            _database_tool.insert_many_playlist(playlist_list)
            _database_tool.insert_many_tag(tag_list)
            _database_tool.commit()
            _database_tool.insert_many_user_playlist(user_playlists_list)
            _database_tool.insert_many_playlist_tag(playlist_tag_list)
            _database_tool.commit()
            _database_tool.close()
        except Exception as e:
            logger.error(
                "get user_playlist save failed",
                "user_id:{},created_playlists_count:{},collected_playlists_count:{},error_type:{},error:{}"
                .format(user_id, created_playlists_count,
                        collected_playlists_count, type(e), e))
            return False, None
        logger.info(
            "get user_playlist success",
            "user_id:{},created_playlists_count:{},collected_playlists_count:{}"
            .format(user_id, created_playlists_count,
                    collected_playlists_count))
        return True, playlist_list, user_playlists_list
Esempio n. 4
0
    def get_playlist_songs_by_playlist_id(self, playlist_id=config.playlist_id, user_id=0,
                                          playlist_type=config.playlist_type,
                                          playlist_songs_max=config.playlist_songs_max):
        """
        通过歌单id获取歌单歌曲列表

        :param playlist_id: 歌单id
        :param user_id: 用户id
        :param playlist_type: 歌单类型,包括 default created collected
        :param playlist_songs_max: 歌单最大歌曲选取数
        :return: status: 是否获取到歌曲
        :return: 歌曲个数
        :return: 歌曲列表
        :return: 歌手列表
        :return: 歌曲歌手列表
        """
        # 请求参数 ----------------------------------------------------
        _first_param = first_param().get_first_param_playlist(playlist_id)
        # 请求数据 ----------------------------------------------------
        try:
            content = request_data().get_request_data(first_param=_first_param[1], url=config.url_playlist)
            if content[0]:
                songs_data = json.loads(content[1])["playlist"]["tracks"]
                tags_data = json.loads(content[1])["playlist"]["tags"]
                creator_data = json.loads(content[1])["playlist"]["creator"]
            else:
                return False, None
        except Exception as e:
            logger.error("get_playlist_songs_by_playlist_id get failed",
                         "playlist_id:{},playlist_type:{},error_type:{},error:{}"
                         .format(playlist_id, playlist_type, type(e), e))
            return False, None
        # 解析数据 ----------------------------------------------------
        song_count = 0
        song_list = []
        artist_list = []
        artist_song_list = []
        song_playlist_list = []
        tag_list = tags_data
        song_tag_list = []
        # user_id user_name
        creator_list = [[
            creator_data["userId"],
            creator_data["nickname"]
        ]]
        # user_id playlist_id playlist_type
        creator_playlist_list = [[
            creator_data["userId"],
            playlist_id,
            config.created_playlist
        ]]
        # 创作者-歌曲列表
        creator_song_list = []
        user_song_list = []
        try:
            while song_count < playlist_songs_max and song_count < len(songs_data):
                song_id = songs_data[song_count]["id"]
                song_pop = int(songs_data[song_count]["pop"])
                # song_id song_name
                song_list.append([
                    song_id,
                    songs_data[song_count]["name"]
                ])
                # 多个歌手
                artist_count = 0
                for artist in songs_data[song_count]['ar']:
                    # artist_id artist_name
                    artist_list.append([
                        artist["id"],
                        artist["name"]
                    ])
                    # artist_id song_id sort
                    artist_song_list.append([
                        artist["id"],
                        song_id,
                        artist_count
                    ])
                    artist_count += 1
                # song_id playlist_id song_pop playlist_type
                song_playlist_list.append([
                    song_id,
                    playlist_id,
                    song_pop,
                    playlist_type
                ])
                # song_id tag_name
                for tag in tag_list:
                    song_tag_list.append([
                        song_id,
                        tag
                    ])
                # user_id song_id playlist_like/create/collect_pop
                user_song_list.append([
                    user_id,
                    song_id,
                    song_pop
                ])
                # user_id song_id playlist_create_pop
                creator_song_list.append([
                    creator_data["userId"],
                    song_id,
                    song_pop
                ])

                song_count += 1
        except Exception as e:
            logger.error("get_playlist_songs_by_playlist_id parse failed",
                         "playlist_id:{},playlist_type:{},song_count:{},error_type:{},error:{}"
                         .format(playlist_id, playlist_type, song_count, type(e), e))
            return False, None
        # 存储数据 ----------------------------------------------------
        try:
            _database_tool = database_tool()
            _database_tool.insert_many_song(song_list)
            _database_tool.insert_many_artist(artist_list)
            _database_tool.insert_many_tag(tag_list)
            _database_tool.insert_many_user(creator_list)
            _database_tool.commit()
            _database_tool.insert_many_song_playlist(song_playlist_list)
            _database_tool.insert_many_artist_song(artist_song_list)
            _database_tool.insert_many_song_tag(song_tag_list)
            _database_tool.insert_many_user_playlist(creator_playlist_list)
            _database_tool.insert_many_user_song_column(column="playlist_create_pop", data_list=creator_song_list)
            if playlist_type == config.default_playlist:
                column = "playlist_like_pop"
            elif playlist_type == config.created_playlist:
                column = "playlist_create_pop"
            elif playlist_type == config.collected_playlist:
                column = "playlist_collect_pop"
            else:
                column = ""
            if column != "":
                _database_tool.insert_many_user_song_column(column=column, data_list=user_song_list)
            _database_tool.commit()
            _database_tool.close()
        except Exception as e:
            logger.error("get_playlist_songs_by_playlist_id save failed",
                         "playlist_id:{},playlist_type:{},error_type:{},error:{}"
                         .format(playlist_id, playlist_type, type(e), e))
            return False, None
        logger.info("get_playlist_songs_by_playlist_id success",
                    "playlist_id:{},playlist_type:{},song_count:{}"
                    .format(playlist_id, playlist_type, song_count))
        return True, len(song_list), song_list, artist_list, artist_song_list
Esempio n. 5
0
    def get_song_page_comments(self,
                               song_id,
                               comment_type=config.song_comments_type_hot,
                               offset=0,
                               limit=0,
                               is_get_comment_total=False):
        """
        通过song_id获取某页评论

        :param song_id: 歌曲id
        :param comment_type: 评论类型,详见config
        :param offset: 位移量
        :param limit: 每页评论限制量
        :param is_get_comment_total: 是否为获取评论总数,默认否
        :return: 评论数据
        """
        # 请求参数 ----------------------------------------------------
        _first_param = first_param().get_first_param_eapi_comment(
            offset=offset, limit=limit)
        if comment_type == config.song_comments_type_hot:
            url = config.get_comments_hot_url(song_id)
            eapi_url = config.get_comment_hot_url_for_eapi_param(song_id)
        elif comment_type == config.song_comments_type_default:
            url = config.get_comments_default_url(song_id)
            eapi_url = config.get_comment_default_url_for_eapi_param(song_id)
        else:
            return False, None
        # 请求数据 ----------------------------------------------------
        try:
            content = request_data().get_request_data(
                first_param=_first_param[1],
                url=url,
                api_type=config.api_eapi,
                eapi_url=eapi_url)
            if content[0]:
                if is_get_comment_total:
                    return True, json.loads(content[1])["total"]
                elif comment_type == config.song_comments_type_default:
                    json_data = json.loads(content[1])["comments"]
                elif comment_type == config.song_comments_type_hot:
                    json_data = json.loads(content[1])["hotComments"]
                else:
                    return False, None
            else:
                return False, None
        except Exception as e:
            logger.error(
                "get_song_page_comments get failed",
                "song_id:{},comment_type:{},offset:{},limit:{},error_type:{},error:{}"
                .format(song_id, comment_type, offset, limit, type(e), e))
            return False, []
        # 解析数据 ----------------------------------------------------
        user_list = []
        comment_list = []
        user_comment_list = []
        song_comment_list = []
        try:
            for comment_json in json_data:
                # user_id user_name
                user_list.append([
                    comment_json["user"]["userId"],
                    comment_json["user"]["nickname"]
                ])
                # comment_id comment_type comment_date comment_content comment_like_count
                comment_list.append([
                    comment_json["commentId"], comment_type,
                    comment_json["time"], comment_json["content"],
                    comment_json["likedCount"]
                ])
                # user_id comment_id
                user_comment_list.append([
                    comment_json["user"]["userId"], comment_json["commentId"]
                ])
                # song_id comment_id
                song_comment_list.append([song_id, comment_json["commentId"]])
            # logger.info("get_song_page_comments parse success",
            #             "song_id:{},comment_total:{},comment_type:{},offset:{},limit:{}"
            #             .format(song_id, len(comment_list), comment_type, offset, limit))
        except Exception as e:
            logger.error(
                "get_song_page_comments parse failed",
                "song_id:{},comment_type:{},offset:{},limit:{},error_type:{},error:{}"
                .format(song_id, comment_type, offset, limit, type(e), e))
        # 存储数据 ----------------------------------------------------
        try:
            _database_tool = database_tool()
            _database_tool.insert_many_user(user_list)
            _database_tool.insert_many_comment(comment_list)
            _database_tool.commit()
            _database_tool.insert_many_song_comment(song_comment_list)
            _database_tool.insert_many_user_comment(user_comment_list)
            _database_tool.commit()
            _database_tool.close()
        except Exception as e:
            logger.error(
                "get_song_page_comments save failed",
                "song_id:{},comment_type:{},offset:{},limit:{},error_type:{},error:{}"
                .format(song_id, comment_type, offset, limit, type(e), e))
        return True, (user_list, comment_list, user_comment_list)