Exemple #1
0
    def fetch(cls, url: str, mode=0) -> Result:
        url = cls.get_url(url)
        if url is None:
            return ErrorResult.URL_NOT_INCORRECT

        res = http_utils.get(url, header=headers)
        if http_utils.is_error(res):
            return Result.error(res)

        try:
            id = re.findall(r"(?<=item\/)(\d+)(?=\?)", res.url)[0]
        except IndexError:
            return Result.failed(res.reason)

        url = "https://h5.pipix.com/bds/webapi/item/detail/?item_id=" + id + "&source=share"

        info_res = http_utils.get(url, header=share_headers)
        if http_utils.is_error(info_res):
            return Result.error(info_res)

        data = json.loads(str(info_res.text))

        try:
            video = data['data']['item']['comments'][0]['item']['video']
            url = cls.get_video(video)
        except (KeyError, IndexError):
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        return Result.success(url)
Exemple #2
0
    def fetch(cls, url: str, model=0) -> Result:
        """
        获取视频详情
        :param url:
        :param model:
        :return:
        """
        url = analyzer.get_url(vtype, url)
        if url is None:
            return ErrorResult.URL_NOT_INCORRECT

        res = http_utils.get(url, header=headers)
        if http_utils.is_error(res):
            return Result.error(res)

        try:
            id = re.findall(r"(?<=item\/)(\d+)(?=\?)", res.url)[0]
        except IndexError:
            return Result.failed(res.reason)

        url = "https://h5.pipix.com/bds/webapi/item/detail/?item_id=" + id + "&source=share"

        info_res = http_utils.get(url, header=share_headers)
        if http_utils.is_error(info_res):
            return Result.error(info_res)

        data = json.loads(str(info_res.text))

        try:
            url = data['data']['item']['origin_video_download']['url_list'][0][
                'url']
        except (KeyError, IndexError):
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        return Result.success(url)
Exemple #3
0
    def fetch(cls, url: str, mode=0) -> Result:
        url = cls.get_url(url)
        if url is None:
            return ErrorResult.URL_NOT_INCORRECT

        # 请求短链接,获得itemId和dytk
        res = http_utils.get(url, header=headers)
        if http_utils.is_error(res):
            return Result.error(res)

        try:
            props = re.findall(r"(?<=<script id=\"__NEXT_DATA__\" type=\"application\/json\" crossorigin=\"anonymous\">)"
                              r"(.*?)"
                              r"(?=<\/script><script crossorigin=\"anonymous\")", res.text)
        except IndexError:
            return Result.failed(res.reason)

        data = json.loads(str(props[0]))

        try:
            video_url = data['props']['pageProps']['videoData']['itemInfos']['video']['urls'][0]
        except (KeyError, IndexError):
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        res = http_utils.get(video_url)
        text = res.content.decode('utf-8', 'ignore')

        index = text.index('vid:')
        vid = text[index+4: index+36]
        download_url = 'https://api2.musical.ly/aweme/v1/playwm/?video_id=' + vid
        result = Result.success(download_url)

        if mode == 1:
            result.ref = res.url
        return result
Exemple #4
0
    def fetch(cls, url: str, mode=0) -> Result:
        burl = cls.get_url(url)
        if burl is None:
            return ErrorResult.URL_NOT_INCORRECT

        bvid = cls.get_bvid(burl)
        if bvid is None:
            return ErrorResult.URL_NOT_INCORRECT

        res = http_utils.get('https://api.bilibili.com/x/player/pagelist',
                             param={
                                 'bvid': bvid,
                                 'jsonp': 'jsonp'
                             },
                             header=headers)
        if http_utils.is_error(res):
            return Result.error(res)

        data = json.loads(str(res.text))
        p = re.findall(r"(?<=p=)(\d)", url)
        if len(p) == 0:
            index = 0
        else:
            index = int(p[0]) - 1

        try:
            cid = data['data'][index]['cid']
        except (KeyError, IndexError):
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        # http://api.bilibili.com/x/player/playurl?cid=227539569&bvid=BV1cD4y1m7ce&qn=112&fnval=16
        res = http_utils.get('http://api.bilibili.com/x/player/playurl',
                             param={
                                 'cid': cid,
                                 'bvid': bvid,
                                 'qn': 112,
                                 'fnval': 0,
                                 'fnver': 0,
                                 'fourk': 1,
                             },
                             header=user_headers)
        if http_utils.is_error(res):
            return Result.error(res)

        data = json.loads(str(res.text))

        try:
            url = data['data']['durl'][0]['url']
            # url = data['data']['dash']['video'][0]['baseUrl']
        except (KeyError, IndexError):
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        result = Result.success(url)
        if mode != 0:
            result.ref = res.url
        return result
Exemple #5
0
    def fetch(cls, url: str, mode=0) -> Result:
        url = cls.get_url(url)
        if url is None:
            return ErrorResult.URL_NOT_INCORRECT

        # 请求短链接,获得itemId
        res = http_utils.get(url, header=headers)
        if http_utils.is_error(res):
            return Result.error(res)

        html = res.text
        try:
            data = re.findall(r"(?<=window\.__INITIAL_STATE__=)(.*?)(?=;\(function\(\))", html)[0]
        except IndexError:
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        data = json.loads(data)

        try:
            bvid = data['epInfo']['bvid']
            cid = data['epInfo']['cid']
        except Exception as e:
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        # http://api.bilibili.com/x/player/playurl?cid=227539569&bvid=BV1cD4y1m7ce&qn=112&fnval=16
        res = http_utils.get('http://api.bilibili.com/x/player/playurl',
                             param={
                                 'cid': cid,
                                 'bvid': bvid,
                                 'qn': 112,
                                 'fnval': 0,
                                 'fnver': 0,
                                 'fourk': 1,
                            }, header=user_headers)
        if http_utils.is_error(res):
            return Result.error(res)

        data = json.loads(str(res.text))

        try:
            url = data['data']['durl'][0]['url']
        except (KeyError, IndexError):
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        result = Result.success(url)
        if mode != 0:
            result.ref = res.url
        return result
Exemple #6
0
    def download(cls, url) -> HttpResponse:
        """
        下载视频
        :param url:
        :return:
        """
        # 检查文件
        index = cls.index(url)
        file = store.find(vtype, index)
        if file is not None:
            return Service.stream(file, index)

        result = cls.fetch(url, model=1)
        if not result.is_success():
            return HttpResponseServerError(result.get_data())

        dheaders = download_headers.copy()
        dheaders['referer'] = result.ref

        res = http_utils.get(url=result.get_data(), header=dheaders)
        if http_utils.is_error(res):
            return HttpResponseServerError(str(res))

        store.save(vtype, res, index)
        res.close()

        file = store.find(vtype, index)
        return Service.stream(file, index)
Exemple #7
0
    def fetch(cls, url: str, mode=0) -> Result:
        url = cls.get_url(url)
        if url is None:
            return ErrorResult.URL_NOT_INCORRECT

        # 请求短链接,获得itemId和dytk
        res = http_utils.get(url, header=headers)
        if http_utils.is_error(res):
            return Result.error(res)

        html = res.text
        try:
            url = re.findall(r"(?<=\"srcNoMark\":\"https://)(.*?)(?=.mp4)",
                             html)[0]
        except IndexError:
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        if not url:
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        url = "https://" + url + ".mp4"
        result = Result.success(url)
        if mode != 0:
            result.ref = res.url
        return result
Exemple #8
0
    def proxy_download(cls, vtype, url, header: dict, extra: str, mode=1) -> HttpResponse:
        # 检查文件
        index = cls.index(url)
        file, filename = store.find(vtype, index, extra)
        if file is not None:
            return Service.stream(file, filename)

        result = cls.fetch(url, mode=mode)
        if not result.is_success():
            return HttpResponseServerError(result.get_data())

        if mode == 1:
            header = header.copy()
            header['referer'] = result.ref

        if result.is_image():
            res = store.save_image(vtype, result.get_data(), index)
            if res is not None:
                return res
        else:
            res = http_utils.get(url=result.get_data(), header=header)
            if http_utils.is_error(res):
                return HttpResponseServerError(str(res))

            store.save(vtype, res, index, result.extra)
            res.close()

        file, filename = store.find(vtype, index, result.extra)
        return Service.stream(file, filename)
Exemple #9
0
    def fetch(cls, url: str, model=0) -> Result:
        """
        获取视频详情
        :param url:
        :param model:
        :return:
        """
        url = analyzer.get_url(vtype, url)
        if url is None:
            return ErrorResult.URL_NOT_INCORRECT

        # 请求短链接,获得itemId和dytk
        res = http_utils.get(url, header=headers)
        if http_utils.is_error(res):
            return Result.error(res)

        html = str(res.content)
        url = re.findall(r"(?<=type=\"video\/mp4\" src=\")(.*?)(?=\")",
                         html)[0]
        if not url:
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        result = Result.success(url)
        if model != 0:
            result.ref = res.url
        return result
Exemple #10
0
    def fetch(cls, url: str, model=0) -> Result:
        """
        获取视频详情
        :param url:
        :param model:
        :return:
        """
        url = analyzer.get_url(vtype, url)
        if url is None:
            return ErrorResult.URL_NOT_INCORRECT

        # 请求短链接,获得itemId和dytk
        res = http_utils.get(url, header=headers)
        if http_utils.is_error(res):
            return Result.error(res)

        html = res.text
        try:
            url = re.findall(r"(?<=srcNoMark&#34;:&#34;)(.*?)(?=&)", html)[0]
        except IndexError:
            return Result.failed(res.text)
        if not url:
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        result = Result.success(url)
        if model != 0:
            result.ref = res.url
        return result
Exemple #11
0
    def fetch(cls, url: str, model=0) -> Result:
        """
        获取视频详情
        :param url:
        :param model:
        :return:
        """
        url = analyzer.get_url(vtype, url)
        if url is None:
            return ErrorResult.URL_NOT_INCORRECT

        # 请求短链接,获得itemId和dytk
        res = http_utils.get(url, header=headers)
        if http_utils.is_error(res):
            return Result.error(res)

        html = str(res.content)
        try:
            item_id = re.findall(r"(?<=itemId:\s\")\d+", html)[0]
            dytk = re.findall(r"(?<=dytk:\s\")(.*?)(?=\")", html)[0]
        except IndexError:
            return Result.failed(res.reason)

        # 组装视频长链接
        infourl = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + item_id + "&dytk=" + dytk

        # 请求长链接,获取play_addr
        url_res = http_utils.get(infourl, header=headers)
        if http_utils.is_error(url_res):
            return Result.error(url_res)

        vhtml = str(url_res.text)
        try:
            uri = re.findall(r'(?<=\"uri\":\")\w{32}(?=\")', vhtml)[0]
        except IndexError:
            return Result.failed(url_res.reason)
        if not uri:
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        link = "https://aweme.snssdk.com/aweme/v1/play/?video_id=" + uri + \
                "&line=0&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0" \
                "&is_play_url=1&is_support_h265=0&source=PackSourceEnum_PUBLISH"
        result = Result.success(link)

        if model != 0:
            result.ref = res.url
        return result
Exemple #12
0
    def fetch(cls, url: str, model=0) -> Result:
        """
        获取视频详情
        :param url:
        :param model:
        :return:
        """
        url = analyzer.get_url(vtype, url)
        if url is None:
            return ErrorResult.URL_NOT_INCORRECT

        # 请求短链接,获得itemId
        res = http_utils.get(url, header=headers)
        if http_utils.is_error(res):
            return Result.error(res)

        try:
            item_id = re.findall(r"(?<=item_id=)\d+(?=\&)", res.url)[0]
        except IndexError:
            return Result.failed(res.reason)

        # 视频信息链接
        infourl = "https://share.huoshan.com/api/item/info?item_id=" + item_id

        # 请求长链接,获取play_addr
        url_res = http_utils.get(infourl, header=info_headers)
        if http_utils.is_error(url_res):
            return Result.error(url_res)

        vhtml = str(url_res.text)
        try:
            video_id = re.findall(r'(?<=video_id\=)\w+(?=\&)', vhtml)[0]
        except IndexError:
            return Result.failed(url_res.reason)

        if not video_id:
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        link = "https://api.huoshan.com/hotsoon/item/video/_source/?video_id=" + video_id + "&line=0&app_id=0&vquality=normal"
        result = Result.success(link)

        if model != 0:
            result.ref = res.url
        return result
Exemple #13
0
def save_image(vtype: Video, images: List[str], index: str):
    filename = get_name(vtype, index) + ".zip"
    with zipfile.ZipFile(filename, 'w') as imgZip:
        index = 1
        for image in images:
            res = http_utils.get(url=image)
            if http_utils.is_error(res):
                return HttpResponseServerError(str(res))
            # res.headers.get('content-type')
            imgZip.writestr(f"{index}.jpg", res.content)
            index += 1
    return None
Exemple #14
0
    def fetch(cls, url: str, mode=0) -> Result:
        url = cls.get_url(url)
        if url is None:
            return ErrorResult.URL_NOT_INCORRECT

        # 请求短链接,获得itemId
        res = http_utils.get(url, header=headers)
        if http_utils.is_error(res):
            return Result.error(res)

        # html = str(res.content)
        try:
            item_id = re.findall(r"(?<=video/)\d+", res.url)[0]
        except IndexError:
            return Result.failed(res.reason)

        # 组装视频长链接
        infourl = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + item_id + "&dytk="# + dytk

        # 请求长链接,获取play_addr
        url_res = http_utils.get(infourl, header=headers)
        if http_utils.is_error(url_res):
            return Result.error(url_res)

        data = json.loads(str(url_res.text))
        if not data['status_code'] == 0:
            return Result.failed(data['status_msg'])

        item = data['item_list'][0]
        if item['aweme_type'] == 4:
            result = DouyinService.get_video(item)
        elif item['aweme_type'] == 2:
            result = DouyinService.get_image(item)
            result.extra = ".zip"
        else:
            return ErrorResult.VIDEO_ADDRESS_NOT_FOUNT

        if mode == 1:
            result.ref = res.url
        return result
Exemple #15
0
    def get_url(cls, text: str) -> Optional[str]:
        if "bilibili" in text:
            urls = re.findall(r'(?<=www\.bilibili\.com\/video\/).+', text,
                              re.I | re.M)
            if urls:
                return "https://www.bilibili.com/video/" + urls[0]
            return None

        urls = re.findall(r'(?<=b23\.tv\/)\w+', text, re.I | re.M)
        url = "https://b23.tv/" + urls[0]
        res = http_utils.get(url, header=headers)
        url = res.url
        return url