コード例 #1
0
    def grab_video(self, user_id, action, content, max_cursor=0):

        url = URL.favorite_url() if action == "USER_LIKE" else URL.post_url()

        favorite_params = copy.deepcopy(self.__USER_VIDEO_PARAMS)
        favorite_params['user_id'] = user_id
        favorite_params['max_cursor'] = max_cursor
        query_params = {**favorite_params, **self.common_params}
        real_url = gen_url(self.token, url, query_params)
        # 目前支持两种类型爬取,用户喜欢过的,和当前用户所有已发布的视频
        cookies = self.__COOKIES
        cookies['install_id'] = str(self.__device["install_id"])

        resp = requests.get(real_url,
                            verify=False,
                            cookies=cookies,
                            headers={"User-Agent": "okhttp/3.10.0.1"},
                            timeout=3)

        favorite_info = json.loads(resp.content.decode("utf-8"))

        hasmore = favorite_info.get('has_more')
        max_cursor = favorite_info.get('max_cursor')
        video_infos = favorite_info.get('aweme_list')

        for per_video in video_infos:
            author_nick_name = per_video['author'].get("nickname")
            author_uid = per_video['author'].get('uid')
            video_desc = per_video.get('desc')
            music_id = per_video['music']['play_url'].get('uri') if content == "-m" else None
            download_item = {
                "author_nick_name": author_nick_name,
                "video_desc": video_desc,
                "author_uid": author_uid,
                "music_id": music_id
            }
            aweme_id = per_video.get("aweme_id")
            self.download_user_video(aweme_id, **download_item)
            time.sleep(5)

        return hasmore, max_cursor
コード例 #2
0
    def grab_video(self, user_id, action, content, max_cursor=0):
        favorite_params = copy.deepcopy(self.__USER_VIDEO_PARAMS)
        favorite_params['user_id'] = user_id
        favorite_params['max_cursor'] = max_cursor
        query_params = {**favorite_params, **self.common_params}
        sign = getSign(self.__get_token(), query_params)
        params = {**query_params, **sign}

        # 目前支持两种类型爬取,用户喜欢过的,和当前用户所有已发布的视频
        url = URL.favorite_url() if action == "USER_LIKE" else URL.post_url()
        resp = requests.get(url,
                            params=params,
                            verify=False,
                            headers=self.__HEADERS)

        favorite_info = resp.json()

        hasmore = favorite_info.get('has_more')
        max_cursor = favorite_info.get('max_cursor')
        video_infos = favorite_info.get('aweme_list')

        for per_video in video_infos:
            author_nick_name = per_video['author'].get("nickname")
            author_uid = per_video['author'].get('uid')
            video_desc = per_video.get('desc')
            music_id = per_video['music']['play_url'].get(
                'uri') if content == "-m" else None
            download_item = {
                "author_nick_name": author_nick_name,
                "video_desc": video_desc,
                "author_uid": author_uid,
                "music_id": music_id
            }
            aweme_id = per_video.get("aweme_id")
            self.download_user_video(aweme_id, **download_item)
            time.sleep(5)

        return hasmore, max_cursor