Spaces:
Sleeping
Sleeping
File size: 49,242 Bytes
0aee47a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 |
"""
bilibili_api.interactive_video
互动视频相关操作
"""
# pylint: skip-file
import os
import copy
import enum
import json
import time
import shutil
import zipfile
from urllib import parse
from random import randint as rand
from asyncio import CancelledError, create_task
from typing import List, Tuple, Union, Callable, Coroutine
import httpx
from . import settings
from .video import Video
from .utils.utils import get_api
from .utils.AsyncEvent import AsyncEvent
from .utils.credential import Credential
from .utils.network import Api
API = get_api("interactive_video")
class InteractiveButtonAlign(enum.Enum):
"""
按钮的文字在按钮中的位置
``` text
-----
|xxx|----o (TEXT_LEFT)
-----
-----
o----|xxx| (TEXT_RIGHT)
-----
----------
|XXXXXXXX| (DEFAULT)
----------
```
- DEFAULT
- TEXT_UP
- TEXT_RIGHT
- TEXT_DOWN
- TEXT_LEFT
"""
DEFAULT = 0
TEXT_UP = 1
TEXT_RIGHT = 2
TEXT_DOWN = 3
TEXT_LEFT = 4
class InteractiveNodeJumpingType(enum.Enum):
"""
对下一节点的跳转的方式
- ASK : 选择
- DEFAULT: 跳转到默认节点
- READY : 选择(只有一个选择)
"""
READY = 1
DEFAULT = 0
ASK = 2
class InteractiveVariable:
"""
互动节点的变量
"""
def __init__(
self,
name: str,
var_id: str,
var_value: int,
show: bool = False,
random: bool = False,
):
"""
Args:
name (str) : 变量名
var_id (str) : 变量 id
var_value (int) : 变量的值
show (bool) : 是否显示
random (bool) : 是否为随机值(1-100)
"""
self.__var_id = var_id
self.__var_value = var_value
self.__name = name
self.__is_show = show
self.__random = random
def get_id(self) -> str:
return self.__var_id
def refresh_value(self) -> None:
"""
刷新变量数值
"""
if self.is_random():
self.__var_value = int(rand(0, 100))
def get_value(self) -> int:
return self.__var_value
def is_show(self) -> bool:
return self.__is_show
def is_random(self) -> bool:
return self.__random
def get_name(self) -> str:
return self.__name
def __str__(self):
return f"{self.__name} {self.__var_value}"
class InteractiveButton:
"""
互动视频节点按钮类
"""
def __init__(
self,
text: str,
x: int,
y: int,
align: Union[InteractiveButtonAlign, int] = InteractiveButtonAlign.DEFAULT,
):
"""
Args:
text (str) : 文字
x (int) : x 轴
y (int) : y 轴
align (InteractiveButtonAlign | int): 按钮的文字在按钮中的位置
"""
self.__text = text
self.__pos = (x, y)
if isinstance(align, InteractiveButtonAlign):
align = align.value
self.__align = align
def get_text(self) -> str:
return self.__text
def get_align(self) -> int:
return self.__align # type: ignore
def get_pos(self) -> Tuple[int, int]:
return self.__pos
def __str__(self):
return f"{self.__text} {self.__pos}"
class InteractiveJumpingCondition:
"""
节点跳转的公式,只有公式成立才会跳转
"""
def __init__(self, var: List[InteractiveVariable] = [], condition: str = "True"):
"""
Args:
var (List[InteractiveVariable]): 所有变量
condition (str) : 公式
"""
self.__vars = var
self.__command = condition
def get_result(self) -> bool:
"""
计算公式获得结果
Returns:
bool: 是否成立
"""
if self.__command == "":
return True
command = copy.copy(self.__command)
for var in self.__vars:
var_name = var.get_id()
var_value = var.get_value()
command = command.replace(var_name, str(var_value))
command = command.replace("&&", " and ")
command = command.replace("||", " or ")
command = command.replace("!", " not ")
command = command.replace("===", "==")
command = command.replace("!==", "!=")
command = command.replace("true", "True")
command = command.replace("false", "False")
return eval(command)
def __str__(self):
return f"{self.__command}"
class InteractiveJumpingCommand:
"""
节点跳转对变量的操作
"""
def __init__(self, var: List[InteractiveVariable] = [], command: str = ""):
"""
Args:
var (List[InteractiveVariable]): 所有变量
command (str) : 公式
"""
self.__vars = var
self.__command = command
def run_command(self) -> List["InteractiveVariable"]:
"""
执行操作
Returns:
List[InteractiveVariable]
"""
if self.__command == "":
return self.__vars
for code in self.__command.split(";"):
var_name_ = code.split("=")[0]
var_new_value = code.split("=")[1]
for var in self.__vars:
var_name = var.get_id()
var_value = var.get_value()
var_new_value = var_new_value.replace(var_name, str(var_value))
var_new_value_calc = eval(var_new_value)
for var in self.__vars:
if var.get_id() == var_name_:
var._InteractiveVariable__var_value = var_new_value_calc # type: ignore
return self.__vars
class InteractiveNode:
"""
互动视频节点类
"""
def __init__(
self,
video: "InteractiveVideo",
node_id: int,
cid: int,
vars: List[InteractiveVariable],
button: Union[InteractiveButton, None] = None,
condition: InteractiveJumpingCondition = InteractiveJumpingCondition(),
native_command: InteractiveJumpingCommand = InteractiveJumpingCommand(),
is_default: bool = False,
):
"""
Args:
video (InteractiveVideo) : 视频类
node_id (int) : 节点 id
cid (int) : CID
vars (List[InteractiveVariable]) : 变量
button (InteractiveButton) : 对应的按钮
condition (InteractiveJumpingCondition): 跳转公式
native_command (InteractiveJumpingCommand) : 跳转时变量操作
is_default (bool) : 是不是默认的跳转的节点
"""
self.__parent = video
self.__id = node_id
self.__cid = cid
self.__button = button
self.__jumping_command = condition
self.__is_default = is_default
self.__vars = vars
self.__command = native_command
self.__vars = self.__command.run_command()
async def get_vars(self) -> List[InteractiveVariable]:
"""
获取节点的所有变量
Returns:
List[InteractiveVariable]: 节点的所有变量
"""
return self.__vars
async def get_children(self) -> List["InteractiveNode"]:
"""
获取节点的所有子节点
Returns:
List[InteractiveNode]: 所有子节点
"""
edge_info = await self.__parent.get_edge_info(self.__id)
nodes = []
if edge_info["edges"].get("questions") == None:
return []
for node in edge_info["edges"]["questions"][0]["choices"]:
node_id = node["id"]
node_cid = node["cid"]
if "text_align" in node.keys():
text_align = node["text_align"]
else:
text_align = 0
if "option" in node.keys():
node_button = InteractiveButton(
node["option"], node.get("x"), node.get("y"), text_align
)
else:
node_button = None
node_condition = InteractiveJumpingCondition(
await self.get_vars(), node["condition"]
)
node_command = InteractiveJumpingCommand(
await self.get_vars(), node["native_action"]
)
if "is_default" in node.keys():
node_is_default = node["is_default"]
else:
node_is_default = False
node_vars = copy.deepcopy(await self.get_vars())
nodes.append(
InteractiveNode(
self.__parent,
node_id,
node_cid,
node_vars,
node_button,
node_condition,
node_command,
node_is_default,
)
)
return nodes
def is_default(self) -> bool:
return self.__is_default
async def get_jumping_type(self) -> int:
"""
获取子节点跳转方式 (参考 InteractiveNodeJumpingType)
"""
edge_info = await self.__parent.get_edge_info(self.__id)
return edge_info["edges"]["questions"][0]["type"]
def get_node_id(self) -> int:
return self.__id
def get_cid(self) -> int:
return self.__cid
def get_self_button(self) -> "InteractiveButton":
if self.__button == None:
return InteractiveButton("", -1, -1)
return self.__button
def get_jumping_condition(self) -> "InteractiveJumpingCondition":
return self.__jumping_command
def get_video(self) -> "InteractiveVideo":
return self.__parent
async def get_info(self) -> dict:
"""
获取节点的简介
Returns:
dict: 调用 API 返回的结果
"""
return await self.__parent.get_edge_info(self.__id)
def __str__(self):
return f"{self.get_node_id()}"
class InteractiveGraph:
"""
情节树类
"""
def __init__(self, video: "InteractiveVideo", skin: dict, root_cid: int):
"""
Args:
video (InteractiveVideo): 互动视频类
skin (dict) : 样式
root_cid (int) : 根节点 CID
"""
self.__parent = video
self.__skin = skin
self.__node = InteractiveNode(self.__parent, 1, root_cid, [])
def get_video(self) -> "InteractiveVideo":
return self.__parent
def get_skin(self) -> dict:
return self.__skin
async def get_root_node(self) -> "InteractiveNode":
"""
获取根节点
Returns:
InteractiveNode: 根节点
"""
edge_info = await self.__parent.get_edge_info(None)
if "hidden_vars" in edge_info.keys():
node_vars = edge_info["hidden_vars"]
else:
node_vars = []
var_list = []
for var in node_vars:
var_value = var["value"]
var_name = var["name"]
var_show = var["is_show"]
var_id = var["id_v2"]
if var["type"] == 2:
random = True
else:
random = False
var_list.append(
InteractiveVariable(var_name, var_id, var_value, var_show, random)
)
self.__node._InteractiveNode__command = InteractiveJumpingCommand( # type: ignore
var_list
)
self.__node._InteractiveNode__vars = var_list # type: ignore
return self.__node
async def get_children(self) -> List["InteractiveNode"]:
"""
获取子节点
Returns:
List[InteractiveNode]: 子节点
"""
return await self.__node.get_children()
class InteractiveVideo(Video):
"""
互动视频类
"""
def __init__(self, bvid=None, aid=None, credential=None):
super().__init__(bvid, aid, credential)
async def up_get_ivideo_pages(self) -> dict:
"""
获取交互视频的分 P 信息。up 主需要拥有视频所有权。
Returns:
dict: 调用 API 返回的结果
"""
credential = self.credential if self.credential else Credential()
api = API["info"]["videolist"]
params = {"bvid": self.get_bvid()}
return await Api(**api, credential=credential).update_params(**params).result
async def up_submit_story_tree(self, story_tree: str) -> dict:
"""
上传交互视频的情节树。up 主需要拥有视频所有权。
Args:
story_tree (str): 情节树的描述,参考 bilibili_storytree.StoryGraph, 需要 Serialize 这个结构
Returns:
dict: 调用 API 返回的结果
"""
credential = self.credential if self.credential else Credential()
api = API["operate"]["savestory"]
form_data = {"preview": "0", "data": story_tree, "csrf": credential.bili_jct}
headers = {
"User-Agent": "Mozilla/5.0",
"Referer": "https://member.bilibili.com",
"Content-Encoding": "gzip, deflate, br",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json, text/plain, */*",
}
data = parse.urlencode(form_data)
return (
await Api(**api, credential=credential, no_csrf=True)
.update_data(**data)
.update_headers(**headers)
.result
)
async def get_graph_version(self) -> int:
"""
获取剧情图版本号,仅供 `get_edge_info()` 使用。
Returns:
int: 剧情图版本号
"""
# 取得初始顶点 cid
bvid = self.get_bvid()
credential = self.credential if self.credential else Credential()
v = Video(bvid=bvid, credential=credential)
page_list = await v.get_pages()
cid = page_list[0]["cid"]
# 获取剧情图版本号
url = "https://api.bilibili.com/x/player/v2"
params = {"bvid": bvid, "cid": cid}
resp = (
await Api(method="GET", url=url, credential=credential)
.update_params(**params)
.result
)
return resp["interaction"]["graph_version"]
async def get_edge_info(self, edge_id: Union[int, None] = None):
"""
获取剧情图节点信息
Args:
edge_id (int, optional) : 节点 ID,为 None 时获取根节点信息. Defaults to None.
Returns:
dict: 调用 API 返回的结果
"""
bvid = self.get_bvid()
credential = self.credential if self.credential is not None else Credential()
api = API["info"]["edge_info"]
params = {"bvid": bvid, "graph_version": (await self.get_graph_version())}
if edge_id is not None:
params["edge_id"] = edge_id
return await Api(**api, credential=credential).update_params(**params).result
async def mark_score(self, score: int = 5):
"""
为互动视频打分
Args:
score (int): 互动视频分数. Defaults to 5.
Returns:
dict: 调用 API 返回的结果
"""
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
api = API["operate"]["mark_score"]
data = {"mark": score, "bvid": self.get_bvid()}
return await Api(**api, credential=self.credential).update_data(**data).result
async def get_cid(self) -> int:
"""
获取稿件 cid
"""
return await super().get_cid(0)
async def get_graph(self):
"""
获取稿件情节树
Returns:
InteractiveGraph: 情节树
"""
edge_info = await self.get_edge_info(1)
cid = await self.get_cid()
return InteractiveGraph(self, edge_info["edges"]["skin"], cid)
class InteractiveVideoDownloaderEvents(enum.Enum):
"""
互动视频下载器事件枚举
| event | meaning | IVI mode | NODE_VIDEOS mode | DOT_GRAPH mode | NO_PACKAGING mode | Is Built-In downloader event |
| ----- | ------- | -------- | ---------------- | -------------- | ----------------- | ------------------------- |
| START | 开始下载 | [x] | [x] | [x] | [x] | [ ] |
| GET | 获取到节点信息 | [x] | [x] | [x] | [x] | [ ] |
| PREPARE_DOWNLOAD | 准备下载单个节点 | [x] | [x] | [ ] | [x] | [ ] |
| DOWNLOAD_START | 开始下载单个文件 | Unknown | Unknown | [ ] | Unknown | [x] |
| DOWNLOAD_PART | 文件分块部分完成 | Unknown | Unknown | [ ] | Unknown | [x] |
| DOWNLOAD_SUCCESS | 完成下载 | Unknown | Unknown | [ ] | Unknown | [x] |
| PACKAGING | 正在打包 | [x] | [ ] | [ ] | [ ] | [ ] |
| SUCCESS | 下载成功 | [x] | [x] | [x] | [x] | [ ] |
| ABORTED | 用户暂停 | [x] | [x] | [x] | [x] | [ ] |
| FAILED | 下载失败 | [x] | [x] | [x] | [x] | [ ] |
"""
START = "START"
GET = "GET"
DOWNLOAD_START = "DOWNLOAD_START"
DOWNLOAD_PART = "DOWNLOAD_PART"
DOWNLOAD_SUCCESS = "DOWNLOAD_SUCCESS"
PACKAGING = "PACKAGING"
SUCCESS = "SUCCESS"
ABORTED = "ABORTED"
FAILED = "FAILED"
class InteractiveVideoDownloaderMode(enum.Enum):
"""
互动视频下载模式
- IVI: 下载可播放的 ivi 文件
- NODE_VIDEOS: 下载所有节点的所有视频并存放在某个文件夹,每一个节点的视频命名为 `{节点 id} {节点标题 (自动去除敏感字符)}.mp4`
- DOT_GRAPH: 下载 dot 格式的情节树图表
- NO_PACKAGING: 前面按照 ivi 文件下载步骤进行下载,但是最终不会打包成为 ivi 文件,所有文件将存放于一个文件夹中。互动视频数据将存放在一个文件夹中,里面的文件命名/含义与拆包后的 ivi 文件完全相同。
"""
IVI = "ivi"
NODE_VIDEOS = "videos"
DOT_GRAPH = "dot"
NO_PACKAGING = "no_pack"
class InteractiveVideoDownloader(AsyncEvent):
"""
互动视频下载类
"""
def __init__(
self,
video: InteractiveVideo,
out: str = "",
self_download_func: Union[Coroutine, None] = None,
downloader_mode: InteractiveVideoDownloaderMode = InteractiveVideoDownloaderMode.IVI,
):
"""
Args:
video (InteractiveVideo) : 互动视频类
out (str) : 输出文件地址 (如果模式为 NODE_VIDEOS/NO_PACKAGING 则此参数表示所有节点视频的存放目录)
self_download_func (Coroutine | None) : 自定义下载函数(需 async 函数)
downloader_mode (InteractiveVideoDownloaderMode): 下载模式
`self_download_func` 函数应接受两个参数(第一个是下载 URL,第二个是输出地址(精确至文件名))
"""
super().__init__()
self.__video = video
if self_download_func == None:
self.__download_func = self.__download
else:
self.__download_func = self_download_func
self.__task = None
self.__out = out
self.__mode = downloader_mode
async def __download(self, url: str, out: str) -> None:
resp = httpx.get(
url,
headers={
"User-Agent": "Mozilla/5.0",
"Referer": "https://www.bilibili.com",
},
proxies={"all://": settings.proxy},
stream=True,
)
resp.raise_for_status()
if os.path.exists(out):
os.remove(out)
parent = os.path.dirname(out)
if not os.path.exists(parent):
os.mkdir(parent)
self.dispatch("DOWNLOAD_START", {"url": url, "out": out})
all_length = int(resp.headers["Content-Length"])
parts = all_length // 1024 + (1 if all_length % 1024 != 0 else 0)
cnt = 0
start_time = time.perf_counter()
with open(out, "wb") as f:
for chunk in resp.iter_bytes(1024):
cnt += 1
self.dispatch(
"DOWNLOAD_PART",
{
"done": cnt,
"total": parts,
"time": int(time.perf_counter() - start_time),
},
)
f.write(chunk)
self.dispatch("DOWNLOAD_SUCCESS")
async def __main(self) -> None:
# 初始化
self.dispatch("START")
if self.__out == "":
self.__out = self.__video.get_bvid() + ".ivi"
if self.__out.endswith(".ivi"):
self.__out = self.__out.rstrip(".ivi")
if os.path.exists(self.__out + ".ivi"):
os.remove(self.__out + ".ivi")
tmp_dir_name = self.__out + ".tmp"
if not os.path.exists(tmp_dir_name):
os.mkdir(tmp_dir_name)
def createEdge(edge_id: int):
"""
创建节点信息到 edges_info
"""
edges_info[edge_id] = {
"title": None,
"cid": None,
"button": None,
"condition": None,
"jump_type": None,
"is_default": None,
"command": None,
"sub": [],
}
def var2dict(var: InteractiveVariable):
return {
"name": var.get_name(),
"id": var.get_id(),
"value": var.get_value(),
"show": var.is_show(),
"random": var.is_random(),
}
# 存储顶点信息
edges_info = {}
# 使用队列来遍历剧情图,初始为 None 是为了从初始顶点开始
queue: List[InteractiveNode] = [
await (await self.__video.get_graph()).get_root_node()
]
# 设置初始顶点
n = await (await self.__video.get_graph()).get_root_node()
if n.get_node_id() not in edges_info:
createEdge(n.get_node_id())
edges_info[n.get_node_id()]["cid"] = n.get_cid()
edges_info[n.get_node_id()]["button"] = {
"text": n.get_self_button().get_text(),
"align": n.get_self_button().get_align(),
"pos": (n.get_self_button().get_pos()),
}
edges_info[n.get_node_id()]["vars"] = [
var2dict(var) for var in (await n.get_vars())
]
edges_info[n.get_node_id()]["condition"] = (n.get_jumping_condition()._InteractiveJumpingCondition__command,) # type: ignore
edges_info[n.get_node_id()]["jump_type"] = 0
edges_info[n.get_node_id()]["is_default"] = True
edges_info[n.get_node_id()]["command"] = n._InteractiveNode__command._InteractiveJumpingCommand__command # type: ignore
while queue:
# 出队
now_node = queue.pop()
if (
now_node.get_node_id() in edges_info
and edges_info[now_node.get_node_id()]["title"] is not None
and edges_info[now_node.get_node_id()]["cid"] is not None
):
# 该情况为已获取到所有信息,说明是跳转到之前已处理的顶点,不作处理
continue
# 获取顶点信息,最大重试 3 次
retry = 3
while True:
try:
node = await now_node.get_info()
subs = await now_node.get_children()
self.dispatch(
"GET",
{"title": node["title"], "node_id": now_node.get_node_id()},
)
break
except Exception as e:
retry -= 1
if retry < 0:
raise e
# 检查节顶点是否在 edges_info 中,本次步骤得到 title 信息
if node["edge_id"] not in edges_info:
# 不在,新建
createEdge(node["edge_id"])
# 设置 title
edges_info[node["edge_id"]]["title"] = node["title"]
# 无可达顶点,即不能再往下走了,类似树的叶子节点
if "questions" not in node["edges"]:
continue
# 遍历所有可达顶点
for n in subs:
# 该步骤获取顶点的 cid(视频分 P 的 ID)
if n.get_node_id() not in edges_info:
createEdge(n.get_node_id())
edges_info[n.get_node_id()]["cid"] = n.get_cid()
edges_info[n.get_node_id()]["button"] = {
"text": n.get_self_button().get_text(),
"align": n.get_self_button().get_align(),
"pos": n.get_self_button().get_pos(),
}
def var2dict(var: InteractiveVariable):
return {
"name": var.get_name(),
"id": var.get_id(),
"value": var.get_value(),
"show": var.is_show(),
"random": var.is_random(),
}
edges_info[n.get_node_id()]["condition"] = n.get_jumping_condition()._InteractiveJumpingCondition__command # type: ignore
edges_info[n.get_node_id()][
"jump_type"
] = await now_node.get_jumping_type()
edges_info[n.get_node_id()]["is_default"] = n.is_default()
edges_info[n.get_node_id()]["command"] = n._InteractiveNode__command._InteractiveJumpingCommand__command # type: ignore
edges_info[now_node.get_node_id()]["sub"] = [
n.get_node_id() for n in subs
]
# 所有可达顶点 ID 入队
queue.insert(0, n)
json.dump(
edges_info,
open(tmp_dir_name + "/ivideo.json", "w+", encoding="utf-8"),
indent=2,
)
json.dump(
{
"bvid": self.__video.get_bvid(),
"title": (await self.__video.get_info())["title"],
},
open(tmp_dir_name + "/bilivideo.json", "w+", encoding="utf-8"),
indent=2,
)
cid_set = set()
for key, item in edges_info.items():
cid = item["cid"]
if not cid in cid_set:
self.dispatch("PREPARE_DOWNLOAD", {"cid": item["cid"]})
cid_set.add(cid)
url = await self.__video.get_download_url(cid=cid, html5=True)
await self.__download_func(
url["durl"][0]["url"],
tmp_dir_name + "/" + str(cid) + ".mp4",
) # type: ignore
root_cid = await self.__video.get_cid()
if not root_cid in cid_set:
self.dispatch("PREPARE_DOWNLOAD", {"cid": root_cid})
cid = await self.__video.get_cid()
url = await self.__video.get_download_url(cid=cid, html5=True)
title = (await self.__video.get_info())["title"]
await self.__download_func(
url["durl"][0]["url"], tmp_dir_name + "/" + str(cid) + ".mp4"
) # type: ignore
self.dispatch("PACKAGING")
zip = zipfile.ZipFile(
open(self.__out + ".ivi", "wb+"), mode="w", compression=zipfile.ZIP_DEFLATED
) # outFullName为压缩文件的完整路径
for path, dirnames, filenames in os.walk(tmp_dir_name):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
fpath = path.replace(tmp_dir_name, "")
for filename in filenames:
zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
zip.close()
shutil.rmtree(tmp_dir_name)
self.dispatch("SUCCESS")
async def __node_videos_main(self) -> None:
# 初始化
self.dispatch("START")
tmp_dir_name = self.__out
if not os.path.exists(tmp_dir_name):
os.mkdir(tmp_dir_name)
def createEdge(edge_id: int):
"""
创建节点信息到 edges_info
"""
edges_info[edge_id] = {
"title": None,
"cid": None,
"button": None,
"condition": None,
"jump_type": None,
"is_default": None,
"command": None,
"sub": [],
}
def var2dict(var: InteractiveVariable):
return {
"name": var.get_name(),
"id": var.get_id(),
"value": var.get_value(),
"show": var.is_show(),
"random": var.is_random(),
}
# 存储顶点信息
edges_info = {}
# 使用队列来遍历剧情图,初始为 None 是为了从初始顶点开始
queue: List[InteractiveNode] = [
await (await self.__video.get_graph()).get_root_node()
]
# 设置初始顶点
n = await (await self.__video.get_graph()).get_root_node()
if n.get_node_id() not in edges_info:
createEdge(n.get_node_id())
edges_info[n.get_node_id()]["cid"] = n.get_cid()
edges_info[n.get_node_id()]["button"] = {
"text": n.get_self_button().get_text(),
"align": n.get_self_button().get_align(),
"pos": (n.get_self_button().get_pos()),
}
edges_info[n.get_node_id()]["vars"] = [
var2dict(var) for var in (await n.get_vars())
]
edges_info[n.get_node_id()]["condition"] = (n.get_jumping_condition()._InteractiveJumpingCondition__command,) # type: ignore
edges_info[n.get_node_id()]["jump_type"] = 0
edges_info[n.get_node_id()]["is_default"] = True
edges_info[n.get_node_id()]["command"] = n._InteractiveNode__command._InteractiveJumpingCommand__command # type: ignore
while queue:
# 出队
now_node = queue.pop()
if (
now_node.get_node_id() in edges_info
and edges_info[now_node.get_node_id()]["title"] is not None
and edges_info[now_node.get_node_id()]["cid"] is not None
):
# 该情况为已获取到所有信息,说明是跳转到之前已处理的顶点,不作处理
continue
# 获取顶点信息,最大重试 3 次
retry = 3
while True:
try:
node = await now_node.get_info()
subs = await now_node.get_children()
self.dispatch(
"GET",
{"title": node["title"], "node_id": now_node.get_node_id()},
)
break
except Exception as e:
retry -= 1
if retry < 0:
raise e
# 检查节顶点是否在 edges_info 中,本次步骤得到 title 信息
if node["edge_id"] not in edges_info:
# 不在,新建
createEdge(node["edge_id"])
# 设置 title
edges_info[node["edge_id"]]["title"] = node["title"]
# 无可达顶点,即不能再往下走了,类似树的叶子节点
if "questions" not in node["edges"]:
continue
# 遍历所有可达顶点
for n in subs:
# 该步骤获取顶点的 cid(视频分 P 的 ID)
if n.get_node_id() not in edges_info:
createEdge(n.get_node_id())
edges_info[n.get_node_id()]["cid"] = n.get_cid()
edges_info[n.get_node_id()]["button"] = {
"text": n.get_self_button().get_text(),
"align": n.get_self_button().get_align(),
"pos": n.get_self_button().get_pos(),
}
def var2dict(var: InteractiveVariable):
return {
"name": var.get_name(),
"id": var.get_id(),
"value": var.get_value(),
"show": var.is_show(),
"random": var.is_random(),
}
edges_info[n.get_node_id()]["condition"] = n.get_jumping_condition()._InteractiveJumpingCondition__command # type: ignore
edges_info[n.get_node_id()][
"jump_type"
] = await now_node.get_jumping_type()
edges_info[n.get_node_id()]["is_default"] = n.is_default()
edges_info[n.get_node_id()]["command"] = n._InteractiveNode__command._InteractiveJumpingCommand__command # type: ignore
edges_info[now_node.get_node_id()]["sub"] = [
n.get_node_id() for n in subs
]
# 所有可达顶点 ID 入队
queue.insert(0, n)
cid_set = set()
for key, item in edges_info.items():
cid = item["cid"]
if not cid in cid_set:
self.dispatch("PREPARE_DOWNLOAD", {"cid": item["cid"]})
cid_set.add(cid)
url = await self.__video.get_download_url(cid=cid, html5=True)
await self.__download_func(
url["durl"][0]["url"],
tmp_dir_name + "/" + str(key) + " " + item["title"] + ".mp4",
) # type: ignore
root_cid = await self.__video.get_cid()
if not root_cid in cid_set:
self.dispatch("PREPARE_DOWNLOAD", {"cid": root_cid})
cid = await self.__video.get_cid()
url = await self.__video.get_download_url(cid=cid, html5=True)
title = (await self.__video.get_info())["title"]
await self.__download_func(
url["durl"][0]["url"], tmp_dir_name + "/1 " + title + ".mp4"
) # type: ignore
self.dispatch("SUCCESS")
async def __dot_graph_main(self) -> None:
self.dispatch("START")
if not self.__out.endswith(".dot"):
self.__out += ".dot"
class node_info:
node_id: int
subs: List[int]
cid: int
title: str
def __eq__(self, info: "node_info"):
self.subs.sort()
info.subs.sort()
return (
(info.subs == self.subs)
and (info.title == self.title)
and (info.cid == self.cid)
)
def __lt__(self, info: "node_info"):
return self.cid < info.cid
def __gt__(self, info: "node_info"):
return self.cid > info.cid
fetched_nodes_info: List[node_info] = []
node_info_dict = {}
scripts = []
graph = await self.__video.get_graph()
queue: List[InteractiveNode] = [await graph.get_root_node()]
while queue:
queue_backup = copy.copy(queue)
queue = []
for cur_node in queue_backup:
cur_node_info = await cur_node.get_info()
cur_node_children = await cur_node.get_children()
self.dispatch(
"GET",
{
"title": cur_node_info["title"],
"node_id": cur_node.get_node_id(),
},
)
cur_node_info_class = node_info()
cur_node_info_class.node_id = cur_node.get_node_id()
cur_node_info_class.cid = cur_node.get_cid()
cur_node_info_class.subs = [n.get_node_id() for n in cur_node_children]
cur_node_info_class.title = cur_node_info["title"]
back_to_pre = False
back_to_node_title = -1
for fetched_info in fetched_nodes_info:
if fetched_info == cur_node_info_class:
back_to_pre = True
back_to_node_title = fetched_info.title
if not back_to_pre:
node_info_dict[cur_node.get_node_id()] = cur_node_info_class
for cur_node_child in cur_node_children:
script_label = ""
if cur_node_child.get_jumping_condition()._InteractiveJumpingCondition__command != "": # type: ignore
script_label = script_label + "Condition: [" + cur_node_child.get_jumping_condition()._InteractiveJumpingCondition__command + "]" # type: ignore
if cur_node_child._InteractiveNode__command._InteractiveJumpingCommand__command != "": # type: ignore
script_label = script_label + "\nNative Command: [" + cur_node_child._InteractiveNode__command._InteractiveJumpingCommand__command + "]" # type: ignore
elif cur_node_child._InteractiveNode__command._InteractiveJumpingCommand__command != "": # type: ignore
script_label = script_label + "\nNative Command: [" + cur_node_child._InteractiveNode__command._InteractiveJumpingCommand__command + "]" # type: ignore
scripts.append(
{
"from": cur_node.get_node_id(),
"to": cur_node_child.get_node_id(),
"label": script_label,
}
)
queue.append(cur_node_child)
fetched_nodes_info.append(cur_node_info_class)
else:
node_info_dict[cur_node.get_node_id()] = f"跳转至 {back_to_node_title}"
graph_content = "digraph {\nfontname=FangSong\nnode [fontname=FangSong]\n"
for script in scripts:
graph_content += f'\t{script["from"]} -> {script["to"]}'
if script["label"] != "":
graph_content += f' [label="{script["label"]}"]\n'
else:
graph_content += "\n"
for node_info_key, node_info_item in node_info_dict.items():
if isinstance(node_info_item, node_info):
graph_content += f'\t{node_info_key} [label="{node_info_item.title}"]\n'
else:
graph_content += f'\t{node_info_key} [label="{node_info_item}"]\n'
vars_string = "Variables: "
for var in await (await graph.get_root_node()).get_vars():
var_attribute = ""
if var.is_random():
var_attribute = "Random"
else:
if var.is_show():
var_attribute = "Normal"
else:
var_attribute = "Hide"
vars_string += f"[{var.get_id()} -> {var.get_name()} = {var.get_value()}, {var_attribute}]\n"
graph_content += f'\tlabel="{vars_string}"'
graph_content += "}"
with open(self.__out, "w+", encoding="utf-8") as dot_file:
dot_file.write(graph_content)
self.dispatch("SUCCESS")
async def __no_packaging_main(self) -> None:
# 初始化
self.dispatch("START")
tmp_dir_name = self.__out
if not os.path.exists(tmp_dir_name):
os.mkdir(tmp_dir_name)
def createEdge(edge_id: int):
"""
创建节点信息到 edges_info
"""
edges_info[edge_id] = {
"title": None,
"cid": None,
"button": None,
"condition": None,
"jump_type": None,
"is_default": None,
"command": None,
"sub": [],
}
def var2dict(var: InteractiveVariable):
return {
"name": var.get_name(),
"id": var.get_id(),
"value": var.get_value(),
"show": var.is_show(),
"random": var.is_random(),
}
# 存储顶点信息
edges_info = {}
# 使用队列来遍历剧情图,初始为 None 是为了从初始顶点开始
queue: List[InteractiveNode] = [
await (await self.__video.get_graph()).get_root_node()
]
# 设置初始顶点
n = await (await self.__video.get_graph()).get_root_node()
if n.get_node_id() not in edges_info:
createEdge(n.get_node_id())
edges_info[n.get_node_id()]["cid"] = n.get_cid()
edges_info[n.get_node_id()]["button"] = {
"text": n.get_self_button().get_text(),
"align": n.get_self_button().get_align(),
"pos": (n.get_self_button().get_pos()),
}
edges_info[n.get_node_id()]["vars"] = [
var2dict(var) for var in (await n.get_vars())
]
edges_info[n.get_node_id()]["condition"] = (n.get_jumping_condition()._InteractiveJumpingCondition__command,) # type: ignore
edges_info[n.get_node_id()]["jump_type"] = 0
edges_info[n.get_node_id()]["is_default"] = True
edges_info[n.get_node_id()]["command"] = n._InteractiveNode__command._InteractiveJumpingCommand__command # type: ignore
while queue:
# 出队
now_node = queue.pop()
if (
now_node.get_node_id() in edges_info
and edges_info[now_node.get_node_id()]["title"] is not None
and edges_info[now_node.get_node_id()]["cid"] is not None
):
# 该情况为已获取到所有信息,说明是跳转到之前已处理的顶点,不作处理
continue
# 获取顶点信息,最大重试 3 次
retry = 3
while True:
try:
node = await now_node.get_info()
subs = await now_node.get_children()
self.dispatch(
"GET",
{"title": node["title"], "node_id": now_node.get_node_id()},
)
break
except Exception as e:
retry -= 1
if retry < 0:
raise e
# 检查节顶点是否在 edges_info 中,本次步骤得到 title 信息
if node["edge_id"] not in edges_info:
# 不在,新建
createEdge(node["edge_id"])
# 设置 title
edges_info[node["edge_id"]]["title"] = node["title"]
# 无可达顶点,即不能再往下走了,类似树的叶子节点
if "questions" not in node["edges"]:
continue
# 遍历所有可达顶点
for n in subs:
# 该步骤获取顶点的 cid(视频分 P 的 ID)
if n.get_node_id() not in edges_info:
createEdge(n.get_node_id())
edges_info[n.get_node_id()]["cid"] = n.get_cid()
edges_info[n.get_node_id()]["button"] = {
"text": n.get_self_button().get_text(),
"align": n.get_self_button().get_align(),
"pos": n.get_self_button().get_pos(),
}
def var2dict(var: InteractiveVariable):
return {
"name": var.get_name(),
"id": var.get_id(),
"value": var.get_value(),
"show": var.is_show(),
"random": var.is_random(),
}
edges_info[n.get_node_id()]["condition"] = n.get_jumping_condition()._InteractiveJumpingCondition__command # type: ignore
edges_info[n.get_node_id()][
"jump_type"
] = await now_node.get_jumping_type()
edges_info[n.get_node_id()]["is_default"] = n.is_default()
edges_info[n.get_node_id()]["command"] = n._InteractiveNode__command._InteractiveJumpingCommand__command # type: ignore
edges_info[now_node.get_node_id()]["sub"] = [
n.get_node_id() for n in subs
]
# 所有可达顶点 ID 入队
queue.insert(0, n)
json.dump(
edges_info,
open(tmp_dir_name + "/ivideo.json", "w+", encoding="utf-8"),
indent=2,
)
json.dump(
{
"bvid": self.__video.get_bvid(),
"title": (await self.__video.get_info())["title"],
},
open(tmp_dir_name + "/bilivideo.json", "w+", encoding="utf-8"),
indent=2,
)
cid_set = set()
for key, item in edges_info.items():
cid = item["cid"]
if not cid in cid_set:
self.dispatch("PREPARE_DOWNLOAD", {"cid": item["cid"]})
cid_set.add(cid)
url = await self.__video.get_download_url(cid=cid, html5=True)
await self.__download_func(
url["durl"][0]["url"],
tmp_dir_name + "/" + str(key) + " " + item["title"] + ".mp4",
) # type: ignore
root_cid = await self.__video.get_cid()
if not root_cid in cid_set:
self.dispatch("PREPARE_DOWNLOAD", {"cid": root_cid})
cid = await self.__video.get_cid()
url = await self.__video.get_download_url(cid=cid, html5=True)
title = (await self.__video.get_info())["title"]
await self.__download_func(
url["durl"][0]["url"], tmp_dir_name + "/1 " + title + ".mp4"
) # type: ignore
self.dispatch("SUCCESS")
async def start(self) -> None:
"""
开始下载
"""
if self.__mode.value == "ivi":
task = create_task(self.__main())
elif self.__mode.value == "dot":
task = create_task(self.__dot_graph_main())
elif self.__mode.value == "no_pack":
task = create_task(self.__no_packaging_main())
else:
task = create_task(self.__node_videos_main())
self.__task = task
try:
result = await task
self.__task = None
return result
except CancelledError:
# 忽略 task 取消异常
pass
except Exception as e:
self.dispatch("FAILED", {"err": e})
raise e
async def abort(self) -> None:
"""
中断下载
"""
if self.__task:
self.__task.cancel("用户手动取消")
self.dispatch("ABORTED", None)
def get_ivi_file_meta(path: str) -> dict:
"""
获取 ivi 文件信息
Args:
path (str): 文件地址
Returns:
dict: 文件信息
"""
ivi = zipfile.ZipFile(open(path, "rb"))
info = ivi.open("bilivideo.json").read()
return json.loads(info)
|