Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

Commit

Permalink
增加搜狐视频弹幕下载并改进输入提示
Browse files Browse the repository at this point in the history
  • Loading branch information
xhlove committed Jan 16, 2020
1 parent 3cfccc1 commit 986ec2b
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 30 deletions.
26 changes: 22 additions & 4 deletions GetDanMu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'''
# 作者: weimo
# 创建日期: 2020-01-04 19:14:39
# 上次编辑时间 : 2020-01-11 18:40:49
# 上次编辑时间 : 2020-01-16 19:24:10
# 一个人的命运啊,当然要靠自我奋斗,但是...
'''

Expand All @@ -14,6 +14,7 @@
from sites.qq import main as qq
from sites.iqiyi import main as iqiyi
from sites.youku import main as youku
from sites.sohu import main as sohu
from pfunc.cfunc import check_url_site

# -------------------------------------------
Expand All @@ -37,19 +38,36 @@ def main():
parser.add_argument("-tvid", "--tvid", default="", help="下载tvid对应视频的弹幕,支持同时多个tvid,需要用逗号隔开")
parser.add_argument("-series", "--series", action="store_true", help="尝试通过单集得到合集的全部弹幕")
parser.add_argument("-u", "--url", default="", help="下载视频链接所指向视频的弹幕")
parser.add_argument("-y", "--y", action="store_false", help="默认覆盖原有弹幕而不提示")
parser.add_argument("-y", "--y", action="store_true", help="默认覆盖原有弹幕而不提示")
args = parser.parse_args()
# print(args.__dict__)
init_args = sys.argv
imode = "command_line"
if init_args.__len__() == 1:
# 双击运行或命令执行exe文件时 传入参数只有exe的路径
# 命令行下执行会传入exe的相对路径(在exe所在路径执行时) 传入完整路径(非exe所在路径下执行)
# 双击运行exe传入完整路径
imode == "non_command_line"
if imode == "non_command_line":
content = input("请输入链接:\n")
check_tip = check_url_site(content)
if check_tip is None:
sys.exit("不支持的网站")
args.url = content
args.site = check_tip
# 要么有url 要么有site和相关参数的组合
if args.url != "":
args.site = check_url_site(args.url)
if args.site == "":
args.site = input("请输入站点(qq/iqiyi/youku):\n")
elif args.site == "":
sys.exit("请传入链接或指定网站+视频相关的参数")
if args.site == "qq":
subtitles = qq(args)
if args.site == "iqiyi":
subtitles = iqiyi(args)
if args.site == "youku":
subtitles = youku(args)
if args.site == "sohu":
subtitles = sohu(args)

if __name__ == "__main__":
# 打包 --> pyinstaller GetDanMu.spec
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
<!--
* @作者: weimo
* @创建日期: 2020-01-04 18:45:58
* @上次编辑时间 : 2020-01-11 18:43:20
* @一个人的命运啊,当然要靠自我奋斗,但是...
-->
# GetDanMu

转换/下载各类视频弹幕的工具
[转换/下载各类视频弹幕的工具][1]

项目主页:https://github.com/xhlove/GetDanMu

## 网站支持
Expand All @@ -15,6 +10,7 @@
| **腾讯视频** | <https://v.qq.com/> ||| |
| **爱奇艺** | <https://www.iqiyi.com/> ||||
| **优酷** | <https://v.youku.com/> ||||
| **搜狐视频** | <https://tv.sohu.com/> ||||

## 可能存在的问题
- 下载进度接近100%时暂时没有反应
Expand All @@ -26,6 +22,11 @@

# 更新日志

## 2020/1/16
- 增加搜狐视频的支持(剧集)
- 改进输入提示(双击运行时)
- 腾讯支持-series设定

## 2020/1/11
- 增加优酷弹幕下载,支持合集,支持通过单集直接下载合集弹幕(暂时仅限优酷)
- 改进去重方式
Expand All @@ -35,4 +36,6 @@
## 2020/1/5

- 增加了通过链接下载爱奇艺视频弹幕的方法,支持综艺合集。
- 增加通过链接判断网站
- 增加通过链接判断网站

[1]: https://blog.weimo.info/archives/431/
5 changes: 4 additions & 1 deletion basic/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
'''
# 作者: weimo
# 创建日期: 2020-01-04 19:14:35
# 上次编辑时间: 2020-01-05 14:46:15
# 上次编辑时间 : 2020-01-16 19:10:06
# 一个人的命运啊,当然要靠自我奋斗,但是...
'''

ALLOW_SITES = ["qq", "iqiyi", "youku", "sohu"]

qqlive = {
"User-Agent":"qqlive"
}
Expand Down
10 changes: 8 additions & 2 deletions pfunc/cfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
'''
# 作者: weimo
# 创建日期: 2020-01-05 12:45:18
# 上次编辑时间 : 2020-01-11 17:37:22
# 上次编辑时间 : 2020-01-16 14:50:34
# 一个人的命运啊,当然要靠自我奋斗,但是...
'''

import hashlib
from urllib.parse import urlparse

from basic.vars import ALLOW_SITES

def remove_same_danmu(comments: list):
# 在原有基础上pop会引起索引变化 所以还是采用下面这个方式
contents = []
Expand All @@ -23,7 +25,11 @@ def remove_same_danmu(comments: list):
return contents

def check_url_site(url):
return urlparse(url).netloc.split(".")[-2]
site = urlparse(url).netloc.split(".")[-2]
if site in ALLOW_SITES:
return site
else:
return None

def check_url_locale(url):
flag = {
Expand Down
10 changes: 7 additions & 3 deletions pfunc/dump_to_ass.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'''
# 作者: weimo
# 创建日期: 2020-01-04 19:17:44
# 上次编辑时间 : 2020-01-11 17:25:09
# 上次编辑时间 : 2020-01-16 20:06:23
# 一个人的命运啊,当然要靠自我奋斗,但是...
'''
import os
Expand Down Expand Up @@ -31,12 +31,16 @@ def write_lines_to_file(ass_head, lines, file_path):
for line in lines:
f.write(line + "\n")

def check_file(name, skip=False, fpath=os.getcwd()):
def check_file(name, args, fpath=os.getcwd()):
flag = True
file_path = os.path.join(fpath, name + ".ass")
if os.path.isfile(file_path):
if skip:
if args.y:
os.remove(file_path)
elif args.series:
# 存在重复的 那么直接pass(认为已经下载好了)
flag = False
return flag, file_path
else:
isremove = input("{}已存在,是否覆盖?(y/n):".format(file_path))
if isremove.strip() == "y":
Expand Down
24 changes: 23 additions & 1 deletion pfunc/request_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'''
# 作者: weimo
# 创建日期: 2020-01-04 19:14:43
# 上次编辑时间 : 2020-01-11 17:42:30
# 上次编辑时间 : 2020-01-16 19:44:55
# 一个人的命运啊,当然要靠自我奋斗,但是...
'''
import re
Expand Down Expand Up @@ -40,6 +40,28 @@ def get_all_vids_by_column_id():
# 综艺类型的
pass

def get_cid_by_vid(vid):
api_url = "http://union.video.qq.com/fcgi-bin/data"
params = {
"tid": "98",
"appid": "10001005",
"appkey": "0d1a9ddd94de871b",
"idlist": vid,
"otype":"json"
}
r = requests.get(api_url, params=params, headers=qqlive).content.decode("utf-8")
data = json.loads(r.lstrip("QZOutputJson=").rstrip(";"))
try:
cid = data["results"][0]["fields"]
except Exception as e:
print("load fields error info -->", e)
return None
if cid.get("sync_cover"):
return cid["sync_cover"]
elif cid.get("cover_list"):
return cid["cover_list"][0]
return

def get_all_vids_by_cid(cid):
api_url = "http://union.video.qq.com/fcgi-bin/data"
params = {
Expand Down
4 changes: 2 additions & 2 deletions sites/iqiyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'''
# 作者: weimo
# 创建日期: 2020-01-04 19:14:41
# 上次编辑时间 : 2020-01-11 17:23:32
# 上次编辑时间 : 2020-01-16 19:58:51
# 一个人的命运啊,当然要靠自我奋斗,但是...
'''

Expand Down Expand Up @@ -83,7 +83,7 @@ def main(args):
subtitles = {}
for name, duration, tvid in vinfos:
print(name, "开始下载...")
flag, file_path = check_file(name, skip=args.y)
flag, file_path = check_file(name, args)
if flag is False:
print("跳过{}".format(name))
continue
Expand Down
31 changes: 24 additions & 7 deletions sites/qq.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'''
# 作者: weimo
# 创建日期: 2020-01-04 19:14:37
# 上次编辑时间 : 2020-01-11 17:25:34
# 上次编辑时间 : 2020-01-16 20:04:51
# 一个人的命运啊,当然要靠自我奋斗,但是...
'''

Expand All @@ -14,6 +14,7 @@

from basic.vars import qqlive
from pfunc.dump_to_ass import check_file, write_one_video_subtitles
from pfunc.request_info import get_cid_by_vid
from pfunc.request_info import get_all_vids_by_cid as get_vids
from pfunc.request_info import get_danmu_target_id_by_vid as get_target_id

Expand Down Expand Up @@ -97,18 +98,18 @@ def get_danmu_by_target_id(vid: str, duration: int, target_id, font="微软雅
return comments


def get_one_subtitle_by_vinfo(vinfo, font="微软雅黑", font_size=25, skip=False):
def get_one_subtitle_by_vinfo(vinfo, font="微软雅黑", font_size=25, args=""):
vid, name, duration, target_id = vinfo
print(name, "开始下载...")
flag, file_path = check_file(name, skip=skip)
flag, file_path = check_file(name, args)
if flag is False:
print("跳过{}".format(name))
return
comments = get_danmu_by_target_id(vid, duration, target_id, font=font, font_size=font_size)
# print("{}弹幕下载完成!".format(name))
return comments, file_path

def ask_input(url=""):
def ask_input(url="", isall=False):
if url == "":
url = input("请输入vid/coverid/链接,输入q退出:\n").strip()
if url == "q" or url == "":
Expand All @@ -117,6 +118,9 @@ def ask_input(url=""):
params = url.replace(".html", "").split("/")
if params[-1].__len__() == 11:
vids = [params[-1]]
if isall:
cid = get_cid_by_vid(params[-1])
vids += get_vids(cid)
elif params[-1].__len__() == 15:
cid = params[-1]
vids = get_vids(cid)
Expand All @@ -132,6 +136,9 @@ def ask_input(url=""):

def main(args):
vids = []
isall = False
if args.series:
isall = True
if args.cid and args.cid.__len__() == 15:
vids += get_vids(args.cid)
if args.vid:
Expand All @@ -141,16 +148,26 @@ def main(args):
vids += [vid for vid in args.vid.strip().replace(" ", "").split(",") if vid.__len__() == 11]
else:
pass
if args.series:
cid = get_cid_by_vid(args.vid)
vids += get_vids(cid)
if args.url:
vids += ask_input(url=args.url)
vids += ask_input(url=args.url, isall=isall)
if args.vid == "" and args.cid == "" and args.url == "":
vids += ask_input()
vids += ask_input(isall=isall)
if vids.__len__() <= 0:
sys.exit("没有任何有效输入")
vids_bak = vids
vids = []
for vid in vids_bak:
if vid in vids:
continue
else:
vids.append(vid)
vinfos = get_video_info_by_vid(vids)
subtitles = {}
for vinfo in vinfos:
infos = get_one_subtitle_by_vinfo(vinfo, args.font, args.font_size, args.y)
infos = get_one_subtitle_by_vinfo(vinfo, args.font, args.font_size, args=args)
if infos is None:
continue
comments, file_path = infos
Expand Down
Loading

0 comments on commit 986ec2b

Please sign in to comment.