├── .github └── ISSUE_TEMPLATE │ └── bug-report.yaml ├── .gitignore ├── LICENSE ├── README.md ├── __init__.py ├── bili_src ├── basicFunc.py ├── biliStream.py ├── biliTelegram.py ├── biliVideo.py ├── bili_client.py ├── bili_dynamic.py ├── bili_dynamic_dbg.py ├── bili_task.py ├── db.py ├── exception.py └── rule.py ├── config.py ├── docs ├── dynamic.jpg ├── follow.jpg ├── help2.jpg ├── list.jpg ├── streampush.jpg ├── updatepush.jpg └── videopush.jpg └── file └── source ├── ChangeLog.md ├── announcement.json ├── help.json └── help.txt /.github/ISSUE_TEMPLATE/bug-report.yaml: -------------------------------------------------------------------------------- 1 | name: 回报错误 2 | description: 在使用 bilibilibot 的过程中遇到了错误 3 | title: '[Bug]: ' 4 | labels: [ "bug?" ] 5 | 6 | body: 7 | # User's README and agreement 8 | - type: markdown 9 | attributes: 10 | value: | 11 | ## 感谢您愿意填写错误回报! 12 | ## 以下是一些注意事项,请务必阅读让我们能够更容易处理 13 | 14 | ### ❗ | 确定没有相同问题的ISSUE已被提出。 15 | ### 🌎| 请准确填写环境信息。 16 | ### ❔ | 打开DEBUG模式复现,并提供出现问题前后至少 10 秒的完整日志内容。请自行删除日志内存在的个人信息及敏感内容。 17 | ### ⚠ | 如果涉及内存泄漏/CPU占用异常请打开DEBUG模式并下载pprof性能分析。 18 | 19 | ## 如果您不知道如何有效、精准地表述,我们建议您先阅读《提问的智慧》 20 | 链接: [《提问的智慧》](https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/blob/main/README-zh_CN.md) 21 | --- 22 | 23 | 24 | # User's data 25 | - type: markdown 26 | attributes: 27 | value: | 28 | ## 环境信息 29 | 请根据实际使用环境修改以下信息。 30 | 31 | # Env | bilibilibot Version 32 | - type: input 33 | id: env-bili-ver 34 | attributes: 35 | label: billibilibot 版本 36 | validations: 37 | required: true 38 | 39 | # Env | nonebot Version 40 | - type: input 41 | id: env-nonebot-ver 42 | attributes: 43 | label: nonebot 版本 44 | validations: 45 | required: true 46 | 47 | # Env | go-cqhttp Version 48 | - type: input 49 | id: env-gocq-ver 50 | attributes: 51 | label: go-cqhttp 版本 52 | validations: 53 | required: true 54 | 55 | # Env | VM Version 56 | - type: dropdown 57 | id: env-vm-ver 58 | attributes: 59 | label: 运行环境 60 | description: 选择运行 bilibilibot 的系统版本 61 | options: 62 | - Windows (64) 63 | - Windows (32/x84) 64 | - MacOS 65 | - Linux 66 | - Ubuntu 67 | - CentOS 68 | - ArchLinux 69 | - UNIX (Android) 70 | - 其它(请在下方说明) 71 | validations: 72 | required: true 73 | 74 | # Input | Reproduce 75 | - type: textarea 76 | id: reproduce-steps 77 | attributes: 78 | label: 重现步骤 79 | description: | 80 | 我们需要执行哪些操作才能让 bug 出现? 81 | 简洁清晰的重现步骤能够帮助我们更迅速地定位问题所在。 82 | validations: 83 | required: true 84 | 85 | # Input | Expected result 86 | - type: textarea 87 | id: expected 88 | attributes: 89 | label: 期望的结果是什么? 90 | validations: 91 | required: true 92 | 93 | # Input | Actual result 94 | - type: textarea 95 | id: actual 96 | attributes: 97 | label: 实际的结果是什么? 98 | validations: 99 | required: true 100 | 101 | # Optional | Logging 102 | - type: textarea 103 | id: logging 104 | attributes: 105 | label: 日志记录(可选) 106 | render: golang 107 | 108 | # Optional | Extra description 109 | - type: textarea 110 | id: extra-desc 111 | attributes: 112 | label: 补充说明(可选) 113 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | # json File 132 | *.json 133 | 134 | # database File 135 | bilibili.db 136 | bilibili_2.db 137 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | nonebot 3 |

4 | 5 |
6 | 7 | # nonebot-plugin-bilibilibot 8 | 9 | 👾 _NoneBot bilibili通知插件_ 👾 10 |

version: 2.3.4

11 | 12 |
13 | 14 | # 简介 15 | 基于[Nonebot2](https://github.com/nonebot/nonebot2)的bilibili通知插件,可将up主投稿视频,主播开播,番剧的更新以及用户更新动态推送到QQ 16 | 17 | **已支持v2.0.0-beta.5** 18 | ## 依赖 19 | - 适配器: onebot v11 20 | - [go-cqhttp](https://github.com/Mrs4s/go-cqhttp) 21 | - 插件: 22 | - [nonebot_plugin_apscheduler](https://pypi.org/project/nonebot-plugin-apscheduler/) 23 | 24 | # 特色功能 25 | - 可通过在B站客户端分享用户主页进行关注up主(私聊) 26 | 27 | - 可通过在B站客户端分享直播间进行关注主播(私聊) 28 | 29 | - 可通过分享番剧播放页面进行关注番剧(私聊) 30 | 31 | - 超级用户可对普通用户进行广播 32 | 33 | - 将机器人加入群组,向群友及时同时直播或更新消息 34 | 35 | # 安装方式 36 | - ` nb plugin install nonebot-plugin-bilibilibot` 37 | 38 | # 配置项 39 | 配置方式: 请在nonebot的全局配置文件中添加如下配置项。 40 | ## SUPERUSERS 41 | - 类型: List[str] 42 | - 说明: 超级用户的列表 43 | > SUPERSUSERS = ["your qq id"] 44 | 45 | # 注意事项 46 | - 将机器人加入群组后,只有**管理员\群主\超级管理员**才能对机器人进行操作 47 | - 未避免误触发,群组中不能使用分享链接来关注的功能 48 | - 由于需要同时处理群消息和私聊消息,建议在非调试环境中使用,否则日志将会出现很多的ignore消息 49 | - 如果需要修改公告内容,请修改file/source/announcement.json文件 50 | 51 | # 示例 52 | ## 获取帮助 53 | ![help](https://github.com/TDK1969/nonebot_plugin_bilibilibot/blob/main/docs/help2.jpg?raw=true) 54 | ## 视频更新推送 55 | ![videopush](https://github.com/TDK1969/nonebot_plugin_bilibilibot/blob/main/docs/updatepush.jpg?raw=true) 56 | ## 番剧更新推送 57 | ![videupdate](https://github.com/TDK1969/nonebot_plugin_bilibilibot/blob/main/docs/videopush.jpg?raw=true) 58 | ## 直播开播推送 59 | ![streampuah](https://github.com/TDK1969/nonebot_plugin_bilibilibot/blob/main/docs/streampush.jpg?raw=true) 60 | ## 动态更新推送 61 | ![dynamicpuah](https://github.com/TDK1969/nonebot_plugin_bilibilibot/blob/main/docs/dynamic.jpg?raw=true) 62 | 63 | ## 通过B站客户端分享进行关注 64 | ![follow](https://github.com/TDK1969/nonebot_plugin_bilibilibot/blob/main/docs/follow.jpg?raw=true) 65 | ## 查询关注列表 66 | ![list](https://github.com/TDK1969/nonebot_plugin_bilibilibot/blob/main/docs/list.jpg?raw=true) 67 | 68 | 69 | # 更新日志 70 | [完整日志](https://github.com/TDK1969/nonebot_plugin_bilibilibot/blob/main/file/source/ChangeLog.md) 71 | 72 | - **ver 2.3.4** 73 | ``` 74 | 1. 修复了番剧集数错误以及更新不推送的问题 75 | 2. 增加了主播的下播提醒 76 | ``` 77 | 78 | - **ver 2.3.3** 79 | ``` 80 | 1. 修复了由于b站接口改变导致-352和-401的错误 81 | 2. 优化了网络IO 82 | ``` 83 | 84 | - **ver 2.3.2** 85 | ``` 86 | 1. 修复了由于b站接口改变导致关注up主命令失败的问题 87 | 2. 为了避免命令冲突,将命令`/help`改为`/bilihelp` 88 | ``` 89 | 90 | - **ver 2.3.1** 91 | ``` 92 | 1. 修复了setuptools打包错误导致的import失败问题 93 | 2. 修复了由于路径改变导致使用`/help`命令失败的问题 94 | 3. 已经完结的番剧不会再进行更新检查了 95 | ``` 96 | 97 | - **ver 2.2.0** 98 | ``` 99 | 1. 修改某些B站的接口,减少被接口风控的风险 100 | 2. 可以使用ep_id, season_id和media_id对番剧进行关注,需要携带前两个字符 101 | 3. 本次更新需要重置数据库,因此会丢失旧版本的关注数据,需要重新关注 102 | ``` 103 | ep_id: https://www.bilibili.com/bangumi/play/ep433947, 中的**ep433947** 104 | 105 | season_id: https://www.bilibili.com/bangumi/play/ss39431, 中的**ss39431** 106 | 107 | media_id: https://www.bilibili.com/bangumi/media/md28235123, 中**md28235123** 108 | 109 | 110 | # 特别鸣谢 111 | - 感谢[@0w0w0](https://github.com/a0w0w0)帮助测试 112 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | from nonebot import get_driver, on_command, on_message, require, permission, get_bot 2 | from nonebot.log import logger 3 | from nonebot.rule import regex 4 | 5 | from nonebot.adapters.onebot.v11 import Message, MessageSegment, MessageEvent 6 | from nonebot.adapters.onebot.v11.permission import GROUP_ADMIN, GROUP_OWNER, PRIVATE_FRIEND, GROUP_MEMBER 7 | from nonebot.adapters.onebot.v11.event import GroupMessageEvent, PrivateMessageEvent 8 | from nonebot.params import CommandArg 9 | from nonebot.permission import SUPERUSER 10 | from .config import Config 11 | import sys 12 | sys.path.append("..") 13 | from .bili_src.biliStream import * 14 | from .bili_src.biliVideo import * 15 | from .bili_src.biliTelegram import * 16 | from .bili_src.basicFunc import * 17 | from .bili_src.bili_dynamic import follow_dynamic_list, unfollow_dynamic_list 18 | from .bili_src.rule import groupMessageRule, privateMessageRule 19 | from .bili_src.bili_task import BiliTaskManager 20 | from .bili_src.bili_dynamic import check_dynamic_update 21 | 22 | import os 23 | import json 24 | import sys 25 | import re 26 | from typing import Union 27 | 28 | global_config = get_driver().config 29 | config = Config.parse_obj(global_config) 30 | 31 | __PLUGIN_NAME = "[bilibilibot]" 32 | 33 | ALL_PERMISSION = GROUP_ADMIN | GROUP_OWNER | PRIVATE_FRIEND | SUPERUSER 34 | PACKAGEPATH = dirname(abspath(__file__)) 35 | 36 | follow_liver_command = on_command("关注主播", permission=ALL_PERMISSION) 37 | @follow_liver_command.handle() 38 | async def follow_liver_command_handler(event: Union[PrivateMessageEvent, GroupMessageEvent], args: Message = CommandArg()): 39 | uid_list = args.extract_plain_text().split() 40 | await create_user(event) 41 | if isinstance(event, PrivateMessageEvent): 42 | success_list, fail_list = await follow_liver_list(event.sender.user_id, uid_list, 0) 43 | if isinstance(event, GroupMessageEvent): 44 | success_list, fail_list = await follow_liver_list(event.group_id, uid_list, 1) 45 | await follow_liver_command.finish(f"关注成功:\n{success_list}\n关注失败:\n{fail_list}") 46 | 47 | unfollow_liver_command = on_command("取关主播", aliases={"切割主播"}, permission=ALL_PERMISSION) 48 | @unfollow_liver_command.handle() 49 | async def unfollow_liver_command_handler(event: Union[PrivateMessageEvent, GroupMessageEvent], args: Message = CommandArg()): 50 | uid_list = args.extract_plain_text().split() 51 | await create_user(event) 52 | if isinstance(event, PrivateMessageEvent): 53 | success_list, fail_list = await unfollow_liver_list(event.sender.user_id, uid_list, 0) 54 | if isinstance(event, GroupMessageEvent): 55 | success_list, fail_list = await unfollow_liver_list(event.group_id, uid_list, 1) 56 | await unfollow_liver_command.finish(f"取关成功:\n{success_list}\n取关失败:\n{fail_list}") 57 | 58 | listFollowingCommand = on_command("查询关注", aliases={"查询成分"}, permission=ALL_PERMISSION) 59 | @listFollowingCommand.handle() 60 | async def listFollowingCommandHandler(event: Union[PrivateMessageEvent, GroupMessageEvent], args: Message = CommandArg()): 61 | await create_user(event) 62 | inputArgs = args.extract_plain_text().split() 63 | defaultArgs = ['直播', 'up主', '番剧', '动态'] 64 | 65 | if isinstance(event, PrivateMessageEvent): 66 | user_id = event.sender.user_id 67 | user_type = 0 68 | elif isinstance(event, GroupMessageEvent): 69 | user_id = event.group_id 70 | user_type = 1 71 | 72 | exceptArgs = set(inputArgs) - set(defaultArgs) 73 | 74 | if len(exceptArgs) != 0: 75 | logger.info(f'{__PLUGIN_NAME}查询失败,存在错误参数: {exceptArgs}') 76 | await listFollowingCommand.finish(f"查询失败,存在错误参数:{exceptArgs}\n请正确输入命令,例如: '查询成分 直播' 或 '查询成分 直播 up主 番剧'") 77 | 78 | if not inputArgs: 79 | inputArgs = defaultArgs 80 | 81 | logger.debug(f'{__PLUGIN_NAME} user_id type: {type(user_id)}') 82 | 83 | 84 | try: 85 | res = bili_database.query_info(user_type, user_id) 86 | logger.debug(f'{__PLUGIN_NAME}res = {res}') 87 | 88 | if res: 89 | if user_type == 0: 90 | followed_up_list = bili_database.query_user_relation(1, user_id) 91 | followed_liver_list = bili_database.query_user_relation(3, user_id) 92 | followed_telegram_list = bili_database.query_user_relation(5, user_id) 93 | logger.debug(f'{__PLUGIN_NAME}followed_telegram_list = {followed_telegram_list}') 94 | 95 | followed_dynamic_list = bili_database.query_user_relation(7, user_id) 96 | else: 97 | followed_up_list = bili_database.query_group_relation(1, user_id) 98 | followed_liver_list = bili_database.query_group_relation(3, user_id) 99 | followed_telegram_list = bili_database.query_group_relation(5, user_id) 100 | followed_dynamic_list = bili_database.query_group_relation(7, user_id) 101 | 102 | textMsg = "" 103 | if 'up主' in inputArgs: 104 | if followed_up_list: 105 | textMsg += '关注的up主:\n' 106 | for up_uid in followed_up_list: 107 | up_uid, up_name, _ = bili_database.query_info(2, up_uid) 108 | textMsg += '> ' + f"{up_name}(uid: {up_uid})" + '\n' 109 | 110 | else: 111 | textMsg += '无关注的up主\n' 112 | textMsg += '\n' 113 | 114 | if '直播' in inputArgs: 115 | if followed_liver_list: 116 | textMsg += '关注的主播:\n' 117 | for liver_uid in followed_liver_list: 118 | liver_uid, liver_name, _, _ = bili_database.query_info(3, liver_uid) 119 | textMsg += '> ' + f"{liver_name}(uid: {liver_uid})" + '\n' 120 | else: 121 | textMsg += '无关注的主播\n' 122 | textMsg += '\n' 123 | 124 | if '番剧' in inputArgs: 125 | if followed_telegram_list: 126 | textMsg += '关注的番剧\n' 127 | for season_id in followed_telegram_list: 128 | season_id, telegram_title, _, _ = bili_database.query_info(4, season_id) 129 | textMsg += '> ' + f"{telegram_title}(season_id: ss{season_id})" + '\n' 130 | 131 | else: 132 | textMsg += '无关注的番剧' 133 | textMsg += '\n' 134 | 135 | if '动态' in inputArgs: 136 | if followed_dynamic_list: 137 | textMsg += '关注的动态主\n' 138 | for uid in followed_dynamic_list: 139 | uid, u_name, _, _ = bili_database.query_info(5, uid) 140 | textMsg += '> ' + f"{u_name}(uid: {uid})" + '\n' 141 | 142 | else: 143 | textMsg += '无关注的动态主' 144 | textMsg += '\n' 145 | 146 | await listFollowingCommand.send(textMsg) 147 | else: 148 | await listFollowingCommand.finish("关注列表为空") 149 | except Exception as _: 150 | ex_type, ex_val, _ = sys.exc_info() 151 | exception_msg = f'【错误报告】\n查询关注时发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 152 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 153 | await listFollowingCommand.finish() 154 | 155 | followUpCommand = on_command("关注up", permission=ALL_PERMISSION) 156 | @followUpCommand.handle() 157 | async def followUpCommandHandler(event: Union[PrivateMessageEvent, GroupMessageEvent], args: Message = CommandArg()): 158 | await create_user(event) 159 | uid_list = args.extract_plain_text().split() 160 | if isinstance(event, PrivateMessageEvent): 161 | success_list, fail_list = await follow_up_list(event.sender.user_id, uid_list, 0) 162 | if isinstance(event, GroupMessageEvent): 163 | success_list, fail_list = await follow_up_list(event.group_id, uid_list, 1) 164 | await followUpCommand.finish(f"关注成功:\n{success_list}\n关注失败:\n{fail_list}") 165 | 166 | unfollowUpCommand = on_command("取关up", permission=ALL_PERMISSION) 167 | @unfollowUpCommand.handle() 168 | async def unfollowUpCommandHandler(event: Union[PrivateMessageEvent, GroupMessageEvent], args: Message = CommandArg()): 169 | await create_user(event) 170 | uid_list = args.extract_plain_text().split() 171 | if isinstance(event, PrivateMessageEvent): 172 | success_list, fail_list = await unfollow_up_list(event.sender.user_id, uid_list, 0) 173 | if isinstance(event, GroupMessageEvent): 174 | success_list, fail_list = await unfollow_up_list(event.group_id, uid_list, 1) 175 | await unfollowUpCommand.finish(f"取关成功:\n{success_list}\n取关失败:\n{fail_list}") 176 | 177 | followTelegramCommand = on_command("关注番剧", permission=ALL_PERMISSION) 178 | @followTelegramCommand.handle() 179 | async def followTelegramCommandHandler(event: Union[PrivateMessageEvent, GroupMessageEvent], args: Message = CommandArg()): 180 | await create_user(event) 181 | tele_id_list = args.extract_plain_text().split() 182 | if isinstance(event, PrivateMessageEvent): 183 | success_list, fail_list = await follow_telegram_list(event.sender.user_id, tele_id_list, 0) 184 | if isinstance(event, GroupMessageEvent): 185 | success_list, fail_list = await follow_telegram_list(event.group_id, tele_id_list, 1) 186 | await followTelegramCommand.finish(f"关注成功:\n{success_list}\n关注失败:\n{fail_list}") 187 | 188 | unfollowTelegramCommand = on_command("取关番剧", permission=ALL_PERMISSION) 189 | @unfollowTelegramCommand.handle() 190 | async def unfollowTelegramCommandHandler(event: Union[PrivateMessageEvent, GroupMessageEvent], args: Message = CommandArg()): 191 | await create_user(event) 192 | season_id_list = args.extract_plain_text().split() 193 | if isinstance(event, PrivateMessageEvent): 194 | success_list, fail_list = await unfollow_telegram_list(event.sender.user_id, season_id_list, 0) 195 | if isinstance(event, GroupMessageEvent): 196 | success_list, fail_list = await unfollow_telegram_list(event.group_id, season_id_list, 1) 197 | await unfollowTelegramCommand.finish(f"取关成功:\n{success_list}\n取关失败:\n{fail_list}") 198 | 199 | follow_dynamic_command = on_command("关注动态", permission=ALL_PERMISSION) 200 | @follow_dynamic_command.handle() 201 | async def follow_dynamic_commandHandler(event: Union[PrivateMessageEvent, GroupMessageEvent], args: Message = CommandArg()): 202 | await create_user(event) 203 | uid_list = args.extract_plain_text().split() 204 | if isinstance(event, PrivateMessageEvent): 205 | success_list, fail_list = await follow_dynamic_list(event.sender.user_id, uid_list, 0) 206 | if isinstance(event, GroupMessageEvent): 207 | success_list, fail_list = await follow_dynamic_list(event.group_id, uid_list, 1) 208 | await follow_dynamic_command.finish(f"关注成功:\n{success_list}\n关注失败:\n{fail_list}") 209 | 210 | unfollow_dynamic_command = on_command("取关动态", permission=ALL_PERMISSION) 211 | @unfollow_dynamic_command.handle() 212 | async def unfollow_dynamic_commandHandler(event: Union[PrivateMessageEvent, GroupMessageEvent], args: Message = CommandArg()): 213 | await create_user(event) 214 | uid_list = args.extract_plain_text().split() 215 | if isinstance(event, PrivateMessageEvent): 216 | success_list, fail_list = await unfollow_dynamic_list(event.sender.user_id, uid_list, 0) 217 | if isinstance(event, GroupMessageEvent): 218 | success_list, fail_list = await unfollow_dynamic_list(event.group_id, uid_list, 1) 219 | await unfollow_dynamic_command.finish(f"取关成功:\n{success_list}\n取关失败:\n{fail_list}") 220 | 221 | followUpByShare = on_message( 222 | rule=regex('\[CQ:json,[\w\W]*"appid":100951776[\w\W]*space.bilibili.com[\w\W]*[\w\W]*\]') & privateMessageRule, 223 | permission=PRIVATE_FRIEND 224 | ) 225 | @followUpByShare.handle() 226 | async def upShareHandler(event: PrivateMessageEvent): 227 | '''响应用户分享up主空间连接 228 | 229 | Args: 230 | event (PrivateMessageEvent): 消息事件 231 | ''' 232 | await create_user(event) 233 | sJson = event.get_message()[-1].get('data') 234 | data = json.loads(sJson['data']) 235 | uid = data['meta']['news']['jumpUrl'].split('?')[0].split('/')[-1] 236 | 237 | success_list, fail_list = await follow_up_list(event.sender.user_id, [uid], 0) 238 | 239 | if success_list: 240 | await followUpByShare.finish(f"关注up成功: <{success_list[0]}>") 241 | elif fail_list: 242 | await followUpByShare.finish(f"关注up失败: <{fail_list[0]}>") 243 | 244 | followStreamerByShare = on_message( 245 | rule=regex('^\[CQ:json,[\w\W]*"appid":100951776[\w\W]*live.bilibili.com[\w\W]*') & privateMessageRule, 246 | permission=PRIVATE_FRIEND 247 | ) 248 | @followStreamerByShare.handle() 249 | async def streamerShareHandler(event: PrivateMessageEvent): 250 | '''响应个人用户的直播间分享 251 | 252 | Args: 253 | event (PrivateMessageEvent): 消息事件 254 | ''' 255 | try: 256 | await create_user(event) 257 | sJson = event.get_message()[-1].get('data') 258 | data = json.loads(sJson['data']) 259 | roomNumber = data['meta']['news']['jumpUrl'].split('?')[0].split('/')[-1] 260 | 261 | uid, _ = await bili_client.init_liver_info_by_room_id(roomNumber) 262 | 263 | success_list, fail_list = await follow_liver_list(event, event.sender.user_id, [uid], 0) 264 | 265 | if success_list: 266 | await followUpByShare.finish(f"关注主播成功: <{success_list[0]}>") 267 | elif fail_list: 268 | await followUpByShare.finish(f"关注主播失败: <{fail_list[0]}> ") 269 | 270 | except nonebot.exception.FinishedException: 271 | pass 272 | except BiliInvalidRoomId: 273 | await followStreamerByShare.finish('关注失败: 无效的房间号') 274 | except Exception: 275 | ex_type, ex_val, _ = sys.exc_info() 276 | logger.error(f'{__PLUGIN_NAME}获取主播 <{uid}> 信息时发生错误') 277 | logger.error(f'{__PLUGIN_NAME}错误类型: {ex_type},错误值: {ex_val}') 278 | await followStreamerByShare.finish('关注失败: 连接错误') 279 | 280 | followTelegramByShare = on_message( 281 | rule = regex('^\[CQ:json[\w\W]*"appid":100951776[\w\W]*www.bilibili.com\/bangumi\/play\/[\w\W]*') & privateMessageRule, 282 | permission=PRIVATE_FRIEND 283 | ) 284 | @followTelegramByShare.handle() 285 | async def telegramShareHandler(event: PrivateMessageEvent): 286 | '''响应用户分享番剧页面 287 | 288 | Args: 289 | event (PrivateMessageEvent): 消息事件 290 | ''' 291 | await create_user(event) 292 | sJson = event.get_message()[-1].get('data') 293 | data = json.loads(sJson['data']) 294 | epID = data['meta']['detail_1']['qqdocurl'].split('?')[0].split('/')[-1] 295 | 296 | success_list, fail_list = await follow_telegram_list(event.sender.user_id, [epID], 0) 297 | 298 | if success_list: 299 | await followUpByShare.finish(f"关注番剧成功: <{success_list[0]}>") 300 | elif fail_list: 301 | await followUpByShare.finish(f"关注番剧失败: <{fail_list[0]}> ") 302 | 303 | follow_by_share_short_url = on_message( 304 | rule = regex('^\[CQ:json[\w\W]*"appid":100951776[\w\W]*b23.tv[\w\W]*') & privateMessageRule, 305 | permission=PRIVATE_FRIEND 306 | ) 307 | @follow_by_share_short_url.handle() 308 | async def short_url_handler(event: PrivateMessageEvent): 309 | try: 310 | await create_user(event) 311 | sJson = event.get_message()[-1].get('data') 312 | shortLink = re.search("https:\/\/b23.tv\/\w+", sJson['data']) 313 | short_url = shortLink.group() 314 | logger.debug(f"get short url = {short_url}") 315 | 316 | msg_type = 0 317 | user_id = event.sender.user_id 318 | id_type, target_uid = await bili_client.parse_short_url(short_url) 319 | #isSuccess, id_type, uid = await parseB23Url(short_url) 320 | if id_type == 0: 321 | success_list, fail_list = await follow_up_list(user_id, [target_uid], msg_type) 322 | if success_list: 323 | await follow_by_share_short_url.finish(f"关注成功: {success_list[0]}") 324 | elif fail_list: 325 | await follow_by_share_short_url.finish(f"关注失败: {fail_list[0]}") 326 | 327 | elif id_type == 1: 328 | liver_uid, _ = await bili_client.init_liver_info_by_room_id(target_uid) 329 | success_list, fail_list = await follow_liver_list(user_id, [liver_uid], msg_type) 330 | if success_list: 331 | await follow_by_share_short_url.finish(f"关注成功: {success_list[0]}") 332 | elif fail_list: 333 | await follow_by_share_short_url.finish(f"关注失败: {fail_list[0]}") 334 | 335 | elif id_type == 2: 336 | success_list, fail_list = await follow_telegram_list(user_id, [target_uid], msg_type) 337 | if success_list: 338 | await follow_by_share_short_url.finish(f"关注成功: {success_list[0]}") 339 | 340 | elif fail_list: 341 | await follow_by_share_short_url.finish(f"关注失败: {fail_list[0]}") 342 | else: 343 | await follow_by_share_short_url.finish(f"关注失败: 非法短链接{short_url}") 344 | except nonebot.exception.FinishedException: 345 | pass 346 | except BiliInvalidShortUrl: 347 | await follow_by_share_short_url.finish('关注失败: 非法或不支持的短链接') 348 | except Exception as _: 349 | ex_type, ex_val, _ = sys.exc_info() 350 | logger.error(f'{__PLUGIN_NAME}【错误报告】\n解析短链接 <{short_url}> 时发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n{traceback.format_exc()}') 351 | await follow_by_share_short_url.finish('关注失败: 连接错误') 352 | 353 | helpCommand = on_command("bilihelp", permission=ALL_PERMISSION, aliases={'B站帮助'}) 354 | @helpCommand.handle() 355 | async def sendHelpMsg(event: MessageEvent): 356 | await create_user(event) 357 | helpMsg = "" 358 | with open(f'{PACKAGEPATH}/file/source/help.json', 'r', encoding='utf-8') as f: 359 | helpMsg = json.load(f) 360 | await helpCommand.finish(helpMsg) 361 | 362 | publicBroacast = on_command("broacast", aliases={'广播'}, permission=permission.SUPERUSER) 363 | @publicBroacast.handle() 364 | async def sendBroacast(event: MessageEvent): 365 | announcement = "" 366 | announcementPath = f'{PACKAGEPATH}/file/source/announcement.json' 367 | if os.path.exists(announcementPath): 368 | with open(announcementPath, 'r', encoding='utf-8') as f: 369 | announcement = json.load(f) 370 | 371 | users = GetAllUser() 372 | await SendMsgToUsers(announcement, users) 373 | 374 | groups = GetAllGroup() 375 | await SendMsgToGroups(announcement, groups) 376 | 377 | await publicBroacast.finish("公告发送成功") 378 | else: 379 | logger.debug(f'{__PLUGIN_NAME}公告文件不存在') 380 | await publicBroacast.finish("公告发送失败: 公告文件不存在") 381 | 382 | require("nonebot_plugin_apscheduler") 383 | from nonebot_plugin_apscheduler import scheduler 384 | logger.debug(f'{__PLUGIN_NAME}注册定时任务') 385 | scheduler.add_job(check_bili_live, "interval", minutes=1, id="bili_stream", misfire_grace_time=90) 386 | scheduler.add_job(check_up_update, "interval", minutes=2, id="bili_up", misfire_grace_time=90) 387 | scheduler.add_job(check_telegram_update, "interval", minutes=10, id="bili_telegram", misfire_grace_time=90) 388 | scheduler.add_job(check_dynamic_update, "interval", minutes=1, id="bili_dynamic", misfire_grace_time=90) -------------------------------------------------------------------------------- /bili_src/basicFunc.py: -------------------------------------------------------------------------------- 1 | from typing import List, Tuple, Union 2 | import re 3 | import httpx 4 | import os 5 | import sys 6 | import traceback 7 | from nonebot import get_bot 8 | from nonebot.log import logger 9 | from os.path import abspath, dirname 10 | from nonebot.adapters.onebot.v11.event import GroupMessageEvent, PrivateMessageEvent 11 | from .db import bili_database 12 | from .exception import * 13 | 14 | __PLUGIN_NAME = "[bilibilibot~基础]" 15 | 16 | def GetAllUser() -> List[str]: 17 | """ 18 | @description : 19 | 获取所有用户 20 | --------- 21 | @param : 22 | 无 23 | ------- 24 | @Returns : 25 | 返回所有用户qq号组成的列表 26 | ------- 27 | """ 28 | qq_users = bili_database.query_all(3) 29 | return [i[0] for i in qq_users] 30 | 31 | def GetAllGroup() -> List[str]: 32 | """ 33 | @description : 34 | 获取所有群 35 | --------- 36 | @param : 37 | 无 38 | ------- 39 | @Returns : 40 | 返回所有群号组成的列表 41 | ------- 42 | """ 43 | 44 | qq_groups = bili_database.query_all(4) 45 | return [i[0] for i in qq_groups] 46 | 47 | async def SendMsgToUsers(msg: str, users: List[str]): 48 | """ 49 | @description : 50 | 向所有用户发送公告 51 | --------- 52 | @param : 53 | msg: 内容 54 | users: 用户的qq列表 55 | ------- 56 | @Returns : 57 | 无 58 | ------- 59 | """ 60 | 61 | bot = get_bot() 62 | for user in users: 63 | await bot.send_msg(message=msg, user_id = user) 64 | 65 | async def SendMsgToGroups(msg: str, groups: List[str]): 66 | '''向所有群组发送公告 67 | 68 | Args: 69 | msg (str): 公告内容 70 | groups (List[str]): 群组列表 71 | ''' 72 | 73 | bot = get_bot() 74 | for group in groups: 75 | await bot.send_msg(message=msg, group_id = group) 76 | 77 | async def create_user(event: Union[PrivateMessageEvent, GroupMessageEvent]) -> None: 78 | '''接受消息后,创建用户 79 | 80 | Args: 81 | event (Union[PrivateMessageEvent, GroupMessageEvent]): 消息事件 82 | ''' 83 | 84 | user_type = 0 if isinstance(event, PrivateMessageEvent) else 1 85 | user_id = event.sender.user_id if user_type == 0 else event.group_id 86 | try: 87 | user_info = bili_database.query_info(user_type, str(user_id)) 88 | if not user_info: 89 | logger.info(f'{__PLUGIN_NAME}用户{user_id}不存在于数据库,即将创建') 90 | name = event.sender.nickname 91 | if user_type == 1: 92 | bot = get_bot() 93 | group_info = await bot.get_group_info(group_id=user_id) 94 | name = group_info["group_name"] 95 | bili_database.insert_info(user_type, user_id, name) 96 | except Exception as _: 97 | ex_type, ex_val, _ = sys.exc_info() 98 | exception_msg = '【错误报告】\n创建用户时发生错误\n错误类型: {}\n错误值: {}\n'.format(ex_type, ex_val) 99 | logger.error(f"{__PLUGIN_NAME}\n" + exception_msg + traceback.format_exc()) 100 | 101 | -------------------------------------------------------------------------------- /bili_src/biliStream.py: -------------------------------------------------------------------------------- 1 | from typing import Tuple, List 2 | import sys 3 | import traceback 4 | import nonebot 5 | from nonebot.log import logger 6 | from nonebot.adapters.onebot.v11 import MessageSegment 7 | from nonebot.adapters.onebot.v11.adapter import Adapter 8 | from .basicFunc import * 9 | from .exception import * 10 | import asyncio 11 | from .bili_client import bili_client 12 | from .bili_task import bili_task_manager 13 | __PLUGIN_NAME = "[bilibilibot~直播]" 14 | async def check_bili_live() -> None: 15 | """ 16 | @description : 17 | 检查数据库中所有主播的开播状态 18 | 如果关注的主播开播,则通知所有关注的用户 19 | 如果主播开播状态改变,则更新数据库 20 | --------- 21 | @param : 22 | ------- 23 | @Returns : 24 | ------- 25 | """ 26 | liver_list = list(bili_task_manager.liver_list.values()) 27 | 28 | sched_bot = nonebot.get_bot() 29 | 30 | """results = await asyncio.gather( 31 | *[bili_client.get_live_status(liver_info[0], liver_info[3]) for liver_info in liver_list], 32 | return_exceptions=True 33 | )""" 34 | async with httpx.AsyncClient(headers={"User-Agent":"Mozilla/5.0"}) as client: 35 | results = await asyncio.gather( 36 | *[bili_client.get_live_status(client, liver_info["liver_uid"], liver_info["room_id"]) for liver_info in liver_list], 37 | return_exceptions=True 38 | ) 39 | for i in range(len(liver_list)): 40 | if isinstance(results[i], tuple): 41 | if results[i][0] and not liver_list[i]["is_live"]: 42 | logger.info(f'[{__PLUGIN_NAME}]检测到主播 <{liver_list[i]["liver_name"]}> 已开播!') 43 | text_msg = '【直播动态】\n<{}>正在直播!\n标题: {}\n链接: {}'.format(liver_list[i]["liver_name"], results[i][1], f"https://live.bilibili.com/{liver_list[i]['room_id']}") 44 | reported_msg = text_msg + MessageSegment.image(results[i][2]) 45 | logger.info(f'[{__PLUGIN_NAME}]向粉丝发送开播通知') 46 | bili_task_manager.update_liver_info(liver_list[i]["liver_uid"], True) 47 | #bili_database.update_info(1, 1, liver_list[i][0]) 48 | 49 | user_list = liver_list[i]["user_follower"] 50 | await asyncio.gather(*[sched_bot.send_msg(message=reported_msg, user_id=user_id) for user_id in user_list]) 51 | group_list = liver_list[i]["group_follower"] 52 | await asyncio.gather(*[sched_bot.send_msg(message=reported_msg, group_id=group_id) for group_id in group_list]) 53 | 54 | elif not results[i][0] and liver_list[i]["is_live"]: 55 | logger.info(f'[{__PLUGIN_NAME}]检测到主播 <{liver_list[i]["liver_name"]}> 已下播') 56 | bili_task_manager.update_liver_info(liver_list[i]["liver_uid"], False) 57 | text_msg = '【直播动态】\n<{}>直播结束!'.format(liver_list[i]["liver_name"]) 58 | user_list = liver_list[i]["user_follower"] 59 | await asyncio.gather(*[sched_bot.send_msg(message=text_msg, user_id=user_id) for user_id in user_list]) 60 | group_list = liver_list[i]["group_follower"] 61 | await asyncio.gather(*[sched_bot.send_msg(message=text_msg, group_id=group_id) for group_id in group_list]) 62 | 63 | elif isinstance(results[i], (BiliAPIRetCodeError, BiliStatusCodeError, BiliConnectionError)): 64 | exception_msg = f'[错误报告]\n检测主播 <{liver_list[i]["liver_name"]}> 开播情况时发生错误\n错误类型: {type(results[i])}\n错误信息: {results[i]}' 65 | logger.error(f"[{__PLUGIN_NAME}]" + exception_msg) 66 | 67 | async def follow_liver(uid: str, user_id: str, user_type: int) -> Tuple[bool, str]: 68 | '''根据用户/群关注主播,修改数据库 69 | 70 | Args: 71 | uid (str): 主播的uid 72 | user_id (str): 用户的uid或群号 73 | user_type (int): 0-用户,1-群 74 | 75 | Returns: 76 | Tuple[bool, str]: [是否成功,主播名(uid) | 主播uid(失败原因)] 77 | ''' 78 | 79 | if not uid.isdigit(): 80 | logger.error(f'{__PLUGIN_NAME}存在错误参数 <{uid}>') 81 | return (False, uid + "(错误参数)") 82 | uid = str(int(uid)) 83 | try: 84 | if uid not in bili_task_manager.liver_list: 85 | liver_name, room_id = await bili_client.init_liver_info(uid) 86 | 87 | bili_task_manager.add_liver_info(uid, liver_name, False, room_id) 88 | if user_type == 0: 89 | bili_task_manager.add_user_follower(1, uid, user_id) 90 | else: 91 | bili_task_manager.add_group_follower(1, uid, user_id) 92 | #bili_database.insert_relation(2 + user_type, uid, user_id) 93 | 94 | logger.info(f"{__PLUGIN_NAME}用户/群 <{user_id}> 关注主播 <{liver_name}> 成功") 95 | return (True, liver_name + f"(uid: {uid})") 96 | 97 | if user_type == 0 and user_id in bili_task_manager.liver_list[uid]["user_follower"] or \ 98 | user_type == 1 and user_id in bili_task_manager.liver_list[uid]["group_follower"]: 99 | logger.debug(f'{__PLUGIN_NAME}主播 <{bili_task_manager.liver_list[uid]["liver_name"]}> 已关注') 100 | return (False, uid + "(已关注)") 101 | 102 | if user_type == 0: 103 | bili_task_manager.add_user_follower(1, uid, user_id) 104 | elif user_type == 1: 105 | bili_task_manager.add_group_follower(1, uid, user_id) 106 | 107 | logger.info(f"{__PLUGIN_NAME}用户/群 <{user_id}> 关注主播 <{bili_task_manager.liver_list[uid]['liver_name']}> 成功") 108 | return (True, bili_task_manager.liver_list[uid]["liver_name"] + f"(uid: {uid})") 109 | except (BiliConnectionError, BiliAPIRetCodeError, BiliStatusCodeError): 110 | ex_type, ex_val, _ = sys.exc_info() 111 | exception_msg = f'【错误报告】\n获取主播 <{uid}> B站信息发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 112 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 113 | return (False, uid + "(网络错误)") 114 | except BiliNoLiveRoom: 115 | return (False, uid + "(未开通直播间)") 116 | except BiliDatebaseError: 117 | ex_type, ex_val, _ = sys.exc_info() 118 | exception_msg = f'【错误报告】\n关注主播 <{uid}> 时数据库发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 119 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 120 | return (False, uid + "(数据库错误)") 121 | except Exception: 122 | ex_type, ex_val, _ = sys.exc_info() 123 | exception_msg = f'【错误报告】\n关注主播 <{uid}> 时发生意料之外的错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 124 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 125 | return (False, uid + "(未知错误,请查看日志)") 126 | 127 | async def unfollower_liver(uid: str, user_id: str, user_type: int) -> Tuple[bool, str]: 128 | '''根据用户取关主播,修改主播文件 129 | 130 | Args: 131 | uid (str): 主播的uid 132 | user_id (int):QQ号/群号 133 | user_type (int): 0-个人用户,1-群 134 | 135 | Returns: 136 | Tuple[bool, str]: [是否成功,主播名 | 失败原因] 137 | ''' 138 | if not uid.isdigit(): 139 | logger.error(f'{__PLUGIN_NAME}存在错误参数 <{uid}>') 140 | return (False, uid + "(错误参数)") 141 | try: 142 | # 处理未关注 143 | if uid not in bili_task_manager.liver_list or \ 144 | user_type == 0 and user_id not in bili_task_manager.liver_list[uid]["user_follower"] or \ 145 | user_type == 1 and user_id not in bili_task_manager.liver_list[uid]["group_follower"]: 146 | logger.info(f'{__PLUGIN_NAME}用户/群 <{user_id}> 未关注主播 <{uid}>') 147 | return (False, uid + "(未关注)") 148 | 149 | # 进行取关 150 | liver_name = bili_task_manager.liver_list[uid]["liver_name"] 151 | if user_type == 0: 152 | bili_task_manager.remove_user_follower(1, uid, user_id) 153 | else: 154 | bili_task_manager.remove_group_follower(1, uid, user_id) 155 | 156 | return (True, liver_name + f"(uid: {uid})") 157 | 158 | except BiliDatebaseError: 159 | ex_type, ex_val, _ = sys.exc_info() 160 | exception_msg = f'【错误报告】\n取关主播 <{uid}> 时数据库发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 161 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 162 | return (False, uid + "(数据库错误)") 163 | 164 | async def follow_liver_list( 165 | user_id: int, 166 | uid_list: List[str], 167 | user_type: int 168 | ) -> List[List[str]]: 169 | '''用户/群对主播进行关注 170 | 171 | Args: 172 | user_id (int): 用户qq或群号 173 | uid_list (List[str]): 关注的主播uid 174 | user_type (int): 0-用户, 1-群 175 | 176 | Returns: 177 | List[List[str]]: [[关注成功], [关注失败]] 178 | ''' 179 | 180 | success_list = [] 181 | fail_list = [] 182 | 183 | for uid in uid_list: 184 | isSuccess, s = await follow_liver(uid, str(user_id), user_type) 185 | if isSuccess: 186 | success_list.append(s) 187 | else: 188 | fail_list.append(s) 189 | 190 | return [success_list, fail_list] 191 | 192 | 193 | async def unfollow_liver_list( 194 | user_id: int, 195 | uid_list: List[str], 196 | user_type: int 197 | ) -> List[List[str]]: 198 | '''用户/群对主播取关 199 | 200 | Args: 201 | user_id (int): 用户qq/群号 202 | uid_list (List[str]): 取关主播列表 203 | user_type (int): 0-用户, 1-群 204 | 205 | Returns: 206 | List[List[str]]: [成功列表,失败列表] 207 | ''' 208 | 209 | success_list = [] 210 | fail_list = [] 211 | 212 | for uid in uid_list: 213 | isSuccess, s = await unfollower_liver(uid, str(user_id), user_type) 214 | if isSuccess: 215 | success_list.append(s) 216 | else: 217 | fail_list.append(s) 218 | 219 | return [success_list, fail_list] -------------------------------------------------------------------------------- /bili_src/biliTelegram.py: -------------------------------------------------------------------------------- 1 | from typing import Tuple, List 2 | import sys 3 | import asyncio 4 | import traceback 5 | import nonebot 6 | from nonebot.log import logger 7 | from nonebot.adapters.onebot.v11 import MessageSegment 8 | from .basicFunc import * 9 | from .exception import * 10 | from .bili_client import bili_client 11 | from .bili_task import bili_task_manager 12 | 13 | __PLUGIN_NAME = "[bilibilibot~番剧]" 14 | 15 | async def check_telegram_update(): 16 | """ 17 | @description : 18 | 检查数据库中的每一个影视节目是否更新,如果更新则向用户发送通知,并且更新文件 19 | --------- 20 | @param : 21 | 无 22 | ------- 23 | @Returns : 24 | 无 25 | ------- 26 | """ 27 | telegram_list = list(bili_task_manager.telegram_list.values()) 28 | telegram_list = [i for i in telegram_list if i["is_finish"] is False] 29 | sched_bot = nonebot.get_bot() 30 | # 只对未完结的番剧进行检查 31 | async with httpx.AsyncClient(headers={"User-Agent":"Mozilla/5.0"}) as client: 32 | results = await asyncio.gather( 33 | *[bili_client.get_telegram_latest_episode(client, telegram_info["season_id"], telegram_info["episode"]) for telegram_info in telegram_list], 34 | return_exceptions=True 35 | ) 36 | 37 | for i in range(len(telegram_list)): 38 | if isinstance(results[i], tuple): 39 | if results[i][0] is True: 40 | logger.info(f'[{__PLUGIN_NAME}]检测到影视剧 <{telegram_list[i]["telegram_title"]}> 更新') 41 | text_msg = "【B站动态】\n《{}》已更新\n标题: {}\n链接: {}\n".format( 42 | telegram_list[i]["telegram_title"], results[i][2], results[i][3] 43 | ) 44 | bili_task_manager.update_telegram_info(telegram_list[i]["season_id"], results[i][1], results[i][5]) 45 | cover_msg = MessageSegment.image(results[i][4]) 46 | reported_msg = text_msg + cover_msg 47 | logger.info(f'[{__PLUGIN_NAME}]向关注用户发送更新通知') 48 | 49 | # 通知用户 50 | user_list = telegram_list[i]["user_follower"] 51 | await asyncio.gather(*[sched_bot.send_msg(message=reported_msg, user_id=user_id) for user_id in user_list]) 52 | 53 | group_list = telegram_list[i]["group_follower"] 54 | await asyncio.gather(*[sched_bot.send_msg(message=reported_msg, group_id=group_id) for group_id in group_list]) 55 | 56 | elif isinstance(results[i], (BiliAPIRetCodeError, BiliStatusCodeError, BiliConnectionError)): 57 | exception_msg = f'[错误报告]\n检测番剧 <{telegram_list[i]["telegram_title"]}> 更新情况时发生错误\n错误类型: {type(results[i])}\n错误信息: {results[i]}' 58 | logger.error(f"[{__PLUGIN_NAME}]" + exception_msg) 59 | 60 | async def follow_telegram(id_prefix: str, telegram_id: int, user_id: str, user_type: int) -> Tuple[bool, str]: 61 | '''根据用户关注节目,修改节目的文件 62 | 63 | Args: 64 | id_prefix (str): ep | ss | md 65 | telegram_id (int): 节目的id 66 | user_id (str): 用户的qq号/群号 67 | user_type (int): 0-个人用户,1-群号 68 | 69 | Returns: 70 | Tuple[bool, str]: [是否成功,信息] 71 | ''' 72 | 73 | try: 74 | if id_prefix == "ep": 75 | res = await bili_client.init_telegram_info_by_ep_id(telegram_id) 76 | elif id_prefix == "ss": 77 | res = await bili_client.init_telegram_info_by_season_id(telegram_id) 78 | elif id_prefix == "md": 79 | res = await bili_client.init_telegram_info_by_media_id(telegram_id) 80 | else: 81 | return (False, telegram_id + "(番剧id错误)") 82 | 83 | season_id, telegram_title, episode, is_finish = res 84 | season_id = str(season_id) 85 | logger.debug(f'{__PLUGIN_NAME}{bili_task_manager.telegram_list}') 86 | 87 | if season_id not in bili_task_manager.telegram_list: 88 | logger.debug(f'{__PLUGIN_NAME}{season_id}不在任务列表中存在') 89 | bili_task_manager.add_telegram_info(season_id, telegram_title, episode, is_finish) 90 | if user_type == 0: 91 | bili_task_manager.add_user_follower(2, season_id, user_id) 92 | else: 93 | bili_task_manager.add_group_follower(2, season_id, user_id) 94 | 95 | logger.info(f"[{__PLUGIN_NAME}]用户/群 <{user_id}> 关注番剧 <{telegram_title}> 成功") 96 | return (True, telegram_title + f"(season_id: {season_id})") 97 | 98 | if user_type == 0 and user_id in bili_task_manager.telegram_list[season_id]["user_follower"] or \ 99 | user_type == 1 and user_id in bili_task_manager.telegram_list[season_id]["group_follower"]: 100 | logger.debug(f'{__PLUGIN_NAME}番剧 <{bili_task_manager.telegram_list[season_id]["telegram_title"]}> 已关注') 101 | return (False, season_id + "(已关注)") 102 | 103 | logger.debug(f'{__PLUGIN_NAME}进行关注{season_id}') 104 | 105 | if user_type == 0: 106 | bili_task_manager.add_user_follower(2, season_id, user_id) 107 | else: 108 | bili_task_manager.add_group_follower(2, season_id, user_id) 109 | logger.info(f"[{__PLUGIN_NAME}]用户/群 <{user_id}> 关注番剧 <{telegram_title}> 成功") 110 | return (True, telegram_title + f"(season_id: {season_id})") 111 | 112 | except BiliAPI404Error: 113 | return (False, f"{id_prefix}{telegram_id}" + "(番剧id错误)") 114 | except (BiliConnectionError, BiliAPIRetCodeError, BiliStatusCodeError): 115 | ex_type, ex_val, _ = sys.exc_info() 116 | exception_msg = f'【错误报告】\n获取番剧 <{id_prefix}{telegram_id}> 信息发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 117 | logger.error(f"[{__PLUGIN_NAME}]" + exception_msg + traceback.format_exc()) 118 | return (False, f"{id_prefix}{telegram_id}" + "(网络错误)") 119 | except BiliDatebaseError: 120 | ex_type, ex_val, _ = sys.exc_info() 121 | exception_msg = f'【错误报告】\n关注番剧 <{season_id}> 时数据库发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 122 | logger.error(f"[{__PLUGIN_NAME}]" + exception_msg + traceback.format_exc()) 123 | return (False, season_id + "(数据库错误)") 124 | except Exception: 125 | ex_type, ex_val, _ = sys.exc_info() 126 | exception_msg = f'【错误报告】\n关注番剧 <{id_prefix}{telegram_id}> 时发生意料之外的错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 127 | logger.error(f"[{__PLUGIN_NAME}]" + exception_msg + traceback.format_exc()) 128 | return (False, f"{id_prefix}{telegram_id}" + "(未知错误,请查看日志)") 129 | 130 | async def unfollow_telegram(season_id: str, user_id: str, user_type: int) -> Tuple[bool, str]: 131 | '''根据用户/群取关节目,修改节目文件 132 | 133 | Args: 134 | season_id (str): 节目的ID 135 | user_id (str): 用户qq号/群号 136 | user_type (int): 0-个人用户,1-群号 137 | 138 | Returns: 139 | Tuple[bool, str]: [是否成功, 信息] 140 | ''' 141 | if season_id[:2] != "ss" and not season_id[2:].isdigit(): 142 | return (False, season_id + "(错误参数)") 143 | 144 | season_id = season_id[2:] 145 | logger.debug(f'{__PLUGIN_NAME}{bili_task_manager.telegram_list}') 146 | 147 | try: 148 | if season_id not in bili_task_manager.telegram_list or \ 149 | user_type == 0 and user_id not in bili_task_manager.telegram_list[season_id]["user_follower"] or \ 150 | user_type == 1 and user_id not in bili_task_manager.telegram_list[season_id]["group_follower"]: 151 | logger.info(f'{__PLUGIN_NAME}用户/群 <{user_id}> 未关注番剧 <{season_id}>') 152 | return (False, season_id + "(未关注)") 153 | 154 | telegram_title = bili_task_manager.telegram_list[season_id]["telegram_title"] 155 | if user_type == 0: 156 | bili_task_manager.remove_user_follower(2, season_id, user_id) 157 | else: 158 | bili_task_manager.remove_group_follower(2, season_id, user_id) 159 | 160 | return (True, telegram_title + f"(season_id: ss{season_id})") 161 | 162 | except BiliDatebaseError: 163 | ex_type, ex_val, _ = sys.exc_info() 164 | exception_msg = f'【错误报告】\n取关番剧 <{season_id}> 时数据库发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 165 | logger.error(f"[{__PLUGIN_NAME}]" + exception_msg + traceback.format_exc()) 166 | return (False, season_id + "(数据库错误)") 167 | 168 | async def follow_telegram_list( 169 | user_id: int, 170 | telegram_id_list: List[str], 171 | user_type: int 172 | ) -> List[List[str]]: 173 | '''个人用户/群关注番剧 174 | 175 | Args: 176 | user_id (int): qq号/群号 177 | telegram_id_list (List[str]): 关注的番剧号 178 | user_type (int): 0-个人用户,1-群 179 | 180 | Returns: 181 | List[List[str]]: [是否成功,信息] 182 | ''' 183 | # 在该函数中同时对telegram_id, season_id, media_id进行区分和统一处理 184 | success_list = [] 185 | fail_list = [] 186 | prefix_type = ('ep', 'ss', "md") 187 | valid_telegram_id_list = [] 188 | for telegram_id in telegram_id_list: 189 | logger.debug(f'{__PLUGIN_NAME}telegram_id = {telegram_id}') 190 | 191 | if telegram_id[:2] not in prefix_type or not telegram_id[2:].isdigit(): 192 | fail_list.append(telegram_id + "(错误参数)") 193 | else: 194 | valid_telegram_id_list.append(telegram_id) 195 | logger.debug(f'{__PLUGIN_NAME}valid list = {valid_telegram_id_list}') 196 | 197 | results = await asyncio.gather( 198 | *[follow_telegram(telegram_id[:2], int(telegram_id[2:]), str(user_id), user_type) for telegram_id in valid_telegram_id_list], 199 | return_exceptions=True 200 | ) 201 | 202 | for is_success, msg in results: 203 | if is_success: 204 | success_list.append(msg) 205 | else: 206 | fail_list.append(msg) 207 | 208 | return [success_list, fail_list] 209 | 210 | async def unfollow_telegram_list( 211 | user_id: int, 212 | season_id_list: List[str], 213 | user_type: int 214 | ) -> List[List[str]]: 215 | '''个人用户/群取关番剧 216 | 217 | Args: 218 | user_id (int): qq号/群号 219 | season_id_list (List[str]): 取关的番剧号 220 | user_type (int): 0-个人用户,1-群 221 | 222 | Returns: 223 | List[List[str]]: [是否成功,信息] 224 | ''' 225 | success_list = [] 226 | fail_list = [] 227 | 228 | for season_id in season_id_list: 229 | isSuccess, s = await unfollow_telegram(season_id, str(user_id), user_type) 230 | if isSuccess: 231 | success_list.append(s) 232 | else: 233 | fail_list.append(s) 234 | 235 | return [success_list, fail_list] 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | -------------------------------------------------------------------------------- /bili_src/biliVideo.py: -------------------------------------------------------------------------------- 1 | from .exception import BiliConnectionError, BiliDatebaseError 2 | import asyncio 3 | from typing import Tuple, List 4 | import sys 5 | import traceback 6 | import nonebot 7 | from nonebot.log import logger 8 | from nonebot.adapters.onebot.v11 import MessageSegment 9 | from .basicFunc import * 10 | from .bili_client import bili_client 11 | from .bili_task import bili_task_manager 12 | 13 | __PLUGIN_NAME = "[bilibilibot~视频]" 14 | 15 | # 视频 16 | async def check_up_update() -> None: 17 | """ 18 | @description : 19 | 检查关注UP主是否更新新视频,如果更新则通知用户并写入文件 20 | --------- 21 | @param : 22 | ------- 23 | @Returns : 24 | ------- 25 | """ 26 | schedBot = nonebot.get_bot() 27 | #assert status == True, "数据库发生错误" 28 | check_up_list = bili_task_manager.get_up_check_update_list() 29 | #logger.debug(f'{__PLUGIN_NAME}check_up_list = {check_up_list}') 30 | async with httpx.AsyncClient(headers={"User-Agent":"Mozilla/5.0"}) as client: 31 | results = await asyncio.gather( 32 | *[bili_client.get_latest_video(client, uid, bili_task_manager.up_list[uid]["latest_timestamp"]) for uid in check_up_list], 33 | return_exceptions=True 34 | ) 35 | 36 | for i in range(len(check_up_list)): 37 | if isinstance(results[i], tuple): 38 | if results[i][0] is True: 39 | up_uid = check_up_list[i] 40 | up_name = bili_task_manager.up_list[up_uid]["up_name"] 41 | 42 | logger.info(f'{__PLUGIN_NAME}检测到up主<{up_name}>更新了视频') 43 | textMsg = f"【B站动态】\n <{up_name}> 更新了视频\n标题: {results[i][2]}\n链接: https://www.bilibili.com/video/{results[i][1]}" 44 | 45 | bili_task_manager.update_up_info(up_uid, results[i][3]) 46 | 47 | user_list = bili_task_manager.up_list[up_uid]["user_follower"] 48 | for user_id in user_list: 49 | await schedBot.send_msg(message=textMsg + MessageSegment.image(results[i][4]), user_id=user_id) 50 | 51 | group_list = bili_task_manager.up_list[up_uid]["group_follower"] 52 | for group_id in group_list: 53 | await schedBot.send_msg(message=textMsg + MessageSegment.image(results[i][4]), group_id=group_id) 54 | elif isinstance(results[i], (BiliAPIRetCodeError, BiliStatusCodeError, BiliConnectionError)): 55 | exception_msg = f'[错误报告]\n检测up主 <{check_up_list[i]}> 更新情况时发生错误\n错误类型: {type(results[i])}\n错误信息: {results[i]}' 56 | logger.error(f"[{__PLUGIN_NAME}]" + exception_msg) 57 | 58 | async def follow_up(uid: str, user_id: str, user_type: int) -> Tuple[bool, str]: 59 | '''根据用户或群关注up,修改数据库 60 | 61 | Args: 62 | uid (str): up的uid 63 | user_id (str): 用户的qq或群号 64 | user_type (int): 0-个人用户,1-群 65 | 66 | Returns: 67 | Tuple[bool, str]: [是否成功,信息] 68 | ''' 69 | 70 | # 处理参数错误 71 | if not uid.isdigit(): 72 | logger.error(f'{__PLUGIN_NAME}存在错误参数<{uid}>') 73 | return (False, uid + "(错误参数)") 74 | 75 | uid = str(int(uid)) 76 | 77 | try: 78 | 79 | # up信息不存在于数据库,对数据库进行更新 80 | if uid not in bili_task_manager.up_list: 81 | up_name, latest_timestamp = await bili_client.init_up_info(uid) 82 | 83 | if up_name: 84 | bili_task_manager.add_up_info(uid, up_name, latest_timestamp) 85 | if user_type == 0: 86 | bili_task_manager.add_user_follower(0, uid, user_id) 87 | else: 88 | bili_task_manager.add_group_follower(0, uid, user_id) 89 | 90 | logger.info(f"{__PLUGIN_NAME}用户/群 <{user_id}> 关注主播 <{up_name}> 成功") 91 | return (True, up_name + f"(uid: {uid})") 92 | else: 93 | logger.info(f'{__PLUGIN_NAME}up({uid})不存在,请检查uid') 94 | return (False, uid + "(uid错误)") 95 | up_name = bili_task_manager.up_list[uid]["up_name"] 96 | # 处理已关注 97 | if user_type == 0 and user_id in bili_task_manager.up_list[uid]["user_follower"] or \ 98 | user_type == 1 and user_id in bili_task_manager.up_list[uid]["group_follower"]: 99 | logger.info(f"{__PLUGIN_NAME}用户/群 <{user_id}> 已经关注了up <{up_name}>") 100 | return (False, up_name + "(已关注)") 101 | 102 | # 进行关注 103 | if user_type == 0: 104 | bili_task_manager.add_user_follower(0, uid, user_id) 105 | elif user_type == 1: 106 | bili_task_manager.add_group_follower(0, uid, user_id) 107 | logger.info(f"{__PLUGIN_NAME}用户/群 <{user_id}> 关注up <{up_name}> 成功") 108 | return (True, up_name + f"(uid: {uid})") 109 | except BiliAPI404Error: 110 | return (False, uid + "(uid错误)") 111 | except (BiliConnectionError, BiliAPIRetCodeError, BiliStatusCodeError): 112 | ex_type, ex_val, _ = sys.exc_info() 113 | exception_msg = f'【错误报告】\n获取up主 <{uid}> B站信息发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 114 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 115 | return (False, uid + "(网络错误)") 116 | except BiliDatebaseError: 117 | ex_type, ex_val, _ = sys.exc_info() 118 | exception_msg = f'【错误报告】\n关注up主 <{uid}>时数据库发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 119 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 120 | return (False, uid + "(数据库错误)") 121 | except Exception: 122 | ex_type, ex_val, _ = sys.exc_info() 123 | exception_msg = f'【错误报告】\n关注up主 <{uid}>时发生意料之外的错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 124 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 125 | return (False, uid + "(未知错误,请查看日志)") 126 | 127 | async def unfollow_up(uid: str, user_id: str, user_type: int) -> Tuple[bool, str]: 128 | '''根据个人用户或群取关up主,修改up文件 129 | 130 | Args: 131 | uid (str): up的uid 132 | user_id (str): 用户的qq号/群号 133 | user_type (int): 0-个人用户,1-群 134 | 135 | Returns: 136 | Tuple[bool, str]: [是否成功,信息] 137 | ''' 138 | 139 | # 处理参数错误 140 | if not uid.isdigit(): 141 | logger.error(f'{__PLUGIN_NAME}存在错误参数 <{uid}>') 142 | return (False, uid + "(错误参数)") 143 | try: 144 | # 处理未关注 145 | logger.debug(f"{uid}的关注列表{bili_task_manager.up_list[uid]['user_follower']}") 146 | if uid not in bili_task_manager.up_list or \ 147 | user_type == 0 and user_id not in bili_task_manager.up_list[uid]["user_follower"] or \ 148 | user_type == 1 and user_id not in bili_task_manager.up_list[uid]["group_follower"]: 149 | logger.info(f'{__PLUGIN_NAME}用户/群 <{user_id}> 未关注up <{uid}>') 150 | return (False, uid + "(未关注)") 151 | 152 | # 进行取关 153 | up_name = bili_task_manager.up_list[uid]["up_name"] 154 | if user_type == 0: 155 | bili_task_manager.remove_user_follower(0, uid, user_id) 156 | else: 157 | bili_task_manager.remove_group_follower(0, uid, user_id) 158 | logger.info(f'{__PLUGIN_NAME}用户/群 <{user_id}> 取关up <{uid}> 成功') 159 | 160 | return (True, up_name + f"(uid: {uid})") 161 | except BiliDatebaseError: 162 | ex_type, ex_val, _ = sys.exc_info() 163 | exception_msg = f'【错误报告】\n取关up主 <{uid}> 时数据库发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 164 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 165 | return (False, uid + "(数据库错误)") 166 | 167 | async def follow_up_list( 168 | user_id: int, 169 | uid_list: List[str], 170 | user_type: int 171 | ) -> List[List[str]]: 172 | '''个人用户/群关注up主 173 | 174 | Args: 175 | user_id (int): qq号/群号 176 | uid_list (List[str]): up主的uid 177 | user_type (int): 0-个人用户,1-群 178 | 179 | Returns: 180 | List[List[str]]: [[关注成功列表],[关注失败列表]] 181 | ''' 182 | successList = [] 183 | failList = [] 184 | 185 | for uid in uid_list: 186 | isSuccess, s = await follow_up(uid, str(user_id), user_type) 187 | if isSuccess: 188 | successList.append(s) 189 | else: 190 | failList.append(s) 191 | 192 | return [successList, failList] 193 | 194 | async def unfollow_up_list( 195 | user_id: int, 196 | uid_list: List[str], 197 | user_type: int 198 | ) -> List[List[str]]: 199 | '''个人用户/群取关up主 200 | 201 | Args: 202 | user_id (int): qq号/群号 203 | uid_list (List[str]): 取关的up主 204 | user_type (int): 0-个人用户,1-群 205 | 206 | Returns: 207 | List[List[str]]: [是否成功,信息] 208 | ''' 209 | successList = [] 210 | failList = [] 211 | 212 | for uid in uid_list: 213 | isSuccess, s = await unfollow_up(uid, str(user_id), user_type) 214 | if isSuccess: 215 | successList.append(s) 216 | else: 217 | failList.append(s) 218 | 219 | return [successList, failList] -------------------------------------------------------------------------------- /bili_src/bili_client.py: -------------------------------------------------------------------------------- 1 | import json 2 | import httpx 3 | from random import choice, uniform, randint 4 | import asyncio 5 | import time 6 | from typing import List, Tuple, Dict 7 | from .exception import BiliAPI404Error, BiliAPIRetCodeError, BiliConnectionError, BiliDatebaseError, BiliInvalidRoomId, BiliInvalidShortUrl, BiliNoLiveRoom, BiliStatusCodeError 8 | from nonebot.log import logger 9 | from functools import reduce 10 | from hashlib import md5 11 | import urllib.parse 12 | import uuid 13 | import re 14 | from nonebot import require 15 | 16 | __PLUGIN_NAME__ = "[bilibilibot~Client]" 17 | class BiliClient(): 18 | def __init__(self) -> None: 19 | self.__proxy_pool__ = [None] 20 | self.__retry_times__ = 3 21 | self.__ua_list__ = [ 22 | "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36", 23 | "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/44.0.2403.155 Safari/537.36", 24 | "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36", 25 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36", 26 | "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36", 27 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36", 28 | "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36", 29 | "Mozilla/5.0 (Windows NT 6.4; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36", 30 | "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36", 31 | "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2224.3 Safari/537.36", 32 | "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36", 33 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36", 34 | "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36", 35 | "Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36", 36 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36", 37 | "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36", 38 | "Mozilla/5.0 (X11; OpenBSD i386) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36", 39 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36", 40 | "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36", 41 | "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36", 42 | "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2117.157 Safari/537.36", 43 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36", 44 | "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1866.237 Safari/537.36", 45 | "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/4E423F", 46 | "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36 Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10", 47 | "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36", 48 | "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36", 49 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1664.3 Safari/537.36", 50 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1664.3 Safari/537.36", 51 | "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36", 52 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1623.0 Safari/537.36", 53 | "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36", 54 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36", 55 | "Mozilla/5.0 (X11; CrOS i686 4319.74.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36", 56 | "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36", 57 | "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36", 58 | "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1467.0 Safari/537.36", 59 | "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1464.0 Safari/537.36", 60 | "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1500.55 Safari/537.36", 61 | "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36", 62 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36", 63 | "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36", 64 | "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36", 65 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36", 66 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36", 67 | "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.90 Safari/537.36", 68 | "Mozilla/5.0 (X11; NetBSD) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36", 69 | "Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36", 70 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17", 71 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17" 72 | ] 73 | 74 | self.API = { 75 | # 用于获取临时cookie 76 | "get_bili_cookie": "https://www.bilibili.com", 77 | "get_user_info_by_uid": "https://api.bilibili.com/x/space/wbi/acc/info", 78 | "get_latest_video_by_uid": "https://api.bilibili.com/x/space/wbi/arc/search", 79 | "get_live_info_by_room_id": "https://api.live.bilibili.com/room/v1/Room/get_info", 80 | "get_liver_info_by_uid": "https://api.live.bilibili.com/live_user/v1/Master/info", 81 | "get_telegram_info_by_media_id": "https://api.bilibili.com/pgc/review/user", 82 | "get_telegram_info_by_ep_id": "https://api.bilibili.com/pgc/view/web/season", 83 | "get_telegram_info_by_season_id": "https://api.bilibili.com/pgc/view/web/season", 84 | "get_telegram_latest_episode": "https://api.bilibili.com/pgc/view/web/season", 85 | "get_dynamic_list_by_uid": "https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space", 86 | "get_detail_dynamic_by_id": "https://t.bilibili.com/{}", 87 | "get_api_access": "https://api.bilibili.com/x/internal/gaia-gateway/ExClimbWuzhi", 88 | "get_img_sub_key": "https://api.bilibili.com/x/web-interface/nav", 89 | } 90 | self.headers = {"User-Agent": "Mozilla/5.0"} 91 | self.img_key = self.sub_key = "" 92 | self.get_img_key_and_sub_key() 93 | self.dynamic_cookie = httpx.Cookies() 94 | self.update_dynamic_cookie() 95 | 96 | 97 | def __request_header__(self) -> httpx.Headers: 98 | '''返回随机UA的header 99 | 100 | Returns: 101 | httpx.Headers: 返回header 102 | ''' 103 | headers = { 104 | 'User-Agent': choice(self.__ua_list__), 105 | } 106 | return httpx.Headers(headers) 107 | 108 | async def get_today_img_key_and_sub_key(self) -> Tuple[str, str]: 109 | '''获取今天的img_key和sub_key参数 110 | 111 | Returns: 112 | Tuple[str, str]: (img_key, sub_key) 113 | ''' 114 | try: 115 | async with httpx.AsyncClient(headers=self.headers) as client: 116 | r1 = await client.get("https://api.bilibili.com/x/web-interface/nav") 117 | except Exception as e: 118 | pass 119 | 120 | r1_json = r1.json() 121 | img_url: str = r1_json['data']['wbi_img']['img_url'] 122 | sub_url: str = r1_json['data']['wbi_img']['sub_url'] 123 | 124 | img_key = img_url.rsplit('/', 1)[1].split('.')[0] 125 | sub_key = sub_url.rsplit('/', 1)[1].split('.')[0] 126 | 127 | return img_key, sub_key 128 | 129 | def get_img_key_and_sub_key(self) -> None: 130 | '''获取今天的img_key和sub_key参数 131 | 132 | Returns: 133 | Tuple[str, str]: (img_key, sub_key) 134 | ''' 135 | try: 136 | r1 = httpx.get(self.API["get_img_sub_key"], headers=self.headers) 137 | except Exception as e: 138 | logger.error(f"获取img_key和sub_key时发生错误: {e}") 139 | return "", "" 140 | 141 | r1_json = r1.json() 142 | img_url: str = r1_json['data']['wbi_img']['img_url'] 143 | sub_url: str = r1_json['data']['wbi_img']['sub_url'] 144 | 145 | img_key = img_url.rsplit('/', 1)[1].split('.')[0] 146 | sub_key = sub_url.rsplit('/', 1)[1].split('.')[0] 147 | 148 | self.img_key = img_key 149 | self.sub_key = sub_key 150 | 151 | def get_mixinkey(self, orig: str): 152 | '对 imgKey 和 subKey 进行字符顺序打乱编码' 153 | mixinKeyEncTab = [ 154 | 46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, 155 | 33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40, 156 | 61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11, 157 | 36, 20, 34, 44, 52 158 | ] 159 | return reduce(lambda s, i: s + orig[i], mixinKeyEncTab, '')[:32] 160 | 161 | def wbi_sign(self, params: dict): 162 | '为请求参数进行 wbi 签名' 163 | mixin_key = self.get_mixinkey(self.img_key + self.sub_key) 164 | curr_time = round(time.time()) 165 | params['wts'] = curr_time # 添加 wts 字段 166 | params = dict(sorted(params.items())) # 按照 key 重排参数 167 | # 过滤 value 中的 "!'()*" 字符 168 | params = { 169 | k : ''.join(filter(lambda chr: chr not in "!'()*", str(v))) 170 | for k, v 171 | in params.items() 172 | } 173 | query = urllib.parse.urlencode(params) # 序列化参数 174 | wbi_sign = md5((query + mixin_key).encode()).hexdigest() # 计算 w_rid 175 | params['w_rid'] = wbi_sign 176 | return params 177 | 178 | async def get_latest_video(self, client: httpx.AsyncClient, uid: str, last_udpate_time: int) -> Tuple[bool, str, str, int, str]: 179 | """ 180 | @description : 181 | 根据uid和时间戳, 返回元组,表示存在新视频或无新视频 182 | --------- 183 | @param : 184 | client: httpx.AsyncClient 185 | uid: 查询新视频用户的uid 186 | last_udpate_time: 数据库中记录的最新视频的时间戳 187 | ------- 188 | @Returns : 189 | 返回一个元组[是否更新,bv号,标题,发布时间戳,封面的链接] 190 | ------- 191 | """ 192 | try: 193 | """mid={}&ps=1&tid=0&pn=1&order=pubdate&jsonp=jsonp""" 194 | params = { 195 | "mid": uid, 196 | "ps": 1, 197 | "tid": 0, 198 | "pn": 1, 199 | "order": "pubdate", 200 | "jsonp": "jsonp" 201 | } 202 | response = await client.get(url=self.API["get_latest_video_by_uid"], params=self.wbi_sign(params)) 203 | except Exception as e: 204 | raise BiliConnectionError(0, uid, e.args[0]) 205 | 206 | if response.status_code != 200: 207 | raise BiliStatusCodeError(0, uid, response.status_code) 208 | 209 | response = response.json() 210 | if response["code"] != 0: 211 | raise BiliAPIRetCodeError(0, uid, response["code"], response["message"]) 212 | 213 | #logger.debug(f'{__PLUGIN_NAME__}{json.dumps(response, ensure_ascii=False, indent=4)}') 214 | 215 | latest_video = response['data']['list']['vlist'][0] if len(response['data']['list']['vlist']) != 0 else {} 216 | post_time = latest_video.get("created", 0) 217 | 218 | if post_time > last_udpate_time: 219 | # 发现新视频 220 | title = latest_video['title'] 221 | bvID = latest_video['bvid'] 222 | cover_url = latest_video['pic'] 223 | return (True, bvID, title, post_time, cover_url) 224 | 225 | return (False, '', '', 0, '') 226 | 227 | async def init_up_info(self, uid: str) -> Tuple[str, int]: 228 | """ 229 | @description : 230 | 根据uid查询up主信息 231 | --------- 232 | @param : 233 | uid: 用户的uid 234 | ------- 235 | @Returns : 236 | 返回一个元组 237 | [up主名字,最新视频的时间戳] 238 | ------- 239 | """ 240 | 241 | async with httpx.AsyncClient(headers=self.headers) as client: 242 | try: 243 | response1 = await client.get(url=self.API["get_user_info_by_uid"], params=self.wbi_sign({"mid": uid})) 244 | except Exception as e: 245 | raise BiliConnectionError(0, uid, e.args[0]) 246 | 247 | if response1.status_code != 200: 248 | raise BiliStatusCodeError(0, uid, response1.status_code) 249 | 250 | response1 = response1.json() 251 | if response1["code"] == -404: 252 | raise BiliAPI404Error() 253 | elif response1["code"] != 0: 254 | raise BiliAPIRetCodeError(0, uid, response1["code"], response1["message"]) 255 | 256 | user_name = response1["data"]["name"] 257 | try: 258 | response2 = await client.get(url=self.API["get_latest_video_by_uid"], params=self.wbi_sign({"mid": uid,"ps": 1,"tid": 0,"pn": 1,"order": "pubdate","jsonp": "jsonp"})) 259 | except Exception as e: 260 | raise BiliConnectionError(0, uid, e.args[0]) 261 | 262 | if response2.status_code != 200: 263 | raise BiliStatusCodeError(0, uid, response2.status_code) 264 | 265 | response2 = response2.json() 266 | if response2["code"] != 0: 267 | raise BiliAPIRetCodeError(0, uid, response2["code"], response2["message"]) 268 | 269 | latest_video = response2['data']['list']['vlist'][0] if len(response2['data']['list']['vlist']) != 0 else {} 270 | post_time = latest_video.get("created", 0) 271 | 272 | return (user_name, post_time) 273 | 274 | async def get_live_status(self, client:httpx.AsyncClient, uid: str, room_id: str) -> Tuple[bool, str, str]: 275 | """ 276 | @description : 277 | 根据房间号,获取直播间是否开播 278 | --------- 279 | @param : 280 | uid: 主播的uid 281 | room_id: 直播间号 282 | ------- 283 | @Returns : 284 | 返回一个元组 285 | [是否正在直播,直播间标题,直播间封面链接] 286 | ------- 287 | """ 288 | 289 | try: 290 | response = await client.get(self.API["get_live_info_by_room_id"], params={"room_id": room_id}) 291 | except Exception as e: 292 | raise BiliConnectionError(1, uid, e.args[0]) 293 | 294 | if response.status_code != 200: 295 | raise BiliStatusCodeError(1, uid, response.status_code) 296 | 297 | response = response.json() 298 | if response["code"] != 0: 299 | raise BiliAPIRetCodeError(1, uid, response["code"], response["message"]) 300 | 301 | live_status = response["data"]["live_status"] 302 | title = response["data"]["title"] 303 | cover_url = response["data"]["user_cover"] 304 | 305 | if live_status == 1: 306 | return (True, title, cover_url) 307 | else: 308 | return (False, "", "") 309 | 310 | async def init_liver_info(self, uid: str) -> Tuple[str, str]: 311 | """ 312 | @description : 313 | 根据uid,返回直播间信息 314 | --------- 315 | @param : 316 | uid: 用户的uid 317 | ------- 318 | @Returns : 319 | 返回一个元组 320 | [用户名,直播间房间号] 321 | ------- 322 | """ 323 | 324 | async with httpx.AsyncClient(headers=self.headers) as client: 325 | try: 326 | response = await client.get(self.API["get_liver_info_by_uid"], params={"uid": uid}) 327 | except Exception as e: 328 | raise BiliConnectionError(0, uid, e.args[0]) 329 | 330 | if response.status_code != 200: 331 | raise BiliStatusCodeError(1, uid, response.status_code) 332 | 333 | response = response.json() 334 | if response["code"] != 0: 335 | raise BiliAPIRetCodeError(1, uid, response["code"], response["message"]) 336 | 337 | liver_name = response["data"]["info"]["uname"] 338 | room_id = response["data"]["room_id"] 339 | 340 | if room_id == 0: 341 | raise BiliNoLiveRoom(liver_name) 342 | 343 | return (liver_name, room_id) 344 | 345 | async def init_liver_info_by_room_id(self, room_id: str) -> Tuple[str, str]: 346 | '''根据直播房间号获得主播uid和主播用户名 347 | 348 | Args: 349 | room_id (str): 直播房间号 350 | 351 | Returns: 352 | Tuple[str, str]: 353 | 返回一个元组 354 | [主播uid, 主播用户名] 355 | ''' 356 | 357 | async with httpx.AsyncClient(headers=self.headers) as client: 358 | try: 359 | response1 = await client.get(url=self.API["get_live_info_by_room_id"], params={"room_id": room_id}) 360 | except Exception as e: 361 | raise BiliConnectionError(1, f"房间号:{room_id}", e.args[0]) 362 | 363 | if response1.status_code != 200: 364 | raise BiliStatusCodeError(1, f"房间号:{room_id}", response1.status_code) 365 | 366 | response1 = response1.json() 367 | if response1["code"] == 1: 368 | raise BiliInvalidRoomId(room_id) 369 | if response1["code"] != 0: 370 | raise BiliAPIRetCodeError(1, f"房间号:{room_id}", response1["code"], response1["message"]) 371 | 372 | uid = str(response1["data"]["uid"]) 373 | 374 | try: 375 | response2 = await client.get(self.API["get_liver_info_by_uid"], params={"uid": uid}) 376 | except Exception as e: 377 | raise BiliConnectionError(0, uid, e.args[0]) 378 | 379 | if response2.status_code != 200: 380 | raise BiliStatusCodeError(1, uid, response2.status_code) 381 | 382 | response2 = response2.json() 383 | if response2["code"] != 0: 384 | raise BiliAPIRetCodeError(1, uid, response2["code"], response2["message"]) 385 | 386 | liver_name = response2["data"]["info"]["uname"] 387 | 388 | return (uid, liver_name) 389 | 390 | async def init_telegram_info_by_ep_id(self, ep_id: int) -> Tuple[int, str, int, bool]: 391 | ''' 根据ep_id初始化番剧信息 392 | 393 | Args: 394 | ep_id (int): ep_id 395 | 396 | Returns: 397 | Tuple[int, str, int, bool]: 398 | 返回一个元组 399 | [season_id, 番剧名, 最新集, 是否完结] 400 | ''' 401 | 402 | async with httpx.AsyncClient(headers=self.headers) as client: 403 | try: 404 | response = await client.get(url=self.API["get_telegram_info_by_ep_id"], params={"ep_id": ep_id}) 405 | except Exception as e: 406 | raise BiliConnectionError(2, f"ep_id:{ep_id}", e.args[0]) 407 | 408 | if response.status_code != 200: 409 | raise BiliStatusCodeError(2, f"ep_id:{ep_id}", response.status_code) 410 | 411 | response = response.json() 412 | if response["code"] == -404: 413 | raise BiliAPI404Error() 414 | elif response["code"] != 0: 415 | raise BiliAPIRetCodeError(2, f"ep_id:{ep_id}", response["code"], response["message"]) 416 | 417 | season_id = response["result"]["season_id"] 418 | season_title = response["result"]["season_title"] 419 | is_finish = bool(response["result"]["publish"]["is_finish"]) 420 | latest_episode = len(response["result"]["episodes"]) 421 | 422 | return (season_id, season_title, latest_episode, is_finish) 423 | 424 | async def init_telegram_info_by_season_id(self, season_id: int) -> Tuple[int, str, int, bool]: 425 | ''' 根据season_id初始化番剧信息 426 | 427 | Args: 428 | season_id (int): season_id 429 | 430 | Returns: 431 | Tuple[int, str, int, bool]: 432 | 返回一个元组 433 | [season_id, 番剧名, 最新集, 是否完结] 434 | ''' 435 | 436 | async with httpx.AsyncClient(headers=self.headers) as client: 437 | try: 438 | response = await client.get(url=self.API["get_telegram_info_by_season_id"], params={"season_id": season_id}) 439 | except Exception as e: 440 | raise BiliConnectionError(2, f"season_id:{season_id}", e.args[0]) 441 | 442 | if response.status_code != 200: 443 | raise BiliStatusCodeError(2, f"season_id:{season_id}", response.status_code) 444 | 445 | response = response.json() 446 | if response["code"] == -404: 447 | raise BiliAPI404Error() 448 | elif response["code"] != 0: 449 | raise BiliAPIRetCodeError(2, f"season_id:{season_id}", response["code"], response["message"]) 450 | 451 | season_title = response["result"]["title"] 452 | is_finish = bool(response["result"]["publish"]["is_finish"]) 453 | latest_episode = len(response["result"]["episodes"]) 454 | 455 | return (season_id, season_title, latest_episode, is_finish) 456 | 457 | async def init_telegram_info_by_media_id(self, media_id: int) -> Tuple[int, str, int, bool]: 458 | ''' 根据media_id初始化番剧信息 459 | 460 | Args: 461 | media_id (int): media_id 462 | 463 | Returns: 464 | Tuple[int, str, int, bool]: 465 | 返回一个元组 466 | [season_id, 番剧名, 最新集, 是否完结] 467 | ''' 468 | 469 | async with httpx.AsyncClient(headers=self.headers) as client: 470 | try: 471 | response = await client.get(url=self.API["get_telegram_info_by_media_id"], params={"media_id": media_id}) 472 | except Exception as e: 473 | raise BiliConnectionError(2, f"media_id:{media_id}", e.args[0]) 474 | 475 | if response.status_code != 200: 476 | raise BiliStatusCodeError(2, f"media_id:{media_id}", response.status_code) 477 | 478 | response = response.json() 479 | if response["code"] == -404: 480 | raise BiliAPI404Error() 481 | elif response["code"] != 0: 482 | raise BiliAPIRetCodeError(2, f"media_id:{media_id}", response["code"], response["message"]) 483 | 484 | season_id = response["result"]["media"]["season_id"] 485 | season_title = response["result"]["media"]["title"] 486 | is_finish = False 487 | latest_episode = int(response["result"]["media"]["new_ep"]["index"]) 488 | 489 | return (season_id, season_title, latest_episode, is_finish) 490 | 491 | async def get_telegram_latest_episode(self, client: httpx.AsyncClient, season_id: int, latest_timestamp: int) -> Tuple[bool, int, str, str, str, bool]: 492 | '''根据season_id获取番剧的最新集信息 493 | 494 | Args: 495 | season_id (int): season_id 496 | latest_timestamp (int): 记录的最新集数的发布时间 497 | 498 | Returns: 499 | Tuple[bool, int, str, str, str, bool]: 500 | 返回一个元组 501 | [是否更新, 最新集数发布时间, 最新集标题, 最新集链接, 封面链接, 是否完结] 502 | ''' 503 | 504 | 505 | try: 506 | response = await client.get(url=self.API["get_telegram_latest_episode"], params={"season_id": season_id}) 507 | except Exception as e: 508 | raise BiliConnectionError(2, f"season_id:{season_id}", e.args[0]) 509 | 510 | if response.status_code != 200: 511 | raise BiliStatusCodeError(2, f"season_id:{season_id}", response.status_code) 512 | 513 | response = response.json() 514 | if response["code"] != 0: 515 | raise BiliAPIRetCodeError(2, f"season_id:{season_id}", response["code"], response["message"]) 516 | 517 | episodes = response['result']['episodes'] 518 | is_finish = bool(response["result"]["publish"]["is_finish"]) 519 | 520 | latest_episode = episodes[-1] 521 | if latest_episode["pub_time"] > latest_timestamp: 522 | # 有更新 523 | cover_url = latest_episode['cover'] 524 | title = latest_episode['share_copy'] 525 | play_url = latest_episode['share_url'] 526 | return (True, latest_episode["pub_time"], title, play_url, cover_url, is_finish) 527 | else: 528 | return (False, 0, "", "", "", is_finish) 529 | 530 | async def parse_short_url(self, short_url: str) -> Tuple[int, str]: 531 | '''解析b23.tv的短链接,返回短链接类型以及目标id 532 | 533 | Args: 534 | short_url (str): 短链接 535 | 536 | Returns: 537 | Tuple[int, str]: 538 | 返回一个元组 539 | [类型:0-up的uid,1-主播的房间号,2-番剧的ep_id, 目标id] 540 | ''' 541 | async with httpx.AsyncClient(headers=self.headers) as client: 542 | try: 543 | response = await client.get(url=short_url) 544 | except Exception as e: 545 | raise BiliConnectionError(3, short_url, e.args[0]) 546 | 547 | if response.status_code != 302: 548 | raise BiliStatusCodeError(3, short_url, response.status_code) 549 | 550 | origin_url = response.headers["Location"].split("?")[0] 551 | logger.debug(f"get origin url = {origin_url}") 552 | target_id = origin_url.split("/")[-1] 553 | if "space" in origin_url: 554 | return (0, target_id) 555 | elif "live" in origin_url: 556 | return (1, target_id) 557 | elif "bangumi" in origin_url: 558 | return (2, target_id) 559 | 560 | raise BiliInvalidShortUrl(short_url) 561 | 562 | def update_dynamic_cookie(self) -> None: 563 | headers = { 564 | 'user-agent': 'Mozilla/5.0', 565 | 'referer': 'https://www.bilibili.com/', 566 | "Host": "space.bilibili.com", 567 | 'accept-language': 'zh-CN,zh;q=0.9', 568 | } 569 | with httpx.Client(headers=headers) as client: 570 | cookies = httpx.Cookies({'_uuid': f'{str(uuid.uuid4()).upper()}{randint(0, 99999):05d}infoc'}) 571 | 572 | response = client.get( 573 | 'https://www.bilibili.com/1/dynamic', 574 | cookies=cookies 575 | ) 576 | spm_prefix = re.search(r'', response.text).group(1) 577 | cookies.update(response.cookies) 578 | 579 | data = { 580 | '3064': 1, 581 | '39c8': f'{spm_prefix}.fp.risk', 582 | '3c43': { 583 | 'adca': 'Linux', 584 | }, 585 | # 'df35': cookies['_uuid'] 586 | } 587 | 588 | json_data = { 589 | 'payload': json.dumps(data, separators=(',', ':')) 590 | } 591 | 592 | response = client.post( 593 | 'https://api.bilibili.com/x/internal/gaia-gateway/ExClimbWuzhi', 594 | cookies=cookies, 595 | json=json_data, 596 | ) 597 | 598 | self.dynamic_cookie.clear() 599 | self.dynamic_cookie.update(cookies) 600 | 601 | async def init_dynamic_info(self, uid: str) -> Tuple[str, str, int]: 602 | """ 603 | @description : 604 | 根据uid查询动态主信息 605 | --------- 606 | @param : 607 | uid: 用户的uid 608 | ------- 609 | @Returns : 610 | 返回一个元组 611 | [动态主名字,置顶动态id, 最新动态的时间戳] 612 | ------- 613 | """ 614 | 615 | async with httpx.AsyncClient(headers=self.headers) as client: 616 | try: 617 | response1 = await client.get(url=self.API["get_user_info_by_uid"], params=self.wbi_sign({"mid": uid})) 618 | except Exception as e: 619 | raise BiliConnectionError(4, uid, e.args[0]) 620 | 621 | if response1.status_code != 200: 622 | raise BiliStatusCodeError(4, uid, response1.status_code) 623 | 624 | response1 = response1.json() 625 | if response1["code"] == -404: 626 | raise BiliAPI404Error() 627 | elif response1["code"] != 0: 628 | raise BiliAPIRetCodeError(3, uid, response1["code"], response1["message"]) 629 | 630 | user_name = response1["data"]["name"] 631 | 632 | try: 633 | response2 = await client.get( 634 | url=self.API["get_dynamic_list_by_uid"], 635 | params={ 636 | "host_mid": uid, 637 | "timezone_offset": -480 638 | }, 639 | cookies=self.dynamic_cookie, 640 | ) 641 | except Exception as e: 642 | raise BiliConnectionError(4, uid, e.args[0]) 643 | 644 | if response2.status_code != 200: 645 | raise BiliStatusCodeError(4, uid, response2.status_code) 646 | 647 | response2 = response2.json() 648 | if response2["code"] != 0: 649 | raise BiliAPIRetCodeError(3, uid, response2["code"], response2["message"]) 650 | 651 | news_list: List = response2["data"]["items"] 652 | 653 | if len(news_list) == 0: 654 | return (user_name, "", 0) 655 | 656 | top_news = news_list[0] 657 | top_news_id = "" 658 | if "module_tag" in top_news["modules"] and top_news["modules"]["module_tag"]["text"] == "置顶": 659 | # 如果存在置顶动态,判断与数据库中记录的是否相同 660 | top_news_id = top_news["id_str"] 661 | latest_timestamp = top_news["modules"]["module_author"]["pub_ts"] 662 | 663 | if len(news_list) == 1: 664 | return (user_name, top_news_id, latest_timestamp) 665 | 666 | latest_timestamp = news_list[1]["modules"]["module_author"]["pub_ts"] 667 | return (user_name, top_news_id, latest_timestamp) 668 | else: 669 | latest_timestamp = news_list[0]["modules"]["module_author"]["pub_ts"] 670 | return (user_name, "", latest_timestamp) 671 | 672 | async def get_latest_dynamic(self, client: httpx.AsyncClient, uid: str, pin_news_id: str, latest_timestamp: int) -> Tuple[bool, bool, str, List[Dict]]: 673 | '''根据uid获取最新的动态 674 | 675 | Args: 676 | uid (str): 查询的uid 677 | pin_news_id (str): 数据库中记录置顶动态的id,无则返为"" 678 | latest_timestamp (int): 数据库中记录最新动态的时间戳 679 | 680 | Returns: 681 | Tuple[bool, bool, str, Dict]: 682 | bool - 置顶动态是否更新 683 | bool - 是否有更新的动态 684 | str - 置顶动态id 685 | List[Dict] - 更新的动态内容 686 | ''' 687 | try: 688 | response = await client.get( 689 | url=self.API["get_dynamic_list_by_uid"], 690 | params={ 691 | "host_mid": uid, 692 | "timezone_offset": -480 693 | }, 694 | cookies=self.dynamic_cookie, 695 | ) 696 | except Exception as e: 697 | raise BiliConnectionError(4, f"uid:{uid}", e.args[0]) 698 | 699 | if response.status_code != 200: 700 | raise BiliStatusCodeError(4, f"uid:{uid}", response.status_code) 701 | response = response.json() 702 | if response["code"] != 0: 703 | raise BiliAPIRetCodeError(3, uid, response["code"], response["message"]) 704 | 705 | news_list: List = response["data"]["items"] 706 | 707 | # 如果动态列表长度为0则直接返回 708 | if len(news_list) == 0: 709 | return (False, False, "", []) 710 | 711 | update_news_list = [] 712 | has_pin_news = 0 713 | top_news_change = False 714 | top_news_id = "" 715 | 716 | # 处理置顶动态 717 | top_news = news_list[0] 718 | if "module_tag" in top_news["modules"] and top_news["modules"]["module_tag"]["text"] == "置顶": 719 | # 如果存在置顶动态,判断与数据库中记录的是否相同 720 | has_pin_news = 1 721 | top_news_id: str = top_news["id_str"] 722 | 723 | if top_news_id == pin_news_id: 724 | # 如果相同,则跳过该动态 725 | pass 726 | else: 727 | # 如果置顶动态发生更换 728 | # 处理置顶旧动态的情况,如果置顶旧动态,则不需要更新通知; 729 | #logger.debug(f'{__PLUGIN_NAME__} <{uid}> 的置顶动态发生变化,由{pin_news_id}变为{top_news_id}') 730 | top_news_change = True 731 | major_module: Dict = top_news["modules"]["module_dynamic"]["major"] 732 | additional_module: Dict = top_news["modules"]["module_dynamic"]["additional"] 733 | desc_module: Dict = top_news["modules"]["module_dynamic"]["desc"] 734 | top_news_timestamp: int = top_news["modules"]["module_author"]["pub_ts"] 735 | top_news_major_type = major_module["type"] if major_module else "MAJOR_TYPE_NONE" 736 | top_news_add_type = additional_module["type"] if additional_module else "ADDITIONAL_TYPE_NONE" 737 | 738 | if top_news_timestamp > latest_timestamp: 739 | # 置顶动态是新动态,需要通知 740 | temp_news = {} 741 | temp_news["tag"] = "NORMAL" 742 | temp_news["major_type"] = top_news_major_type 743 | temp_news["add_type"] = top_news_add_type 744 | temp_news["news_id"] = top_news_id 745 | temp_news["text"] = desc_module["text"] if desc_module else "" 746 | temp_news["image"] = [] 747 | temp_news["timestamp"] = top_news_timestamp 748 | 749 | if top_news_major_type == "MAJOR_TYPE_DRAW": 750 | for image_item in major_module["draw"]["items"]: 751 | temp_news["image"].append(image_item["src"]) 752 | elif top_news_major_type == "MAJOR_TYPE_ARTICLE": 753 | temp_news["tag"] = "MAJOR_ARTICLE" 754 | temp_news["article"] = { 755 | "title": major_module["article"]["title"], 756 | "cover": major_module["article"]["covers"][0], 757 | "jump_url": major_module["article"]["jump_url"] 758 | } 759 | if top_news_add_type == "ADDITIONAL_TYPE_VOTE": 760 | temp_news["tag"] = "ADD_VOTE" 761 | temp_news["vote"] = { 762 | "desc": additional_module["vote"]["desc"], 763 | "vote_id": additional_module["vote"]["vote_id"] 764 | } 765 | elif top_news_add_type == "ADDITIONAL_TYPE_UGC": 766 | temp_news["tag"] = "ADD_UGC" 767 | temp_news["tgc"] = { 768 | "title" : additional_module["ugc"]["title"], 769 | "jump_url": additional_module["ugc"]["jump_url"], 770 | "cover": additional_module["ugc"]["cover"] 771 | } 772 | elif top_news_add_type == "ADDITIONAL_TYPE_RESERVE": 773 | temp_news["tag"] = "ADD_RESERVE" 774 | temp_news["reserve"] = { 775 | "title": additional_module["reserve"]["title"], 776 | "desc": additional_module["reserve"]["desc1"]["text"] 777 | } 778 | 779 | update_news_list.append(temp_news) 780 | 781 | # 处理置顶动态外的动态 782 | for news_item in news_list[has_pin_news:]: 783 | news_id = news_item["id_str"] 784 | major_module: Dict = news_item["modules"]["module_dynamic"]["major"] 785 | additional_module: Dict = news_item["modules"]["module_dynamic"]["additional"] 786 | desc_module: Dict = news_item["modules"]["module_dynamic"]["desc"] 787 | news_item_major_type = major_module["type"] if major_module else "MAJOR_TYPE_NONE" 788 | news_item_add_type = additional_module["type"] if additional_module else "ADDITIONAL_TYPE_NONE" 789 | news_timestamp = news_item["modules"]["module_author"]["pub_ts"] 790 | 791 | if news_timestamp <= latest_timestamp: 792 | break 793 | # 发现新动态 794 | logger.debug(f'{__PLUGIN_NAME__} <{uid}> 更新了动态 {news_id}') 795 | temp_news = {} 796 | temp_news["tag"] = "NORMAL" 797 | temp_news["news_id"] = news_id 798 | temp_news["major_type"] = news_item_major_type 799 | temp_news["add_type"] = news_item_add_type 800 | temp_news["text"] = desc_module["text"] if desc_module else "" 801 | temp_news["image"] = [] 802 | temp_news["timestamp"] = news_timestamp 803 | 804 | if news_item_major_type == "MAJOR_TYPE_DRAW": 805 | for image_item in major_module["draw"]["items"]: 806 | temp_news["image"].append(image_item["src"]) 807 | elif news_item_major_type == "MAJOR_TYPE_ARTICLE": 808 | temp_news["tag"] = "MAJOR_ARTICLE" 809 | temp_news["article"] = { 810 | "title": major_module["article"]["title"], 811 | "cover": major_module["article"]["covers"][0], 812 | "jump_url": major_module["article"]["jump_url"] 813 | } 814 | if news_item_add_type == "ADDITIONAL_TYPE_VOTE": 815 | temp_news["tag"] = "ADD_VOTE" 816 | temp_news["vote"] = { 817 | "desc": additional_module["vote"]["desc"], 818 | "vote_id": additional_module["vote"]["vote_id"] 819 | } 820 | elif news_item_add_type == "ADDITIONAL_TYPE_UGC": 821 | temp_news["tag"] = "ADD_UGC" 822 | temp_news["tgc"] = { 823 | "title" : additional_module["ugc"]["title"], 824 | "jump_url": additional_module["ugc"]["jump_url"], 825 | "cover": additional_module["ugc"]["cover"] 826 | } 827 | elif news_item_add_type == "ADDITIONAL_TYPE_RESERVE": 828 | temp_news["tag"] = "ADD_RESERVE" 829 | temp_news["reserve"] = { 830 | "title": additional_module["reserve"]["title"], 831 | "desc": additional_module["reserve"]["desc1"]["text"] 832 | } 833 | elif news_item_add_type == "ADDITIONAL_TYPE_MATCH": 834 | temp_news["tag"] = "ADD_MATCH" 835 | temp_news["match"] = { 836 | "match_title": additional_module["match"]["head_text"], 837 | "jump_url": additional_module["match"]["button"]["jump_url"] 838 | } 839 | 840 | update_news_list.append(temp_news) 841 | 842 | return (top_news_change, bool(len(update_news_list)), top_news_id, update_news_list) 843 | 844 | def web_request(self, url: str) -> httpx.Response: 845 | '''根据url进行网络访问 846 | 847 | Args: 848 | url (str): 目标网站 849 | 850 | Returns: 851 | httpx.Response: 返回响应 852 | ''' 853 | with httpx.Client(headers=self.headers) as client: 854 | try: 855 | response = client.get(url=url) 856 | except Exception as e: 857 | logger.debug(f'出现网络连接错误:{e}') 858 | return None 859 | else: 860 | return response 861 | 862 | 863 | bili_client = BiliClient() 864 | require("nonebot_plugin_apscheduler") 865 | from nonebot_plugin_apscheduler import scheduler 866 | scheduler.add_job(bili_client.update_dynamic_cookie, "interval", hours=12, id="update_dynamic_cookie", misfire_grace_time=90) 867 | scheduler.add_job(bili_client.get_img_key_and_sub_key, "interval", minutes=10, id="update_img_sub_key",misfire_grace_time=90) 868 | -------------------------------------------------------------------------------- /bili_src/bili_dynamic.py: -------------------------------------------------------------------------------- 1 | from .exception import BiliConnectionError, BiliDatebaseError 2 | import asyncio 3 | from typing import Tuple, List 4 | import sys 5 | import traceback 6 | import nonebot 7 | from nonebot.log import logger 8 | from nonebot.adapters.onebot.v11 import Message, MessageSegment 9 | from .basicFunc import * 10 | from .bili_client import bili_client 11 | from .bili_task import bili_task_manager 12 | from datetime import datetime 13 | import json 14 | 15 | __PLUGIN_NAME = "[bilibilibot~动态]" 16 | 17 | # 视频 18 | async def check_dynamic_update() -> None: 19 | """ 20 | @description : 21 | 检查关注动态主是否更新新视频,如果更新则通知用户并更新缓存和数据库 22 | --------- 23 | @param : 24 | ------- 25 | @Returns : 26 | ------- 27 | """ 28 | schedBot = nonebot.get_bot() 29 | check_dynamic_list = bili_task_manager.get_dynamic_check_update_list() 30 | #logger.debug(f'{__PLUGIN_NAME}check_dynamic_list = {check_dynamic_list}') 31 | async with httpx.AsyncClient(headers={"User-Agent":"Mozilla/5.0"}) as client: 32 | results = await asyncio.gather( 33 | *[bili_client.get_latest_dynamic( 34 | client, 35 | uid, 36 | bili_task_manager.dynamic_list[uid]["pin_id_str"], 37 | bili_task_manager.dynamic_list[uid]["latest_timestamp"] 38 | ) for uid in check_dynamic_list], 39 | return_exceptions=True 40 | ) 41 | 42 | for i in range(len(check_dynamic_list)): 43 | if isinstance(results[i], tuple): 44 | uid = check_dynamic_list[i] 45 | u_name = bili_task_manager.dynamic_list[uid]["u_name"] 46 | if results[i][0] is True: 47 | # 修改缓存中的置顶动态信息 48 | logger.debug(f'{__PLUGIN_NAME}原置顶动态为{bili_task_manager.dynamic_list[uid]["pin_id_str"]}') 49 | logger.debug(f'{__PLUGIN_NAME}新置顶动态为{results[i][2]}') 50 | 51 | 52 | logger.debug(f'{__PLUGIN_NAME}检测到<{u_name}>置顶动态发生了改变') 53 | bili_task_manager.update_dynamic_pin_id(uid, results[i][2]) 54 | if results[i][1] is True: 55 | latest_timestamp = 0 56 | 57 | logger.info(f'{__PLUGIN_NAME}检测到<{u_name}>更新了动态') 58 | logger.debug(json.dumps(results[i][3], ensure_ascii=False, indent=4)) 59 | 60 | info_msg = Message() 61 | text_msg = f"【B站动态】\n<{u_name}>更新了动态:\n" 62 | info_msg.append(text_msg) 63 | 64 | 65 | for dynamic_item in results[i][3]: 66 | latest_timestamp = max(latest_timestamp, dynamic_item["timestamp"]) 67 | if dynamic_item["major_type"] in ("MAJOR_TYPE_DRAW", "MAJOR_TYPE_NONE", "MAJOR_TYPE_ARTICLE"): 68 | dynamic_msg = Message() 69 | dynamic_msg.append(MessageSegment.text("=============\n")) 70 | 71 | # add time 72 | dynamic_msg.append(MessageSegment.text( 73 | "--" + 74 | datetime.fromtimestamp(dynamic_item["timestamp"]).strftime("%Y-%m-%d %H:%M") 75 | + "--\n")) 76 | # add text 77 | dynamic_msg.append(MessageSegment.text(dynamic_item["text"] + "\n")) 78 | 79 | # add image 80 | for img_src in dynamic_item["image"]: 81 | dynamic_msg.append(MessageSegment.image(img_src)) 82 | 83 | # 如果是专栏,添加专栏信息 84 | if dynamic_item["tag"] == "MAJOR_ARTICLE": 85 | dynamic_msg.append( 86 | MessageSegment.text( 87 | f'\n<{u_name}>发布了专栏 《{dynamic_item["article"]["title"]}》\n\ 88 | 链接: {dynamic_item["article"]["jump_url"]}\n' 89 | ) 90 | ) 91 | dynamic_msg.append(MessageSegment.image(dynamic_item["article"]["cover"])) 92 | # 添加附加信息 93 | # 附加信息是投票 94 | elif dynamic_item["tag"] == "ADD_VOTE": 95 | dynamic_msg.append(MessageSegment.text("----附加信息----\n")) 96 | dynamic_msg.append(MessageSegment.text(f'<{u_name}>发布了投票: <{dynamic_item["vote"]["desc"]}>\n')) 97 | # 附加信息是视频 98 | elif dynamic_item["tag"] == "ADD_UGC": 99 | dynamic_msg.append(MessageSegment.text("----附加信息----\n")) 100 | dynamic_msg.append(MessageSegment.text(f'标题: {dynamic_item["tgc"]["title"]}\n链接: {dynamic_item["tgc"]["jump_url"]}\n')) 101 | dynamic_msg.append(MessageSegment.image(dynamic_item["tgc"]["cover"])) 102 | # 附加信息是预约 103 | elif dynamic_item["tag"] == "ADD_RESERVE": 104 | dynamic_msg.append(MessageSegment.text("----附加信息----\n")) 105 | dynamic_msg.append(MessageSegment.text(f'<{u_name}>发布了预约: <{dynamic_item["reserve"]["title"]}>\n{dynamic_item["reserve"]["desc"]}\n')) 106 | dynamic_msg.append(MessageSegment.text("=============\n")) 107 | 108 | info_msg.extend(dynamic_msg) 109 | bili_task_manager.update_dynamic_latest_timestamp(uid, latest_timestamp) 110 | 111 | if len(info_msg) != 1: 112 | user_list = bili_task_manager.dynamic_list[uid]["user_follower"] 113 | for user_id in user_list: 114 | await schedBot.send_msg(message=info_msg, user_id=user_id) 115 | 116 | group_list = bili_task_manager.dynamic_list[uid]["group_follower"] 117 | for group_id in group_list: 118 | await schedBot.send_msg(message=info_msg, group_id=group_id) 119 | 120 | elif isinstance(results[i], (BiliAPIRetCodeError, BiliStatusCodeError, BiliConnectionError)): 121 | exception_msg = f'[错误报告]\n检测动态主 <{check_dynamic_list[i]}> 更新情况时发生错误\n错误类型: {type(results[i])}\n错误信息: {results[i]}' 122 | logger.error(f"[{__PLUGIN_NAME}]" + exception_msg) 123 | 124 | async def follow_dynamic(uid: str, user_id: str, user_type: int) -> Tuple[bool, str]: 125 | '''根据用户或群关注动态主,修改数据库 126 | 127 | Args: 128 | uid (str): 动态主的uid 129 | user_id (str): 用户的qq或群号 130 | user_type (int): 0-个人用户,1-群 131 | 132 | Returns: 133 | Tuple[bool, str]: [是否成功,信息] 134 | ''' 135 | 136 | # 处理参数错误 137 | if not uid.isdigit(): 138 | logger.error(f'{__PLUGIN_NAME}存在错误参数<{uid}>') 139 | return (False, uid + "(错误参数)") 140 | 141 | uid = str(int(uid)) 142 | 143 | try: 144 | 145 | logger.debug(f'{__PLUGIN_NAME}uid = {uid}') 146 | if uid not in bili_task_manager.dynamic_list: 147 | logger.debug(f'{__PLUGIN_NAME}uid not in dynamic_list') 148 | else: 149 | logger.debug(f'{__PLUGIN_NAME}uid in dynamic_list: {bili_task_manager.dynamic_list[uid]}') 150 | 151 | 152 | if uid not in bili_task_manager.dynamic_list: 153 | u_name, pin_id_str, latest_timestamp = await bili_client.init_dynamic_info(uid) 154 | 155 | bili_task_manager.add_dynamic_info(uid, u_name, pin_id_str, latest_timestamp) 156 | logger.debug(f'{__PLUGIN_NAME}after update dynamic_list[uid] = {bili_task_manager.dynamic_list[uid]}') 157 | 158 | if user_type == 0: 159 | bili_task_manager.add_user_follower(3, uid, user_id) 160 | else: 161 | bili_task_manager.add_group_follower(3, uid, user_id) 162 | logger.debug(f'{__PLUGIN_NAME}after follow, dynamic_list[uid] = {bili_task_manager.dynamic_list[uid]}') 163 | 164 | logger.info(f"{__PLUGIN_NAME}用户/群 <{user_id}> 关注动态主 <{u_name}> 成功") 165 | logger.debug(f"{uid}-{u_name}-{pin_id_str}-{latest_timestamp}") 166 | return (True, u_name + f"(uid: {uid})") 167 | 168 | u_name = bili_task_manager.dynamic_list[uid]["u_name"] 169 | # 处理已关注 170 | if user_type == 0 and user_id in bili_task_manager.dynamic_list[uid]["user_follower"] or \ 171 | user_type == 1 and user_id in bili_task_manager.dynamic_list[uid]["group_follower"]: 172 | logger.info(f"{__PLUGIN_NAME}用户/群 <{user_id}> 已经关注了动态主 <{u_name}>") 173 | return (False, u_name + "(已关注)") 174 | 175 | # 进行关注 176 | if user_type == 0: 177 | bili_task_manager.add_user_follower(3, uid, user_id) 178 | elif user_type == 1: 179 | bili_task_manager.add_group_follower(3, uid, user_id) 180 | logger.info(f"{__PLUGIN_NAME}用户/群 <{user_id}> 关注动态主 <{u_name}> 成功") 181 | return (True, u_name + f"(uid: {uid})") 182 | except BiliAPI404Error: 183 | return (False, uid + "(uid错误)") 184 | except (BiliConnectionError, BiliAPIRetCodeError, BiliStatusCodeError): 185 | ex_type, ex_val, _ = sys.exc_info() 186 | exception_msg = f'【错误报告】\n获取动态主 <{uid}> B站信息发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 187 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 188 | return (False, uid + "(网络错误)") 189 | except BiliDatebaseError: 190 | ex_type, ex_val, _ = sys.exc_info() 191 | exception_msg = f'【错误报告】\n关注动态主 <{uid}>时数据库发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 192 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 193 | return (False, uid + "(数据库错误)") 194 | except Exception: 195 | ex_type, ex_val, _ = sys.exc_info() 196 | exception_msg = f'【错误报告】\n关注动态主 <{uid}>时发生意料之外的错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 197 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 198 | return (False, uid + "(未知错误,请查看日志)") 199 | 200 | async def unfollow_dynamic(uid: str, user_id: str, user_type: int) -> Tuple[bool, str]: 201 | '''根据个人用户或群取关动态主,修改up文件 202 | 203 | Args: 204 | uid (str): 动态主的uid 205 | user_id (str): 用户的qq号/群号 206 | user_type (int): 0-个人用户,1-群 207 | 208 | Returns: 209 | Tuple[bool, str]: [是否成功,信息] 210 | ''' 211 | 212 | # 处理参数错误 213 | if not uid.isdigit(): 214 | logger.error(f'{__PLUGIN_NAME}存在错误参数 <{uid}>') 215 | return (False, uid + "(错误参数)") 216 | try: 217 | # 处理未关注 218 | #logger.debug(f"{uid}的关注列表{bili_task_manager.dynamic_list[uid]['user_follower']}") 219 | if uid not in bili_task_manager.dynamic_list or \ 220 | user_type == 0 and user_id not in bili_task_manager.dynamic_list[uid]["user_follower"] or \ 221 | user_type == 1 and user_id not in bili_task_manager.dynamic_list[uid]["group_follower"]: 222 | logger.info(f'{__PLUGIN_NAME}用户/群 <{user_id}> 未关注动态主 <{uid}>') 223 | return (False, uid + "(未关注)") 224 | 225 | # 进行取关 226 | u_name = bili_task_manager.dynamic_list[uid]["u_name"] 227 | if user_type == 0: 228 | bili_task_manager.remove_user_follower(3, uid, user_id) 229 | else: 230 | bili_task_manager.remove_group_follower(3, uid, user_id) 231 | logger.info(f'{__PLUGIN_NAME}用户/群 <{user_id}> 取关动态主 <{uid}> 成功') 232 | 233 | return (True, u_name + f"(uid: {uid})") 234 | except BiliDatebaseError: 235 | ex_type, ex_val, _ = sys.exc_info() 236 | exception_msg = f'【错误报告】\n取关动态主 <{uid}> 时数据库发生错误\n错误类型: {ex_type}\n错误值: {ex_val}\n' 237 | logger.error(f"{__PLUGIN_NAME}" + exception_msg + traceback.format_exc()) 238 | return (False, uid + "(数据库错误)") 239 | 240 | async def follow_dynamic_list( 241 | user_id: int, 242 | uid_list: List[str], 243 | user_type: int 244 | ) -> List[List[str]]: 245 | '''个人用户/群关注动态主 246 | 247 | Args: 248 | user_id (int): qq号/群号 249 | uid_list (List[str]): 动态主的uid 250 | user_type (int): 0-个人用户,1-群 251 | 252 | Returns: 253 | List[List[str]]: [[关注成功列表],[关注失败列表]] 254 | ''' 255 | successList = [] 256 | failList = [] 257 | 258 | for uid in uid_list: 259 | isSuccess, s = await follow_dynamic(uid, str(user_id), user_type) 260 | if isSuccess: 261 | successList.append(s) 262 | else: 263 | failList.append(s) 264 | 265 | return [successList, failList] 266 | 267 | async def unfollow_dynamic_list( 268 | user_id: int, 269 | uid_list: List[str], 270 | user_type: int 271 | ) -> List[List[str]]: 272 | '''个人用户/群取关动态主 273 | 274 | Args: 275 | user_id (int): qq号/群号 276 | uid_list (List[str]): 取关的动态主 277 | user_type (int): 0-个人用户,1-群 278 | 279 | Returns: 280 | List[List[str]]: [是否成功,信息] 281 | ''' 282 | successList = [] 283 | failList = [] 284 | 285 | for uid in uid_list: 286 | isSuccess, s = await unfollow_dynamic(uid, str(user_id), user_type) 287 | if isSuccess: 288 | successList.append(s) 289 | else: 290 | failList.append(s) 291 | 292 | return [successList, failList] -------------------------------------------------------------------------------- /bili_src/bili_dynamic_dbg.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | #from .db import bili_database 3 | #from .exception import BiliConnectionError, BiliDatebaseError 4 | import httpx 5 | import json 6 | from typing import Dict, Tuple, List 7 | import sys 8 | import traceback 9 | import nonebot 10 | from nonebot.log import logger 11 | from nonebot.adapters.onebot.v11 import MessageSegment 12 | #import basicFunc 13 | #from .basicFunc import * 14 | 15 | __PLUGIN_NAME = "B站整合~动态" 16 | GET_NEWS_BY_UID_API = "https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space?host_mid={}&timezone_offset=-480" 17 | DYNAMIC_DETAIL_URL = "https://t.bilibili.com/{}" 18 | header = { 19 | 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; rv2.0.1) Gecko/20100101 Firefox/4.0.1' 20 | } 21 | 22 | async def get_latest_news(uid: str, pin_news_id: str, record_timestamp: int) -> Tuple[str, List[Dict]]: 23 | '''根据uid获取最新的动态 24 | 25 | Args: 26 | uid (str): 查询的uid 27 | pin_news_id (str): 数据库中记录置顶动态的id,无则返为"" 28 | record_timestamp (int): 数据库中记录最新动态的时间戳 29 | 30 | Returns: 31 | Tuple[str, Dict]: 32 | str - 置顶动态id 33 | List[Dict] - 更新的动态内容 34 | ''' 35 | 36 | try: 37 | async with httpx.AsyncClient() as client: 38 | response = await client.get(url=GET_NEWS_BY_UID_API.format(uid), headers=header) 39 | except Exception as e: 40 | pass 41 | raise BiliConnectionError(f"获取用户 <{uid}> 的动态时发生网络错误:{e.args[0]}") 42 | if response.status_code != 200: 43 | pass 44 | raise BiliConnectionError(f"获取用户 <{uid}> 的动态时连接出错:状态码={response.status_code}") 45 | response: Dict = response.json() 46 | if response["code"] != 0: 47 | raise BiliConnectionError(f"获取用户 <{uid}> 的动态时发生错误:{response['message']}") 48 | 49 | news_list: List = response["data"]["items"] 50 | 51 | # 如果动态列表长度为0则直接返回 52 | if len(news_list) == 0: 53 | return ("", []) 54 | 55 | update_news_list = [] 56 | has_pin_news = 0 57 | top_news_id = "" 58 | 59 | # 处理置顶动态 60 | top_news = news_list[0] 61 | if "module_tag" in top_news["modules"] and top_news["modules"]["module_tag"]["text"] == "置顶": 62 | # 如果存在置顶动态,判断与数据库中记录的是否相同 63 | has_pin_news = 1 64 | top_news_id: str = top_news["id_str"] 65 | if top_news_id == pin_news_id: 66 | # 如果相同,则跳过该动态 67 | pass 68 | else: 69 | # 如果置顶动态发生更换 70 | # 处理置顶旧动态的情况,如果置顶旧动态,则不需要更新通知; 71 | logger.debug(f'{__PLUGIN_NAME} <{uid}> 的置顶动态发生变化,由{pin_news_id}变为{top_news_id}') 72 | major_module: Dict = top_news["modules"]["module_dynamic"]["major"] 73 | additional_module: Dict = top_news["modules"]["module_dynamic"]["additional"] 74 | desc_module: Dict = top_news["modules"]["module_dynamic"]["desc"] 75 | top_news_timestamp: int = top_news["modules"]["module_author"]["pub_ts"] 76 | top_news_major_type = major_module["type"] if major_module else "MAJOR_TYPE_NONE" 77 | top_news_add_type = additional_module["type"] if additional_module else "ADDITIONAL_TYPE_NONE" 78 | 79 | if top_news_timestamp > record_timestamp: 80 | # 置顶动态是新动态,需要通知 81 | temp_news = {} 82 | temp_news["tag"] = "NORMAL" 83 | temp_news["news_id"] = top_news_id 84 | temp_news["text"] = desc_module["text"] if desc_module else "" 85 | temp_news["image"] = [] 86 | temp_news["timestamp"] = top_news_timestamp 87 | 88 | if top_news_major_type == "MAJOR_TYPE_DRAW": 89 | for image_item in major_module["draw"]["items"]: 90 | temp_news["image"].append(image_item["src"]) 91 | elif top_news_major_type == "MAJOR_TYPE_ARTICLE": 92 | temp_news["tag"] = "MAJOR_ARTICLE" 93 | temp_news["article"] = { 94 | "title": major_module["article"]["title"], 95 | "cover": major_module["article"]["covers"][0], 96 | "jump_url": major_module["article"]["jump_url"] 97 | } 98 | if top_news_add_type == "ADDITIONAL_TYPE_VOTE": 99 | temp_news["tag"] = "ADD_VOTE" 100 | temp_news["vote"] = { 101 | "desc": additional_module["vote"]["desc"], 102 | "vote_id": additional_module["vote"]["vote_id"] 103 | } 104 | elif top_news_add_type == "ADDITIONAL_TYPE_UGC": 105 | temp_news["tag"] = "ADD_UGC" 106 | temp_news["tgc"] = { 107 | "title" : additional_module["ugc"]["title"], 108 | "jump_url": additional_module["ugc"]["jump_url"], 109 | "cover": additional_module["ugc"]["cover"] 110 | } 111 | elif top_news_add_type == "ADDITIONAL_TYPE_RESERVE": 112 | temp_news["tag"] = "ADD_RESERVE", 113 | temp_news["reserve"] = { 114 | "title": additional_module["reserve"]["title"], 115 | "desc": additional_module["reserve"]["desc1"]["text"] 116 | } 117 | 118 | if top_news_major_type in ("MAJOR_TYPE_DRAW", "MAJOR_TYPE_NONE", "MAJOR_TYPE_ARTICLE"): 119 | update_news_list.append(temp_news) 120 | print(json.dumps(temp_news, ensure_ascii=False, indent=4)) 121 | 122 | # 处理置顶动态外的动态 123 | for news_item in news_list[has_pin_news:]: 124 | news_id = news_item["id_str"] 125 | major_module: Dict = news_item["modules"]["module_dynamic"]["major"] 126 | additional_module: Dict = news_item["modules"]["module_dynamic"]["additional"] 127 | desc_module: Dict = news_item["modules"]["module_dynamic"]["desc"] 128 | news_item_major_type = major_module["type"] if major_module else "MAJOR_TYPE_NONE" 129 | news_item_add_type = additional_module["type"] if additional_module else "ADDITIONAL_TYPE_NONE" 130 | news_timestamp = news_item["modules"]["module_author"]["pub_ts"] 131 | 132 | if news_timestamp <= record_timestamp: 133 | break 134 | # 发现新动态 135 | logger.debug(f'{__PLUGIN_NAME} <{uid}> 更新了动态 {news_id}') 136 | temp_news = {} 137 | temp_news["tag"] = "NORMAL" 138 | temp_news["news_id"] = news_id 139 | temp_news["text"] = desc_module["text"] if desc_module else "" 140 | temp_news["image"] = [] 141 | temp_news["timestamp"] = news_timestamp 142 | 143 | if news_item_major_type == "MAJOR_TYPE_DRAW": 144 | for image_item in major_module["draw"]["items"]: 145 | temp_news["image"].append(image_item["src"]) 146 | elif news_item_major_type == "MAJOR_TYPE_ARTICLE": 147 | temp_news["tag"] = "MAJOR_ARTICLE" 148 | temp_news["article"] = { 149 | "title": major_module["article"]["title"], 150 | "cover": major_module["article"]["covers"][0], 151 | "jump_url": major_module["article"]["jump_url"] 152 | } 153 | if news_item_add_type == "ADDITIONAL_TYPE_VOTE": 154 | temp_news["tag"] = "ADD_VOTE" 155 | temp_news["vote"] = { 156 | "desc": additional_module["vote"]["desc"], 157 | "vote_id": additional_module["vote"]["vote_id"] 158 | } 159 | elif news_item_add_type == "ADDITIONAL_TYPE_UGC": 160 | temp_news["tag"] = "ADD_UGC" 161 | temp_news["tgc"] = { 162 | "title" : additional_module["ugc"]["title"], 163 | "jump_url": additional_module["ugc"]["jump_url"], 164 | "cover": additional_module["ugc"]["cover"] 165 | } 166 | elif news_item_add_type == "ADDITIONAL_TYPE_RESERVE": 167 | temp_news["tag"] = "ADD_RESERVE", 168 | temp_news["reserve"] = { 169 | "title": additional_module["reserve"]["title"], 170 | "desc": additional_module["reserve"]["desc1"]["text"] 171 | } 172 | 173 | if news_item_major_type in ("MAJOR_TYPE_DRAW", "MAJOR_TYPE_NONE", "MAJOR_TYPE_ARTICLE"): 174 | update_news_list.append(temp_news) 175 | print(json.dumps(temp_news, ensure_ascii=False, indent=4)) 176 | 177 | return (top_news_id, update_news_list) 178 | 179 | loop = asyncio.get_event_loop() 180 | tasks = [get_latest_news("6700458", "", 0)] 181 | loop.run_until_complete(asyncio.wait(tasks)) 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | -------------------------------------------------------------------------------- /bili_src/bili_task.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | from .db import bili_database 3 | from .exception import BiliDatebaseError 4 | from nonebot.log import logger 5 | 6 | 7 | class BiliTaskManager(): 8 | def __init__(self) -> None: 9 | self.up_list = dict() 10 | self.liver_list = dict() 11 | self.telegram_list = dict() 12 | self.dynamic_list = dict() 13 | 14 | self.user_follower_list = dict() 15 | self.group_follower_list = dict() 16 | 17 | self.__up_update_check_ptr__ = 0 18 | self.__dynamic_update_check_ptr__ = 0 19 | 20 | self.__update_check_len__ = 5 21 | 22 | self.__init_from_database__() 23 | 24 | 25 | def __init_from_database__(self): 26 | '''从数据库中进行初始化 27 | ''' 28 | i = 0 29 | try: 30 | up_list = bili_database.query_all(0) 31 | for up_uid, up_name, latest_timestamp in up_list: 32 | self.up_list[up_uid] = { 33 | "uid": up_uid, 34 | "up_name": up_name, 35 | "latest_timestamp": latest_timestamp, 36 | "user_follower": set(bili_database.query_user_relation(0, up_uid)), 37 | "group_follower": set(bili_database.query_group_relation(0, up_uid)) 38 | } 39 | if not self.up_list[up_uid]["user_follower"] and not self.up_list[up_uid]["group_follower"]: 40 | self.up_list.pop(up_uid) 41 | logger.debug(f'{up_uid}已经无人关注') 42 | else: 43 | self.up_list[up_uid]["next_update_check"] = i 44 | i = (i + 1) % self.__update_check_len__ 45 | 46 | liver_list = bili_database.query_all(1) 47 | for liver_uid, liver_name, is_live, room_id in liver_list: 48 | self.liver_list[liver_uid] = { 49 | "liver_uid": liver_uid, 50 | "liver_name": liver_name, 51 | "is_live": is_live, 52 | "room_id": room_id, 53 | "user_follower": set(bili_database.query_user_relation(2, liver_uid)), 54 | "group_follower": set(bili_database.query_group_relation(2, liver_uid)) 55 | } 56 | if not self.liver_list[liver_uid]["user_follower"] and not self.liver_list[liver_uid]["group_follower"]: 57 | self.liver_list.pop(liver_uid) 58 | logger.debug(f'{liver_uid}已经无人关注') 59 | 60 | telegram_list = bili_database.query_all(2) 61 | 62 | for season_id, telegram_title, episode, is_finish in telegram_list: 63 | 64 | self.telegram_list[season_id] = { 65 | "season_id": season_id, 66 | "telegram_title": telegram_title, 67 | "episode": episode, 68 | "is_finish": bool(is_finish), 69 | "user_follower": set(bili_database.query_user_relation(4, season_id)), 70 | "group_follower": set(bili_database.query_group_relation(4, season_id)) 71 | } 72 | if not self.telegram_list[season_id]["user_follower"] and not self.telegram_list[season_id]["group_follower"]: 73 | self.telegram_list.pop(season_id) 74 | logger.debug(f'{season_id}已经无人关注') 75 | 76 | dynamic_list = bili_database.query_all(5) 77 | for uid, u_name, pin_id_str, latest_timestamp in dynamic_list: 78 | self.dynamic_list[uid] = { 79 | "uid": uid, 80 | "u_name": u_name, 81 | "pin_id_str": pin_id_str, 82 | "latest_timestamp": latest_timestamp, 83 | "user_follower": set(bili_database.query_user_relation(6, uid)), 84 | "group_follower": set(bili_database.query_group_relation(6, uid)) 85 | } 86 | if not self.dynamic_list[uid]["user_follower"] and not self.dynamic_list[uid]["group_follower"]: 87 | self.dynamic_list.pop(uid) 88 | logger.debug(f'{uid}已经无人关注') 89 | else: 90 | self.dynamic_list[uid]["next_update_check"] = i 91 | i = (i + 1) % self.__update_check_len__ 92 | 93 | except Exception as e: 94 | logger.debug(f'{e}') 95 | 96 | logger.error(f'初始化任务管理器失败') 97 | 98 | def get_up_check_update_list(self) -> List[str]: 99 | '''每次触发检查更新的up主 100 | 101 | Returns: 102 | List[str]: 应检查更新的up和时间戳的列表 103 | ''' 104 | check_update_list = [] 105 | for up_uid in self.up_list.keys(): 106 | if self.up_list[up_uid]["next_update_check"] == self.__up_update_check_ptr__: 107 | check_update_list.append(up_uid) 108 | self.__up_update_check_ptr__ = (self.__up_update_check_ptr__ + 1) % self.__update_check_len__ 109 | return check_update_list 110 | 111 | def get_dynamic_check_update_list(self) -> List[str]: 112 | '''每次触发检查更新的动态 113 | 114 | Returns: 115 | List[str]: 应检查更新动态主的uid 116 | ''' 117 | check_update_list = [] 118 | for uid in self.dynamic_list.keys(): 119 | if self.dynamic_list[uid]["next_update_check"] == self.__dynamic_update_check_ptr__: 120 | check_update_list.append(uid) 121 | self.__dynamic_update_check_ptr__ = (self.__dynamic_update_check_ptr__ + 1) % self.__update_check_len__ 122 | return check_update_list 123 | 124 | def update_up_info(self, up_uid: str, latest_timestamp: int) -> bool: 125 | '''更新任务管理器以及数据库中的up信息 126 | 127 | Args: 128 | up_uid (str): up的uid 129 | latest_timestamp (int): 最新视频时间戳 130 | 131 | Returns: 132 | bool: 是否成功 133 | ''' 134 | try: 135 | bili_database.update_info(0, latest_timestamp, up_uid) 136 | self.up_list[up_uid]["latest_timestamp"] = latest_timestamp 137 | return True 138 | except BiliDatebaseError as e: 139 | exception_msg = f'【错误报告】\n更新主播 <{up_uid}> 时数据库发生错误\n错误信息: {e}\n' 140 | logger.error(exception_msg) 141 | return False 142 | 143 | def update_liver_info(self, liver_uid: str, is_live: bool) -> bool: 144 | '''更新任务管理器以及数据库中的主播信息 145 | 146 | Args: 147 | liver_uid (str): 主播的uid 148 | is_live (int): 是否直播 149 | 150 | Returns: 151 | bool: 是否成功 152 | ''' 153 | try: 154 | bili_database.update_info(1, is_live, liver_uid) 155 | self.liver_list[liver_uid]["is_live"] = is_live 156 | return True 157 | except BiliDatebaseError as e: 158 | exception_msg = f'【错误报告】\n更新主播 <{liver_uid}> 时数据库发生错误\n错误信息: {e}\n' 159 | logger.error(exception_msg) 160 | return False 161 | 162 | def update_telegram_info(self, season_id: str, episode: int, is_finish: bool) -> bool: 163 | '''更新任务管理器以及数据库中的番剧信息 164 | 165 | Args: 166 | season_id (str): season_id 167 | episode (int): 最新集数的时间 168 | is_finish (bool): 是否完结 169 | 170 | Returns: 171 | bool: 是否成功 172 | ''' 173 | try: 174 | bili_database.update_info(2, episode, is_finish, season_id) 175 | self.telegram_list[season_id]["is_finish"] = is_finish 176 | self.telegram_list[season_id]["episode"] = episode 177 | return True 178 | except BiliDatebaseError as e: 179 | exception_msg = f'【错误报告】\n更新番剧 <{season_id}> 时数据库发生错误\n错误信息: {e}\n' 180 | logger.error(exception_msg) 181 | return False 182 | 183 | def update_dynamic_pin_id(self, uid: str, pin_id_str: str) -> bool: 184 | '''更新任务管理器以及数据库中的动态主的置顶id 185 | 186 | Args: 187 | uid (str): 动态主的uid 188 | pin_id_str (str): 置顶动态的id 189 | 190 | Returns: 191 | bool: 是否成功 192 | ''' 193 | try: 194 | bili_database.update_info(3, pin_id_str, uid) 195 | self.dynamic_list[uid]["pin_id_str"] = pin_id_str 196 | return True 197 | except BiliDatebaseError as e: 198 | exception_msg = f'【错误报告】\n更新动态主 <{uid}> 时数据库发生错误\n错误信息: {e}\n' 199 | logger.error(exception_msg) 200 | return False 201 | 202 | def update_dynamic_latest_timestamp(self, uid: str, latest_timestamp: int) -> bool: 203 | '''更新任务管理器以及数据库中的动态主的最新动态时间戳 204 | 205 | Args: 206 | uid (str): 动态主的uid 207 | latest_timestamp (int): 最新动态的时间戳 208 | 209 | Returns: 210 | bool: 是否成功 211 | ''' 212 | try: 213 | bili_database.update_info(4, latest_timestamp, uid) 214 | self.dynamic_list[uid]["latest_timestamp"] = latest_timestamp 215 | return True 216 | except BiliDatebaseError as e: 217 | exception_msg = f'【错误报告】\n更新动态主 <{uid}> 时数据库发生错误\n错误信息: {e}\n' 218 | logger.error(exception_msg) 219 | return False 220 | 221 | def add_user_follower(self, target_type: int, target_id: str, user_id: str) -> bool: 222 | '''_summary_ 223 | 224 | Args: 225 | target_type (int): 0-up主;1-主播;2-番剧;3-动态 226 | target_id (str): up主/主播/番剧/动态主的id 227 | user_id (str): 关注者的id 228 | 229 | Returns: 230 | bool: 是否成功 231 | ''' 232 | # 写入数据库,如果不存在于缓存,则从数据库写入缓存 233 | try: 234 | bili_database.insert_relation(2 * target_type, target_id, user_id) 235 | 236 | if target_type == 0: 237 | if target_id not in self.up_list: 238 | _, up_name, latest_timestamp = bili_database.query_info(2, target_id) 239 | self.up_list[target_id] = { 240 | "uid": target_id, 241 | "up_name": up_name, 242 | "latest_timestamp": latest_timestamp, 243 | "user_follower": set(bili_database.query_user_relation(0, target_id)), 244 | "group_follower": set(bili_database.query_group_relation(0, target_id)), 245 | "next_update_check": self.__up_update_check_ptr__ 246 | } 247 | else: 248 | self.up_list[target_id]["user_follower"].add(user_id) 249 | elif target_type == 1 : 250 | if target_id not in self.liver_list: 251 | _, liver_name, is_live, room_id = bili_database.query_info(3, target_id) 252 | self.liver_list[target_id] = { 253 | "liver_uid": target_id, 254 | "liver_name": liver_name, 255 | "is_live": is_live, 256 | "room_id": room_id, 257 | "user_follower": set(bili_database.query_user_relation(2, target_id)), 258 | "group_follower": set(bili_database.query_group_relation(2, target_id)) 259 | } 260 | else: 261 | self.liver_list[target_id]["user_follower"].add(user_id) 262 | elif target_type == 2: 263 | if target_id not in self.telegram_list: 264 | _, telegram_title, episode, is_finish = bili_database.query_info(4, target_id) 265 | self.telegram_list[target_id] = { 266 | "season_id": target_id, 267 | "telegram_title": telegram_title, 268 | "episode": episode, 269 | "is_finish": bool(is_finish), 270 | "user_follower": set(bili_database.query_user_relation(4, target_id)), 271 | "group_follower": set(bili_database.query_group_relation(4, target_id)) 272 | } 273 | else: 274 | self.telegram_list[target_id]["user_follower"].add(user_id) 275 | elif target_type == 3: 276 | if target_id not in self.dynamic_list: 277 | _, u_name, pin_id_str, latest_timestamp = bili_database.query_info(5, target_id) 278 | self.dynamic_list[target_id] = { 279 | "uid": target_id, 280 | "u_name": u_name, 281 | "pin_id_str": pin_id_str, 282 | "latest_timestamp": latest_timestamp, 283 | "next_update_check": self.__dynamic_update_check_ptr__, 284 | "user_follower": set(bili_database.query_user_relation(6, target_id)), 285 | "group_follower": set(bili_database.query_group_relation(6, target_id)) 286 | } 287 | else: 288 | self.dynamic_list[target_id]["user_follower"].add(user_id) 289 | 290 | return True 291 | except BiliDatebaseError as e: 292 | exception_msg = f'【错误报告】\n用户<{user_id}>关注<{target_id}>时数据库发生错误\n错误信息: {e}\n' 293 | logger.error(exception_msg) 294 | return False 295 | 296 | def add_group_follower(self, target_type: int, target_id: str, group_id: str) -> bool: 297 | '''_summary_ 298 | 299 | Args: 300 | target_type (int): 0-up主;1-主播;2-番剧;3-动态 301 | target_id (str): up主/主播/番剧/动态主的id 302 | group_id (str): 关注群的id 303 | 304 | Returns: 305 | bool: 是否成功 306 | ''' 307 | # 写入数据库,如果不存在于缓存,则从数据库写入缓存 308 | try: 309 | bili_database.insert_relation(2 * target_type + 1, target_id, group_id) 310 | 311 | if target_type == 0: 312 | if target_id not in self.up_list: 313 | _, up_name, latest_timestamp = bili_database.query_info(2, target_id) 314 | self.up_list[target_id] = { 315 | "uid": target_id, 316 | "up_name": up_name, 317 | "latest_timestamp": latest_timestamp, 318 | "next_update_check": self.__up_update_check_ptr__, 319 | "user_follower": set(bili_database.query_user_relation(0, target_id)), 320 | "group_follower": set(bili_database.query_group_relation(0, target_id)) 321 | } 322 | else: 323 | self.up_list[target_id]["group_follower"].add(group_id) 324 | elif target_type == 1 : 325 | if target_id not in self.liver_list: 326 | _, liver_name, is_live, room_id = bili_database.query_info(3, target_id) 327 | self.liver_list[target_id] = { 328 | "liver_uid": target_id, 329 | "liver_name": liver_name, 330 | "is_live": is_live, 331 | "room_id": room_id, 332 | "user_follower": set(bili_database.query_user_relation(2, target_id)), 333 | "group_follower": set(bili_database.query_group_relation(2, target_id)) 334 | } 335 | else: 336 | self.liver_list[target_id]["group_follower"].add(group_id) 337 | elif target_type == 2: 338 | if target_id not in self.telegram_list: 339 | _, telegram_title, episode, is_finish = bili_database.query_info(4, target_id) 340 | self.telegram_list[target_id] = { 341 | "season_id": target_id, 342 | "telegram_title": telegram_title, 343 | "episode": episode, 344 | "is_finish": bool(is_finish), 345 | "user_follower": set(bili_database.query_user_relation(4, target_id)), 346 | "group_follower": set(bili_database.query_group_relation(4, target_id)) 347 | } 348 | else: 349 | self.telegram_list[target_id]["group_follower"].add(group_id) 350 | elif target_type == 3: 351 | if target_id not in self.dynamic_list: 352 | _, u_name, pin_id_str, latest_timestamp = bili_database.query_info(5, target_id) 353 | self.dynamic_list[target_id] = { 354 | "uid": target_id, 355 | "u_name": u_name, 356 | "pin_id_str": pin_id_str, 357 | "latest_timestamp": latest_timestamp, 358 | "next_update_check": self.__dynamic_update_check_ptr__, 359 | "user_follower": set(bili_database.query_user_relation(6, target_id)), 360 | "group_follower": set(bili_database.query_group_relation(6, target_id)) 361 | } 362 | else: 363 | self.dynamic_list[target_id]["group_follower"].add(group_id) 364 | return True 365 | except BiliDatebaseError as e: 366 | exception_msg = f'【错误报告】\n群<{group_id}>关注<{target_id}>时数据库发生错误\n错误信息: {e}\n' 367 | logger.error(exception_msg) 368 | return False 369 | 370 | def remove_user_follower(self, target_type: int, target_id: str, user_id: str) -> bool: 371 | '''移除个人用户的关注 372 | 373 | Args: 374 | target_type (int): 0-up主/1-主播/2-番剧/3-动态 375 | target_id (str): up主/主播/番剧的id 376 | user_id (str): 关注者的id 377 | 378 | Returns: 379 | bool: 是否成功 380 | ''' 381 | try: 382 | bili_database.delete_relation(target_type * 2, user_id, target_id) 383 | if target_type == 0: 384 | self.up_list[target_id]["user_follower"].remove(user_id) 385 | if not self.up_list[target_id]["user_follower"] and not self.up_list[target_id]["group_follower"]: 386 | self.up_list.pop(target_id) 387 | logger.debug(f'{target_id}已经无人关注') 388 | elif target_type == 1: 389 | self.liver_list[target_id]["user_follower"].remove(user_id) 390 | if not self.liver_list[target_id]["user_follower"] and not self.liver_list[target_id]["group_follower"]: 391 | self.liver_list.pop(target_id) 392 | logger.debug(f'{target_id}已经无人关注') 393 | elif target_type == 2: 394 | self.telegram_list[target_id]["user_follower"].remove(user_id) 395 | if not self.telegram_list[target_id]["user_follower"] and not self.telegram_list[target_id]["group_follower"]: 396 | self.telegram_list.pop(target_id) 397 | logger.debug(f'{target_id}已经无人关注') 398 | elif target_type == 3: 399 | self.dynamic_list[target_id]["user_follower"].remove(user_id) 400 | if not self.dynamic_list[target_id]["user_follower"] and not self.dynamic_list[target_id]["group_follower"]: 401 | self.dynamic_list.pop(target_id) 402 | logger.debug(f'{target_id}已经无人关注') 403 | except BiliDatebaseError as e: 404 | exception_msg = f'【错误报告】\n用户<{user_id}>取关<{target_id}>时数据库发生错误\n错误信息: {e}\n' 405 | logger.error(exception_msg) 406 | return False 407 | 408 | def remove_group_follower(self, target_type: int, target_id: str, group_id: str) -> bool: 409 | '''移除个人用户的关注 410 | 411 | Args: 412 | target_type (int): 0-up主/1-主播/2-番剧/3-动态 413 | target_id (str): up主/主播/番剧的id 414 | group_id (str): 关注者的id 415 | 416 | Returns: 417 | bool: 是否成功 418 | ''' 419 | try: 420 | bili_database.delete_relation(target_type * 2 + 1, group_id, target_id) 421 | if target_type == 0: 422 | self.up_list[target_id]["group_follower"].remove(group_id) 423 | if not self.up_list[target_id]["user_follower"] and not self.up_list[target_id]["group_follower"]: 424 | self.up_list.pop(target_id) 425 | logger.debug(f'{target_id}已经无人关注') 426 | elif target_type == 1: 427 | self.liver_list[target_id]["group_follower"].remove(group_id) 428 | if not self.liver_list[target_id]["user_follower"] and not self.liver_list[target_id]["group_follower"]: 429 | self.liver_list.pop(target_id) 430 | logger.debug(f'{target_id}已经无人关注') 431 | elif target_type == 2: 432 | self.telegram_list[target_id]["group_follower"].remove(group_id) 433 | if not self.telegram_list[target_id]["user_follower"] and not self.telegram_list[target_id]["group_follower"]: 434 | self.telegram_list.pop(target_id) 435 | logger.debug(f'{target_id}已经无人关注') 436 | elif target_type == 3: 437 | self.dynamic_list[target_id]["group_follower"].remove(group_id) 438 | if not self.dynamic_list[target_id]["user_follower"] and not self.dynamic_list[target_id]["group_follower"]: 439 | self.dynamic_list.pop(target_id) 440 | logger.debug(f'{target_id}已经无人关注') 441 | except BiliDatebaseError as e: 442 | exception_msg = f'【错误报告】\n群<{group_id}>取关<{target_id}>时数据库发生错误\n错误信息: {e}\n' 443 | logger.error(exception_msg) 444 | return False 445 | 446 | def add_up_info(self, up_uid: str, up_name: str, latest_timestamp: int) -> bool: 447 | '''向任务管理器以及数据库中增加up信息 448 | 449 | Args: 450 | up_uid (str): up的uid 451 | up_name (str): up的用户名 452 | latest_timestamp (int): 最新视频的时间戳 453 | 454 | Returns: 455 | bool: 是否成功 456 | ''' 457 | try: 458 | if not bili_database.query_info(2, up_uid): 459 | bili_database.insert_info(2, up_uid, up_name, latest_timestamp) 460 | self.up_list[up_uid] = { 461 | "uid": up_uid, 462 | "up_name": up_name, 463 | "latest_timestamp": latest_timestamp, 464 | "user_follower": set(bili_database.query_user_relation(0, up_uid)), 465 | "group_follower": set(bili_database.query_group_relation(0, up_uid)), 466 | "next_update_check": self.__up_update_check_ptr__ 467 | } 468 | return True 469 | except BiliDatebaseError as e: 470 | exception_msg = f'【错误报告】\n创建up主<{up_uid}>信息时数据库发生错误\n错误信息: {e}\n' 471 | logger.error(exception_msg) 472 | return False 473 | 474 | def add_liver_info(self, liver_uid: str, liver_name: str, is_live: bool, room_id: str) -> bool: 475 | '''向任务管理器以及数据库中增加主播信息 476 | 477 | Args: 478 | liver_uid (str): 主播的uid 479 | liver_name (str): 主播的用户名 480 | is_live (bool): 是否正在直播 481 | room_id (str): 房间id 482 | 483 | Returns: 484 | bool: 是否成功 485 | ''' 486 | try: 487 | if not bili_database.query_info(3, liver_uid): 488 | bili_database.insert_info(3, liver_uid, liver_name, is_live, room_id) 489 | self.liver_list[liver_uid] = { 490 | "liver_uid": liver_uid, 491 | "liver_name": liver_name, 492 | "is_live": is_live, 493 | "room_id": room_id, 494 | "user_follower": set(bili_database.query_user_relation(2, liver_uid)), 495 | "group_follower": set(bili_database.query_group_relation(2, liver_uid)) 496 | } 497 | return True 498 | except BiliDatebaseError as e: 499 | exception_msg = f'【错误报告】\n创建主播<{liver_uid}>信息时数据库发生错误\n错误信息: {e}\n' 500 | logger.error(exception_msg) 501 | return False 502 | 503 | def add_telegram_info(self, season_id: str, telegram_title: str, episode: int, is_finish: bool) -> bool: 504 | '''向任务管理器以及数据库中增加番剧信息 505 | 506 | Args: 507 | season_id (str): 番剧的season_id 508 | telegram_title (str): 番剧名 509 | episode (int): 最新集数 510 | is_finish (bool): 是否完结 511 | 512 | Returns: 513 | bool: 是否成功 514 | ''' 515 | try: 516 | if not bili_database.query_info(4, season_id): 517 | bili_database.insert_info(4, season_id, telegram_title, episode, is_finish) 518 | self.telegram_list[season_id] = { 519 | "season_id": season_id, 520 | "telegram_title": telegram_title, 521 | "episode": episode, 522 | "is_finish": is_finish, 523 | "user_follower": set(bili_database.query_user_relation(4, season_id)), 524 | "group_follower": set(bili_database.query_group_relation(4, season_id)) 525 | } 526 | return True 527 | except BiliDatebaseError as e: 528 | exception_msg = f'【错误报告】\n创建番剧<{season_id}>信息时数据库发生错误\n错误信息: {e}\n' 529 | logger.error(exception_msg) 530 | return False 531 | 532 | def add_dynamic_info(self, uid: str, u_name: str, pin_id_str: str, latest_timestamp: int) -> bool: 533 | '''向任务管理器以及数据库中增加动态主信息 534 | 535 | Args: 536 | uid (str): uid 537 | u_name (str): 动态主的用户名 538 | pin_id_str (str): 置顶动态的id 539 | latest_timestamp (int): 最新动态的时间戳 540 | 541 | Returns: 542 | bool: 是否成功 543 | ''' 544 | try: 545 | if not bili_database.query_info(5, uid): 546 | bili_database.insert_info(5, uid, u_name, pin_id_str, latest_timestamp) 547 | self.dynamic_list[uid] = { 548 | "uid": uid, 549 | "u_name": u_name, 550 | "pin_id_str": pin_id_str, 551 | "latest_timestamp": latest_timestamp, 552 | "next_update_check": self.__dynamic_update_check_ptr__, 553 | "user_follower": set(bili_database.query_user_relation(6, uid)), 554 | "group_follower": set(bili_database.query_group_relation(6, uid)) 555 | } 556 | logger.debug(f'in add_dynamic_info {self.dynamic_list[uid]}') 557 | 558 | return True 559 | except BiliDatebaseError as e: 560 | exception_msg = f'【错误报告】\n创建动态主<{uid}>信息时数据库发生错误\n错误信息: {e}\n' 561 | logger.error(exception_msg) 562 | return False 563 | 564 | 565 | bili_task_manager = BiliTaskManager() 566 | logger.debug(f' bili task manager - up_list = {bili_task_manager.up_list}') 567 | logger.debug(f' bili task manager - liver_list = {bili_task_manager.liver_list}') 568 | logger.debug(f' bili task manager - telegram_list = {bili_task_manager.telegram_list}') 569 | logger.debug(f' bili task manager - dynamic_list = {bili_task_manager.dynamic_list}') 570 | 571 | 572 | 573 | 574 | 575 | 576 | -------------------------------------------------------------------------------- /bili_src/db.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | from typing import List, Tuple 3 | from nonebot.log import logger 4 | from os.path import abspath, dirname 5 | from .exception import * 6 | 7 | class BiliDatabase(): 8 | def __init__(self) -> None: 9 | self.db_name = dirname(abspath(__file__)) + "/../bilibili_2.db" 10 | self.conn = sqlite3.connect(self.db_name) 11 | self.init_database() 12 | 13 | def init_database(self) -> None: 14 | '''初始化数据库,如果不存在表则创建 15 | 16 | ''' 17 | cur = self.conn.cursor() 18 | logger.debug(f'初始化数据库') 19 | 20 | #创建up表 21 | cur.execute('''CREATE TABLE IF NOT EXISTS up 22 | ( 23 | up_uid VARCHAR(20) PRIMARY KEY NOT NULL, 24 | up_name VARCHAR(50) NOT NULL, 25 | latest_update INT NOT NULL 26 | ) 27 | ''') 28 | # 创建liver表 29 | cur.execute('''CREATE TABLE IF NOT EXISTS liver 30 | ( 31 | liver_uid VARCHAR(20) PRIMARY KEY NOT NULL, 32 | liver_name VARCHAR(50) NOT NULL, 33 | is_live TINYINT(1) DEFAULT 0 NOT NULL, 34 | live_room VARCHAR(30) NOT NULL 35 | ) 36 | ''') 37 | 38 | # 创建telegram表 39 | cur.execute('''CREATE TABLE IF NOT EXISTS telegram 40 | ( 41 | season_id VARCHAR(20) PRIMARY KEY NOT NULL, 42 | telegram_title VARCHAR(50) NOT NULL, 43 | episode INT NOT NULL, 44 | is_finish TINYINT(1) DEFAULT 0 NOT NULL 45 | ) 46 | ''') 47 | 48 | #创建动态表 49 | cur.execute('''CREATE TABLE IF NOT EXISTS dynamic 50 | ( 51 | uid VARCHAR(20) PRIMARY KEY NOT NULL, 52 | u_name VARCHAR(50) NOT NULL, 53 | pin_id_str VARCHAR(25) NOT NULL, 54 | latest_timestamp INT NOT NULL 55 | ) 56 | ''') 57 | 58 | # 创建个人用户表 59 | cur.execute('''CREATE TABLE IF NOT EXISTS qq_user 60 | ( 61 | user_id VARCHAR(20) PRIMARY KEY NOT NULL, 62 | user_name VARCHAR(50) NOT NULL 63 | ) 64 | ''') 65 | 66 | # 创建群用户表 67 | cur.execute('''CREATE TABLE IF NOT EXISTS qq_group 68 | ( 69 | group_id VARCHAR(20) PRIMARY KEY NOT NULL, 70 | group_name VARCHAR(50) NOT NULL 71 | ) 72 | ''') 73 | 74 | # 创建关注up表 75 | cur.execute('''CREATE TABLE IF NOT EXISTS up_follower 76 | ( 77 | id INTERGER PRIMARY KEY, 78 | up_uid VARCHAR(20) NOT NULL, 79 | user_id VARCHAR(20), 80 | group_id VARCHAR(20) 81 | ) 82 | ''') 83 | 84 | # 创建wbi参数记录表 85 | cur.execute('''CREATE TABLE IF NOT EXISTS wbi_record 86 | ( 87 | date INTERGER NOT NULL, 88 | img_key VARCHAR(32) NOT NULL, 89 | sub_key VARCHAR(32) NOT NULL 90 | ) 91 | ''') 92 | 93 | # 创建索引 94 | cur.execute("CREATE INDEX IF NOT EXISTS up_index ON up_follower (up_uid)") 95 | cur.execute("CREATE INDEX IF NOT EXISTS up_user_index ON up_follower (user_id)") 96 | cur.execute("CREATE INDEX IF NOT EXISTS up_group_index ON up_follower (group_id)") 97 | 98 | # 创建关注主播表 99 | cur.execute('''CREATE TABLE IF NOT EXISTS liver_follower 100 | ( 101 | id INTERGER PRIMARY KEY, 102 | liver_uid VARCHAR(20) NOT NULL, 103 | user_id VARCHAR(20), 104 | group_id VARCHAR(20) 105 | ) 106 | ''') 107 | 108 | # 创建索引 109 | cur.execute("CREATE INDEX IF NOT EXISTS liver_index ON liver_follower (liver_uid)") 110 | cur.execute("CREATE INDEX IF NOT EXISTS liver_user_index ON liver_follower (user_id)") 111 | cur.execute("CREATE INDEX IF NOT EXISTS liver_group_index ON liver_follower (group_id)") 112 | 113 | # 创建关注节目表 114 | cur.execute('''CREATE TABLE IF NOT EXISTS telegram_follower 115 | ( 116 | id INTERGER PRIMARY KEY, 117 | season_id VARCHAR(20) NOT NULL, 118 | user_id VARCHAR(20), 119 | group_id VARCHAR(20) 120 | ) 121 | ''') 122 | 123 | # 创建索引 124 | cur.execute("CREATE INDEX IF NOT EXISTS tele_index ON telegram_follower (season_id)") 125 | cur.execute("CREATE INDEX IF NOT EXISTS tele_user_index ON telegram_follower (user_id)") 126 | cur.execute("CREATE INDEX IF NOT EXISTS tele_group_index ON telegram_follower (group_id)") 127 | 128 | # 创建关注动态表 129 | cur.execute('''CREATE TABLE IF NOT EXISTS dynamic_follower 130 | ( 131 | id INTERGER PRIMARY KEY, 132 | uid VARCHAR(20) NOT NULL, 133 | user_id VARCHAR(20), 134 | group_id VARCHAR(20) 135 | ) 136 | ''') 137 | 138 | logger.debug(f'数据库初始化完毕') 139 | 140 | cur.close() 141 | self.conn.commit() 142 | 143 | def insert_info(self, sql_type: int, *args) -> bool: 144 | '''插入信息表的通用操作 145 | 146 | Args: 147 | type (int): 0-用户;1-群组;2-up;3-主播;4-番剧;5-动态 148 | args: 149 | 0-用户: user_id, user_name 150 | 1-群组: group_id, group_name 151 | 2-up主: up_uid, up_name, latest_update 152 | 3-主播: liver_uid, liver_name, is_live, live_room 153 | 4-番剧: season_id, telegram_title, episode, is_finish 154 | 5-动态: uid, u_name, pin_id_str, latest_timestamp 155 | 156 | Returns: 157 | bool: 是否成功 158 | ''' 159 | cur = self.conn.cursor() 160 | 161 | sqls = [ 162 | "INSERT INTO qq_user(user_id, user_name) VALUES (?, ?)", 163 | "INSERT INTO qq_group(group_id, group_name) VALUES (?, ?)", 164 | "INSERT INTO up(up_uid, up_name, latest_update) VALUES (?, ?, ?)", 165 | "INSERT INTO liver(liver_uid, liver_name, is_live, live_room) VALUES (?, ?, ?, ?)", 166 | "INSERT INTO telegram(season_id, telegram_title, episode, is_finish) VALUES (?, ?, ?, ?)", 167 | "INSERT INTO dynamic(uid, u_name, pin_id_str, latest_timestamp) VALUES (?, ?, ?, ?)" 168 | ] 169 | #logger.info(f'信息表进行插入') 170 | 171 | try: 172 | cur.execute(sqls[sql_type], args) 173 | except Exception as e: 174 | ##logger.error(f'[]插入信息表时发生错误:\n{e}') 175 | self.conn.rollback() 176 | cur.close() 177 | raise BiliDatebaseError(f"数据库插入信息表时发生错误:{e.args[0]}") 178 | else: 179 | cur.close() 180 | self.conn.commit() 181 | #logger.info(f'插入信息表成功') 182 | return True 183 | 184 | def insert_relation(self, sql_type: int, uid: str, follower_id: str) -> bool: 185 | '''插入关注表 186 | 187 | Args: 188 | type (int): 0-个人关注up;1-群关注up;2-个人关注主播;3-群关注主播;4-个人关注番剧;5-群关注番剧;6-个人关注动态;7-群关注动态 189 | uid (str): 被关注者的id 190 | follower_id (str): 粉丝的id 191 | 192 | Returns: 193 | bool: 是否成功 194 | ''' 195 | 196 | sqls = [ 197 | "INSERT INTO up_follower(up_uid, user_id) VALUES (?, ?)", 198 | "INSERT INTO up_follower(up_uid, group_id) VALUES (?, ?)", 199 | "INSERT INTO liver_follower(liver_uid, user_id) VALUES (?, ?)", 200 | "INSERT INTO liver_follower(liver_uid, group_id) VALUES (?, ?)", 201 | "INSERT INTO telegram_follower(season_id, user_id) VALUES (?, ?)", 202 | "INSERT INTO telegram_follower(season_id, group_id) VALUES (?, ?)", 203 | "INSERT INTO dynamic_follower(uid, user_id) VALUES (?, ?)", 204 | "INSERT INTO dynamic_follower(uid, group_id) VALUES (?, ?)" 205 | ] 206 | cur = self.conn.cursor() 207 | 208 | assert 0 <= sql_type < len(sqls), "索引长度错误" 209 | #logger.info(f'关注表进行插入') 210 | try: 211 | cur.execute(sqls[sql_type], (uid, follower_id)) 212 | except Exception as e: 213 | ##logger.error(f'插入关注表时发生错误:\n{e}') 214 | cur.close() 215 | self.conn.rollback() 216 | raise BiliDatebaseError(f"数据库插入关注表时发生错误:{e.args[0]}") 217 | else: 218 | #logger.info(f'插入关注表成功') 219 | cur.close() 220 | self.conn.commit() 221 | return True 222 | 223 | def query_info(self, sql_type: int, target_id: str) -> Tuple: 224 | '''查询信息表通用操作 225 | 226 | Args: 227 | sql_type (int): 0-用户;1-群组;2-up;3-主播;4-番剧;5-动态 228 | target_id (str): up/主播/番剧/用户/群/动态发布者的id 229 | 230 | Returns: 231 | Tuple: 查询结果 232 | ''' 233 | cur = self.conn.cursor() 234 | sqls = [ 235 | "SELECT user_id, user_name FROM qq_user WHERE user_id = ?", 236 | "SELECT group_id, group_name FROM qq_group WHERE group_id = ?", 237 | "SELECT up_uid, up_name, latest_update FROM up WHERE up_uid = ?", 238 | "SELECT liver_uid, liver_name, is_live, live_room FROM liver WHERE liver_uid = ?", 239 | "SELECT season_id, telegram_title, episode, is_finish FROM telegram WHERE season_id = ?", 240 | "SELECT uid, u_name, pin_id_str, latest_timestamp FROM dynamic WHERE uid = ?" 241 | ] 242 | assert 0 <= sql_type < len(sqls), "索引长度错误" 243 | #logger.info(f'查询信息表中{target_id}的信息') 244 | 245 | try: 246 | cur.execute(sqls[sql_type], (target_id,)) 247 | except Exception as e: 248 | ##logger.error(f'查询信息表时发生错误:\n{e}') 249 | raise BiliDatebaseError(f"数据库查询信息表时发生错误:{e.args[0]}") 250 | else: 251 | result = cur.fetchone() 252 | return result 253 | 254 | def query_all(self, sql_type: int) -> List[Tuple]: 255 | '''获得所有的up/主播/番剧以进行更新检查 256 | 257 | Args: 258 | sql_type (int): 0-up;1-主播;2-番剧;3-用户;4-群组;5-动态 259 | 260 | Returns: 261 | List[Tuple]: 查询结果 262 | ''' 263 | 264 | sqls = [ 265 | "SELECT up_uid, up_name, latest_update FROM up", 266 | "SELECT liver_uid, liver_name, is_live, live_room FROM liver", 267 | "SELECT season_id, telegram_title, episode, is_finish FROM telegram", 268 | "SELECT user_id FROM qq_user", 269 | "SELECT group_id FROM qq_group", 270 | "SELECT uid, u_name, pin_id_str, latest_timestamp FROM dynamic" 271 | ] 272 | 273 | assert 0 <= sql_type < len(sqls), "索引长度错误" 274 | cur = self.conn.cursor() 275 | 276 | try: 277 | cur.execute(sqls[sql_type]) 278 | except Exception as e: 279 | ##logger.error(f'查询信息表时发生错误:\n{e}') 280 | raise BiliDatebaseError(f"数据库查询信息表时发生错误:{e.args[0]}") 281 | 282 | else: 283 | result = cur.fetchall() 284 | return result 285 | 286 | def query_user_relation(self, sql_type: int, target_id: str) -> List[str]: 287 | '''查询个人相关的关注 288 | 289 | Args: 290 | sql_type (int): 291 | 0-关注up的所有用户;1-用户关注的所有up; 292 | 2-关注主播的所有用户;3-用户关注的所有主播; 293 | 4-关注番剧的所有用户;5-用户关注的所有番剧; 294 | 6-关注动态主的所有用户;7-用户关注的所有动态主 295 | target_id (str): 用户/up/主播/番剧/动态主的id 296 | 297 | Returns: 298 | List[str]: 查询结果列表 299 | ''' 300 | 301 | sqls = [ 302 | "SELECT user_id FROM up_follower WHERE up_uid = ?", 303 | "SELECT up_uid FROM up_follower WHERE user_id = ?", 304 | "SELECT user_id FROM liver_follower WHERE liver_uid = ?", 305 | "SELECT liver_uid FROM liver_follower WHERE user_id = ?", 306 | "SELECT user_id FROM telegram_follower WHERE season_id = ?", 307 | "SELECT season_id FROM telegram_follower WHERE user_id = ?", 308 | "SELECT user_id FROM dynamic_follower WHERE uid = ?", 309 | "SELECT uid FROM dynamic_follower WHERE user_id = ?" 310 | ] 311 | assert 0 <= sql_type < len(sqls), "索引长度错误" 312 | 313 | cur = self.conn.cursor() 314 | ##logger.info(f'查询个人关注') 315 | try: 316 | cur.execute(sqls[sql_type], (target_id, )) 317 | except Exception as e: 318 | ##logger.error(f'查询个人关注时发生错误:\n{e}') 319 | raise BiliDatebaseError(f"数据库查询个人关注时发生错误:{e.args[0]}") 320 | else: 321 | temp = cur.fetchall() 322 | if not temp: 323 | return [] 324 | else: 325 | result = [i[0] for i in temp if i[0] is not None] 326 | return result 327 | 328 | def query_group_relation(self, sql_type: int, target_id: str) -> List[Tuple]: 329 | '''查询群相关的关注 330 | 331 | Args: 332 | sql_type (int): 333 | 0-关注up的所有群;1-群关注的所有up; 334 | 2-关注主播的所有群;3-群关注的所有主播; 335 | 4-关注番剧的所有群;5-群关注的所有番剧; 336 | 6-关注动态主的所有群;7-群关注的所有动态主 337 | target_id (str): 群/up/主播/番剧的id 338 | 339 | Returns: 340 | List[Tuple]: 查询结果 341 | ''' 342 | 343 | sqls = [ 344 | "SELECT group_id FROM up_follower WHERE up_uid = ?", 345 | "SELECT up_uid FROM up_follower WHERE group_id = ?", 346 | "SELECT group_id FROM liver_follower WHERE liver_uid = ?", 347 | "SELECT liver_uid FROM liver_follower WHERE group_id = ?", 348 | "SELECT group_id FROM telegram_follower WHERE season_id = ?", 349 | "SELECT season_id FROM telegram_follower WHERE group_id = ?", 350 | "SELECT group_id FROM dynamic_follower WHERE uid = ?", 351 | "SELECT uid FROM dynamic_follower WHERE group_id = ?" 352 | ] 353 | assert 0 <= sql_type < len(sqls), "索引长度错误" 354 | 355 | cur = self.conn.cursor() 356 | ##logger.info(f'查询群组关注') 357 | try: 358 | cur.execute(sqls[sql_type], (target_id, )) 359 | except Exception as e: 360 | ##logger.error(f'查询群组关注时发生错误:\n{e}') 361 | raise BiliDatebaseError(f"数据库查询群组关注时发生错误:{e.args[0]}") 362 | else: 363 | temp = cur.fetchall() 364 | if not temp: 365 | return [] 366 | else: 367 | result = [i[0] for i in temp if i[0] is not None] 368 | return result 369 | 370 | def query_specified_realtion(self, sql_type: int, user_id: str, target_id: str) -> bool: 371 | '''查询一对一的关注 372 | 373 | Args: 374 | sql_type (int): 0 个人-up;1 群组-up;2 个人-主播;3 群组-主播;4 个人-番剧;5 群组-番剧;6 个人-动态;7 群组-动态 375 | user_id (str): 用户/群组id 376 | target_id (str): up/主播/番剧/动态主id 377 | 378 | Returns: 379 | bool: 是否关注 380 | ''' 381 | 382 | sqls = [ 383 | "SELECT user_id, up_uid FROM up_follower WHERE user_id = ? AND up_uid = ?", 384 | "SELECT group_id, up_uid FROM up_follower WHERE group_id = ? AND up_uid = ?", 385 | "SELECT user_id, liver_uid FROM liver_follower WHERE user_id = ? AND liver_uid = ?", 386 | "SELECT group_id, liver_uid FROM liver_follower WHERE group_id = ? AND liver_uid = ?", 387 | "SELECT user_id, season_id FROM telegram_follower WHERE user_id = ? AND season_id = ?", 388 | "SELECT group_id, season_id FROM telegram_follower WHERE group_id = ? AND season_id = ?", 389 | "SELECT user_id, uid FROM dynamic_follower WHERE user_id = ? AND uid = ?", 390 | "SELECT group_id, uid FROM dynamic_follower WHERE group_id = ? AND uid = ?" 391 | ] 392 | 393 | assert 0 <= sql_type < len(sqls), "索引长度错误" 394 | 395 | cur = self.conn.cursor() 396 | ##logger.info(f'查询一对一关注') 397 | try: 398 | cur.execute(sqls[sql_type], (user_id, target_id)) 399 | except Exception as e: 400 | ##logger.error(f'查询一对一关注时发生错误:\n{e}') 401 | raise BiliDatebaseError(f"数据库查询一对一关注时发生错误:{e.args[0]}") 402 | else: 403 | result = cur.fetchone() 404 | if result: 405 | return True 406 | else: 407 | return False 408 | 409 | 410 | def delete_relation(self, sql_type: int, user_id: str, uid: str) -> bool: 411 | '''进行取关操作 412 | 413 | Args: 414 | sql_type (int): 415 | 0-用户取关up;1-群取关up; 416 | 2-用户取关主播;3-群取关主播; 417 | 4-用户取关番剧;5-群取关番剧; 418 | 6-用户取关动态;7-群取关动态; 419 | user_id (str): 用户/群id 420 | uid (str): up/主播/番剧id 421 | 422 | Returns: 423 | bool: 是否成功 424 | ''' 425 | sqls = [ 426 | "DELETE FROM up_follower WHERE user_id = ? AND up_uid = ?", 427 | "DELETE FROM up_follower WHERE group_id = ? AND up_uid = ?", 428 | "DELETE FROM liver_follower WHERE user_id = ? AND liver_uid = ?", 429 | "DELETE FROM liver_follower WHERE group_id = ? AND liver_uid = ?", 430 | "DELETE FROM telegram_follower WHERE user_id = ? AND season_id = ?", 431 | "DELETE FROM telegram_follower WHERE group_id = ? AND season_id = ?", 432 | "DELETE FROM dynamic_follower WHERE user_id = ? AND uid = ?", 433 | "DELETE FROM dynamic_follower WHERE group_id = ? AND uid = ?" 434 | ] 435 | assert 0 <= sql_type < len(sqls), "索引长度错误" 436 | 437 | cur = self.conn.cursor() 438 | ##logger.info(f'进行取关操作,取消{user_id}与{uid}之间的关注关系') 439 | 440 | try: 441 | cur.execute(sqls[sql_type], (user_id, uid)) 442 | except Exception as e: 443 | ##logger.error(f'取关时发生错误:\n{e}') 444 | raise BiliDatebaseError(f"数据库取关时发生错误:{e.args[0]}") 445 | 446 | else: 447 | ##logger.info(f'取关成功') 448 | self.conn.commit() 449 | return True 450 | 451 | def update_info(self, sql_type: int, *args) -> bool: 452 | '''更新up/主播/番剧信息 453 | 454 | Args: 455 | sql_type (int): 0-更新up;1-更新主播;2-更新番剧 456 | args : 457 | 0 up-整数时间戳,uid; 458 | 1 主播-True/False, uid; 459 | 2 番剧-整数集数, 是否完结, season_id; 460 | 3 动态-置顶动态id, uid; 461 | 4 动态-最新动态时间戳, uid; 462 | 463 | Returns: 464 | bool: 是否成功 465 | ''' 466 | 467 | sqls = [ 468 | "UPDATE up SET latest_update = ? WHERE up_uid = ?", 469 | "UPDATE liver SET is_live = ? WHERE liver_uid = ?", 470 | "UPDATE telegram SET episode = ?, is_finish = ? WHERE season_id = ?", 471 | "UPDATE dynamic SET pin_id_str = ? WHERE uid = ?", 472 | "UPDATE dynamic SET latest_timestamp = ? WHERE uid = ?" 473 | ] 474 | assert 0 <= sql_type < len(sqls), "索引长度错误" 475 | 476 | cur = self.conn.cursor() 477 | ##logger.info(f'更新信息表') 478 | 479 | try: 480 | cur.execute(sqls[sql_type], args) 481 | except Exception as e: 482 | ##logger.debug(f'更新信息表时发生错误:\n{e}') 483 | raise e 484 | else: 485 | ##logger.info(f'更新信息表成功') 486 | self.conn.commit() 487 | return True 488 | 489 | def delete_info(self, sql_type: int, target_id: str) -> bool: 490 | '''删除用户/群组/up/主播/番剧/动态主信息 491 | 492 | Args: 493 | sql_type (int): 0-用户;1-群组;2-up;3-主播;4-番剧;5-动态主信息 494 | target_id (str): 用户/群组/up/主播/番剧/动态主id 495 | 496 | Returns: 497 | bool: 是否成功 498 | ''' 499 | 500 | sqls = [ 501 | "DELETE FROM qq_user WHERE user_id = ?", 502 | "DELETE FROM qq_group WHERE group_id = ?", 503 | "DELETE FROM up WHERE up_uid = ?", 504 | "DELETE FROM liver WHERE liver_uid = ?", 505 | "DELETE FROM telegram WHERE season_id = ?", 506 | "DELETE FROM dynamic WHERE uid = ?" 507 | ] 508 | 509 | cur = self.conn.cursor() 510 | ##logger.info(f'从信息表中删除{target_id}的信息') 511 | 512 | try: 513 | cur.execute(sqls[sql_type], (target_id,)) 514 | except Exception as e: 515 | ##logger.error(f'从信息表中删除时发生错误:\n{e}') 516 | raise BiliDatebaseError(f"数据库删除信息时发生错误:{e.args[0]}") 517 | 518 | else: 519 | ##logger.info(f'删除成功') 520 | self.conn.commit() 521 | return True 522 | 523 | def check_dynamic_init(self) -> bool: 524 | '''检查是否完成动态初始化 525 | 526 | Returns: 527 | bool: 是否完成 528 | ''' 529 | cur = self.conn.cursor() 530 | try: 531 | cur.execute("SELECT is_dynamic_init FROM bili_sys") 532 | except Exception as e: 533 | raise BiliDatebaseError(f"数据库查询是否完成动态初始化时发生错误:{e.args[0]}") 534 | else: 535 | result = cur.fetchone() 536 | if result: 537 | return True 538 | else: 539 | cur.execute("UPDATE bili_sys SET is_dynamic_init = 1") 540 | self.conn.commit() 541 | return False 542 | 543 | def dbg_check_telegram_follow(self) -> List[Tuple]: 544 | 545 | cur = self.conn.cursor() 546 | ##logger.info(f'查询群组关注') 547 | try: 548 | cur.execute("SELECT rowid, season_id, user_id, group_id FROM telegram_follower") 549 | except Exception as e: 550 | ##logger.error(f'查询群组关注时发生错误:\n{e}') 551 | raise BiliDatebaseError(f"数据库查询群组关注时发生错误:{e.args[0]}") 552 | else: 553 | temp = cur.fetchall() 554 | return temp 555 | 556 | def get_wbi_param(self) -> Tuple[str, str, str]: 557 | '''获取数据库中的wbi参数 558 | 559 | Returns: 560 | Tuple[str, str, str]: (date, img_key, sub_key) 561 | ''' 562 | cur = self.conn.cursor() 563 | try: 564 | cur.execute("SELECT date, img_key, sub_key FROM wbi_record") 565 | except Exception as e: 566 | raise BiliDatebaseError(f"数据库获取wbi参数时发生错误:{e.args[0]}") 567 | else: 568 | temp = cur.fetchone() 569 | return temp 570 | 571 | def set_wbi_param(self, date: int, img_key: str, sub_key: str) -> bool: 572 | '''设置新的wbi参数 573 | 574 | Args: 575 | date (int): 日期 576 | img_key (str): img_key 577 | sub_key (str): sub_key 578 | ''' 579 | cur = self.conn.cursor() 580 | try: 581 | cur.execute("TRUNCATE wbi_record") 582 | cur.execute("INSERT INTO wbi_record VALUES (?, ?, ?)", (date, img_key, sub_key)) 583 | except Exception as e: 584 | cur.close() 585 | self.conn.rollback() 586 | raise BiliDatebaseError(f"数据库设置wbi参数时发生错误:{e.args[0]}") 587 | else: 588 | cur.close() 589 | self.conn.commit() 590 | return True 591 | 592 | 593 | bili_database = BiliDatabase() -------------------------------------------------------------------------------- /bili_src/exception.py: -------------------------------------------------------------------------------- 1 | class BiliConnectionError(Exception): 2 | def __init__(self, target_type: int, target: str, reason: str): 3 | self.reason = reason 4 | self.target = target 5 | self.target_type = ["up主", "主播", "番剧", "短链接", "动态"][target_type] 6 | 7 | def __str__(self) -> str: 8 | return f"获取 {self.target_type} <{self.target}> 时发生网络错误: {self.reason}" 9 | 10 | class BiliStatusCodeError(Exception): 11 | def __init__(self, target_type: int, target: str, status_code: int) -> None: 12 | '''网络连接状态码错误 13 | 14 | Args: 15 | target_type (int): 0-up;1-主播;2-番剧;3-短链接;4-动态 16 | target (str): uid或season id 17 | status_code (int): 状态码 18 | ''' 19 | self.status_code = status_code 20 | self.target = target 21 | self.target_type = ["up主", "主播", "番剧", "短链接", "动态"][target_type] 22 | 23 | def __str__(self) -> str: 24 | ret_str = f"获取 {self.target_type} <{self.target}> 时发生网络错误, 状态码为:{self.status_code}" 25 | return ret_str 26 | 27 | class BiliAPIRetCodeError(Exception): 28 | def __init__(self, target_type: int, target: str, ret_code: int, ret_msg: str) -> None: 29 | '''_summary_ 30 | 31 | Args: 32 | target_type (int): 0-up;1-主播;2-番剧;3-动态 33 | target (str): uid或season id 34 | ret_code (int): b站接口返回的返回码 35 | msg (str): b站接口返回的错误信息 36 | 37 | Returns: 38 | _type_: _description_ 39 | ''' 40 | self.target = target 41 | self.target_type = ["up主", "主播", "番剧", "动态"][target_type] 42 | self.ret_code = ret_code 43 | self.ret_msg = ret_msg 44 | 45 | def __str__(self) -> str: 46 | ret_str = f"获取 {self.target_type} <{self.target}> 时接口返回错误\n接口错误码为:{self.ret_code}\n接口错误信息为:{self.ret_msg}" 47 | return ret_str 48 | 49 | class BiliDatebaseError(Exception): 50 | def __init__(self, reason: str): 51 | self.reason = reason 52 | 53 | def __str__(self) -> str: 54 | return self.reason 55 | 56 | class BiliNoLiveRoom(Exception): 57 | def __init__(self, liver_name: str): 58 | self.ret_str = f"用户 <{liver_name}> 未开通直播间" 59 | 60 | def __str__(self) -> str: 61 | return self.ret_str 62 | 63 | class BiliAPI404Error(Exception): 64 | def __init__(self) -> None: 65 | pass 66 | 67 | class BiliInvalidShortUrl(Exception): 68 | def __init__(self, short_url: str) -> None: 69 | self.short_url = short_url 70 | 71 | class BiliInvalidRoomId(Exception): 72 | def __init__(self, room_id: str) -> None: 73 | self.room_id = room_id -------------------------------------------------------------------------------- /bili_src/rule.py: -------------------------------------------------------------------------------- 1 | from nonebot.adapters.onebot.v11 import Message, MessageSegment, MessageEvent, Event 2 | from nonebot.adapters.onebot.v11.event import GroupMessageEvent, PrivateMessageEvent 3 | from nonebot.log import logger 4 | 5 | from nonebot.rule import Rule 6 | async def isGroupMessage(event: Event) -> bool: 7 | if event.get_type() != "message": 8 | return False 9 | if not isinstance(event, GroupMessageEvent): 10 | return False 11 | return True 12 | 13 | async def isPrivateMessage(event: Event) -> bool: 14 | if event.get_type() != "message": 15 | return False 16 | if not isinstance(event, PrivateMessageEvent): 17 | return False 18 | return True 19 | 20 | groupMessageRule = Rule(isGroupMessage) 21 | privateMessageRule = Rule(isPrivateMessage) -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | from pydantic import BaseSettings 2 | 3 | class Config(BaseSettings): 4 | # Your Config Here 5 | #bili_db_name: str = "bilibili.db" 6 | 7 | class Config: 8 | extra = "ignore" -------------------------------------------------------------------------------- /docs/dynamic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TDK1969/nonebot_plugin_bilibilibot/9614d4eb93f10dda2c36b27c3504f581d381b7d1/docs/dynamic.jpg -------------------------------------------------------------------------------- /docs/follow.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TDK1969/nonebot_plugin_bilibilibot/9614d4eb93f10dda2c36b27c3504f581d381b7d1/docs/follow.jpg -------------------------------------------------------------------------------- /docs/help2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TDK1969/nonebot_plugin_bilibilibot/9614d4eb93f10dda2c36b27c3504f581d381b7d1/docs/help2.jpg -------------------------------------------------------------------------------- /docs/list.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TDK1969/nonebot_plugin_bilibilibot/9614d4eb93f10dda2c36b27c3504f581d381b7d1/docs/list.jpg -------------------------------------------------------------------------------- /docs/streampush.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TDK1969/nonebot_plugin_bilibilibot/9614d4eb93f10dda2c36b27c3504f581d381b7d1/docs/streampush.jpg -------------------------------------------------------------------------------- /docs/updatepush.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TDK1969/nonebot_plugin_bilibilibot/9614d4eb93f10dda2c36b27c3504f581d381b7d1/docs/updatepush.jpg -------------------------------------------------------------------------------- /docs/videopush.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TDK1969/nonebot_plugin_bilibilibot/9614d4eb93f10dda2c36b27c3504f581d381b7d1/docs/videopush.jpg -------------------------------------------------------------------------------- /file/source/ChangeLog.md: -------------------------------------------------------------------------------- 1 | # 更新日志 2 | - **ver 2.3.4** 3 | ``` 4 | 1. 修复了番剧集数错误以及更新不推送的问题 5 | 2. 增加了主播的下播提醒 6 | ``` 7 | 8 | - **ver 2.3.3** 9 | ``` 10 | 1. 修复了由于b站接口改变导致-352和-401的错误 11 | 2. 优化了网络IO 12 | ``` 13 | 14 | - **ver 2.3.2** 15 | ``` 16 | 1. 修复了由于b站接口改变导致关注up主命令失败的问题 17 | 2. 为了避免命令冲突,将命令`/help`改为`/bilihelp` 18 | ``` 19 | 20 | - **ver 2.3.1** 21 | ``` 22 | 1. 修复了setuptools打包错误导致的import失败问题 23 | 2. 修复了由于路径改变导致使用`/help`命令失败的问题 24 | 3. 已经完结的番剧不会再进行更新检查了 25 | ``` 26 | 27 | - **ver 2.3.0** 28 | ``` 29 | 1. 增加对动态更新推送的功能 30 | 2. 修改了文件目录结构 31 | ``` 32 | 33 | - **ver 2.2.1** 34 | ``` 35 | 1. 对应用层和数据库层进行解耦 36 | ``` 37 | 38 | - **ver 2.2.0** 39 | ``` 40 | 1. 修改某些B站的接口,减少被接口风控的风险 41 | 2. 可以使用ep_id, season_id和media_id对番剧进行关注,需要携带前两个字符 42 | 3. 本次更新需要重置数据库,因此会丢失旧版本的关注数据,需要重新关注 43 | ``` 44 | 45 | - **ver 2.1.0** 46 | ``` 47 | 1. 将文件存储方式修改为数据库,取代原来的JSON文件存储,解决编码或异步的问题 48 | 2. 适配Nonebot v2.0.0-beta.5 49 | ``` 50 | 51 | - **ver 2.0.5** 52 | ``` 53 | 1. 网络通信修改为使用httpx使用异步通信 54 | ``` 55 | 56 | - **ver 2.0.4** 57 | ``` 58 | 1. 修复在新的群中使用"查询关注"命令,创建文件失败的bug 59 | 2. 修复由于没有指定文件编码而产生的特殊字符编码错误的bug 60 | 3. 将"查询关注"命令的结果合并为一条发送 61 | 4. 添加了超级管理员权限,超级管理员可以在非管理员的群进行机器人的操作 62 | ``` 63 | 64 | - **ver 2.0.3** 65 | ``` 66 | 1. 修复Windows中使用帮助和公告命令时发生txt文件编码错误的bug 67 | ``` 68 | 69 | - **ver 2.0.2** 70 | ``` 71 | 1. 修复使用手机客户端分享链接关注up操作失败的bug 72 | 2. 删除了一些无关文字 73 | ``` 74 | 75 | - **ver 2.0.1** 76 | ``` 77 | 1. 修复取关操作时将文件删除的bug 78 | ``` 79 | 80 | - **ver 2.0.0** 81 | ``` 82 | 1. 正式支持群功能 83 | 2. 删除了错误指令的提示 84 | ``` -------------------------------------------------------------------------------- /file/source/announcement.json: -------------------------------------------------------------------------------- 1 | "【2022-4-2】公告\n> 更新至版本2.0\n> 正式加入群消息功能\n> 感谢@0w0w0帮助进行测试" -------------------------------------------------------------------------------- /file/source/help.json: -------------------------------------------------------------------------------- 1 | "命令列表:\n\n获取帮助\n> /bilihelp\n> /帮助\n\n关注主播\n> /关注主播 uid\n> 从B站app转发直播间(群不适用)\n\n取关主播\n> /切割主播 uid \n> /取关主播 uid\n\n关注up主\n> /关注up\n> 从B站app转发个人空间(群不适用)\n\n取关up主\n> /取关up主 uid\n\n关注番剧\n> /关注番剧 ep_id或season_id或media_id\n(番剧相关页面,url中以ep,ss,md开头的字符串)\n> 从B站app转发播放页面(推荐)(群不适用)\n\n\n取关番剧\n> /取关番剧 seasonid\n(seasonid: 与epid不同,请通过/查询关注 命令查看番剧的seasonid)\n\n查询关注\n> /查询关注\n> /查询成分\n\nAbout\n> 项目主页: https://github.com/TDK1969/nonebot_plugin_bilibilibot\n> 邮箱: tdk1969@foxmail.com\n> 出现问题请在github开issue或联系邮箱" -------------------------------------------------------------------------------- /file/source/help.txt: -------------------------------------------------------------------------------- 1 | 命令列表: 2 | 3 | 获取帮助 4 | > /bilihelp 5 | > /帮助 6 | 7 | 关注主播 8 | > /关注主播 uid 9 | > 从B站app转发直播间(群不适用) 10 | 11 | 取关主播 12 | > /unfollowStreamer uid 13 | > /切割主播 uid 14 | 15 | 关注up主 16 | > /关注up 17 | > 从B站app转发个人空间(群不适用) 18 | 19 | 取关up主 20 | > /取关up主 uid 21 | 22 | 关注番剧 23 | > /关注番剧 epid 24 | > 从B站app转发播放页面(推荐)(群不适用) 25 | (epid: 番剧播放界面网址,例如https://www.bilibili.com/bangumi/play/ep468705中,以ep开头的字符串) 26 | 27 | 28 | 取关番剧 29 | > /取关番剧 seasonid 30 | (seasonid: 与epid不同,请通过/查询关注 命令查看番剧的seasonid) 31 | 32 | 查询关注 33 | > /查询关注 34 | > /查询成分 35 | 36 | About 37 | > 项目主页: https://github.com/TDK1969/nonebot_plugin_bilibilibot 38 | > 邮箱: tdk1969@foxmail.com 39 | > 出现问题请在github开issue或联系邮箱 --------------------------------------------------------------------------------