Spaces:
Sleeping
Sleeping
File size: 87,232 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 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 |
"""
bilibili_api.video
视频相关操作
注意,同时存在 page_index 和 cid 的参数,两者至少提供一个。
"""
import re
import os
import json
import struct
import asyncio
import logging
import datetime
from enum import Enum
from inspect import isfunction
from functools import cmp_to_key
from dataclasses import dataclass
from typing import Any, List, Union, Optional
import httpx
import aiohttp
from . import settings
from .utils.aid_bvid_transformer import bvid2aid, aid2bvid
from .utils.utils import get_api
from .utils.AsyncEvent import AsyncEvent
from .utils.credential import Credential
from .utils.BytesReader import BytesReader
from .utils.danmaku import Danmaku, SpecialDanmaku
from .utils.network import get_aiohttp_session, Api, get_session
from .exceptions import (
ArgsException,
NetworkException,
ResponseException,
DanmakuClosedException,
)
API = get_api("video")
def get_cid_info_sync(cid: int):
"""
获取 cid 信息 (对应的视频,具体分 P 序号,up 等)
Returns:
dict: 调用 https://hd.biliplus.com 的 API 返回的结果
"""
api = API["info"]["cid_info"]
params = {"cid": cid}
return Api(**api).update_params(**params).result_sync
async def get_cid_info(cid: int):
"""
获取 cid 信息 (对应的视频,具体分 P 序号,up 等)
Returns:
dict: 调用 https://hd.biliplus.com 的 API 返回的结果
"""
api = API["info"]["cid_info"]
params = {"cid": cid}
return await Api(**api).update_params(**params).result
class DanmakuOperatorType(Enum):
"""
弹幕操作枚举
+ DELETE - 删除弹幕
+ PROTECT - 保护弹幕
+ UNPROTECT - 取消保护弹幕
"""
DELETE = 1
PROTECT = 2
UNPROTECT = 3
class VideoAppealReasonType:
"""
视频投诉原因枚举
注意: 每一项均为函数,部分项有参数,没有参数的函数无需调用函数,直接传入即可,有参数的函数请调用结果之后传入。
- ILLEGAL(): 违法违禁
- PRON(): 色情
- VULGAR(): 低俗
- GAMBLED_SCAMS(): 赌博诈骗
- VIOLENT(): 血腥暴力
- PERSONAL_ATTACK(): 人身攻击
- PLAGIARISM(bvid: str): 与站内其他视频撞车
- BAD_FOR_YOUNGS(): 青少年不良信息
- CLICKBAIT(): 不良封面/标题
- POLITICAL_RUMORS(): 涉政谣言
- SOCIAL_RUMORS(): 涉社会事件谣言
- COVID_RUMORS(): 疫情谣言
- UNREAL_EVENT(): 虚假不实消息
- OTHER(): 有其他问题
- LEAD_WAR(): 引战
- CANNOT_CHARGE(): 不能参加充电
- UNREAL_COPYRIGHT(source: str): 转载/自制类型错误
"""
ILLEGAL = lambda: 2
PRON = lambda: 3
VULGAR = lambda: 4
GAMBLED_SCAMS = lambda: 5
VIOLENT = lambda: 6
PERSONAL_ATTACK = lambda: 7
BAD_FOR_YOUNGS = lambda: 10000
CLICKBAIT = lambda: 10013
POLITICAL_RUMORS = lambda: 10014
SOCIAL_RUMORS = lambda: 10015
COVID_RUMORS = lambda: 10016
UNREAL_EVENT = lambda: 10017
OTHER = lambda: 1
LEAD_WAR = lambda: 9
CANNOT_CHARGE = lambda: 10
@staticmethod
def PLAGIARISM(bvid: str):
"""
与站内其他视频撞车
Args:
bvid (str): 撞车对象
"""
return {"tid": 8, "撞车对象": bvid}
@staticmethod
def UNREAL_COPYRIGHT(source: str):
"""
转载/自制类型错误
Args:
source (str): 原创视频出处
"""
return {"tid": 52, "出处": source}
class Video:
"""
视频类,各种对视频的操作均在里面。
"""
def __init__(
self,
bvid: Union[None, str] = None,
aid: Union[None, int] = None,
credential: Union[None, Credential] = None,
):
"""
Args:
bvid (str | None, optional) : BV 号. bvid 和 aid 必须提供其中之一。
aid (int | None, optional) : AV 号. bvid 和 aid 必须提供其中之一。
credential (Credential | None, optional): Credential 类. Defaults to None.
"""
# ID 检查
if bvid is not None:
self.set_bvid(bvid)
elif aid is not None:
self.set_aid(aid)
else:
# 未提供任一 ID
raise ArgsException("请至少提供 bvid 和 aid 中的其中一个参数。")
# 未提供 credential 时初始化该类
self.credential: Credential = Credential() if credential is None else credential
# 用于存储视频信息,避免接口依赖视频信息时重复调用
self.__info: Union[dict, None] = None
def set_bvid(self, bvid: str) -> None:
"""
设置 bvid。
Args:
bvid (str): 要设置的 bvid。
"""
# 检查 bvid 是否有效
if not re.search("^BV[a-zA-Z0-9]{10}$", bvid):
raise ArgsException("bvid 提供错误,必须是以 BV 开头的纯字母和数字组成的 12 位字符串(大小写敏感)。")
self.__bvid = bvid
self.__aid = bvid2aid(bvid)
def get_bvid(self) -> str:
"""
获取 BVID。
Returns:
str: BVID。
"""
return self.__bvid
def set_aid(self, aid: int) -> None:
"""
设置 aid。
Args:
aid (int): AV 号。
"""
if aid <= 0:
raise ArgsException("aid 不能小于或等于 0。")
self.__aid = aid
self.__bvid = aid2bvid(aid)
def get_aid(self) -> int:
"""
获取 AID。
Returns:
int: aid。
"""
return self.__aid
async def get_info(self) -> dict:
"""
获取视频信息。
Returns:
dict: 调用 API 返回的结果。
"""
api = API["info"]["info"]
params = {"bvid": self.get_bvid(), "aid": self.get_aid()}
resp = (
await Api(**api, credential=self.credential).update_params(**params).result
)
# 存入 self.__info 中以备后续调用
self.__info = resp
return resp
async def get_detail(self) -> dict:
"""
获取视频详细信息
Returns:
dict: 调用 API 返回的结果。
"""
api = API["info"]["detail"]
params = {
"bvid": self.get_bvid(),
"aid": self.get_aid(),
"need_operation_card": 0,
"need_elec": 0,
}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def __get_info_cached(self) -> dict:
"""
获取视频信息,如果已获取过则使用之前获取的信息,没有则重新获取。
Returns:
dict: 调用 API 返回的结果。
"""
if self.__info is None:
return await self.get_info()
return self.__info
# get_stat 403/404 https://github.com/SocialSisterYi/bilibili-API-collect/issues/797 等恢复
# async def get_stat(self) -> dict:
# """
# 获取视频统计数据(播放量,点赞数等)。
# Returns:
# dict: 调用 API 返回的结果。
# """
# api = API["info"]["stat"]
# params = {"bvid": self.get_bvid(), "aid": self.get_aid()}
# return await Api(**api, credential=self.credential).update_params(**params).result
async def get_up_mid(self) -> int:
"""
获取视频 up 主的 mid。
Returns:
int: up_mid
"""
info = await self.__get_info_cached()
return info["owner"]["mid"]
async def get_tags(
self, page_index: Union[int, None] = 0, cid: Union[int, None] = None
) -> List[dict]:
"""
获取视频标签。
Args:
page_index (int | None): 分 P 序号. Defaults to 0.
cid (int | None): 分 P 编码. Defaults to None.
Returns:
List[dict]: 调用 API 返回的结果。
"""
if cid == None:
if page_index == None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.get_cid(page_index=page_index)
api = API["info"]["tags"]
params = {"bvid": self.get_bvid(), "aid": self.get_aid(), "cid": cid}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def get_chargers(self) -> dict:
"""
获取视频充电用户。
Returns:
dict: 调用 API 返回的结果。
"""
info = await self.__get_info_cached()
mid = info["owner"]["mid"]
api = API["info"]["chargers"]
params = {"aid": self.get_aid(), "bvid": self.get_bvid(), "mid": mid}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def get_pages(self) -> List[dict]:
"""
获取分 P 信息。
Returns:
dict: 调用 API 返回的结果。
"""
api = API["info"]["pages"]
params = {"aid": self.get_aid(), "bvid": self.get_bvid()}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def __get_cid_by_index(self, page_index: int) -> int:
"""
根据分 p 号获取 cid。
Args:
page_index (int): 分 P 号,从 0 开始。
Returns:
int: cid 分 P 的唯一 ID。
"""
if page_index < 0:
raise ArgsException("分 p 号必须大于或等于 0。")
info = await self.__get_info_cached()
pages = info["pages"]
if len(pages) <= page_index:
raise ArgsException("不存在该分 p。")
page = pages[page_index]
cid = page["cid"]
return cid
async def get_video_snapshot(
self,
cid: Union[int, None] = None,
json_index: bool = False,
pvideo: bool = True,
) -> dict:
"""
获取视频快照(视频各个时间段的截图拼图)
Args:
cid(int): 分 P CID(可选)
json_index(bool): json 数组截取时间表 True 为需要,False 不需要
pvideo(bool): 是否只获取预览
Returns:
dict: 调用 API 返回的结果,数据中 Url 没有 http 头
"""
params: dict[str, Any] = {"aid": self.get_aid()}
if pvideo:
api = API["info"]["video_snapshot_pvideo"]
else:
params["bvid"] = self.get_bvid()
if json_index:
params["index"] = 1
if cid:
params["cid"] = cid
api = API["info"]["video_snapshot"]
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def get_cid(self, page_index: int) -> int:
"""
获取稿件 cid
Args:
page_index(int): 分 P
Returns:
int: cid
"""
return await self.__get_cid_by_index(page_index)
async def get_download_url(
self,
page_index: Union[int, None] = None,
cid: Union[int, None] = None,
html5: bool = False,
) -> dict:
"""
获取视频下载信息。
返回结果可以传入 `VideoDownloadURLDataDetecter` 进行解析。
page_index 和 cid 至少提供其中一个,其中 cid 优先级最高
Args:
page_index (int | None, optional) : 分 P 号,从 0 开始。Defaults to None
cid (int | None, optional) : 分 P 的 ID。Defaults to None
html5 (bool, optional): 是否以 html5 平台访问,这样子能直接在网页中播放,但是链接少。
Returns:
dict: 调用 API 返回的结果。
"""
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
api = API["info"]["playurl"]
if html5:
params = {
"avid": self.get_aid(),
"cid": cid,
"qn": "127",
"otype": "json",
"fnval": 4048,
"fourk": 1,
"platform": "html5",
"high_quality": "1",
}
else:
params = {
"avid": self.get_aid(),
"cid": cid,
"qn": "127",
"otype": "json",
"fnval": 4048,
"fourk": 1,
}
result = (
await Api(**api, credential=self.credential).update_params(**params).result
)
result.update({"is_html5": True} if html5 else {})
return result
async def get_related(self) -> dict:
"""
获取相关视频信息。
Returns:
dict: 调用 API 返回的结果。
"""
api = API["info"]["related"]
params = {"aid": self.get_aid(), "bvid": self.get_bvid()}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def get_relation(self) -> dict:
"""
获取用户与视频的关系
Returns:
dict: 调用 API 返回的结果。
"""
self.credential.raise_for_no_sessdata()
api = API["info"]["relation"]
params = {"aid": self.get_aid(), "bvid": self.get_bvid()}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def has_liked(self) -> bool:
"""
视频是否点赞过。
Returns:
bool: 视频是否点赞过。
"""
self.credential.raise_for_no_sessdata()
api = API["info"]["has_liked"]
params = {"bvid": self.get_bvid(), "aid": self.get_aid()}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def get_pay_coins(self) -> int:
"""
获取视频已投币数量。
Returns:
int: 视频已投币数量。
"""
self.credential.raise_for_no_sessdata()
api = API["info"]["get_pay_coins"]
params = {"bvid": self.get_bvid(), "aid": self.get_aid()}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)["multiply"]
async def has_favoured(self) -> bool:
"""
是否已收藏。
Returns:
bool: 视频是否已收藏。
"""
self.credential.raise_for_no_sessdata()
api = API["info"]["has_favoured"]
params = {"bvid": self.get_bvid(), "aid": self.get_aid()}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)["favoured"]
async def is_forbid_note(self) -> bool:
"""
是否禁止笔记。
Returns:
bool: 是否禁止笔记。
"""
api = API["info"]["is_forbid"]
params = {"aid": self.get_aid()}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)["forbid_note_entrance"]
async def get_private_notes_list(self) -> list:
"""
获取稿件私有笔记列表。
Returns:
list: note_Ids。
"""
self.credential.raise_for_no_sessdata()
api = API["info"]["private_notes"]
params = {"oid": self.get_aid(), "oid_type": 0}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)["noteIds"]
async def get_public_notes_list(self, pn: int, ps: int) -> dict:
"""
获取稿件公开笔记列表。
Args:
pn (int): 页码
ps (int): 每页项数
Returns:
dict: 调用 API 返回的结果。
"""
api = API["info"]["public_notes"]
params = {"oid": self.get_aid(), "oid_type": 0, "pn": pn, "ps": ps}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def get_ai_conclusion(self, cid: Optional[int] = None, page_index: Optional[int] = None,
up_mid: Optional[int] = None) -> dict:
"""
获取稿件 AI 总结结果。
cid 和 page_index 至少提供其中一个,其中 cid 优先级最高
Args:
cid (Optional, int): 分 P 的 cid。
page_index (Optional, int): 分 P 号,从 0 开始。
up_mid (Optional, int): up 主的 mid。
Returns:
dict: 调用 API 返回的结果。
"""
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
api = API["info"]["ai_conclusion"]
params = {"aid": self.get_aid(), "bvid": self.get_bvid(), "cid": cid,
"up_mid": await self.get_up_mid() if up_mid is None else up_mid}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def get_danmaku_view(
self, page_index: Union[int, None] = None, cid: Union[int, None] = None
) -> dict:
"""
获取弹幕设置、特殊弹幕、弹幕数量、弹幕分段等信息。
Args:
page_index (int, optional): 分 P 号,从 0 开始。Defaults to None
cid (int, optional): 分 P 的 ID。Defaults to None
Returns:
dict: 调用 API 返回的结果。
"""
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
session = get_session()
api = API["danmaku"]["view"]
config = {}
config["url"] = api["url"]
config["params"] = {"type": 1, "oid": cid, "pid": self.get_aid()}
config["cookies"] = self.credential.get_cookies()
config["headers"] = {
"Referer": "https://www.bilibili.com",
"User-Agent": "Mozilla/5.0",
}
try:
resp = await session.get(**config)
except Exception as e:
raise NetworkException(-1, str(e))
resp_data = resp.read()
json_data = {}
reader = BytesReader(resp_data)
# 解析二进制数据流
def read_dm_seg(stream: bytes):
reader_ = BytesReader(stream)
data = {}
while not reader_.has_end():
t = reader_.varint() >> 3
if t == 1:
data["page_size"] = reader_.varint()
elif t == 2:
data["total"] = reader_.varint()
else:
continue
return data
def read_flag(stream: bytes):
reader_ = BytesReader(stream)
data = {}
while not reader_.has_end():
t = reader_.varint() >> 3
if t == 1:
data["rec_flag"] = reader_.varint()
elif t == 2:
data["rec_text"] = reader_.string()
elif t == 3:
data["rec_switch"] = reader_.varint()
else:
continue
return data
def read_command_danmakus(stream: bytes):
reader_ = BytesReader(stream)
data = {}
while not reader_.has_end():
t = reader_.varint() >> 3
if t == 1:
data["id"] = reader_.varint()
elif t == 2:
data["oid"] = reader_.varint()
elif t == 3:
data["mid"] = reader_.varint()
elif t == 4:
data["commend"] = reader_.string()
elif t == 5:
data["content"] = reader_.string()
elif t == 6:
data["progress"] = reader_.varint()
elif t == 7:
data["ctime"] = reader_.string()
elif t == 8:
data["mtime"] = reader_.string()
elif t == 9:
data["extra"] = json.loads(reader_.string())
elif t == 10:
data["id_str"] = reader_.string()
else:
continue
return data
def read_settings(stream: bytes):
reader_ = BytesReader(stream)
data = {}
while not reader_.has_end():
t = reader_.varint() >> 3
if t == 1:
data["dm_switch"] = reader_.bool()
elif t == 2:
data["ai_switch"] = reader_.bool()
elif t == 3:
data["ai_level"] = reader_.varint()
elif t == 4:
data["enable_top"] = reader_.bool()
elif t == 5:
data["enable_scroll"] = reader_.bool()
elif t == 6:
data["enable_bottom"] = reader_.bool()
elif t == 7:
data["enable_color"] = reader_.bool()
elif t == 8:
data["enable_special"] = reader_.bool()
elif t == 9:
data["prevent_shade"] = reader_.bool()
elif t == 10:
data["dmask"] = reader_.bool()
elif t == 11:
data["opacity"] = reader_.float(True)
elif t == 12:
data["dm_area"] = reader_.varint()
elif t == 13:
data["speed_plus"] = reader_.float(True)
elif t == 14:
data["font_size"] = reader_.float(True)
elif t == 15:
data["screen_sync"] = reader_.bool()
elif t == 16:
data["speed_sync"] = reader_.bool()
elif t == 17:
data["font_family"] = reader_.string()
elif t == 18:
data["bold"] = reader_.bool()
elif t == 19:
data["font_border"] = reader_.varint()
elif t == 20:
data["draw_type"] = reader_.string()
else:
continue
return data
def read_image_danmakus(string: bytes):
image_list = []
reader_ = BytesReader(string)
while not reader_.has_end():
type_ = reader_.varint() >> 3
if type_ == 1:
details_dict: dict[Any, Any] = {"texts": []}
img_details = reader_.bytes_string()
reader_details = BytesReader(img_details)
while not reader_details.has_end():
type_details = reader_details.varint() >> 3
if type_details == 1:
details_dict["texts"].append(reader_details.string())
elif type_details == 2:
details_dict["image"] = reader_details.string()
elif type_details == 3:
id_string = reader_details.bytes_string()
id_reader = BytesReader(id_string)
while not id_reader.has_end():
type_id = id_reader.varint() >> 3
if type_id == 2:
details_dict["id"] = id_reader.varint()
else:
raise ResponseException("解析响应数据错误")
image_list.append(details_dict)
else:
raise ResponseException("解析响应数据错误")
return image_list
while not reader.has_end():
type_ = reader.varint() >> 3
if type_ == 1:
json_data["state"] = reader.varint()
elif type_ == 2:
json_data["text"] = reader.string()
elif type_ == 3:
json_data["text_side"] = reader.string()
elif type_ == 4:
json_data["dm_seg"] = read_dm_seg(reader.bytes_string())
elif type_ == 5:
json_data["flag"] = read_flag(reader.bytes_string())
elif type_ == 6:
if "special_dms" not in json_data:
json_data["special_dms"] = []
json_data["special_dms"].append(reader.string())
elif type_ == 7:
json_data["check_box"] = reader.bool()
elif type_ == 8:
json_data["count"] = reader.varint()
elif type_ == 9:
if "command_dms" not in json_data:
json_data["command_dms"] = []
json_data["command_dms"].append(
read_command_danmakus(reader.bytes_string())
)
elif type_ == 10:
json_data["dm_setting"] = read_settings(reader.bytes_string())
elif type_ == 12:
json_data["image_dms"] = read_image_danmakus(reader.bytes_string())
else:
continue
return json_data
async def get_danmakus(
self,
page_index: int = 0,
date: Union[datetime.date, None] = None,
cid: Union[int, None] = None,
from_seg: Union[int, None] = None,
to_seg: Union[int, None] = None,
) -> List[Danmaku]:
"""
获取弹幕。
Args:
page_index (int, optional): 分 P 号,从 0 开始。Defaults to None
date (datetime.Date | None, optional): 指定日期后为获取历史弹幕,精确到年月日。Defaults to None.
cid (int | None, optional): 分 P 的 ID。Defaults to None
from_seg (int, optional): 从第几段开始(0 开始编号,None 为从第一段开始,一段 6 分钟). Defaults to None.
to_seg (int, optional): 到第几段结束(0 开始编号,None 为到最后一段,包含编号的段,一段 6 分钟). Defaults to None.
注意:
- 1. 段数可以使用 `get_danmaku_view()["dm_seg"]["total"]` 查询。
- 2. `from_seg` 和 `to_seg` 仅对 `date == None` 的时候有效果。
- 3. 例:取前 `12` 分钟的弹幕:`from_seg=0, to_seg=1`
Returns:
List[Danmaku]: Danmaku 类的列表。
"""
if date is not None:
self.credential.raise_for_no_sessdata()
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
session = get_session()
aid = self.get_aid()
params: dict[str, Any] = {"oid": cid, "type": 1, "pid": aid}
if date is not None:
# 获取历史弹幕
api = API["danmaku"]["get_history_danmaku"]
params["date"] = date.strftime("%Y-%m-%d")
params["type"] = 1
from_seg = to_seg = 0
else:
api = API["danmaku"]["get_danmaku"]
if from_seg == None:
from_seg = 0
if to_seg == None:
view = await self.get_danmaku_view(cid=cid)
to_seg = view["dm_seg"]["total"] - 1
danmakus = []
for seg in range(from_seg, to_seg + 1):
if date is None:
# 仅当获取当前弹幕时需要该参数
params["segment_index"] = seg + 1
config = {}
config["url"] = api["url"]
config["params"] = params
config["headers"] = {
"Referer": "https://www.bilibili.com",
"User-Agent": "Mozilla/5.0",
}
config["cookies"] = self.credential.get_cookies()
try:
req = await session.get(**config)
except Exception as e:
raise NetworkException(-1, str(e))
if "content-type" not in req.headers.keys():
break
else:
content_type = req.headers["content-type"]
if content_type != "application/octet-stream":
raise ResponseException("返回数据类型错误:")
# 解析二进制流数据
data = req.read()
if data == b"\x10\x01":
# 视频弹幕被关闭
raise DanmakuClosedException()
reader = BytesReader(data)
while not reader.has_end():
type_ = reader.varint() >> 3
if type_ != 1:
if type_ == 4:
reader.bytes_string()
# 什么鬼?我用 protoc 解析出乱码!
elif type_ == 5:
# 大会员专属颜色
reader.varint()
reader.varint()
reader.varint()
reader.bytes_string()
elif type_ == 13:
# ???
continue
else:
raise ResponseException("解析响应数据错误")
dm = Danmaku("")
dm_pack_data = reader.bytes_string()
dm_reader = BytesReader(dm_pack_data)
while not dm_reader.has_end():
data_type = dm_reader.varint() >> 3
if data_type == 1:
dm.id_ = dm_reader.varint()
elif data_type == 2:
dm.dm_time = dm_reader.varint() / 1000
elif data_type == 3:
dm.mode = dm_reader.varint()
elif data_type == 4:
dm.font_size = dm_reader.varint()
elif data_type == 5:
color = dm_reader.varint()
if color != 60001:
color = hex(color)[2:]
else:
color = "special"
dm.color = color
elif data_type == 6:
dm.crc32_id = dm_reader.string()
elif data_type == 7:
dm.text = dm_reader.string()
elif data_type == 8:
dm.send_time = dm_reader.varint()
elif data_type == 9:
dm.weight = dm_reader.varint()
elif data_type == 10:
dm.action = str(dm_reader.string())
elif data_type == 11:
dm.pool = dm_reader.varint()
elif data_type == 12:
dm.id_str = dm_reader.string()
elif data_type == 13:
dm.attr = dm_reader.varint()
elif data_type == 14:
dm.uid = dm_reader.varint()
elif data_type == 15:
dm_reader.varint()
elif data_type == 20:
dm_reader.bytes_string()
elif data_type == 21:
dm_reader.bytes_string()
elif data_type == 22:
dm_reader.bytes_string()
elif data_type == 25:
dm_reader.varint()
elif data_type == 26:
dm_reader.varint()
else:
break
danmakus.append(dm)
return danmakus
async def get_special_dms(
self, page_index: int = 0, cid: Union[int, None] = None
) -> List[SpecialDanmaku]:
"""
获取特殊弹幕
Args:
page_index (int, optional) : 分 P 号. Defaults to 0.
cid (int | None, optional): 分 P id. Defaults to None.
Returns:
List[SpecialDanmaku]: 调用接口解析后的结果
"""
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
view = await self.get_danmaku_view(cid=cid)
special_dms = view["special_dms"][0]
if settings.proxy != "":
sess = httpx.AsyncClient(proxies={"all://": settings.proxy})
else:
sess = httpx.AsyncClient()
dm_content = await sess.get(special_dms, cookies=self.credential.get_cookies())
dm_content.raise_for_status()
reader = BytesReader(dm_content.content)
dms: List[SpecialDanmaku] = []
while not reader.has_end():
spec_dm = SpecialDanmaku("")
type_ = reader.varint() >> 3
if type_ == 1:
reader_ = BytesReader(reader.bytes_string())
while not reader_.has_end():
type__ = reader_.varint() >> 3
if type__ == 1:
spec_dm.id_ = reader_.varint()
elif type__ == 3:
spec_dm.mode = reader_.varint()
elif type__ == 4:
reader_.varint()
elif type__ == 5:
reader_.varint()
elif type__ == 6:
reader_.string()
elif type__ == 7:
spec_dm.content = reader_.string()
elif type__ == 8:
reader_.varint()
elif type__ == 11:
spec_dm.pool = reader_.varint()
elif type__ == 12:
spec_dm.id_str = reader_.string()
else:
continue
else:
continue
dms.append(spec_dm)
return dms
async def get_history_danmaku_index(
self,
page_index: Union[int, None] = None,
date: Union[datetime.date, None] = None,
cid: Union[int, None] = None,
) -> Union[None, List[str]]:
"""
获取特定月份存在历史弹幕的日期。
Args:
page_index (int | None, optional): 分 P 号,从 0 开始。Defaults to None
date (datetime.date | None): 精确到年月. Defaults to None。
cid (int | None, optional): 分 P 的 ID。Defaults to None
Returns:
None | List[str]: 调用 API 返回的结果。不存在时为 None。
"""
if date is None:
raise ArgsException("请提供 date 参数")
self.credential.raise_for_no_sessdata()
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
api = API["danmaku"]["get_history_danmaku_index"]
params = {"oid": cid, "month": date.strftime("%Y-%m"), "type": 1}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def has_liked_danmakus(
self,
page_index: Union[int, None] = None,
ids: Union[List[int], None] = None,
cid: Union[int, None] = None,
) -> dict:
"""
是否已点赞弹幕。
Args:
page_index (int | None, optional): 分 P 号,从 0 开始。Defaults to None
ids (List[int] | None): 要查询的弹幕 ID 列表。
cid (int | None, optional): 分 P 的 ID。Defaults to None
Returns:
dict: 调用 API 返回的结果。
"""
if ids is None or len(ids) == 0:
raise ArgsException("请提供 ids 参数并至少有一个元素")
self.credential.raise_for_no_sessdata()
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
api = API["danmaku"]["has_liked_danmaku"]
params = {"oid": cid, "ids": ",".join(ids)} # type: ignore
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def send_danmaku(
self,
page_index: Union[int, None] = None,
danmaku: Union[Danmaku, None] = None,
cid: Union[int, None] = None,
) -> dict:
"""
发送弹幕。
Args:
page_index (int | None, optional): 分 P 号,从 0 开始。Defaults to None
danmaku (Danmaku | None) : Danmaku 类。
cid (int | None, optional): 分 P 的 ID。Defaults to None
Returns:
dict: 调用 API 返回的结果。
"""
if danmaku is None:
raise ArgsException("请提供 danmaku 参数")
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
api = API["danmaku"]["send_danmaku"]
if danmaku.is_sub:
pool = 1
else:
pool = 0
data = {
"type": 1,
"oid": cid,
"msg": danmaku.text,
"aid": self.get_aid(),
"bvid": self.get_bvid(),
"progress": int(danmaku.dm_time * 1000),
"color": int(danmaku.color, 16),
"fontsize": danmaku.font_size,
"pool": pool,
"mode": danmaku.mode,
"plat": 1,
}
return await Api(**api, credential=self.credential).update_data(**data).result
async def get_danmaku_xml(
self, page_index: Union[int, None] = None, cid: Union[int, None] = None
) -> str:
"""
获取所有弹幕的 xml 源文件(非装填)
Args:
page_index (int, optional) : 分 P 序号. Defaults to 0.
cid (int | None, optional): cid. Defaults to None.
Return:
xml 文件源
"""
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
url = f"https://comment.bilibili.com/{cid}.xml"
sess = get_session()
config: dict[Any, Any] = {"url": url}
# 代理
if settings.proxy:
config["proxies"] = {"all://", settings.proxy}
resp = await sess.get(**config)
return resp.content.decode("utf-8")
async def like_danmaku(
self,
page_index: Union[int, None] = None,
dmid: Union[int, None] = None,
status: Union[bool, None] = True,
cid: Union[int, None] = None,
) -> dict:
"""
点赞弹幕。
Args:
page_index (int | None, optional) : 分 P 号,从 0 开始。Defaults to None
dmid (int | None) : 弹幕 ID。
status (bool | None, optional): 点赞状态。Defaults to True
cid (int | None, optional) : 分 P 的 ID。Defaults to None
Returns:
dict: 调用 API 返回的结果。
"""
if dmid is None:
raise ArgsException("请提供 dmid 参数")
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
api = API["danmaku"]["like_danmaku"]
data = {
"dmid": dmid,
"oid": cid,
"op": 1 if status else 2,
"platform": "web_player",
}
return await Api(**api, credential=self.credential).update_data(**data).result
async def get_online(self, cid: Optional[int] = None, page_index: Optional[int] = 0) -> dict:
"""
获取实时在线人数
Returns:
dict: 调用 API 返回的结果。
"""
api = API["info"]["online"]
params = {"aid": self.get_aid(), "bvid": self.get_bvid(),
"cid": cid if cid is not None else await self.get_cid(page_index=page_index)}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def operate_danmaku(
self,
page_index: Union[int, None] = None,
dmids: Union[List[int], None] = None,
cid: Union[int, None] = None,
type_: Union[DanmakuOperatorType, None] = None,
) -> dict:
"""
操作弹幕
Args:
page_index (int | None, optional) : 分 P 号,从 0 开始。Defaults to None
dmids (List[int] | None) : 弹幕 ID 列表。
cid (int | None, optional) : 分 P 的 ID。Defaults to None
type_ (DanmakuOperatorType | None): 操作类型
Returns:
dict: 调用 API 返回的结果。
"""
if dmids is None or len(dmids) == 0:
raise ArgsException("请提供 dmid 参数")
if type_ is None:
raise ArgsException("请提供 type_ 参数")
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
api = API["danmaku"]["edit_danmaku"]
data = {
"type": 1,
"dmids": ",".join(map(lambda x: str(x), dmids)),
"oid": cid,
"state": type_.value,
}
return await Api(**api, credential=self.credential).update_data(**data).result
async def like(self, status: bool = True) -> dict:
"""
点赞视频。
Args:
status (bool, optional): 点赞状态。Defaults to True.
Returns:
dict: 调用 API 返回的结果。
"""
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
api = API["operate"]["like"]
data = {"aid": self.get_aid(), "like": 1 if status else 2}
return await Api(**api, credential=self.credential).update_data(**data).result
async def pay_coin(self, num: int = 1, like: bool = False) -> dict:
"""
投币。
Args:
num (int, optional) : 硬币数量,为 1 ~ 2 个。Defaults to 1.
like (bool, optional): 是否同时点赞。Defaults to False.
Returns:
dict: 调用 API 返回的结果。
"""
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
if num not in (1, 2):
raise ArgsException("投币数量只能是 1 ~ 2 个。")
api = API["operate"]["coin"]
data = {
"aid": self.get_aid(),
"bvid": self.get_bvid(),
"multiply": num,
"like": 1 if like else 0,
}
return await Api(**api, credential=self.credential).update_data(**data).result
async def share(self) -> int:
"""
分享视频
Returns:
int: 当前分享数
"""
api = API["operate"]["share"]
data = {
"bvid": self.get_bvid(),
"aid": self.get_aid(),
"csrf": self.credential.bili_jct,
}
return await Api(**api, credential=self.credential).update_data(**data).result
async def triple(self) -> dict:
"""
给阿婆主送上一键三连
Returns:
dict: 调用 API 返回的结果
"""
api = API["operate"]["yjsl"]
data = {"bvid": self.get_bvid(), "aid": self.get_aid()}
return await Api(**api, credential=self.credential).update_data(**data).result
async def add_tag(self, name: str) -> dict:
"""
添加标签。
Args:
name (str): 标签名字。
Returns:
dict: 调用 API 返回的结果。会返回标签 ID。
"""
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
api = API["operate"]["add_tag"]
data = {"aid": self.get_aid(), "bvid": self.get_bvid(), "tag_name": name}
return await Api(**api, credential=self.credential).update_data(**data).result
async def delete_tag(self, tag_id: int) -> dict:
"""
删除标签。
Args:
tag_id (int): 标签 ID。
Returns:
dict: 调用 API 返回的结果。
"""
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
api = API["operate"]["del_tag"]
data = {"tag_id": tag_id, "aid": self.get_aid(), "bvid": self.get_bvid()}
return await Api(**api, credential=self.credential).update_data(**data).result
async def appeal(self, reason: Any, detail: str):
"""
投诉稿件
Args:
reason (Any): 投诉类型。传入 VideoAppealReasonType 中的项目即可。
detail (str): 详情信息。
Returns:
dict: 调用 API 返回的结果
"""
api = API["operate"]["appeal"]
data = {"aid": self.get_aid(), "desc": detail}
if isfunction(reason):
reason = reason()
if isinstance(reason, int):
reason = {"tid": reason}
data.update(reason)
# XXX: 暂不支持上传附件
return await Api(**api, credential=self.credential).update_data(**data).result
async def set_favorite(
self, add_media_ids: List[int] = [], del_media_ids: List[int] = []
) -> dict:
"""
设置视频收藏状况。
Args:
add_media_ids (List[int], optional): 要添加到的收藏夹 ID. Defaults to [].
del_media_ids (List[int], optional): 要移出的收藏夹 ID. Defaults to [].
Returns:
dict: 调用 API 返回结果。
"""
if len(add_media_ids) + len(del_media_ids) == 0:
raise ArgsException("对收藏夹无修改。请至少提供 add_media_ids 和 del_media_ids 中的其中一个。")
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
api = API["operate"]["favorite"]
data = {
"rid": self.get_aid(),
"type": 2,
"add_media_ids": ",".join(map(lambda x: str(x), add_media_ids)),
"del_media_ids": ",".join(map(lambda x: str(x), del_media_ids)),
}
return await Api(**api, credential=self.credential).update_data(**data).result
async def get_subtitle(
self,
cid: Union[int, None] = None,
) -> dict:
"""
获取视频上一次播放的记录,字幕和地区信息。需要分集的 cid, 返回数据中含有json字幕的链接
Args:
cid (int | None): 分 P ID,从视频信息中获取
Returns:
调用 API 返回的结果
"""
if cid is None:
raise ArgsException("需要 cid")
return (await self.get_player_info(cid=cid)).get("subtitle")
async def get_player_info(
self,
cid: Union[int, None] = None,
epid: Union[int, None] = None,
) -> dict:
"""
获取字幕信息
Args:
cid (int | None): 分 P ID,从视频信息中获取
epid (int | None): 番剧分集 ID,从番剧信息中获取
Returns:
调用 API 返回的结果
"""
if cid is None:
raise ArgsException("需要 cid")
api = API["info"]["get_player_info"]
params = {
"aid": self.get_aid(),
"cid": cid,
}
if epid:
params["epid"] = epid
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def submit_subtitle(
self,
lan: str,
data: dict,
submit: bool,
sign: bool,
page_index: Union[int, None] = None,
cid: Union[int, None] = None,
) -> dict:
"""
上传字幕
字幕数据 data 参考:
```json
{
"font_size": "float: 字体大小,默认 0.4",
"font_color": "str: 字体颜色,默认 \"#FFFFFF\"",
"background_alpha": "float: 背景不透明度,默认 0.5",
"background_color": "str: 背景颜色,默认 \"#9C27B0\"",
"Stroke": "str: 描边,目前作用未知,默认为 \"none\"",
"body": [
{
"from": "int: 字幕开始时间(秒)",
"to": "int: 字幕结束时间(秒)",
"location": "int: 字幕位置,默认为 2",
"content": "str: 字幕内容"
}
]
}
```
Args:
lan (str) : 字幕语言代码,参考 https://s1.hdslb.com/bfs/subtitle/subtitle_lan.json
data (dict) : 字幕数据
submit (bool) : 是否提交,不提交为草稿
sign (bool) : 是否署名
page_index (int | None, optional): 分 P 索引. Defaults to None.
cid (int | None, optional): 分 P id. Defaults to None.
Returns:
dict: API 调用返回结果
"""
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
api = API["operate"]["submit_subtitle"]
# lan check,应该是这里面的语言代码
with open(
os.path.join(os.path.dirname(__file__), "data/subtitle_lan.json"),
encoding="utf-8",
) as f:
subtitle_lans = json.load(f)
for lan in subtitle_lans:
if lan["lan"] == lan:
break
else:
raise ArgsException("lan 参数错误,请参见 https://s1.hdslb.com/bfs/subtitle/subtitle_lan.json")
payload = {
"type": 1,
"oid": cid,
"lan": lan,
"data": json.dumps(data),
"submit": submit,
"sign": sign,
"bvid": self.get_bvid(),
}
return await Api(**api, credential=self.credential).update_data(**payload).result
async def get_danmaku_snapshot(self) -> dict:
"""
获取弹幕快照
Returns:
调用 API 返回的结果
"""
api = API["danmaku"]["snapshot"]
params = {"aid": self.get_aid()}
return (
await Api(**api, credential=self.credential).update_params(**params).result
)
async def recall_danmaku(
self,
page_index: Union[int, None] = None,
dmid: int = 0,
cid: Union[int, None] = None,
) -> dict:
"""
撤回弹幕
Args:
page_index(int | None, optional): 分 P 号
dmid(int) : 弹幕 id
cid(int | None, optional) : 分 P 编码
Returns:
调用 API 返回的结果
"""
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
api = API["danmaku"]["recall"]
data = {"dmid": dmid, "cid": cid}
return await Api(**api, credential=self.credential).update_data(**data).result
async def get_pbp(
self, page_index: Union[int, None] = None, cid: Union[int, None] = None
) -> dict:
"""
获取高能进度条
Args:
page_index(int | None): 分 P 号
cid(int | None) : 分 P 编码
Returns:
调用 API 返回的结果
"""
if cid is None:
if page_index is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.__get_cid_by_index(page_index)
api = API["info"]["pbp"]
params = {"cid": cid}
session = get_session()
return json.loads(
(
await session.get(
api["url"], params=params, cookies=self.credential.get_cookies()
)
).text
)
async def add_to_toview(self) -> dict:
"""
添加视频至稍后再看列表
Returns:
调用 API 返回的结果
"""
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
api = get_api("toview")["operate"]["add"]
datas = {
"aid": self.get_aid(),
}
return await Api(**api, credential=self.credential).update_data(**datas).result
async def delete_from_toview(self) -> dict:
"""
从稍后再看列表删除视频
Returns:
调用 API 返回的结果
"""
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
api = get_api("toview")["operate"]["del"]
datas = {"viewed": "false", "aid": self.get_aid()}
return await Api(**api, credential=self.credential).update_data(**datas).result
class VideoOnlineMonitor(AsyncEvent):
"""
视频在线人数实时监测。
示例代码:
```python
import asyncio
from bilibili_api import video
# 实例化
r = video.VideoOnlineMonitor("BV1Bf4y1Q7QP")
# 装饰器方法注册事件监听器
@r.on("ONLINE")
async def handler(data):
print(data)
# 函数方法注册事件监听器
async def handler2(data):
print(data)
r.add_event_listener("ONLINE", handler2)
asyncio.get_event_loop().run_until_complete(r.connect())
```
Extends: AsyncEvent
Events:
ONLINE: 在线人数更新。 CallbackData: dict。
DANMAKU: 收到实时弹幕。 CallbackData: Danmaku。
DISCONNECTED: 正常断开连接。 CallbackData: None。
ERROR: 发生错误。 CallbackData: aiohttp.ClientWebSocketResponse。
CONNECTED: 成功连接。 CallbackData: None。
"""
class Datapack(Enum):
"""
数据包类型枚举。
+ CLIENT_VERIFY : 客户端发送验证信息。
+ SERVER_VERIFY : 服务端响应验证信息。
+ CLIENT_HEARTBEAT: 客户端发送心跳包。
+ SERVER_HEARTBEAT: 服务端响应心跳包。
+ DANMAKU : 实时弹幕更新。
"""
CLIENT_VERIFY = 0x7
SERVER_VERIFY = 0x8
CLIENT_HEARTBEAT = 0x2
SERVER_HEARTBEAT = 0x3
DANMAKU = 0x3E8
def __init__(
self,
bvid: Union[str, None] = None,
aid: Union[int, None] = None,
page_index: int = 0,
credential: Union[Credential, None] = None,
debug: bool = False,
):
"""
Args:
bvid (str | None, optional) : BVID. Defaults to None.
aid (int | None, optional) : AID. Defaults to None.
page_index (int, optional) : 分 P 序号. Defaults to 0.
credential (Credential | None, optional): Credential 类. Defaults to None.
debug (bool, optional) : 调试模式,将输出更详细信息. Defaults to False.
"""
super().__init__()
self.credential = credential
self.__video = Video(bvid, aid, credential=credential)
# 智能选择在 log 中展示的 ID。
id_showed = None
if bvid is not None:
id_showed = bvid
else:
id_showed = aid
# logger 初始化
self.logger = logging.getLogger(f"VideoOnlineMonitor-{id_showed}")
if not self.logger.handlers:
handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter(
"[" + str(id_showed) + "][%(asctime)s][%(levelname)s] %(message)s"
)
)
self.logger.addHandler(handler)
self.logger.setLevel(logging.INFO if not debug else logging.DEBUG)
self.__page_index = page_index
self.__tasks = []
async def connect(self):
"""
连接服务器
"""
await self.__main()
async def disconnect(self):
"""
断开服务器
"""
self.logger.info("主动断开连接。")
self.dispatch("DISCONNECTED")
await self.__cancel_all_tasks()
await self.__ws.close()
async def __main(self):
"""
入口。
"""
# 获取分 P id
pages = await self.__video.get_pages()
if self.__page_index >= len(pages):
raise ArgsException("不存在该分 P。")
cid = pages[self.__page_index]["cid"]
# 获取服务器信息
self.logger.debug(f"准备连接:{self.__video.get_bvid()}")
self.logger.debug(f"获取服务器信息中...")
api = API["info"]["video_online_broadcast_servers"]
resp = await Api(**api, credential=self.credential).result
uri = f"wss://{resp['domain']}:{resp['wss_port']}/sub"
self.__heartbeat_interval = resp["heartbeat"]
self.logger.debug(f"服务器信息获取成功,URI:{uri}")
# 连接服务器
self.logger.debug("准备连接服务器...")
session = get_aiohttp_session()
async with session.ws_connect(uri) as ws:
self.__ws = ws
# 发送认证信息
self.logger.debug("服务器连接成功,准备发送认证信息...")
verify_info = {
"room_id": f"video://{self.__video.get_aid()}/{cid}",
"platform": "web",
"accepts": [1000, 1015],
}
verify_info = json.dumps(verify_info, separators=(",", ":"))
await ws.send_bytes(
self.__pack(
VideoOnlineMonitor.Datapack.CLIENT_VERIFY, 1, verify_info.encode()
)
)
# 循环接收消息
async for msg in ws:
if msg.type == aiohttp.WSMsgType.BINARY:
data = self.__unpack(msg.data)
self.logger.debug(f"收到消息:{data}")
await self.__handle_data(data) # type: ignore
elif msg.type == aiohttp.WSMsgType.ERROR:
self.logger.warning("连接被异常断开")
await self.__cancel_all_tasks()
self.dispatch("ERROR", msg)
break
async def __handle_data(self, data: List[dict]):
"""
处理数据。
Args:
data (List[dict]): 收到的数据(已解析好)。
"""
for d in data:
if d["type"] == VideoOnlineMonitor.Datapack.SERVER_VERIFY.value:
# 服务器认证反馈。
if d["data"]["code"] == 0:
# 创建心跳 Task
heartbeat = asyncio.create_task(self.__heartbeat_task())
self.__tasks.append(heartbeat)
self.logger.info("连接服务器并验证成功")
elif d["type"] == VideoOnlineMonitor.Datapack.SERVER_HEARTBEAT.value:
# 心跳包反馈,同时包含在线人数。
self.logger.debug(f'收到服务器心跳包反馈,编号:{d["number"]}')
self.logger.info(f'实时观看人数:{d["data"]["data"]["room"]["online"]}')
self.dispatch("ONLINE", d["data"])
elif d["type"] == VideoOnlineMonitor.Datapack.DANMAKU.value:
# 实时弹幕。
info = d["data"][0].split(",")
text = d["data"][1]
if info[5] == "0":
is_sub = False
else:
is_sub = True
dm = Danmaku(
dm_time=float(info[0]),
send_time=int(info[4]),
crc32_id=info[6],
color=info[3],
mode=info[1],
font_size=info[2],
is_sub=is_sub,
text=text,
)
self.logger.info(f"收到实时弹幕:{dm.text}")
self.dispatch("DANMAKU", dm)
else:
# 未知类型数据包
self.logger.warning("收到未知的数据包类型,无法解析:" + json.dumps(d))
async def __heartbeat_task(self):
"""
心跳 Task。
"""
index = 2
while True:
self.logger.debug(f"发送心跳包,编号:{index}")
await self.__ws.send_bytes(
self.__pack(
VideoOnlineMonitor.Datapack.CLIENT_HEARTBEAT,
index,
b"[object Object]",
)
)
index += 1
await asyncio.sleep(self.__heartbeat_interval)
async def __cancel_all_tasks(self):
"""
取消所有 Task。
"""
for task in self.__tasks:
task.cancel()
@staticmethod
def __pack(data_type: Datapack, number: int, data: bytes):
"""
打包数据。
# 数据包格式:
16B 头部:
| offset(bytes) | length(bytes) | type | description |
| ------------- | ------------- | ---- | ------------------- |
| 0 | 4 | I | 数据包长度 |
| 4 | 4 | I | 固定 0x00120001 |
| 8 | 4 | I | 数据包类型 |
| 12 | 4 | I | 递增数据包编号 |
| 16 | 2 | H | 固定 0x0000 |
之后是有效载荷。
# 数据包类型表:
+ 0x7 客户端发送认证信息
+ 0x8 服务端回应认证结果
+ 0x2 客户端发送心跳包,有效载荷:'[object Object]'
+ 0x3 服务端回应心跳包,会带上在线人数等信息,返回 JSON
+ 0x3e8 实时弹幕更新,返回列表,[0]弹幕信息,[1]弹幕文本
Args:
data_type (VideoOnlineMonitor.DataType): 数据包类型枚举。
Returns:
bytes: 打包好的数据。
"""
packed_data = bytearray()
packed_data += struct.pack(">I", 0x00120001)
packed_data += struct.pack(">I", data_type.value)
packed_data += struct.pack(">I", number)
packed_data += struct.pack(">H", 0)
packed_data += data
packed_data = struct.pack(">I", len(packed_data) + 4) + packed_data
return bytes(packed_data)
@staticmethod
def __unpack(data: bytes):
"""
解包数据。
Args:
data (bytes): 原始数据。
Returns:
tuple(dict): 解包后的数据。
"""
offset = 0
real_data = []
while offset < len(data):
region_header = struct.unpack(">IIII", data[:16])
region_data = data[offset: offset + region_header[0]]
real_data.append(
{
"type": region_header[2],
"number": region_header[3],
"data": json.loads(
region_data[offset + 18: offset + 18 + (region_header[0] - 16)]
),
}
)
offset += region_header[0]
return tuple(real_data)
class VideoQuality(Enum):
"""
视频的视频流分辨率枚举
- _360P: 流畅 360P
- _480P: 清晰 480P
- _720P: 高清 720P60
- _1080P: 高清 1080P
- _1080P_PLUS: 高清 1080P 高码率
- _1080P_60: 高清 1080P 60 帧码率
- _4K: 超清 4K
- HDR: 真彩 HDR
- DOLBY: 杜比视界
- _8K: 超高清 8K
"""
_360P = 16
_480P = 32
_720P = 64
_1080P = 80
_1080P_PLUS = 112
_1080P_60 = 116
_4K = 120
HDR = 125
DOLBY = 126
_8K = 127
class VideoCodecs(Enum):
"""
视频的视频流编码枚举
- HEV: HEVC(H.265)
- AVC: AVC(H.264)
- AV1: AV1
"""
HEV = "hev"
AVC = "avc"
AV1 = "av01"
class AudioQuality(Enum):
"""
视频的音频流清晰度枚举
- _64K: 64K
- _132K: 132K
- _192K: 192K
- HI_RES: Hi-Res 无损
- DOLBY: 杜比全景声
"""
_64K = 30216
_132K = 30232
DOLBY = 30250
HI_RES = 30251
_192K = 30280
@dataclass
class VideoStreamDownloadURL:
"""
(@dataclass)
视频流 URL 类
Attributes:
url (str) : 视频流 url
video_quality (VideoQuality): 视频流清晰度
video_codecs (VideoCodecs) : 视频流编码
"""
url: str
video_quality: VideoQuality
video_codecs: VideoCodecs
@dataclass
class AudioStreamDownloadURL:
"""
(@dataclass)
音频流 URL 类
Attributes:
url (str) : 音频流 url
audio_quality (AudioQuality): 音频流清晰度
"""
url: str
audio_quality: AudioQuality
@dataclass
class FLVStreamDownloadURL:
"""
(@dataclass)
FLV 视频流
Attributes:
url (str): FLV 流 url
"""
url: str
@dataclass
class HTML5MP4DownloadURL:
"""
(@dataclass)
可供 HTML5 播放的 mp4 视频流
Attributes:
url (str): HTML5 mp4 视频流
"""
url: str
@dataclass
class EpisodeTryMP4DownloadURL:
"""
(@dataclass)
番剧/课程试看的 mp4 播放流
Attributes:
url (str): 番剧试看的 mp4 播放流
"""
url: str
class VideoDownloadURLDataDetecter:
"""
`Video.get_download_url` 返回结果解析类。
在调用 `Video.get_download_url` 之后可以将代入 `VideoDownloadURLDataDetecter`,此类将一键解析。
目前支持:
- 视频清晰度: 360P, 480P, 720P, 1080P, 1080P 高码率, 1080P 60 帧, 4K, HDR, 杜比视界, 8K
- 视频编码: HEVC(H.265), AVC(H.264), AV1
- 音频清晰度: 64K, 132K, Hi-Res 无损音效, 杜比全景声, 192K
- FLV 视频流
- 番剧/课程试看视频流
"""
def __init__(self, data: dict):
"""
Args:
data (dict): `Video.get_download_url` 返回的结果
"""
self.__data = data
def check_video_and_audio_stream(self) -> bool:
"""
判断是否为音视频分离流
Returns:
bool: 是否为音视频分离流
"""
if "dash" in self.__data.keys():
return True
return False
def check_flv_stream(self) -> bool:
"""
判断是否为 FLV 视频流
Returns:
bool: 是否为 FLV 视频流
"""
if "durl" in self.__data.keys():
if self.__data["format"].startswith("flv"):
return True
return False
def check_html5_mp4_stream(self) -> bool:
"""
判断是否为 HTML5 可播放的 mp4 视频流
Returns:
bool: 是否为 HTML5 可播放的 mp4 视频流
"""
if "durl" in self.__data.keys():
if self.__data["format"].startswith("mp4"):
if self.__data.get("is_html5") == True:
return True
return False
def check_episode_try_mp4_stream(self):
"""
判断是否为番剧/课程试看的 mp4 视频流
Returns:
bool: 是否为番剧试看的 mp4 视频流
"""
if "durl" in self.__data.keys():
if self.__data["format"].startswith("mp4"):
if self.__data.get("is_html5") != True:
return True
return False
def detect_all(self):
"""
解析并返回所有数据
Returns:
List[VideoStreamDownloadURL | AudioStreamDownloadURL | FLVStreamDownloadURL | HTML5MP4DownloadURL | EpisodeTryMP4DownloadURL]: 所有的视频/音频流
"""
return self.detect()
def detect(
self,
video_max_quality: VideoQuality = VideoQuality._8K,
audio_max_quality: AudioQuality = AudioQuality._192K,
video_min_quality: VideoQuality = VideoQuality._360P,
audio_min_quality: AudioQuality = AudioQuality._64K,
video_accepted_qualities: List[VideoQuality] = [
item
for _, item in VideoQuality.__dict__.items()
if isinstance(item, VideoQuality)
],
audio_accepted_qualities: List[AudioQuality] = [
item
for _, item in AudioQuality.__dict__.items()
if isinstance(item, AudioQuality)
],
codecs: List[VideoCodecs] = [VideoCodecs.AV1, VideoCodecs.AVC, VideoCodecs.HEV],
no_dolby_video: bool = False,
no_dolby_audio: bool = False,
no_hdr: bool = False,
no_hires: bool = False,
) -> List[
Union[
VideoStreamDownloadURL,
AudioStreamDownloadURL,
FLVStreamDownloadURL,
HTML5MP4DownloadURL,
EpisodeTryMP4DownloadURL,
]
]:
"""
解析数据
Args:
**以下参数仅能在音视频流分离的情况下产生作用,flv / mp4 试看流 / html5 mp4 流下以下参数均没有作用**
video_max_quality (VideoQuality, optional) : 设置提取的视频流清晰度最大值,设置此参数绝对不会禁止 HDR/杜比. Defaults to VideoQuality._8K.
audio_max_quality (AudioQuality, optional) : 设置提取的音频流清晰度最大值. 设置此参数绝对不会禁止 Hi-Res/杜比. Defaults to AudioQuality._192K.
video_min_quality (VideoQuality, optional) : 设置提取的视频流清晰度最小值,设置此参数绝对不会禁止 HDR/杜比. Defaults to VideoQuality._360P.
audio_min_quality (AudioQuality, optional) : 设置提取的音频流清晰度最小值. 设置此参数绝对不会禁止 Hi-Res/杜比. Defaults to AudioQuality._64K.
video_accepted_qualities(List[VideoQuality], optional): 设置允许的所有视频流清晰度. Defaults to ALL.
audio_accepted_qualities(List[AudioQuality], optional): 设置允许的所有音频清晰度. Defaults to ALL.
codecs (List[VideoCodecs], optional) : 设置所有允许提取出来的视频编码. 此项不会忽略 HDR/杜比. Defaults to ALL codecs.
no_dolby_video (bool, optional) : 是否禁止提取杜比视界视频流. Defaults to False.
no_dolby_audio (bool, optional) : 是否禁止提取杜比全景声音频流. Defaults to False.
no_hdr (bool, optional) : 是否禁止提取 HDR 视频流. Defaults to False.
no_hires (bool, optional) : 是否禁止提取 Hi-Res 音频流. Defaults to False.
Returns:
List[VideoStreamDownloadURL | AudioStreamDownloadURL | FLVStreamDownloadURL | HTML5MP4DownloadURL | EpisodeTryMP4DownloadURL]: 提取出来的视频/音频流
"""
if "durl" in self.__data.keys():
if self.__data["format"].startswith("flv"):
# FLV 视频流
return [FLVStreamDownloadURL(url=self.__data["durl"][0]["url"])]
else:
if self.check_html5_mp4_stream():
# HTML5 MP4 视频流
return [HTML5MP4DownloadURL(url=self.__data["durl"][0]["url"])]
else:
# 会员番剧试看 MP4 流
return [EpisodeTryMP4DownloadURL(url=self.__data["durl"][0]["url"])]
else:
# 正常情况
streams = []
videos_data = self.__data["dash"]["video"]
audios_data = self.__data["dash"]["audio"]
flac_data = self.__data["dash"]["flac"]
dolby_data = self.__data["dash"]["dolby"]
for video_data in videos_data:
video_stream_url = video_data["baseUrl"]
video_stream_quality = VideoQuality(video_data["id"])
if video_stream_quality == VideoQuality.HDR and no_hdr:
continue
if video_stream_quality == VideoQuality.DOLBY and no_dolby_video:
continue
if (
video_stream_quality != VideoQuality.DOLBY
and video_stream_quality != VideoQuality.HDR
and video_stream_quality.value > video_max_quality.value
):
continue
if (
video_stream_quality != VideoQuality.DOLBY
and video_stream_quality != VideoQuality.HDR
and video_stream_quality.value < video_min_quality.value
):
continue
if (
video_stream_quality != VideoQuality.DOLBY
and video_stream_quality != VideoQuality.HDR
and (not video_stream_quality in video_accepted_qualities)
):
continue
video_stream_codecs = None
for val in VideoCodecs:
if val.value in video_data["codecs"]:
video_stream_codecs = val
if (not video_stream_codecs in codecs) and (
video_stream_codecs != None
):
continue
video_stream = VideoStreamDownloadURL(
url=video_stream_url,
video_quality=video_stream_quality,
video_codecs=video_stream_codecs, # type: ignore
)
streams.append(video_stream)
if audios_data:
for audio_data in audios_data:
audio_stream_url = audio_data["baseUrl"]
audio_stream_quality = AudioQuality(audio_data["id"])
if audio_stream_quality.value > audio_max_quality.value:
continue
if audio_stream_quality.value < audio_min_quality.value:
continue
if not audio_stream_quality in audio_accepted_qualities:
continue
audio_stream = AudioStreamDownloadURL(
url=audio_stream_url, audio_quality=audio_stream_quality
)
streams.append(audio_stream)
if flac_data and (not no_hires):
if flac_data["audio"]:
flac_stream_url = flac_data["audio"]["baseUrl"]
flac_stream_quality = AudioQuality(flac_data["audio"]["id"])
flac_stream = AudioStreamDownloadURL(
url=flac_stream_url, audio_quality=flac_stream_quality
)
streams.append(flac_stream)
if dolby_data and (not no_dolby_audio):
if dolby_data["audio"]:
dolby_stream_data = dolby_data["audio"][0]
dolby_stream_url = dolby_stream_data["baseUrl"]
dolby_stream_quality = AudioQuality(dolby_stream_data["id"])
dolby_stream = AudioStreamDownloadURL(
url=dolby_stream_url, audio_quality=dolby_stream_quality
)
streams.append(dolby_stream)
return streams
def detect_best_streams(
self,
video_max_quality: VideoQuality = VideoQuality._8K,
audio_max_quality: AudioQuality = AudioQuality._192K,
video_min_quality: VideoQuality = VideoQuality._360P,
audio_min_quality: AudioQuality = AudioQuality._64K,
video_accepted_qualities: List[VideoQuality] = [
item
for _, item in VideoQuality.__dict__.items()
if isinstance(item, VideoQuality)
],
audio_accepted_qualities: List[AudioQuality] = [
item
for _, item in AudioQuality.__dict__.items()
if isinstance(item, AudioQuality)
],
codecs: List[VideoCodecs] = [VideoCodecs.AV1, VideoCodecs.AVC, VideoCodecs.HEV],
no_dolby_video: bool = False,
no_dolby_audio: bool = False,
no_hdr: bool = False,
no_hires: bool = False,
) -> Union[
List[FLVStreamDownloadURL],
List[HTML5MP4DownloadURL],
List[EpisodeTryMP4DownloadURL],
List[Union[VideoStreamDownloadURL, AudioStreamDownloadURL, None]],
]:
"""
提取出分辨率、音质等信息最好的音视频流。
Args:
**以下参数仅能在音视频流分离的情况下产生作用,flv / mp4 试看流 / html5 mp4 流下以下参数均没有作用**
video_max_quality (VideoQuality) : 设置提取的视频流清晰度最大值,设置此参数绝对不会禁止 HDR/杜比. Defaults to VideoQuality._8K.
audio_max_quality (AudioQuality) : 设置提取的音频流清晰度最大值. 设置此参数绝对不会禁止 Hi-Res/杜比. Defaults to AudioQuality._192K.
video_min_quality (VideoQuality, optional) : 设置提取的视频流清晰度最小值,设置此参数绝对不会禁止 HDR/杜比. Defaults to VideoQuality._360P.
audio_min_quality (AudioQuality, optional) : 设置提取的音频流清晰度最小值. 设置此参数绝对不会禁止 Hi-Res/杜比. Defaults to AudioQuality._64K.
video_accepted_qualities(List[VideoQuality], optional): 设置允许的所有视频流清晰度. Defaults to ALL.
audio_accepted_qualities(List[AudioQuality], optional): 设置允许的所有音频清晰度. Defaults to ALL.
codecs (List[VideoCodecs]) : 设置所有允许提取出来的视频编码. 在数组中越靠前的编码选择优先级越高. 此项不会忽略 HDR/杜比. Defaults to [VideoCodecs.AV1, VideoCodecs.AVC, VideoCodecs.HEV].
no_dolby_video (bool) : 是否禁止提取杜比视界视频流. Defaults to False.
no_dolby_audio (bool) : 是否禁止提取杜比全景声音频流. Defaults to False.
no_hdr (bool) : 是否禁止提取 HDR 视频流. Defaults to False.
no_hires (bool) : 是否禁止提取 Hi-Res 音频流. Defaults to False.
Returns:
List[VideoStreamDownloadURL | AudioStreamDownloadURL | FLVStreamDownloadURL | HTML5MP4DownloadURL | None]: FLV 视频流 / HTML5 MP4 视频流 / 番剧或课程试看 MP4 视频流返回 `[FLVStreamDownloadURL | HTML5MP4StreamDownloadURL | EpisodeTryMP4DownloadURL]`, 否则为 `[VideoStreamDownloadURL, AudioStreamDownloadURL]`, 如果未匹配上任何合适的流则对应的位置位 `None`
"""
if self.check_flv_stream():
return self.detect_all() # type: ignore
elif self.check_html5_mp4_stream():
return self.detect_all() # type: ignore
elif self.check_episode_try_mp4_stream():
return self.detect_all() # type: ignore
else:
data = self.detect(
video_max_quality=video_max_quality,
audio_max_quality=audio_max_quality,
video_min_quality=video_min_quality,
audio_min_quality=audio_min_quality,
video_accepted_qualities=video_accepted_qualities,
audio_accepted_qualities=audio_accepted_qualities,
codecs=codecs,
)
video_streams = []
audio_streams = []
for stream in data:
if isinstance(stream, VideoStreamDownloadURL):
video_streams.append(stream)
if isinstance(stream, AudioStreamDownloadURL):
audio_streams.append(stream)
def video_stream_cmp(
s1: VideoStreamDownloadURL, s2: VideoStreamDownloadURL
):
# 杜比/HDR 优先
if s1.video_quality == VideoQuality.DOLBY and (not no_dolby_video):
return 1
elif s2.video_quality == VideoQuality.DOLBY and (not no_dolby_video):
return -1
elif s1.video_quality == VideoQuality.HDR and (not no_hdr):
return 1
elif s2.video_quality == VideoQuality.HDR and (not no_hdr):
return -1
if s1.video_quality.value != s2.video_quality.value:
return s1.video_quality.value - s2.video_quality.value
# Detect the high quality stream to the end.
elif s1.video_codecs.value != s2.video_codecs.value:
return codecs.index(s2.video_codecs) - codecs.index(s1.video_codecs)
return -1
def audio_stream_cmp(
s1: AudioStreamDownloadURL, s2: AudioStreamDownloadURL
):
# 杜比/Hi-Res 优先
if s1.audio_quality == AudioQuality.DOLBY and (not no_dolby_audio):
return 1
if s2.audio_quality == AudioQuality.DOLBY and (not no_dolby_audio):
return -1
if s1.audio_quality == AudioQuality.HI_RES and (not no_hires):
return 1
if s2.audio_quality == AudioQuality.HI_RES and (not no_hires):
return -1
return s1.audio_quality.value - s2.audio_quality.value
video_streams.sort(key=cmp_to_key(video_stream_cmp), reverse=True)
audio_streams.sort(key=cmp_to_key(audio_stream_cmp), reverse=True)
if len(video_streams) == 0:
video_streams = [None]
if len(audio_streams) == 0:
audio_streams = [None]
return [video_streams[0], audio_streams[0]]
|