|
| 1 | +# -*-coding:utf-8 -*- |
| 2 | +# Website: https://cuijiahua.com |
| 3 | +# Author: Jack Cui |
| 4 | +# Date: 2020.07.22 |
| 5 | +import requests |
| 6 | +import json |
| 7 | +import re |
| 8 | +import json |
| 9 | +import math |
| 10 | +import xml2ass |
| 11 | +import time |
| 12 | +from contextlib import closing |
| 13 | + |
| 14 | +from bs4 import BeautifulSoup |
| 15 | + |
| 16 | +import os |
| 17 | +from win32com.client import Dispatch |
| 18 | + |
| 19 | +def addTasktoXunlei(down_url): |
| 20 | + flag = False |
| 21 | + o = Dispatch('ThunderAgent.Agent64.1') |
| 22 | + try: |
| 23 | + o.AddTask(down_url, "", "", "", "", -1, 0, 5) |
| 24 | + o.CommitTasks() |
| 25 | + flag = True |
| 26 | + except Exception: |
| 27 | + print(Exception.message) |
| 28 | + print(" AddTask is fail!") |
| 29 | + return flag |
| 30 | + |
| 31 | +def get_download_url(arcurl): |
| 32 | + # 微信搜索 JackCui-AI 关注公众号,后台回复「B 站」获取视频解析地址 |
| 33 | + jiexi_url = 'xxx' |
| 34 | + payload = {'url': arcurl} |
| 35 | + jiexi_req = requests.get(jiexi_url, params=payload) |
| 36 | + jiexi_bf = BeautifulSoup(jiexi_req.text) |
| 37 | + jiexi_dn_url = jiexi_bf.iframe.get('src') |
| 38 | + dn_req = requests.get(jiexi_dn_url) |
| 39 | + dn_bf = BeautifulSoup(dn_req.text) |
| 40 | + video_script = dn_bf.find('script',src = None) |
| 41 | + DPlayer = str(video_script.string) |
| 42 | + download_url = re.findall('\'(http[s]?:(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)\'', DPlayer)[0] |
| 43 | + download_url = download_url.replace('\\', '') |
| 44 | + return download_url |
| 45 | + |
| 46 | +space_url = 'https://space.bilibili.com/280793434' |
| 47 | +search_url = 'https://api.bilibili.com/x/space/arc/search' |
| 48 | +mid = space_url.split('/')[-1] |
| 49 | +sess = requests.Session() |
| 50 | +search_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36', |
| 51 | + 'Accept-Language': 'zh-CN,zh;q=0.9', |
| 52 | + 'Accept-Encoding': 'gzip, deflate, br', |
| 53 | + 'Accept': 'application/json, text/plain, */*'} |
| 54 | + |
| 55 | +# 获取视频个数 |
| 56 | +ps = 1 |
| 57 | +pn = 1 |
| 58 | +search_params = {'mid': mid, |
| 59 | + 'ps': ps, |
| 60 | + 'tid': 0, |
| 61 | + 'pn': pn} |
| 62 | +req = sess.get(url=search_url, headers=search_headers, params=search_params, verify=False) |
| 63 | +info = json.loads(req.text) |
| 64 | +video_count = info['data']['page']['count'] |
| 65 | + |
| 66 | +ps = 10 |
| 67 | +page = math.ceil(video_count/ps) |
| 68 | +videos_list = [] |
| 69 | +for pn in range(1, page+1): |
| 70 | + search_params = {'mid': mid, |
| 71 | + 'ps': ps, |
| 72 | + 'tid': 0, |
| 73 | + 'pn': pn} |
| 74 | + req = sess.get(url=search_url, headers=search_headers, params=search_params, verify=False) |
| 75 | + info = json.loads(req.text) |
| 76 | + vlist = info['data']['list']['vlist'] |
| 77 | + for video in vlist: |
| 78 | + title = video['title'] |
| 79 | + bvid = video['bvid'] |
| 80 | + vurl = 'https://www.bilibili.com/video/' + bvid |
| 81 | + videos_list.append([title, vurl]) |
| 82 | +print('共 %d 个视频' % len(videos_list)) |
| 83 | +all_video = {} |
| 84 | +# 下载前 10 个视频 |
| 85 | +for video in videos_list[:10]: |
| 86 | + download_url = get_download_url(video[1]) |
| 87 | + print(video[0] + ':' + download_url) |
| 88 | + # 记录视频名字 |
| 89 | + xunlei_video_name = download_url.split('?')[0].split('/')[-1] |
| 90 | + filename = video[0] |
| 91 | + for c in u'´☆<img draggable="false" data-mce-resize="false" data-mce-placeholder="1" data-wp-emoji="1" class="emoji" alt="❤" src="https://s.w.org/images/core/emoji/11.2.0/svg/2764.svg">◦\/:*?"<>| ': |
| 92 | + filename = filename.replace(c, '') |
| 93 | + save_video_name = filename + '.mp4' |
| 94 | + all_video[xunlei_video_name] = save_video_name |
| 95 | + |
| 96 | + addTasktoXunlei(download_url) |
| 97 | + # 弹幕下载 |
| 98 | + danmu_name = filename + '.xml' |
| 99 | + danmu_ass = filename + '.ass' |
| 100 | + oid = download_url.split('/')[6] |
| 101 | + danmu_url = 'https://api.bilibili.com/x/v1/dm/list.so?oid={}'.format(oid) |
| 102 | + danmu_header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36', |
| 103 | + 'Accept': '*/*', |
| 104 | + 'Accept-Encoding': 'gzip, deflate, br', |
| 105 | + 'Accept-Language': 'zh-CN,zh;q=0.9'} |
| 106 | + with closing(sess.get(danmu_url, headers=danmu_header, stream=True, verify=False)) as response: |
| 107 | + if response.status_code == 200: |
| 108 | + with open(danmu_name, 'wb') as file: |
| 109 | + for data in response.iter_content(): |
| 110 | + file.write(data) |
| 111 | + file.flush() |
| 112 | + else: |
| 113 | + print('链接异常') |
| 114 | + time.sleep(0.5) |
| 115 | + xml2ass.Danmaku2ASS(danmu_name, danmu_ass, 1280, 720) |
| 116 | +# 视频重命名 |
| 117 | +for key, item in all_video.items(): |
| 118 | + while key not in os.listdir('./'): |
| 119 | + time.sleep(1) |
| 120 | + os.rename(key, item) |
0 commit comments