├── .editorconfig ├── .eslintignore ├── .eslintrc.cjs ├── .github └── workflows │ └── build.yml ├── .gitignore ├── .prettierignore ├── .prettierrc.yaml ├── .vscode ├── extensions.json ├── launch.json └── settings.json ├── LICENSE ├── README.md ├── build ├── entitlements.mac.plist ├── icon.icns ├── icon.ico └── icon.png ├── dev-app-update.yml ├── doc ├── imgages │ ├── batch.gif │ ├── ca.png │ ├── main.jpg │ ├── monitoring.gif │ └── setting.jpg └── mysql.sql ├── electron-builder.yml ├── electron.vite.config.ts ├── package-lock.json ├── package.json ├── resources └── icon.png ├── src ├── main │ ├── epubWorker.ts │ ├── index.ts │ ├── logger.ts │ ├── service.ts │ ├── utils.ts │ └── worker.ts ├── preload │ ├── index.d.ts │ └── index.ts └── renderer │ ├── auto-imports.d.ts │ ├── components.d.ts │ ├── index.html │ └── src │ ├── App.vue │ ├── assets │ ├── base.css │ └── main.css │ ├── env.d.ts │ ├── main.ts │ └── views │ ├── EpubCreator.vue │ ├── Home.vue │ └── Setting.vue ├── tsconfig.json ├── tsconfig.node.json └── tsconfig.web.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | # 编码格式 5 | charset = utf-8 6 | # 缩进方式 7 | indent_style = space 8 | # 缩进空格数 9 | indent_size = 2 10 | # 定义换行符 11 | end_of_line = lf 12 | # 文件是否以空白行结尾 13 | insert_final_newline = true 14 | # 是否去处行首行尾的空白字符 15 | trim_trailing_whitespace = true -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | out 4 | .gitignore 5 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | /* eslint-env node */ 2 | require('@rushstack/eslint-patch/modern-module-resolution'); 3 | 4 | module.exports = { 5 | extends: ['eslint:recommended', 'plugin:vue/vue3-recommended', '@electron-toolkit', '@electron-toolkit/eslint-config-ts/eslint-recommended', '@vue/eslint-config-typescript/recommended', '@vue/eslint-config-prettier'], 6 | rules: { 7 | '@typescript-eslint/no-explicit-any': 'warn', 8 | 'vue/require-default-prop': 'off', 9 | 'vue/multi-word-component-names': 'off', 10 | semi: [0] 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | tags: 6 | - "*" 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: windows-latest 12 | 13 | steps: 14 | - name: Check out Git repository 15 | uses: actions/checkout@v3 16 | 17 | - name: Use Node.js 18 | uses: actions/setup-node@v3 19 | with: 20 | node-version: 18.x 21 | cache: 'npm' 22 | 23 | - name: Install dependencies 24 | run: npm install 25 | 26 | - name: build win 27 | run: npm run build:win 28 | env: 29 | GH_TOKEN: ${{ secrets.GH_TOKEN }} 30 | 31 | - name: release 32 | uses: softprops/action-gh-release@v1 33 | with: 34 | files: | 35 | dist/*.exe 36 | dist/*.exe.blockmap 37 | dist/latest.yml 38 | draft: false 39 | env: 40 | GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} 41 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | out 4 | .DS_Store 5 | *.log* -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | out 2 | dist 3 | pnpm-lock.yaml 4 | LICENSE.md 5 | tsconfig.json 6 | tsconfig.*.json 7 | -------------------------------------------------------------------------------- /.prettierrc.yaml: -------------------------------------------------------------------------------- 1 | singleQuote: true 2 | semi: true 3 | printWidth: 500 4 | trailingComma: none 5 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": ["dbaeumer.vscode-eslint"] 3 | } 4 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "Debug Main Process", 6 | "type": "node", 7 | "request": "launch", 8 | "cwd": "${workspaceRoot}", 9 | "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron-vite", 10 | "windows": { 11 | "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron-vite.cmd" 12 | }, 13 | "runtimeArgs": ["--sourcemap"], 14 | "env": { 15 | "REMOTE_DEBUGGING_PORT": "9222" 16 | } 17 | }, 18 | { 19 | "name": "Debug Renderer Process", 20 | "port": 9222, 21 | "request": "attach", 22 | "type": "chrome", 23 | "webRoot": "${workspaceFolder}/src/renderer", 24 | "timeout": 60000, 25 | "presentation": { 26 | "hidden": true 27 | } 28 | } 29 | ], 30 | "compounds": [ 31 | { 32 | "name": "Debug All", 33 | "configurations": ["Debug Main Process", "Debug Renderer Process"], 34 | "presentation": { 35 | "order": 1 36 | } 37 | } 38 | ] 39 | } 40 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "[typescript]": { 3 | "editor.defaultFormatter": "esbenp.prettier-vscode" 4 | }, 5 | "[javascript]": { 6 | "editor.defaultFormatter": "esbenp.prettier-vscode" 7 | }, 8 | "[json]": { 9 | "editor.defaultFormatter": "esbenp.prettier-vscode" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /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 | # wechatDownload 2 | 3 | 微信公众号文章下载工具 4 | 5 | 此仓库已停止维护,感谢使用。 6 | 7 | ## 前言 8 | 9 | 能来 github 的,我默认都是友好的技术人员。大家提 issues 前,请确保你已经按照下面的说明正确安装证书。在 issues 中详细描述清楚你的环境(系统版本、软件版本、数据库版本等)和遇到的问题,并附上日志(设置中心->打开日志位置) 10 | 11 | 参考:[提问的智慧](https://github.com/tvvocold/How-To-Ask-Questions-The-Smart-Way) 12 | 13 | ## 项目介绍 14 | 15 | ### 技术栈 16 | 17 | Electron + Typescript + VUE3 18 | 19 | ### 原理 20 | 21 | 获取微信公号文章列表,需要 3 个特殊参数: 22 | 23 | - \_biz:公众号的 id 24 | - uin:微信用户的 ID 25 | - key:不知道是啥 26 | 27 | 这 3 个参数通过 http 代理获取,剩下的就是普通爬虫的做法了 28 | 29 | ### 使用 30 | 31 | ![image-20230112181356841](doc/imgages/main.jpg) 32 | 33 | ![image-20230821104149231](doc/imgages/setting.jpg) 34 | 35 | - 单篇文章下载 36 | 37 | 直接输入链接,点击下载按钮即可 38 | 39 | 此方式无需登录微信,也因此无法获取评论和文章中QQ音乐音频,如需要这两样数据,请使用批量下载或监控下载 40 | 41 | - 批量下载 42 | 43 | 1. 初次使用请安装证书, 44 | 45 | - 自动安装(仅限window系统) 46 | 47 | 需要管理员权限(右击软件图标 -> 以管理员身份运行) 48 | 49 | 设置中心 → 安装证书 50 | 51 | - 手动安装 52 | 53 | 设置中心 → 打开证书路径 → 打开rootCA.crt文件 54 | ![Untitled](doc/imgages/ca.png) 55 | 56 | 2. 需要安装电脑版微信 57 | 58 | 3. 点击**批量下载**按钮,开始监听微信公号数据 59 | 60 | 4. 在电脑版微信打开一篇需要下载的公号的文章 61 | 62 | 5. 回到WechatDownload,会弹框提示 63 | ![wechatDownload.gif](doc/imgages/batch.gif) 64 | 65 | - 监控下载 66 | 67 | 1. 需要安装电脑版微信 68 | 69 | 2. 在WechatDownload点击**监控下载**按钮(按钮会变颜色) 70 | 71 | 3. 在电脑版微信打开需要下载的文章(可以打开多篇文章) 72 | 73 | 4. 回到WechatDownload,再次点击**监控下载**按钮即可开始下载 74 | 75 | ![wechatDownload](doc/imgages/monitoring.gif) 76 | 77 | - 保存至 MySql 78 | 79 | 需要执行 /doc/mysql.sql 文件中的 SQL 语句创建表 80 | 81 | - 线程配置 82 | 83 | 时间间隔:单位是毫秒,假设时间间隔500,单线程是下载完一篇文章,等待500毫秒再继续下载。多线程就是每500毫秒异步下载文章,无需等待上一篇文章下载完成。 84 | 85 | 单批数量:假设单批数量10,每次会同时异步下载10篇文章,等待这10篇下载完成,再继续下载10篇。 86 | 87 | - 过滤规则 88 | 89 | 目前支持对标题和作者进行关键词过滤 90 | 91 | ```json 92 | { 93 | "title": { 94 | "include": ["包含关键词1", "包含关键词2"], 95 | "exclude": ["排除关键词1","排除关键词2"] 96 | }, 97 | "auth": { 98 | "include": ["包含关键词1", "包含关键词2"], 99 | "exclude": ["排除关键词1", "排除关键词2"] 100 | } 101 | } 102 | ``` 103 | 104 | 举例子,如果需要作者是 张三 并且标题包含 好人,那就是 105 | 106 | ```json 107 | { 108 | "title": { 109 | "include": ["好人"] 110 | }, 111 | "auth": { 112 | "include": ["张三"] 113 | } 114 | } 115 | ``` 116 | 117 | - 生成Epub 118 | 119 | 支持通过 HTML 文件生成 Epub 电子书,所以使用需要先使用**批量下载**将公众号文章保存到本地,再生成 Epub 120 | 121 | 使用参数如下 122 | 123 | - 文件名:必要参数。例如填写 **test**,最后就会生成 **test.epub** 文件 124 | 125 | - 文件夹:必要参数。保存了 HTML 文件的文件夹,也就是 Epub 的数据来源 126 | - 封面图片:Epub 文件的封面图片,支持 jpg、png 格式 127 | 128 | ### 功能 129 | 130 | 设置中心有啥就支持啥 131 | 132 | - 支持选择下载范围 133 | - 将网页抓换成HTML、Markdown、PDF 134 | - 将网页源码保存至Mysql(下载来源是网络才有效) 135 | - 下载图片、音频到本地 136 | - 添加原文链接、元数据(作者、时间、公号名) 137 | - 跳过现有文章 138 | - 下载评论 139 | - 下载来源(此选项只影响批量下载): 140 | - 网络:就是从微信接口获取文章 141 | - 数据库:如果选择了**保存至Mysql**选项,数据库中会保存文章的网页源码,此时如果需要将源码转换成HTML、Markdown ,选择下载来源是数据库即可。(微信接口用得多会被限制) 142 | 143 | ## 源码运行&编译 144 | 145 | ### 安装 146 | 147 | ```bash 148 | $ npm install 149 | ``` 150 | 151 | ### 调试 152 | 153 | ```bash 154 | $ npm run dev 155 | ``` 156 | 157 | ### 编译 158 | 159 | ```bash 160 | # For windows 161 | $ npm run build:win 162 | 163 | # For macOS 164 | $ npm run build:mac 165 | 166 | # For Linux 167 | $ npm run build:linux 168 | ``` 169 | 170 | ## 特别感谢 171 | 172 | [![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=wechatDownload) 173 | 174 | 感谢 [JetBrains](https://www.jetbrains.com/?from=wechatDownload) 提供的开源开发许可证 -------------------------------------------------------------------------------- /build/entitlements.mac.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.cs.allow-jit 6 | 7 | com.apple.security.cs.allow-unsigned-executable-memory 8 | 9 | com.apple.security.cs.allow-dyld-environment-variables 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /build/icon.icns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoguyu/wechatDownload/0e458f26187b64dc472455d2521d75f64ff0c003/build/icon.icns -------------------------------------------------------------------------------- /build/icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoguyu/wechatDownload/0e458f26187b64dc472455d2521d75f64ff0c003/build/icon.ico -------------------------------------------------------------------------------- /build/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoguyu/wechatDownload/0e458f26187b64dc472455d2521d75f64ff0c003/build/icon.png -------------------------------------------------------------------------------- /dev-app-update.yml: -------------------------------------------------------------------------------- 1 | version: 1.3.0 2 | files: 3 | - url: wechatDownload-Setup-1.3.0.exe 4 | sha512: 9wuFd/Vhsq9irgaGR5iDrGA6NQwGtyXo0F/3FxmaqcUGw6cR505MylVTa2M9iTFAUETAtVMRBfAVAgWP4POaUQ== 5 | size: 77122997 6 | path: wechatDownload-Setup-1.3.0.exe 7 | sha512: 9wuFd/Vhsq9irgaGR5iDrGA6NQwGtyXo0F/3FxmaqcUGw6cR505MylVTa2M9iTFAUETAtVMRBfAVAgWP4POaUQ== 8 | releaseDate: '2023-08-11T09:44:38.105Z' 9 | provider: github 10 | owner: xiaoguyu 11 | repo: wechatDownload -------------------------------------------------------------------------------- /doc/imgages/batch.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoguyu/wechatDownload/0e458f26187b64dc472455d2521d75f64ff0c003/doc/imgages/batch.gif -------------------------------------------------------------------------------- /doc/imgages/ca.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoguyu/wechatDownload/0e458f26187b64dc472455d2521d75f64ff0c003/doc/imgages/ca.png -------------------------------------------------------------------------------- /doc/imgages/main.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoguyu/wechatDownload/0e458f26187b64dc472455d2521d75f64ff0c003/doc/imgages/main.jpg -------------------------------------------------------------------------------- /doc/imgages/monitoring.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoguyu/wechatDownload/0e458f26187b64dc472455d2521d75f64ff0c003/doc/imgages/monitoring.gif -------------------------------------------------------------------------------- /doc/imgages/setting.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoguyu/wechatDownload/0e458f26187b64dc472455d2521d75f64ff0c003/doc/imgages/setting.jpg -------------------------------------------------------------------------------- /doc/mysql.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS wx_article; 2 | CREATE TABLE wx_article ( 3 | id INT ( 11 ) NOT NULL AUTO_INCREMENT, 4 | title VARCHAR ( 255 ) NULL DEFAULT NULL COMMENT '标题', 5 | content LONGTEXT NULL COMMENT '内容', 6 | author VARCHAR ( 255 ) NULL DEFAULT NULL COMMENT '作者', 7 | content_url VARCHAR ( 1023 ) NULL DEFAULT NULL COMMENT '详情链接', 8 | create_time datetime ( 0 ) NULL DEFAULT NULL, 9 | copyright_stat INT ( 11 ) NULL DEFAULT NULL, 10 | PRIMARY KEY ( id ) USING BTREE, 11 | UNIQUE INDEX uni_title ( title, create_time ) USING BTREE 12 | ) ; 13 | 14 | -- 2023-4-1 添加评论字段 15 | ALTER TABLE wx_article ADD COLUMN comm LONGTEXT NULL COMMENT '精选评论', 16 | ADD COLUMN comm_reply LONGTEXT NULL COMMENT '评论回复'; 17 | 18 | -- 2024-3-19 19 | ALTER TABLE wx_article ADD COLUMN digest VARCHAR(1023) NULL COMMENT '摘要', 20 | ADD COLUMN cover VARCHAR(511) NULL COMMENT '封面', 21 | ADD COLUMN js_name VARCHAR ( 255 ) NULL COMMENT '公众号', 22 | ADD COLUMN md_content LONGTEXT NULL COMMENT 'markdown内容'; -------------------------------------------------------------------------------- /electron-builder.yml: -------------------------------------------------------------------------------- 1 | appId: com.javaedit.app 2 | productName: wechatDownload 3 | directories: 4 | buildResources: build 5 | files: 6 | - '!**/.vscode/*' 7 | - '!src/*' 8 | - '!electron.vite.config.{js,ts,mjs,cjs}' 9 | - '!{.eslintignore,.eslintrc.cjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}' 10 | - '!{.env,.env.*,.npmrc,pnpm-lock.yaml}' 11 | - '!{tsconfig.json,tsconfig.node.json,tsconfig.web.json}' 12 | asarUnpack: 13 | - resources/** 14 | win: 15 | executableName: wechatDownload 16 | nsis: 17 | oneClick: false # 创建一键安装程序还是辅助安装程序(默认是一键安装) 18 | allowElevation: true # 是否允许请求提升,如果为false,则用户必须使用提升的权限重新启动安装程序 (仅作用于辅助安装程序) 19 | allowToChangeInstallationDirectory: true # 是否允许修改安装目录 (仅作用于辅助安装程序) 20 | createStartMenuShortcut: true # 是否创建开始菜单快捷方式 21 | artifactName: ${productName}-${version}-${platform}-${arch}.${ext} 22 | shortcutName: ${productName} 23 | uninstallDisplayName: ${productName} 24 | createDesktopShortcut: always 25 | mac: 26 | entitlementsInherit: build/entitlements.mac.plist 27 | extendInfo: 28 | - NSCameraUsageDescription: Application requests access to the device's camera. 29 | - NSMicrophoneUsageDescription: Application requests access to the device's microphone. 30 | - NSDocumentsFolderUsageDescription: Application requests access to the user's Documents folder. 31 | - NSDownloadsFolderUsageDescription: Application requests access to the user's Downloads folder. 32 | notarize: false 33 | dmg: 34 | artifactName: ${name}-${version}.${ext} 35 | linux: 36 | target: 37 | - AppImage 38 | - snap 39 | - deb 40 | maintainer: electronjs.org 41 | category: Utility 42 | appImage: 43 | artifactName: ${name}-${version}.${ext} 44 | npmRebuild: false 45 | publish: 46 | provider: github 47 | owner: xiaoguyu 48 | repo: wechatDownload 49 | releaseInfo: 50 | releaseNotes: | 51 | 修复单线程下载无法获取评论的问题 -------------------------------------------------------------------------------- /electron.vite.config.ts: -------------------------------------------------------------------------------- 1 | import { resolve } from 'path'; 2 | import { defineConfig, externalizeDepsPlugin } from 'electron-vite'; 3 | import vue from '@vitejs/plugin-vue'; 4 | import AutoImport from 'unplugin-auto-import/vite'; 5 | import Components from 'unplugin-vue-components/vite'; 6 | import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'; 7 | 8 | export default defineConfig({ 9 | main: { 10 | plugins: [externalizeDepsPlugin()], 11 | build: { 12 | rollupOptions: { 13 | output: { 14 | format: 'es' 15 | } 16 | } 17 | } 18 | }, 19 | preload: { 20 | plugins: [externalizeDepsPlugin()] 21 | }, 22 | renderer: { 23 | resolve: { 24 | alias: { 25 | '@renderer': resolve('src/renderer/src') 26 | } 27 | }, 28 | plugins: [ 29 | vue(), 30 | AutoImport({ 31 | resolvers: [ElementPlusResolver()] 32 | }), 33 | Components({ 34 | resolvers: [ElementPlusResolver()] 35 | }) 36 | ] 37 | } 38 | }); 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wechatDownload", 3 | "version": "1.7.1", 4 | "description": "An Electron application with Vue and TypeScript", 5 | "main": "./out/main/index.mjs", 6 | "author": "javaedit.com", 7 | "homepage": "https://electron-vite.org", 8 | "scripts": { 9 | "format": "prettier --write .", 10 | "lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts,.vue --fix", 11 | "typecheck:node": "tsc --noEmit -p tsconfig.node.json --composite false", 12 | "typecheck:web": "vue-tsc --noEmit -p tsconfig.web.json --composite false", 13 | "typecheck": "npm run typecheck:node && npm run typecheck:web", 14 | "start": "electron-vite preview", 15 | "dev": "electron-vite dev", 16 | "build": "npm run typecheck && electron-vite build", 17 | "postinstall": "electron-builder install-app-deps", 18 | "build:unpack": "npm run build && electron-builder --dir", 19 | "build:win": "npm run build && electron-builder --win", 20 | "build:mac": "npm run build && electron-builder --mac", 21 | "build:linux": "npm run build && electron-builder --linux" 22 | }, 23 | "dependencies": { 24 | "@electron-toolkit/preload": "^3.0.0", 25 | "@electron-toolkit/utils": "^3.0.0", 26 | "@lesjoursfr/html-to-epub": "^4.2.3", 27 | "@mozilla/readability": "^0.4.4", 28 | "@types/anyproxy": "^4.1.5", 29 | "@types/blueimp-md5": "^2.18.2", 30 | "anyproxy": "^4.1.3", 31 | "axios": "^1.6.8", 32 | "axios-retry": "^4.1.0", 33 | "blueimp-md5": "^2.19.0", 34 | "cheerio": "^1.0.0-rc.12", 35 | "electron-log": "^5.1.2", 36 | "electron-store": "^8.2.0", 37 | "electron-updater": "^6.1.7", 38 | "element-plus": "^2.6.3", 39 | "jsdom": "^24.0.0", 40 | "mysql2": "^3.9.3", 41 | "readability": "^0.1.0", 42 | "turndown": "^7.1.3" 43 | }, 44 | "devDependencies": { 45 | "@electron-toolkit/eslint-config": "^1.0.2", 46 | "@electron-toolkit/eslint-config-ts": "^1.0.1", 47 | "@electron-toolkit/tsconfig": "^1.0.1", 48 | "@rushstack/eslint-patch": "^1.7.1", 49 | "@types/node": "^18.19.9", 50 | "@vitejs/plugin-vue": "^5.0.3", 51 | "@vue/eslint-config-prettier": "^9.0.0", 52 | "@vue/eslint-config-typescript": "^12.0.0", 53 | "electron": "^28.2.0", 54 | "electron-builder": "^24.9.1", 55 | "electron-vite": "^2.0.0", 56 | "eslint": "^8.56.0", 57 | "eslint-plugin-vue": "^9.20.1", 58 | "prettier": "^3.2.4", 59 | "typescript": "^5.3.3", 60 | "unplugin-auto-import": "^0.17.5", 61 | "unplugin-vue-components": "^0.26.0", 62 | "vite": "^5.0.12", 63 | "vue": "^3.4.15", 64 | "vue-tsc": "^1.8.27" 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /resources/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaoguyu/wechatDownload/0e458f26187b64dc472455d2521d75f64ff0c003/resources/icon.png -------------------------------------------------------------------------------- /src/main/epubWorker.ts: -------------------------------------------------------------------------------- 1 | import { parentPort, workerData } from 'worker_threads'; 2 | import logger from './logger'; 3 | import * as fs from 'fs'; 4 | import { EPub, EpubOptions, EpubContentOptions } from '@lesjoursfr/html-to-epub'; 5 | import * as cheerio from 'cheerio'; 6 | import { JSDOM } from 'jsdom'; 7 | import * as path from 'path'; 8 | 9 | import { NodeWorkerResponse, NwrEnum } from './service'; 10 | 11 | const port = parentPort; 12 | if (!port) throw new Error('IllegalState'); 13 | 14 | // epub文件名 15 | const title: string = workerData.title; 16 | // 数据来源文件夹 17 | const epubDataPath: string = workerData.epubDataPath; 18 | // 封面图片 19 | const epubCover: string = workerData.epubCover && workerData.epubCover.length > 0 ? workerData.epubCover : undefined; 20 | // 缓存路径 21 | const tmpPath: string = workerData.tmpPath; 22 | 23 | // 接收消息,执行任务 24 | port.on('message', async (message: NodeWorkerResponse) => { 25 | if (message.code == NwrEnum.START) { 26 | resp(NwrEnum.SUCCESS, '正在生成Epub,开始获取文章...'); 27 | 28 | const limitCount = 666; 29 | // 遍历文件夹,获取文章 30 | const htmlArr: string[] = []; 31 | listHtmlFile(epubDataPath, htmlArr, 0); 32 | resp(NwrEnum.SUCCESS, `已获取到${htmlArr.length}篇文章,开始转换...`); 33 | 34 | // 生成Epub 35 | if (htmlArr.length > 0) { 36 | if (htmlArr.length > limitCount) { 37 | resp(NwrEnum.SUCCESS, `文章数量超出限制,只转换${limitCount}篇文章`); 38 | htmlArr.length = limitCount; 39 | } 40 | 41 | const epubItemArr: EpubContentOptions[] = []; 42 | for (const htmlPath of htmlArr) { 43 | const itemData = await getEpubItemData(htmlPath); 44 | epubItemArr.push(itemData); 45 | resp(NwrEnum.SUCCESS, `【${itemData.title}】转换完成`); 46 | } 47 | 48 | resp(NwrEnum.SUCCESS, '开始创建Epub'); 49 | 50 | const option: EpubOptions = { 51 | title: title, 52 | description: 'created by wechatDownload', 53 | tocTitle: '目录', 54 | author: 'Nobody', 55 | tempDir: tmpPath, 56 | cover: epubCover, 57 | content: epubItemArr 58 | }; 59 | 60 | const savePath = path.join(epubDataPath, title + '.epub'); 61 | const epub = new EPub(option, savePath); 62 | 63 | epub 64 | .render() 65 | .then(() => { 66 | resp(NwrEnum.SUCCESS, `Epub创建成功,存放位置:${savePath}`); 67 | resp(NwrEnum.CLOSE, ''); 68 | }) 69 | .catch((err) => { 70 | console.error('Epub创建失败', err); 71 | resp(NwrEnum.SUCCESS, 'Epub创建失败'); 72 | resp(NwrEnum.CLOSE, ''); 73 | }); 74 | } 75 | } 76 | }); 77 | 78 | port.on('close', () => { 79 | logger.info('on 线程关闭'); 80 | }); 81 | 82 | port.addListener('close', () => { 83 | logger.info('addListener 线程关闭'); 84 | }); 85 | 86 | /** 87 | * 递归文件夹获取html文件 88 | * @param dirPath 文件夹路径 89 | * @param htmlArr 存放html文件的数组 90 | * @param inLevel 递归层级 91 | */ 92 | function listHtmlFile(dirPath: string, htmlArr: string[], inLevel: number) { 93 | const files = fs.readdirSync(dirPath); 94 | files.forEach((file) => { 95 | const filePath = path.join(dirPath, file); 96 | const stats = fs.statSync(filePath); 97 | 98 | if (stats.isDirectory()) { 99 | if (inLevel < 3) { 100 | listHtmlFile(filePath, htmlArr, inLevel + 1); 101 | } 102 | } else { 103 | if (filePath.endsWith('.html')) { 104 | htmlArr.push(filePath); 105 | } 106 | } 107 | }); 108 | } 109 | 110 | /** 111 | * 将html文件转成epub数据 112 | * @param htmlPath html文件路径 113 | */ 114 | async function getEpubItemData(htmlPath: string): Promise { 115 | const htmlStr = fs.readFileSync(htmlPath, 'utf8'); 116 | const dom = new JSDOM(htmlStr, { runScripts: 'dangerously' }); 117 | // 等待页面渲染完成 118 | await new Promise((resolve, reject) => { 119 | dom.window.addEventListener('load', () => { 120 | resolve('ok'); 121 | }); 122 | 123 | setTimeout(() => { 124 | reject(); 125 | }, 2000); 126 | }); 127 | 128 | const folderPath = path.dirname(htmlPath); 129 | // 处理html内容,删除标题和js,将相对路径图片转成绝对路径 130 | const $ = cheerio.load(dom.serialize()); 131 | $('script').remove(); 132 | $('h1').remove(); 133 | const srcArr = $('[src]'); 134 | for (let i = 0; i < srcArr.length; i++) { 135 | const $ele = $(srcArr[i]); 136 | const src = $ele.attr('src'); 137 | if (src && src.length > 0) { 138 | if (!src.startsWith('http')) { 139 | // 获取相对路径 140 | $ele.attr('src', 'file://' + path.resolve(folderPath, src)); 141 | } 142 | } 143 | } 144 | 145 | const title = path.basename(folderPath); 146 | 147 | return { 148 | title: title, 149 | data: $.xml() 150 | }; 151 | } 152 | 153 | function resp(code: NwrEnum, message: string, data?) { 154 | logger.info('resp', code, message, data); 155 | port!.postMessage(new NodeWorkerResponse(code, message, data)); 156 | } 157 | -------------------------------------------------------------------------------- /src/main/index.ts: -------------------------------------------------------------------------------- 1 | import { app, dialog, shell, ipcMain, BrowserWindow, OpenDialogOptions, MessageBoxOptions } from 'electron'; 2 | import { electronApp, optimizer, is } from '@electron-toolkit/utils'; 3 | import Store from 'electron-store'; 4 | import * as mysql from 'mysql2'; 5 | import * as AnyProxy from 'anyproxy'; 6 | import * as path from 'path'; 7 | import * as os from 'os'; 8 | import { HttpUtil } from './utils'; 9 | import logger from './logger'; 10 | import { GzhInfo, ArticleInfo, PdfInfo, NodeWorkerResponse, NwrEnum, DlEventEnum, DownloadOption } from './service'; 11 | import creatWorker from './worker?nodeWorker'; 12 | import createEpubWorker from './epubWorker?nodeWorker'; 13 | import * as fs from 'fs'; 14 | import icon from '../../resources/icon.png?asset'; 15 | import * as child_process from 'child_process'; 16 | import * as iconv from 'iconv-lite'; 17 | 18 | import electronUpdater, { type AppUpdater, type UpdateInfo } from 'electron-updater'; 19 | 20 | export function getAutoUpdater(): AppUpdater { 21 | // Using destructuring to access autoUpdater due to the CommonJS module of 'electron-updater'. 22 | // It is a workaround for ESM compatibility issues, see https://github.com/electron-userland/electron-builder/issues/7976. 23 | const { autoUpdater } = electronUpdater; 24 | return autoUpdater; 25 | } 26 | const autoUpdater = getAutoUpdater(); 27 | 28 | const exec = child_process.exec; 29 | const store = new Store(); 30 | // const service = new Service(); 31 | 32 | // 代理 33 | let PROXY_SERVER: AnyProxy.ProxyServer; 34 | let MAIN_WINDOW: BrowserWindow; 35 | // 存储公众号信息的对象 36 | let GZH_INFO: GzhInfo; 37 | // 用于定时关闭代理的对象 38 | let TIMER: NodeJS.Timeout; 39 | let DL_TYPE = DlEventEnum.BATCH_WEB; 40 | let articleArr; 41 | 42 | // 配置的保存文件的路径 43 | logger.debug('store.path', store.path); 44 | 45 | function createWindow(): void { 46 | MAIN_WINDOW = new BrowserWindow({ 47 | width: 900, 48 | height: 680, 49 | show: false, 50 | autoHideMenuBar: true, 51 | ...(process.platform === 'linux' ? { icon } : {}), 52 | webPreferences: { 53 | preload: path.join(__dirname, '../preload/index.js'), 54 | sandbox: false 55 | } 56 | }); 57 | 58 | MAIN_WINDOW.setMenu(null); 59 | 60 | MAIN_WINDOW.on('ready-to-show', () => { 61 | MAIN_WINDOW.show(); 62 | }); 63 | 64 | MAIN_WINDOW.webContents.setWindowOpenHandler((details) => { 65 | shell.openExternal(details.url); 66 | return { action: 'deny' }; 67 | }); 68 | 69 | // HMR热加载 70 | if (is.dev && process.env['ELECTRON_RENDERER_URL']) { 71 | MAIN_WINDOW.loadURL(process.env['ELECTRON_RENDERER_URL']); 72 | } else { 73 | MAIN_WINDOW.loadFile(path.join(__dirname, '../renderer/index.html')); 74 | } 75 | 76 | // 打开f12调试 77 | // MAIN_WINDOW.webContents.openDevTools(); 78 | } 79 | 80 | app.whenReady().then(() => { 81 | setDefaultSetting(); 82 | 83 | // Set app user model id for windows 84 | electronApp.setAppUserModelId('com.javaedit'); 85 | 86 | // Default open or close DevTools by F12 in development 87 | // and ignore CommandOrControl + R in production. 88 | // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils 89 | app.on('browser-window-created', (_, window) => { 90 | optimizer.watchWindowShortcuts(window); 91 | }); 92 | 93 | // 安装证书 94 | ipcMain.on('install-licence', () => { 95 | installCAFile(path.join(store.get('caPath'), 'rootCA.crt')); 96 | }); 97 | // 打开日志文件夹 98 | ipcMain.on('open-logs-dir', () => { 99 | shell.openPath(path.join(app.getPath('appData'), 'wechatDownload', 'logs')); 100 | }); 101 | // electron-store的api 102 | ipcMain.on('electron-store-get', (event, val) => { 103 | event.returnValue = store.get(val); 104 | }); 105 | ipcMain.on('electron-store-set', async (_event, key, val) => { 106 | logger.info('change setting', key, val); 107 | store.set(key, val); 108 | }); 109 | // 选择路径 110 | ipcMain.on('show-open-dialog', (event, options: OpenDialogOptions, callbackMsg: string) => { 111 | const _win = BrowserWindow.fromWebContents(event.sender); 112 | if (_win) { 113 | dialog 114 | .showOpenDialog(_win, options) 115 | .then((result) => { 116 | if (!result.canceled) { 117 | // 路径信息回调 118 | event.sender.send('open-dialog-callback', callbackMsg, result.filePaths[0]); 119 | store.set(callbackMsg, result.filePaths[0]); 120 | } 121 | }) 122 | .catch((err) => { 123 | logger.error(err); 124 | }); 125 | } 126 | }); 127 | // 消息弹框 128 | ipcMain.on('show-message-box', (event, options: MessageBoxOptions) => { 129 | const _win = BrowserWindow.fromWebContents(event.sender); 130 | if (_win) { 131 | dialog.showMessageBox(_win, options); 132 | } 133 | }); 134 | // 根据url下载单篇文章 135 | ipcMain.on('download-one', (_event, url: string) => downloadOne(url)); 136 | // 批量下载,开启公号文章监测,获取用户参数 137 | ipcMain.on('monitor-article', () => monitorArticle()); 138 | // 批量下载,开启公号文章监测,获取用户参数和文章地址 139 | ipcMain.on('monitor-limit-article', () => monitorLimitArticle()); 140 | ipcMain.on('stop-monitor-limit-article', () => stopMonitorLimitArticle()); 141 | // 测试数据库连接 142 | ipcMain.on('test-connect', async () => testMysqlConnection()); 143 | // 检查更新 144 | ipcMain.on('check-for-update', () => { 145 | logger.info('触发检查更新'); 146 | autoUpdater.checkForUpdates(); 147 | }); 148 | // 返回初始化页面需要的信息 149 | ipcMain.on('load-init-info', (event) => { 150 | // 暂时只需要版本号 151 | event.returnValue = app.getVersion(); 152 | }); 153 | // 生成epub 154 | ipcMain.on('create-epub', (_event, options: any) => { 155 | options.tmpPath = store.get('tmpPath'); 156 | const epubWorker = createEpubWorker({ 157 | workerData: options 158 | }); 159 | 160 | epubWorker.on('message', (message) => { 161 | const nwResp: NodeWorkerResponse = message; 162 | switch (nwResp.code) { 163 | case NwrEnum.SUCCESS: 164 | case NwrEnum.FAIL: 165 | outputLog(nwResp.message, true); 166 | break; 167 | case NwrEnum.CLOSE: 168 | outputEpubLog('
', true, true); 169 | // 关闭线程 170 | epubWorker.terminate(); 171 | } 172 | }); 173 | 174 | outputLog('生成Epub线程启动中'); 175 | epubWorker.postMessage(new NodeWorkerResponse(NwrEnum.START, '')); 176 | }); 177 | 178 | createWindow(); 179 | 180 | // CA证书处理 181 | createCAFile(); 182 | 183 | app.on('activate', function () { 184 | if (BrowserWindow.getAllWindows().length === 0) createWindow(); 185 | }); 186 | }); 187 | 188 | app.on('window-all-closed', () => { 189 | if (PROXY_SERVER) { 190 | PROXY_SERVER.close(); 191 | // 关闭代理 192 | AnyProxy.utils.systemProxyMgr.disableGlobalProxy(); 193 | } 194 | if (process.platform !== 'darwin') { 195 | app.quit(); 196 | } 197 | }); 198 | 199 | /* 200 | * 创建CA证书 201 | * 如果没有创建ca证书,则创建,默认目录在C:\Users\xxx\.anyproxy\certificates 202 | */ 203 | function createCAFile() { 204 | if (!AnyProxy.utils.certMgr.ifRootCAFileExists()) { 205 | AnyProxy.utils.certMgr.generateRootCA((error, keyPath) => { 206 | if (!error) { 207 | const certDir = path.dirname(keyPath); 208 | logger.info('CA证书创建成功,路径:', certDir); 209 | // 安装证书 210 | installCAFile(path.join(certDir, 'rootCA.crt')); 211 | } else { 212 | logger.error('CA证书创建失败', error); 213 | dialog.showMessageBox(MAIN_WINDOW, { 214 | type: 'error', 215 | message: '证书创建失败' 216 | }); 217 | } 218 | }); 219 | } 220 | } 221 | 222 | /** 223 | * 安装ca证书 224 | * @param filePath 证书路径 225 | */ 226 | function installCAFile(filePath: string) { 227 | // 如果是window系统,则自动安装证书 228 | if (process.platform === 'win32') { 229 | exec(`certutil -addstore root ${filePath}`, { encoding: 'buffer' }, (err, _stdout, stderr) => { 230 | if (err) { 231 | logger.error('CA证书安装失败-stderr', iconv.decode(stderr, 'cp936')); 232 | logger.error('CA证书安装失败-err', err); 233 | dialog.showMessageBox(MAIN_WINDOW, { 234 | type: 'error', 235 | message: '证书安装失败,请以管理员身份运行本软件重新安装证书或手动安装' 236 | }); 237 | } else { 238 | logger.info('CA证书安装成功'); 239 | dialog 240 | .showMessageBox(MAIN_WINDOW, { 241 | type: 'info', 242 | message: '证书安装成功,准备重启软件' 243 | }) 244 | .then(() => { 245 | app.relaunch(); 246 | app.exit(); 247 | }); 248 | } 249 | }); 250 | } else { 251 | dialog.showMessageBox(MAIN_WINDOW, { 252 | type: 'error', 253 | message: '不是window系统,请手动安装' 254 | }); 255 | } 256 | } 257 | 258 | /* 259 | * 下载单个页面 260 | */ 261 | async function downloadOne(url: string) { 262 | outputLog(`正在下载文章,url:${url}`); 263 | // 开启线程下载 264 | createDlWorker(DlEventEnum.ONE, url); 265 | } 266 | 267 | /* 268 | * 创建下载文章的线程 269 | */ 270 | let worker; 271 | function createDlWorker(dlEvent: DlEventEnum, data?) { 272 | worker = creatWorker({ 273 | workerData: loadWorkerData(dlEvent, data) 274 | }); 275 | 276 | worker.on('message', (message) => { 277 | const nwResp: NodeWorkerResponse = message; 278 | switch (nwResp.code) { 279 | case NwrEnum.SUCCESS: 280 | case NwrEnum.FAIL: 281 | outputLog(nwResp.message, true); 282 | break; 283 | case NwrEnum.ONE_FINISH: 284 | if (nwResp.message) outputLog(nwResp.message, true); 285 | outputLog('
', true, true); 286 | break; 287 | case NwrEnum.BATCH_FINISH: 288 | if (nwResp.message) outputLog(nwResp.message, true); 289 | outputLog('
', true, true); 290 | MAIN_WINDOW.webContents.send('download-fnish'); 291 | break; 292 | case NwrEnum.CLOSE: 293 | // 关闭线程 294 | worker.terminate(); 295 | break; 296 | case NwrEnum.PDF: 297 | html2Pdf(nwResp.data); 298 | } 299 | }); 300 | 301 | worker.postMessage(new NodeWorkerResponse(NwrEnum.START, '')); 302 | } 303 | 304 | async function html2Pdf(pdfInfo: PdfInfo) { 305 | const pdfWindow = new BrowserWindow({ 306 | show: false, 307 | width: 1000, 308 | height: 800 309 | }); 310 | 311 | const htmlPath = path.join(pdfInfo.savePath, 'pdf.html'); 312 | pdfWindow.loadFile(htmlPath); 313 | 314 | pdfWindow.webContents.on('did-finish-load', () => { 315 | pdfWindow.webContents 316 | .printToPDF({}) 317 | .then((data) => { 318 | const fileName = pdfInfo.fileName || 'index'; 319 | fs.writeFileSync(path.join(pdfInfo.savePath, `${fileName}.pdf`), data); 320 | outputLog(`【${pdfInfo.title}】保存PDF完成`, true); 321 | }) 322 | .catch((error) => { 323 | logger.error(`保存PDF失败:${pdfInfo.title}`, error); 324 | outputLog(`【${pdfInfo.title}】保存PDF失败`, true); 325 | }) 326 | .finally(() => { 327 | pdfWindow.close(); 328 | fs.unlink(htmlPath, () => {}); 329 | // 任务完成,通知worker线程 330 | worker?.postMessage(new NodeWorkerResponse(NwrEnum.PDF_FINISHED, '', pdfInfo.id)); 331 | }); 332 | }); 333 | } 334 | 335 | /* 336 | * 开启公号文章监测,获取用户参数 337 | */ 338 | async function monitorArticle() { 339 | if ('db' == store.get('dlSource')) { 340 | // 下载来源是数据库 341 | outputLog('下载来源为数据库'); 342 | // 开启线程下载 343 | createDlWorker(DlEventEnum.BATCH_DB); 344 | } else { 345 | // 下载来源是网络 346 | if (!PROXY_SERVER) { 347 | PROXY_SERVER = createProxy(); 348 | } 349 | DL_TYPE = DlEventEnum.BATCH_WEB; 350 | // 开启代理 351 | AnyProxy.utils.systemProxyMgr.enableGlobalProxy('127.0.0.1', '8001'); 352 | AnyProxy.utils.systemProxyMgr.enableGlobalProxy('127.0.0.1', '8001', 'https'); 353 | outputLog('下载来源为网络'); 354 | outputLog('代理开启成功,准备批量下载...', true); 355 | outputLog('请在微信打开任意一篇需要批量下载的公号的文章', true); 356 | outputLog('别偷懒,已经打开的不算...', true); 357 | 358 | // 10秒之后自动关闭代理 359 | TIMER = setTimeout(() => { 360 | outputLog('批量下载超时,未监测到公号文章!', true); 361 | AnyProxy.utils.systemProxyMgr.disableGlobalProxy(); 362 | MAIN_WINDOW.webContents.send('download-fnish'); 363 | }, 15000); 364 | } 365 | } 366 | /* 367 | * 开启公号文章监测,获取文章地址和用户参数 368 | */ 369 | async function monitorLimitArticle() { 370 | if (!PROXY_SERVER) { 371 | PROXY_SERVER = createProxy(); 372 | } 373 | DL_TYPE = DlEventEnum.BATCH_SELECT; 374 | articleArr = []; 375 | // 开启代理 376 | AnyProxy.utils.systemProxyMgr.enableGlobalProxy('127.0.0.1', '8001'); 377 | AnyProxy.utils.systemProxyMgr.enableGlobalProxy('127.0.0.1', '8001', 'https'); 378 | outputLog('代理开启成功,准备监控下载...'); 379 | outputLog('请在微信打开需要下载的文章,可打开多篇文章', true); 380 | outputLog('

最后再点击一次 监控下载 按钮即可开始下载

', true, true); 381 | } 382 | 383 | async function stopMonitorLimitArticle() { 384 | // 关闭代理 385 | AnyProxy.utils.systemProxyMgr.disableGlobalProxy(); 386 | 387 | // 开启线程下载 388 | if (articleArr && articleArr.length > 0) { 389 | outputLog(`已获取${articleArr.length}篇文章,准备下载...`, true); 390 | createDlWorker(DlEventEnum.BATCH_SELECT, articleArr); 391 | } else { 392 | outputLog('获取文章失败', true); 393 | } 394 | articleArr = []; 395 | } 396 | /* 397 | * 创建代理 398 | */ 399 | function createProxy(): AnyProxy.ProxyServer { 400 | const options: AnyProxy.ProxyOptions = { 401 | port: 8001, 402 | forceProxyHttps: true, 403 | silent: true, 404 | rule: { 405 | summary: 'My Custom Rule', 406 | beforeSendResponse(requestDetail, responseDetail) { 407 | // 批量下载 408 | if (DL_TYPE == DlEventEnum.BATCH_WEB && requestDetail.url.indexOf('https://mp.weixin.qq.com/mp/getbizbanner') == 0) { 409 | const uin = HttpUtil.getQueryVariable(requestDetail.url, 'uin'); 410 | const biz = HttpUtil.getQueryVariable(requestDetail.url, '__biz'); 411 | const key = HttpUtil.getQueryVariable(requestDetail.url, 'key'); 412 | const passTicket = HttpUtil.getQueryVariable(requestDetail.url, 'pass_ticket'); 413 | if (uin && biz && key) { 414 | GZH_INFO = new GzhInfo(biz, key, uin); 415 | GZH_INFO.passTicket = passTicket; 416 | const headers = requestDetail.requestOptions.headers; 417 | if (headers) { 418 | GZH_INFO.Host = headers['Host'] as string; 419 | GZH_INFO.Cookie = headers['Cookie'] as string; 420 | GZH_INFO.UserAgent = headers['User-Agent'] as string; 421 | } 422 | 423 | logger.debug('微信公号参数', GZH_INFO); 424 | outputLog(`已监测到文章,请确认是否批量下载该文章所属公号`, true); 425 | if (!MAIN_WINDOW.focusable) { 426 | MAIN_WINDOW.focus(); 427 | } 428 | // 页面弹框确认 429 | dialog 430 | .showMessageBox(MAIN_WINDOW, { 431 | type: 'info', 432 | title: '下载', 433 | message: '请确认是否批量下载该文章所属公号', 434 | buttons: ['取消', '确定'] 435 | }) 436 | .then((index) => { 437 | if (index.response === 1) { 438 | // 开启线程下载 439 | createDlWorker(DlEventEnum.BATCH_WEB, GZH_INFO); 440 | } else { 441 | outputLog(`已取消下载!`, true); 442 | MAIN_WINDOW.webContents.send('download-fnish'); 443 | } 444 | }); 445 | 446 | AnyProxy.utils.systemProxyMgr.disableGlobalProxy(); 447 | if (TIMER) clearTimeout(TIMER); 448 | } else { 449 | logger.error('微信公号参数获取失败', requestDetail); 450 | } 451 | } 452 | // 单条下载 453 | if (DL_TYPE == DlEventEnum.BATCH_SELECT && requestDetail.url.indexOf('https://mp.weixin.qq.com/mp/geticon') == 0) { 454 | const headers = requestDetail.requestOptions.headers; 455 | if (headers) { 456 | const referer = headers['Referer'] as string; 457 | const uin = HttpUtil.getQueryVariable(referer, 'uin'); 458 | const biz = HttpUtil.getQueryVariable(referer, '__biz'); 459 | const key = HttpUtil.getQueryVariable(referer, 'key'); 460 | const mid = HttpUtil.getQueryVariable(referer, 'mid'); 461 | const sn = HttpUtil.getQueryVariable(referer, 'sn'); 462 | const chksm = HttpUtil.getQueryVariable(referer, 'chksm'); 463 | const idx = HttpUtil.getQueryVariable(referer, 'idx'); 464 | const gzhInfo = new GzhInfo(biz, key, uin); 465 | gzhInfo.Cookie = headers['Cookie'] as string; 466 | gzhInfo.UserAgent = headers['User-Agent'] as string; 467 | 468 | const articleUrl = `http://mp.weixin.qq.com/s?__biz=${biz}&mid=${mid}&idx=${idx}&sn=${sn}&chksm=${chksm}&scene=27#wechat_redirect`; 469 | 470 | const articleInfo = new ArticleInfo(null, null, ''); 471 | articleInfo.contentUrl = articleUrl; 472 | articleInfo.gzhInfo = gzhInfo; 473 | 474 | articleArr.push(articleInfo); 475 | 476 | outputLog(`已获取文章,mid:${mid}`, true); 477 | } 478 | } 479 | 480 | return responseDetail; 481 | } 482 | } 483 | }; 484 | const proxyServer = new AnyProxy.ProxyServer(options); 485 | 486 | // proxyServer.on('ready', () => { 487 | // outputLog(`代理开启成功,准备批量下载,请在微信打开任意一篇需要批量下载的公号的文章`); 488 | // }); 489 | proxyServer.on('error', () => { 490 | outputLog(`代理开启失败,请重试`); 491 | }); 492 | proxyServer.start(); 493 | return proxyServer; 494 | } 495 | 496 | /* 497 | * 测试mysql数据库连接 498 | */ 499 | async function testMysqlConnection() { 500 | if (1 != store.get('dlMysql') && 'db' != store.get('dlSource')) return; 501 | 502 | const CONNECTION = mysql.createConnection({ 503 | host: store.get('mysqlHost'), 504 | port: store.get('mysqlPort'), 505 | user: store.get('mysqlUser'), 506 | password: store.get('mysqlPassword'), 507 | database: store.get('mysqlDatabase'), 508 | charset: 'utf8mb4' 509 | }); 510 | const sql = 'show tables'; 511 | CONNECTION.query(sql, (err) => { 512 | if (err) { 513 | logger.error('mysql连接失败', err); 514 | dialog.showMessageBox(MAIN_WINDOW, { 515 | type: 'error', 516 | message: '连接失败,请检查参数' 517 | }); 518 | } else { 519 | dialog.showMessageBox(MAIN_WINDOW, { 520 | type: 'info', 521 | message: '连接成功' 522 | }); 523 | } 524 | return CONNECTION; 525 | }); 526 | } 527 | 528 | /* 529 | * 输出日志到主页面 530 | * msg:输出的消息 531 | * append:是否追加 532 | * flgHtml:消息是否是html 533 | */ 534 | async function outputLog(msg: string, append = false, flgHtml = false) { 535 | MAIN_WINDOW.webContents.send('output-log', msg, append, flgHtml); 536 | } 537 | /* 538 | * 输出日志到生成Epub页面 539 | * msg:输出的消息 540 | * append:是否追加 541 | * flgHtml:消息是否是html 542 | */ 543 | async function outputEpubLog(msg: string, append = false, flgHtml = false) { 544 | MAIN_WINDOW.webContents.send('output-epub-log', msg, append, flgHtml); 545 | } 546 | /* 547 | * 第一次运行,默认设置 548 | */ 549 | function setDefaultSetting() { 550 | const default_setting: DownloadOption = { 551 | firstRun: false, 552 | // 下载来源 553 | dlSource: 'web', 554 | // 线程类型 555 | threadType: 'multi', 556 | // 下载间隔 557 | dlInterval: 500, 558 | // 单批数量 559 | batchLimit: 10, 560 | // 下载为html 561 | dlHtml: 1, 562 | // 下载为markdown 563 | dlMarkdown: 1, 564 | // 下载为pdf 565 | dlPdf: 0, 566 | // 保存至mysql 567 | dlMysql: 0, 568 | // 下载音频到本地 569 | dlAudio: 0, 570 | // 下载图片到本地 571 | dlImg: 0, 572 | // 跳过现有文章 573 | skinExist: 1, 574 | // 是否保存元数据 575 | saveMeta: 1, 576 | // 按公号名字分类 577 | classifyDir: 1, 578 | // 添加原文链接 579 | sourceUrl: 1, 580 | // 是否下载评论 581 | dlComment: 0, 582 | // 是否下载评论回复 583 | dlCommentReply: 0, 584 | // 下载范围-7天内 585 | dlScpoe: 'seven', 586 | // 缓存目录 587 | tmpPath: path.join(os.tmpdir(), 'wechatDownload'), 588 | // 在安装目录下创建文章的保存路径 589 | savePath: path.join(app.getPath('userData'), 'savePath'), 590 | // CA证书路径 591 | caPath: (AnyProxy as any).utils.certMgr.getRootDirPath(), 592 | // mysql配置-端口 593 | mysqlHost: 'localhost', 594 | mysqlPort: 3306 595 | }; 596 | 597 | for (const i in default_setting) { 598 | sotreSetNotExit(i, default_setting[i]); 599 | } 600 | } 601 | 602 | function sotreSetNotExit(key, value): boolean { 603 | const oldValue = store.get(key); 604 | if (oldValue === '' || oldValue === null || oldValue === undefined) { 605 | store.set(key, value); 606 | logger.info('setting', key, value); 607 | return true; 608 | } 609 | logger.info('setting', key, oldValue); 610 | return false; 611 | } 612 | 613 | /* 614 | * 获取设置中心页面的配置 615 | */ 616 | function loadDownloadOption(): DownloadOption { 617 | const downloadOption = new DownloadOption(); 618 | for (const key in downloadOption) { 619 | downloadOption[key] = store.get(key); 620 | } 621 | return downloadOption; 622 | } 623 | // 获取nodeWorker的配置 624 | function loadWorkerData(dlEvent: DlEventEnum, data?) { 625 | const connectionConfig = { 626 | host: store.get('mysqlHost'), 627 | port: store.get('mysqlPort'), 628 | user: store.get('mysqlUser'), 629 | password: store.get('mysqlPassword'), 630 | database: store.get('mysqlDatabase'), 631 | charset: 'utf8mb4' 632 | }; 633 | return { 634 | connectionConfig: connectionConfig, 635 | downloadOption: loadDownloadOption(), 636 | tableName: store.get('tableName') || 'wx_article', 637 | dlEvent: dlEvent, 638 | data: data 639 | }; 640 | } 641 | 642 | /*******************以下是自动更新相关************************/ 643 | 644 | // 这里是为了在本地做应用升级测试使用 645 | // if (is.dev && process.env['ELECTRON_RENDERER_URL']) { 646 | // autoUpdater.updateConfigPath = path.join(__dirname, '../../dev-app-update.yml'); 647 | // } 648 | // Object.defineProperty(app, 'isPackaged', { 649 | // get() { 650 | // return true; 651 | // } 652 | // }); 653 | 654 | // 定义返回给渲染层的相关提示文案 655 | const updateMessage = { 656 | error: { code: 1, msg: '检查更新出错' }, 657 | checking: { code: 2, msg: '正在检查更新……' }, 658 | updateAva: { code: 3, msg: '检测到新版本,正在下载……' }, 659 | updateNotAva: { code: 4, msg: '现在使用的就是最新版本,不用更新' } 660 | }; 661 | 662 | function sendUpdateMessage(msg: any) { 663 | MAIN_WINDOW.webContents.send('update-msg', msg); 664 | } 665 | 666 | // 设置自动下载为false,也就是说不开始自动下载 667 | autoUpdater.autoDownload = false; 668 | // 检测下载错误 669 | autoUpdater.on('error', (error) => { 670 | logger.error('更新异常', error); 671 | sendUpdateMessage(updateMessage.error); 672 | }); 673 | 674 | // 检测是否需要更新 675 | autoUpdater.on('checking-for-update', () => { 676 | logger.info(updateMessage.checking); 677 | sendUpdateMessage(updateMessage.checking); 678 | }); 679 | // 检测到可以更新时 680 | autoUpdater.on('update-available', (releaseInfo: UpdateInfo) => { 681 | const releaseNotes = releaseInfo.releaseNotes; 682 | let releaseContent = ''; 683 | if (releaseNotes) { 684 | if (typeof releaseNotes === 'string') { 685 | releaseContent = releaseNotes; 686 | } else if (releaseNotes instanceof Array) { 687 | releaseNotes.forEach((releaseNote) => { 688 | releaseContent += `${releaseNote}\n`; 689 | }); 690 | } 691 | } else { 692 | releaseContent = '暂无更新说明'; 693 | } 694 | dialog 695 | .showMessageBox({ 696 | type: 'info', 697 | title: '应用有新的更新', 698 | detail: releaseContent, 699 | message: '发现新版本,是否现在更新?', 700 | buttons: ['否', '是'] 701 | }) 702 | .then(({ response }) => { 703 | if (response === 1) { 704 | sendUpdateMessage(updateMessage.updateAva); 705 | // 下载更新 706 | autoUpdater.downloadUpdate(); 707 | } 708 | }); 709 | }); 710 | // 检测到不需要更新时 711 | autoUpdater.on('update-not-available', () => { 712 | logger.info(updateMessage.updateNotAva); 713 | sendUpdateMessage(updateMessage.updateNotAva); 714 | }); 715 | // 更新下载进度 716 | autoUpdater.on('download-progress', (progress) => { 717 | MAIN_WINDOW.webContents.send('download-progress', progress); 718 | }); 719 | // 当需要更新的内容下载完成后 720 | autoUpdater.on('update-downloaded', () => { 721 | logger.info('下载完成,准备更新'); 722 | dialog 723 | .showMessageBox({ 724 | title: '安装更新', 725 | message: '更新下载完毕,应用将重启并进行安装' 726 | }) 727 | .then(() => { 728 | // 退出并安装应用 729 | setImmediate(() => autoUpdater.quitAndInstall()); 730 | }); 731 | }); 732 | -------------------------------------------------------------------------------- /src/main/logger.ts: -------------------------------------------------------------------------------- 1 | import logger from 'electron-log'; 2 | 3 | // logger.transports.file.level = 'debug'; 4 | logger.transports.file.level = 'info'; 5 | logger.transports.file.maxSize = 1002430; // 10M 6 | logger.transports.file.format = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}]{scope} {text}'; 7 | const date = new Date(); 8 | const fileName = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + '.log'; 9 | logger.transports.file.fileName = fileName; 10 | 11 | export default { 12 | info(...params: any[]) { 13 | logger.info(params); 14 | }, 15 | warn(...params: any[]) { 16 | logger.warn(params); 17 | }, 18 | error(...params: any[]) { 19 | logger.error(params); 20 | }, 21 | debug(...params: any[]) { 22 | logger.debug(params); 23 | }, 24 | verbose(...params: any[]) { 25 | logger.verbose(params); 26 | }, 27 | silly(...params: any[]) { 28 | logger.silly(params); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /src/main/service.ts: -------------------------------------------------------------------------------- 1 | import { StrUtil } from './utils'; 2 | 3 | import * as cheerio from 'cheerio'; 4 | import TurndownService from 'turndown'; 5 | 6 | // 获取公号文章列表需要的信息 7 | class GzhInfo { 8 | public biz: string; 9 | public key: string; 10 | public uin: string; 11 | public passTicket?: string | null; 12 | public Host?: string | null; 13 | public Cookie?: string | null; 14 | public UserAgent?: string | null; 15 | 16 | constructor(biz, key, uin) { 17 | this.biz = biz; 18 | this.key = key; 19 | this.uin = uin; 20 | } 21 | } 22 | // 公号文章信息类 23 | class ArticleInfo { 24 | // 标题 25 | public title?: string; 26 | // 摘要 27 | public digest?: string; 28 | // 保存文件名(因为有些标题不一定符合文件名的格式) 29 | public fileName?: string; 30 | // 时间 31 | public datetime?: Date; 32 | // 详情url 33 | public contentUrl: string; 34 | // html源码 35 | public html?: string; 36 | // 封面 37 | public cover?: string; 38 | // 作者 39 | public author?: string; 40 | // 元数据 41 | public metaInfo?: ArticleMeta; 42 | // 评论列表数据 43 | public commentList?: []; 44 | // 评论详细数据 45 | public replyDetailMap?: Map; 46 | public copyrightStat?: number; 47 | public gzhInfo?: GzhInfo; 48 | 49 | constructor(title, datetime, contentUrl) { 50 | this.title = title; 51 | this.datetime = datetime; 52 | this.contentUrl = contentUrl; 53 | } 54 | } 55 | // 文章元数据类 56 | class ArticleMeta { 57 | // 原创标识 58 | public copyrightFlg?: boolean; 59 | // 作者 60 | public author?: string; 61 | // 公号名 62 | public jsName?: string; 63 | // 发布时间 64 | public publicTime?: string; 65 | // ip位置 66 | public ipWording?: string; 67 | } 68 | // 配置类 69 | class DownloadOption { 70 | // 首次运行 71 | public firstRun?: boolean; 72 | // 下载来源 73 | public dlSource?: string; 74 | // 线程类型 75 | public threadType?: string; 76 | // 下载间隔 77 | public dlInterval?: number; 78 | // 单批数量 79 | public batchLimit?: number; 80 | // 下载为html 81 | public dlHtml?: number; 82 | // 下载为markdown 83 | public dlMarkdown?: number; 84 | // 下载为pdf 85 | public dlPdf?: number; 86 | // 保存至mysql 87 | public dlMysql?: number; 88 | // 下载音频到本地 89 | public dlAudio?: number; 90 | // 下载图片到本地 91 | public dlImg?: number; 92 | // 跳过现有文章 93 | public skinExist?: number; 94 | // 是否保存元数据 95 | public saveMeta?: number; 96 | // 是否按公号名字归类 97 | public classifyDir?: number; 98 | // 是否添加原文链接 99 | public sourceUrl?: number; 100 | // 是否下载评论 101 | public dlComment?: number; 102 | // 是否下载回复 103 | public dlCommentReply?: number; 104 | // 下载范围 105 | public dlScpoe?: string; 106 | // 下载开始时间 107 | public startDate?: string; 108 | // 下载结束时间 109 | public endDate?: string; 110 | // 保存路径 111 | public savePath?: string; 112 | // 缓存路径 113 | public tmpPath?: string; 114 | // CA证书路径 115 | public caPath?: string; 116 | // mysql配置-主机 117 | public mysqlHost?: string; 118 | // mysql配置-端口 119 | public mysqlPort?: number; 120 | // mysql配置-用户名 121 | public mysqlUser?: string; 122 | // mysql配置-密码 123 | public mysqlPassword?: string; 124 | // 是否清洗markdown,并保存数据库 125 | public cleanMarkdown?: number; 126 | // 过滤规则 127 | public filterRule?: string; 128 | } 129 | class FilterRuleInfo { 130 | // 标题包含 131 | public titleInclude: string[] = []; 132 | // 标题不包含 133 | public titleExclude: string[] = []; 134 | // 作者包含 135 | public authInclude: string[] = []; 136 | // 作者不包含 137 | public authExclude: string[] = []; 138 | } 139 | // nodeWorker交互使用的通用消息响应类 140 | class NodeWorkerResponse { 141 | public code: NwrEnum; 142 | public message: string; 143 | public data?: any; 144 | constructor(code: NwrEnum, message: string, data?) { 145 | this.code = code; 146 | this.message = message; 147 | this.data = data; 148 | } 149 | } 150 | // PDF信息类 151 | class PdfInfo { 152 | public id: string; 153 | // 标题 154 | public title: string; 155 | // 保存文件名(因为有些标题不一定符合文件名的格式) 156 | public fileName?: string; 157 | // 保存路径 158 | public savePath: string; 159 | 160 | constructor(id: string, title: string, savePath: string, fileName?: string) { 161 | this.id = id; 162 | this.title = title; 163 | this.fileName = fileName; 164 | this.savePath = savePath; 165 | } 166 | } 167 | // NodeWorkerResponse的code枚举类 168 | enum NwrEnum { 169 | START, // 启动 170 | SUCCESS, // 成功,输出日志 171 | FAIL, // 失败,输出日志并失败处理 172 | ONE_FINISH, // 单个下载结束,输出日志并做结束处理 173 | BATCH_FINISH, // 多个下载结束,输出日志并做结束处理 174 | CLOSE, // 结束线程 175 | PDF, // 创建pdf 176 | PDF_FINISHED // 创建pdf完成 177 | } 178 | // 下载事件枚举类 179 | enum DlEventEnum { 180 | ONE, // 下载单篇文章 181 | BATCH_WEB, // 微信接口批量下载 182 | BATCH_DB, // 数据库批量下载 183 | BATCH_SELECT // 批量选择下载 184 | } 185 | /* 186 | * 业务方法类 187 | */ 188 | class Service { 189 | /* 190 | * 获取开始时间和结束时间 191 | */ 192 | public getTimeScpoe(downloadOption: DownloadOption): { startDate: Date; endDate: Date } { 193 | const scpoe = downloadOption.dlScpoe; 194 | const now: Date = new Date(); 195 | let startDate: Date = new Date(); 196 | startDate.setTime(0); 197 | let endDate: Date = new Date(); 198 | endDate.setHours(23, 59, 59, 0); 199 | 200 | const startDateStr = downloadOption.startDate; 201 | const endDateStr = downloadOption.endDate; 202 | switch (scpoe) { 203 | case 'one': 204 | startDate = now; 205 | startDate.setHours(0, 0, 0, 0); 206 | break; 207 | case 'seven': 208 | startDate = now; 209 | startDate.setHours(0, 0, 0, 0); 210 | startDate.setDate(startDate.getDate() - 7); 211 | break; 212 | case 'month': 213 | startDate = now; 214 | startDate.setHours(0, 0, 0, 0); 215 | startDate.setMonth(startDate.getMonth() - 1); 216 | break; 217 | case 'diy': 218 | if (startDateStr) { 219 | startDate.setHours(0, 0, 0, 0); 220 | startDate = new Date(startDateStr); 221 | } 222 | if (endDateStr) { 223 | endDate = new Date(endDateStr); 224 | endDate.setHours(23, 59, 59, 0); 225 | } 226 | break; 227 | } 228 | return { startDate: startDate, endDate: endDate }; 229 | } 230 | 231 | /* 232 | * 预处理微信公号文章html 233 | */ 234 | public prepHtml(html: string): any { 235 | const $ = cheerio.load(html); 236 | // 处理图片:1.将data-src赋值给src 2.提取style中的宽高 237 | const imgArr = $('img'); 238 | imgArr.each((_i, elem) => { 239 | const $ele = $(elem); 240 | const dataSrc = $ele.attr('data-src'); 241 | if (dataSrc) { 242 | $ele.attr('src', dataSrc); 243 | } 244 | const styleStr = $ele.attr('style'); 245 | if (styleStr) { 246 | const width = StrUtil.getStyleWidth(styleStr); 247 | if (width) { 248 | $ele.attr('width', width); 249 | } 250 | } 251 | }); 252 | return $; 253 | } 254 | /* 255 | * 美化保存成html的样式 256 | */ 257 | public getArticleCss(): string { 258 | return ` 259 | 473 | `; 474 | } 475 | /* 476 | * 处理评论的js 477 | */ 478 | public getHtmlComment(commentList, replyDetailMap, showAllComment = false): string { 479 | return ` 480 | 618 | `; 619 | } 620 | /** 621 | * 获取元数据渲染的html 622 | * @param articleMeta 文章元数据 623 | * @returns 元数据渲染的html 624 | */ 625 | public getMetaHtml(articleMeta?: ArticleMeta): string { 626 | if (!articleMeta) { 627 | return ''; 628 | } 629 | let htmlStr = '
'; 630 | htmlStr += `${articleMeta.copyrightFlg ? '原创' : '非原创'} `; 631 | if (articleMeta.author) { 632 | htmlStr += `作者:${articleMeta.author} `; 633 | } 634 | if (articleMeta.jsName) { 635 | htmlStr += `公号:${articleMeta.jsName} `; 636 | } 637 | if (articleMeta.publicTime) { 638 | htmlStr += `发布时间:${articleMeta.publicTime} `; 639 | } 640 | if (articleMeta.ipWording) { 641 | htmlStr += `发表于${articleMeta.ipWording} `; 642 | } 643 | htmlStr += '
'; 644 | return htmlStr; 645 | } 646 | /* 647 | * markdown格式的评论内容 648 | */ 649 | public getMarkdownComment(commentList, replyDetailMap): string { 650 | let markdownStr = ''; 651 | if (commentList) { 652 | markdownStr += '\n\n---\n\n精选留言\n\n'; 653 | for (const electedComment of commentList) { 654 | const contentId = electedComment['content_id']; 655 | const nickName = electedComment['nick_name']; 656 | let provinceName = this.getPlaceName(electedComment); 657 | provinceName = provinceName ? '(来自' + provinceName + ')' : ''; 658 | const content: string = electedComment['content']; 659 | markdownStr += `\n- **${nickName}**${provinceName}\n ${content.replaceAll('\n', '\n ')}\n`; 660 | let replyList = replyDetailMap ? replyDetailMap[contentId] : null; 661 | if (!replyList) { 662 | replyList = electedComment['reply_new']['reply_list']; 663 | } 664 | if (!replyList) { 665 | continue; 666 | } 667 | for (const replyItem of replyList as []) { 668 | let replyNickName: string = replyItem['nick_name']; 669 | if (replyItem['is_from'] == 2) { 670 | replyNickName += '(作者)'; 671 | } 672 | let replyProvinceName = this.getPlaceName(electedComment); 673 | replyProvinceName = replyProvinceName ? '(来自' + replyProvinceName + ')' : ''; 674 | const replyContent: string = replyItem['content']; 675 | const replyToNickName = replyItem['to_nick_name']; 676 | const toNickNameStr = replyToNickName ? `回复 ${replyToNickName} :` : ''; 677 | markdownStr += `\n - **${replyNickName}**${replyProvinceName}\n ${toNickNameStr + replyContent.replaceAll('\n', '\n ')}\n`; 678 | } 679 | } 680 | } 681 | return markdownStr; 682 | } 683 | private getPlaceName(electedComment): string { 684 | let placeName = ''; 685 | if (electedComment['ip_wording']) { 686 | placeName = electedComment['ip_wording']['province_name'] ? electedComment['ip_wording']['province_name'] : electedComment['ip_wording']['country_name']; 687 | } 688 | return placeName; 689 | } 690 | /* 691 | * 创建html转markdown的TurndownService 692 | */ 693 | public createTurndownService() { 694 | const turndownService = new TurndownService({ codeBlockStyle: 'fenced' }); 695 | // 音频原样输出 696 | turndownService.addRule('audio', { 697 | filter: function (node) { 698 | return node.nodeName == 'AUDIO'; 699 | }, 700 | replacement: function (_content, node: HTMLElement) { 701 | return node.outerHTML; 702 | } 703 | }); 704 | // 专门针对微信公号文章页面做得规则 705 | turndownService.addRule('pre', { 706 | filter: function (node, options) { 707 | let isCodeBlock = false; 708 | for (const childNode of node.childNodes) { 709 | if (childNode.nodeName === 'CODE') { 710 | isCodeBlock = true; 711 | break; 712 | } 713 | } 714 | return options.codeBlockStyle === 'fenced' && node.nodeName === 'PRE' && isCodeBlock; 715 | 716 | // return options.codeBlockStyle === 'fenced' && node.nodeName === 'PRE' && node.firstChild && node.firstChild.nodeName === 'CODE'; 717 | }, 718 | 719 | replacement: function (_content, node: HTMLElement, options) { 720 | let codeNode; 721 | let language; 722 | const codeArr: string[] = []; 723 | for (const childNode of node.childNodes) { 724 | if (childNode.nodeName === 'CODE') { 725 | codeNode = childNode.cloneNode(true); 726 | 727 | if (!language) { 728 | const className = codeNode.getAttribute('class') || ''; 729 | language = (className.match(/language-(\S+)/) || [null, ''])[1]; 730 | } 731 | 732 | const innerHTMLStr = codeNode.innerHTML.replaceAll('
', '\n'); 733 | codeNode.innerHTML = innerHTMLStr; 734 | codeArr.push(codeNode.textContent); 735 | } 736 | } 737 | const code = codeArr.join('\n'); 738 | 739 | if (!language) { 740 | language = node.getAttribute('data-lang'); 741 | } 742 | 743 | const fenceChar = options.fence.charAt(0); 744 | let fenceSize = 3; 745 | const fenceInCodeRegex = new RegExp('^' + fenceChar + '{3,}', 'gm'); 746 | 747 | let match; 748 | while ((match = fenceInCodeRegex.exec(code))) { 749 | if (match[0].length >= fenceSize) { 750 | fenceSize = match[0].length + 1; 751 | } 752 | } 753 | 754 | const fence = Array(fenceSize + 1).join(fenceChar); 755 | 756 | return '\n\n' + fence + (language || '') + '\n' + code.replace(/\n$/, '') + '\n' + fence + '\n\n'; 757 | } 758 | }); 759 | 760 | return turndownService; 761 | } 762 | public getTmpHtml(html: string): string { 763 | const $ = cheerio.load(html); 764 | this.replaceSrc($, $('img[tmpsrc]')); 765 | this.replaceSrc($, $('source[tmpsrc]')); 766 | 767 | return $.html(); 768 | } 769 | 770 | private replaceSrc($, eleArr) { 771 | for (const elem of eleArr) { 772 | const $ele = $(elem); 773 | const tmpsrc = $ele.attr('tmpsrc'); 774 | if (tmpsrc && tmpsrc.length > 0) { 775 | $ele.attr('src', tmpsrc); 776 | } 777 | } 778 | } 779 | 780 | /* 781 | * 将json转成ArticleInfo 782 | */ 783 | public objToArticle(appMsgExtInfo, dateTime: Date, articleArr: ArticleInfo[]) { 784 | const title = appMsgExtInfo['title']; 785 | const contentUrl = appMsgExtInfo['content_url']; 786 | if (contentUrl) { 787 | const article: ArticleInfo = { title: title, datetime: dateTime, contentUrl: contentUrl }; 788 | article.author = appMsgExtInfo['author']; 789 | article.copyrightStat = appMsgExtInfo['copyright_stat']; 790 | article.digest = appMsgExtInfo['digest']; 791 | article.cover = appMsgExtInfo['cover']; 792 | articleArr.push(article); 793 | } 794 | } 795 | /* 796 | * 将数据库的json对象转为ArticleInfo 797 | */ 798 | public dbObjToArticle(dbObj, downloadOption: DownloadOption): ArticleInfo { 799 | const article = new ArticleInfo(dbObj['title'], dbObj['create_time'], dbObj['content_url']); 800 | article.author = dbObj['author']; 801 | article.html = dbObj['content']; 802 | article.digest = dbObj['digest']; 803 | article.cover = dbObj['cover']; 804 | if (1 == downloadOption.dlComment) article.commentList = JSON.parse(dbObj['comm']); 805 | if (1 == downloadOption.dlCommentReply) article.replyDetailMap = JSON.parse(dbObj['comm_reply']); 806 | return article; 807 | } 808 | /* 809 | * 获取html源码中的comment_id 810 | */ 811 | commentIdRegex = /var comment_id = "(.*)" \|\| "(.*)" \* 1;/; 812 | postCommentIdRegex = /getXmlValue\('comment_id\.DATA'\)\s?:\s?'(\d*)';/; 813 | public matchCommentId(html: string): string { 814 | let match = this.commentIdRegex.exec(html); 815 | if (match) { 816 | return match[1]; 817 | } 818 | match = this.postCommentIdRegex.exec(html); 819 | if (match) { 820 | return match[1]; 821 | } 822 | return ''; 823 | } 824 | /* 825 | * 获取html源码中的时间戳 826 | */ 827 | // 匹配var create_time = "1699399873" * 1; 828 | createTimeRegex = /var create_time = "(\d*)" \* 1;/; 829 | // 匹配window.ct = '1695861587', 830 | postCreateTimeRegex = /window.ct\s?=\s?'(\d*)'/; 831 | public matchCreateTime(html: string): Date | undefined { 832 | let match = this.createTimeRegex.exec(html); 833 | if (match) { 834 | return new Date(Number(match[1]) * 1000); 835 | } 836 | match = this.postCreateTimeRegex.exec(html); 837 | if (match) { 838 | return new Date(Number(match[1]) * 1000); 839 | } 840 | return undefined; 841 | } 842 | /** 843 | * 获取html源码中的发布地址 844 | */ 845 | ipWordingRegex = /provinceName: '([\u4e00-\u9fa5]*)'/; 846 | public matchIpWording(html: string): string | undefined { 847 | const match = this.ipWordingRegex.exec(html); 848 | if (match) { 849 | return match[1]; 850 | } 851 | return undefined; 852 | } 853 | } 854 | 855 | export { GzhInfo, ArticleInfo, ArticleMeta, PdfInfo, DownloadOption, FilterRuleInfo, NodeWorkerResponse, Service, NwrEnum, DlEventEnum }; 856 | -------------------------------------------------------------------------------- /src/main/utils.ts: -------------------------------------------------------------------------------- 1 | import * as fs from 'fs'; 2 | import * as path from 'path'; 3 | import axios from 'axios'; 4 | import logger from './logger'; 5 | 6 | class FileUtil { 7 | /* 8 | * 下载文件 9 | * url 是图片地址,如,http://wximg.233.com/attached/image/20160815/20160815162505_0878.png 10 | * filepath 是文件下载的本地目录 11 | * name 是下载后的文件名 12 | */ 13 | public static async downloadFile(url: string, filepath: string, name: string): Promise { 14 | const mypath = path.resolve(filepath, name); 15 | // 文件存在不覆盖 16 | if (fs.existsSync(mypath)) { 17 | // return new Promise((resolve) => { 18 | // resolve(name); 19 | // }); 20 | return name; 21 | } 22 | const writer = fs.createWriteStream(mypath); 23 | await axios 24 | .get(url, { 25 | responseType: 'stream' 26 | }) 27 | .then((response) => { 28 | response.data.pipe(writer); 29 | }) 30 | .catch((error) => { 31 | logger.error(`下载文件失败,url:${url}`, error); 32 | }); 33 | 34 | return new Promise((resolve, reject) => { 35 | writer.on('finish', () => resolve(name)); 36 | writer.on('error', (err) => reject(err)); 37 | }); 38 | } 39 | } 40 | 41 | class StrUtil { 42 | /* 43 | * 获取style中的宽度 44 | */ 45 | static widthExpression = /width:\s*(\d+px)/g; 46 | public static getStyleWidth(styleStr): string | null { 47 | const widthResultArr = StrUtil.widthExpression.exec(styleStr); 48 | if (widthResultArr && widthResultArr.length > 1) { 49 | return widthResultArr[1].replace('px', ''); 50 | } 51 | return null; 52 | } 53 | 54 | /* 55 | * 将字符串转换成文件夹名字允许的格式 56 | */ 57 | static cleanDirExpression = /^\.*?|\n|\\n|[\\\\/:*?"<>|]|\.*?$/gim; 58 | public static strToDirName(title: string): string { 59 | const cleanTitle = title.replaceAll(StrUtil.cleanDirExpression, ''); 60 | if (cleanTitle.length > 250) { 61 | return cleanTitle.substring(0, 250); 62 | } 63 | return cleanTitle; 64 | } 65 | 66 | /* 67 | * 去除两边空白字符 68 | */ 69 | static trimExpression = /^\s*|\s*$/g; 70 | public static trim(str: string): string { 71 | return str.replaceAll(StrUtil.trimExpression, ''); 72 | } 73 | } 74 | 75 | class HttpUtil { 76 | // 获取url参数 77 | public static getQueryVariable(url: string, variable: string): string | null { 78 | if (url.indexOf('?') == -1) { 79 | return null; 80 | } 81 | const query = url.split('?')[1]; 82 | const vars = query.split('&'); 83 | for (let i = 0; i < vars.length; i++) { 84 | const pair = vars[i].split('='); 85 | if (pair[0] == variable) { 86 | let result = ''; 87 | for (let j = 1; j < pair.length; j++) { 88 | const pairItem = pair[j]; 89 | if (pairItem == '') { 90 | result += '='; 91 | } else { 92 | result += pair[j]; 93 | } 94 | } 95 | return result; 96 | } 97 | } 98 | return null; 99 | } 100 | 101 | // 从url中获取文件类型后缀 102 | // 例如 url = http://www.baidu.com/ddd.png?id=12.22 103 | public static getSuffByUrl(url: string): string | null { 104 | const questIdx = url.lastIndexOf('?'); 105 | const dotIdx = url.lastIndexOf('.', questIdx > 0 ? questIdx : url.length); 106 | if (dotIdx <= 0) { 107 | return null; 108 | } 109 | const suff = url.substring(dotIdx + 1, questIdx > 0 ? questIdx : url.length); 110 | if (suff?.length > 5) { 111 | return null; 112 | } 113 | return suff; 114 | } 115 | } 116 | 117 | class DateUtil { 118 | public static format(datetime: Date | string, formatting: string): string { 119 | let timestamp: Date = datetime as Date; 120 | if (typeof datetime === 'string') { 121 | timestamp = new Date(Date.parse(datetime)); 122 | } 123 | const fullYear: string = timestamp.getFullYear().toString(); 124 | const monthNum = timestamp.getMonth() + 1; 125 | const month: string = monthNum.toString(); 126 | const date: string = timestamp.getDate().toString(); 127 | const hours: string = timestamp.getHours().toString(); 128 | const minutes: string = timestamp.getMinutes().toString(); 129 | const seconds: string = timestamp.getSeconds().toString(); 130 | const milliseconds: string = timestamp.getMilliseconds().toString(); 131 | formatting = this.parse(formatting, /[y|Y]+/, fullYear); 132 | formatting = this.parse(formatting, /[M]+/, month, '00'); 133 | formatting = this.parse(formatting, /[d|D]+/, date, '00'); 134 | formatting = this.parse(formatting, /[h|H]+/, hours, '0'); 135 | formatting = this.parse(formatting, /[m]+/, minutes, '00'); 136 | formatting = this.parse(formatting, /[s]+/, seconds, '00'); 137 | formatting = this.parse(formatting, /[S]+/, milliseconds, '000'); 138 | return formatting; 139 | } 140 | 141 | private static parse(formatting: string, pattern: RegExp, val: string, min?: string): string { 142 | while (pattern.test(formatting)) { 143 | pattern.exec(formatting)?.forEach((value) => { 144 | const length = value.length; 145 | const valLen = val.length; 146 | const number = valLen - length; 147 | let element = val.substring(number); 148 | if (min) { 149 | element = min.substring(element.length) + element; 150 | } 151 | formatting = formatting.replace(value, element); 152 | }); 153 | } 154 | return formatting; 155 | } 156 | } 157 | 158 | export { HttpUtil, StrUtil, FileUtil, DateUtil }; 159 | -------------------------------------------------------------------------------- /src/main/worker.ts: -------------------------------------------------------------------------------- 1 | import { parentPort, workerData } from 'worker_threads'; 2 | import logger from './logger'; 3 | import { StrUtil, FileUtil, DateUtil, HttpUtil } from './utils'; 4 | import { GzhInfo, ArticleInfo, ArticleMeta, PdfInfo, DownloadOption, FilterRuleInfo, Service, NodeWorkerResponse, NwrEnum, DlEventEnum } from './service'; 5 | import axios from 'axios'; 6 | import md5 from 'blueimp-md5'; 7 | import * as fs from 'fs'; 8 | import * as path from 'path'; 9 | import * as mysql from 'mysql2'; 10 | import * as Readability from '@mozilla/readability'; 11 | import * as cheerio from 'cheerio'; 12 | import { JSDOM } from 'jsdom'; 13 | import axiosRetry from 'axios-retry'; 14 | 15 | const onRetry = (retryCount, _err, requestConfig) => { 16 | logger.info(`第${retryCount}次请求失败`, requestConfig.url, requestConfig.params); 17 | }; 18 | axiosRetry(axios, { retries: 3, onRetry }); 19 | 20 | const service = new Service(); 21 | // html转markdown的TurndownService 22 | const turndownService = service.createTurndownService(); 23 | // 下载数量限制 24 | // 获取文章列表时,数量查过此限制不再继续获取列表,而是采集详情页后再继续获取列表 25 | const DOWNLOAD_LIMIT = workerData.downloadOption.batchLimit ?? 10; 26 | // 获取文章列表的url 27 | const LIST_URL = 'https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&f=json&count=10&is_ok=1'; 28 | const COMMENT_LIST_URL = 'https://mp.weixin.qq.com/mp/appmsg_comment?action=getcomment&offset=0&limit=100&f=json'; 29 | const COMMENT_REPLY_URL = 'https://mp.weixin.qq.com/mp/appmsg_comment?action=getcommentreply&offset=0&limit=100&is_first=1&f=json'; 30 | const QQ_MUSIC_INFO_URL = 'https://mp.weixin.qq.com/mp/qqmusic?action=get_song_info'; 31 | // 插入数据库的sql 32 | const TABLE_NAME = workerData.tableName; 33 | // const INSERT_SQL = `INSERT INTO ${TABLE_NAME} ( title, content, author, content_url, create_time, copyright_stat, comm, comm_reply) VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE title = ? , create_time=?`; 34 | const INSERT_SQL = `INSERT INTO ${TABLE_NAME} ( title, content, author, content_url, create_time, copyright_stat, comm, comm_reply, digest, cover, js_name, md_content) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE title = ? , create_time=?`; 35 | const SELECT_SQL = `SELECT title, content, author, content_url, create_time, comm, comm_reply FROM ${TABLE_NAME} WHERE create_time >= ? AND create_time <= ?`; 36 | 37 | // 数据库连接配置 38 | const connectionConfig: mysql.ConnectionConfig = workerData.connectionConfig; 39 | // 设置中心的配置 40 | const downloadOption: DownloadOption = workerData.downloadOption; 41 | // 下载事件(单个下载还是批量) 42 | const dlEvent: DlEventEnum = workerData.dlEvent; 43 | // 存储公众号信息的对象 44 | let GZH_INFO: GzhInfo; 45 | // 数据库连接 46 | let CONNECTION: mysql.Connection; 47 | let CONNECTION_STATE = false; 48 | // 存放保存pdf任务钩子的map 49 | const PDF_RESOLVE_MAP = new Map(); 50 | // 失败次数map 51 | const FAIL_COUNT_MAP = new Map(); 52 | // 触发公众号反爬机制时重试等待时间(单位秒) 53 | // 别问为什么是60秒,因为30秒不够 54 | const FAIL_WAIT_SECOND = 60; 55 | // 触发公众号反爬机制时重试次数 56 | const FAIL_RETRY = 1; 57 | 58 | // 阻塞线程的sleep函数 59 | function sleep(delay?: number): Promise { 60 | return new Promise((resolve) => { 61 | if (delay && delay > 0) { 62 | setTimeout(resolve, delay); 63 | } else { 64 | resolve(); 65 | } 66 | }); 67 | } 68 | 69 | const port = parentPort; 70 | if (!port) throw new Error('IllegalState'); 71 | 72 | // 接收消息,执行任务 73 | port.on('message', async (message: NodeWorkerResponse) => { 74 | if (message.code == NwrEnum.START) { 75 | // 初始化数据库连接 76 | await createMysqlConnection(); 77 | 78 | // 下载单个文章 79 | if (dlEvent == DlEventEnum.ONE) { 80 | const url = workerData.data; 81 | const articleInfo = new ArticleInfo(null, null, url); 82 | await axiosDlOne(articleInfo); 83 | resp(NwrEnum.ONE_FINISH, ''); 84 | finish(); 85 | } else if (dlEvent == DlEventEnum.BATCH_WEB) { 86 | // 从微信接口批量下载 87 | GZH_INFO = workerData.data; 88 | await batchDownloadFromWeb(); 89 | } else if (dlEvent == DlEventEnum.BATCH_DB) { 90 | // 从数据库批量下载 91 | await batchDownloadFromDb(); 92 | } else if (dlEvent == DlEventEnum.BATCH_SELECT) { 93 | await batchDownloadFromWebSelect(workerData.data); 94 | } 95 | } else if (message.code == NwrEnum.PDF_FINISHED) { 96 | // pdf保存完成的回调 97 | const pdfKey = message.data || ''; 98 | const resolve = PDF_RESOLVE_MAP.get(pdfKey); 99 | if (resolve) { 100 | resolve(); 101 | PDF_RESOLVE_MAP.delete(pdfKey); 102 | } 103 | } 104 | }); 105 | 106 | port.on('close', () => { 107 | logger.info('on 线程关闭'); 108 | }); 109 | 110 | port.addListener('close', () => { 111 | logger.info('addListener 线程关闭'); 112 | }); 113 | 114 | /** 115 | * 下载文章 116 | * @param articleInfo 文章信息 117 | * @param recall 是否重试 118 | */ 119 | async function axiosDlOne(articleInfo: ArticleInfo, reCall = false) { 120 | // 如果标题不为空,则是从批量下载过来的,可以提前判断跳过 121 | if (articleInfo.title && !reCall) { 122 | const timeStr = articleInfo.datetime ? DateUtil.format(articleInfo.datetime, 'yyyy-MM-dd') + '-' : ''; 123 | const saveDirName = StrUtil.strToDirName(articleInfo.title); 124 | articleInfo.fileName = saveDirName; 125 | // 创建保存文件夹 126 | const savePath = path.join(downloadOption.savePath || '', timeStr + saveDirName); 127 | if (fs.existsSync(savePath) && downloadOption.skinExist && downloadOption.skinExist == 1) { 128 | resp(NwrEnum.SUCCESS, `【${saveDirName}】已存在,跳过此文章`); 129 | return; 130 | } 131 | } 132 | 133 | const gzhInfo = articleInfo.gzhInfo; 134 | await axios 135 | .get(articleInfo.contentUrl, { 136 | params: { 137 | key: gzhInfo ? gzhInfo.key : '', 138 | uin: gzhInfo ? gzhInfo.uin : '' 139 | } 140 | }) 141 | .then((response) => { 142 | if (response.status != 200) { 143 | logger.error(`获取页面数据失败,状态码:${response.status}, URL:${articleInfo.contentUrl}`); 144 | resp(NwrEnum.FAIL, `下载失败,状态码:${response.status}, URL:${articleInfo.contentUrl}`); 145 | return; 146 | } 147 | articleInfo.html = response.data; 148 | }) 149 | .catch((error) => { 150 | logger.error('获取页面数据失败', error); 151 | }); 152 | if (!articleInfo.html) return; 153 | await dlOne(articleInfo, true); 154 | } 155 | 156 | /* 157 | * 下载单个页面 158 | */ 159 | async function dlOne(articleInfo: ArticleInfo, saveToDb = true) { 160 | // 预处理微信公号文章html 161 | if (!articleInfo.html) return; 162 | const url = articleInfo.contentUrl; 163 | const $source = service.prepHtml(articleInfo.html); 164 | // 提取正文 165 | /* 166 | * 总共有3种格式: 167 | * 1.普通格式 168 | * 2.海报格式:https://mp.weixin.qq.com/s/00XdizbDQtKRWxFv6iGqIA 169 | * 3.纯文字格式:https://mp.weixin.qq.com/s/vLuVL5owS5VdTDmMRzu3vQ 170 | */ 171 | let _article; 172 | if ($source('#js_article').hasClass('share_content_page')) { 173 | _article = parsePostHtml(articleInfo, $source); 174 | } else if ($source('#js_article').hasClass('page_content')) { 175 | _article = parseShortTextHtml(articleInfo, $source); 176 | } else { 177 | const doc = new JSDOM($source.html()); 178 | const reader = new Readability.Readability(doc.window.document, { keepClasses: true }); 179 | // #issues/45 因为文章会出现 selection或者p标签包含着mpvoice,但是里面又没有文字,会被Readability自动删除,需要修改源码 180 | // 修改Readability的源码(不推荐这种做法) 181 | (reader as any)._isElementWithoutContent = function (node) { 182 | return node.getElementsByTagName('mpvoice').length == 0 && node.nodeType === 1 && node.textContent.trim().length == 0 && (node.children.length == 0 || node.children.length == node.getElementsByTagName('br').length + node.getElementsByTagName('hr').length); 183 | }; 184 | (reader as any)._removeNodes = function (nodeList, filterFn) { 185 | // Avoid ever operating on live node lists. 186 | if (this._docJSDOMParser && nodeList._isLiveNodeList) { 187 | throw new Error('Do not pass live node lists to _removeNodes'); 188 | } 189 | for (let i = nodeList.length - 1; i >= 0; i--) { 190 | const node = nodeList[i]; 191 | const parentNode = node.parentNode; 192 | if (parentNode) { 193 | const mpvoiceCount = parentNode.getElementsByTagName('mpvoice').length; 194 | if (!filterFn || (filterFn.call(this, node, i, nodeList) && mpvoiceCount.length == 0)) { 195 | parentNode.removeChild(node); 196 | } 197 | } 198 | } 199 | }; 200 | _article = reader.parse(); 201 | } 202 | if (!_article) { 203 | resp(NwrEnum.FAIL, '提取正文失败'); 204 | return; 205 | } 206 | const article = _article; 207 | 208 | // 触发了公众号的反爬机制,进行重试 209 | if (article.title == '验证') { 210 | let failCount = FAIL_COUNT_MAP.get(articleInfo.contentUrl) || 0; 211 | failCount++; 212 | if (failCount > FAIL_RETRY) { 213 | resp(NwrEnum.FAIL, `触发公众号的反爬机制,停止采集!`); 214 | resp(NwrEnum.FAIL, `建议设置合理的下载间隔`); 215 | resp(NwrEnum.FAIL, `建议选择合适的下载范围,按区间分批下载`); 216 | resp(NwrEnum.BATCH_FINISH, ''); 217 | finish(); 218 | } 219 | FAIL_COUNT_MAP.set(articleInfo.contentUrl, failCount); 220 | resp(NwrEnum.FAIL, `【${articleInfo.title}】触发公众号的反爬机制,等待${FAIL_WAIT_SECOND}秒后进行重试!`); 221 | await sleep(FAIL_WAIT_SECOND * 1000); 222 | resp(NwrEnum.FAIL, `【${articleInfo.title}】进行第${failCount}次重试`); 223 | await axiosDlOne(articleInfo, true); 224 | return; 225 | } 226 | 227 | if (!articleInfo.title) articleInfo.title = article.title; 228 | if (!articleInfo.author) articleInfo.author = article.byline; 229 | if (!articleInfo.datetime) articleInfo.datetime = service.matchCreateTime($source.html()); 230 | 231 | // 提取元数据 232 | parseMeta(articleInfo, $source, article.byline); 233 | 234 | // 过滤规则 235 | const { flgFilter, filterMsg } = doFilter(articleInfo); 236 | if (flgFilter) { 237 | resp(NwrEnum.SUCCESS, `【${article.title}】已过滤:${filterMsg}`); 238 | return; 239 | } 240 | 241 | // 下载评论 242 | await downloadComment(articleInfo); 243 | 244 | // 创建保存文件夹 245 | const timeStr = articleInfo.datetime ? DateUtil.format(articleInfo.datetime, 'yyyy-MM-dd') + '-' : ''; 246 | const saveDirName = StrUtil.strToDirName(articleInfo.title || ''); 247 | const jsName = 1 == downloadOption.classifyDir ? StrUtil.strToDirName(articleInfo.metaInfo?.jsName || '') : ''; 248 | articleInfo.fileName = saveDirName; 249 | const savePath = path.join(downloadOption.savePath || '', jsName, timeStr + saveDirName); 250 | if (!fs.existsSync(savePath)) { 251 | try { 252 | fs.mkdirSync(savePath, { recursive: true }); 253 | } catch (err) { 254 | resp(NwrEnum.FAIL, `【${saveDirName}】创建失败,跳过此文章`); 255 | return; 256 | } 257 | } else { 258 | // 跳过已有文章 259 | if (downloadOption.skinExist && downloadOption.skinExist == 1) { 260 | resp(NwrEnum.SUCCESS, `【${saveDirName}】已存在,跳过此文章`); 261 | return; 262 | } 263 | } 264 | // 创建缓存文件夹 265 | const tmpPath = path.join(downloadOption.tmpPath || '', md5(url)); 266 | if (!fs.existsSync(tmpPath)) { 267 | fs.mkdirSync(tmpPath, { recursive: true }); 268 | } 269 | 270 | // 判断是否需要下载图片 271 | let imgCount = 0; 272 | const $ = cheerio.load(article.content); 273 | 274 | if (1 == downloadOption.dlImg) { 275 | await downloadImgToHtml($, savePath, tmpPath, articleInfo).then((obj) => { 276 | imgCount = obj.imgCount; 277 | }); 278 | } 279 | 280 | // 处理音频 281 | await convertAudio($, savePath, tmpPath, articleInfo); 282 | 283 | const readabilityPage = $('#readability-page-1'); 284 | // 插入原文链接 285 | if (1 == downloadOption.sourceUrl) { 286 | readabilityPage.prepend(`
原文地址:${article.title}
`); 287 | } 288 | // 插入元数据 289 | if (1 == downloadOption.saveMeta) { 290 | readabilityPage.prepend(service.getMetaHtml(articleInfo.metaInfo)); 291 | } 292 | // 插入标题 293 | readabilityPage.prepend(`

${article.title}

`); 294 | 295 | const proArr: Promise[] = []; 296 | 297 | // 判断是否保存markdown 298 | if (1 == downloadOption.dlMarkdown) { 299 | proArr.push( 300 | new Promise((resolve) => { 301 | const markdownStr = turndownService.turndown($.html()); 302 | // 添加评论 303 | const commentStr = service.getMarkdownComment(articleInfo.commentList, articleInfo.replyDetailMap); 304 | fs.writeFile(path.join(savePath, `${articleInfo.fileName}.md`), markdownStr + commentStr, () => { 305 | resp(NwrEnum.SUCCESS, `【${article.title}】保存Markdown完成`); 306 | resolve(); 307 | }); 308 | }) 309 | ); 310 | } 311 | // 判断是否保存html 312 | if (1 == downloadOption.dlHtml) { 313 | proArr.push( 314 | new Promise((resolve) => { 315 | const $html = cheerio.load($.html()); 316 | // 添加样式美化 317 | const headEle = $html('head'); 318 | headEle.append(service.getArticleCss()); 319 | // 添加评论数据 320 | if (articleInfo.commentList) { 321 | headEle.after(service.getHtmlComment(articleInfo.commentList, articleInfo.replyDetailMap)); 322 | } 323 | const htmlReadabilityPage = $html('#readability-page-1'); 324 | // 评论的div块 325 | htmlReadabilityPage.after('
留言×
'); 326 | fs.writeFile(path.join(savePath, `${articleInfo.fileName}.html`), $html.html(), () => { 327 | resp(NwrEnum.SUCCESS, `【${article.title}】保存HTML完成`); 328 | resolve(); 329 | }); 330 | }) 331 | ); 332 | } 333 | // 判断是否保存pdf 334 | if (1 == downloadOption.dlPdf) { 335 | proArr.push( 336 | new Promise((resolve) => { 337 | const $pdf = cheerio.load($.html()); 338 | // 添加样式美化 339 | const headEle = $pdf('head'); 340 | headEle.append(service.getArticleCss()); 341 | // 添加评论数据 342 | if (articleInfo.commentList) { 343 | headEle.after(service.getHtmlComment(articleInfo.commentList, articleInfo.replyDetailMap, true)); 344 | } 345 | const htmlReadabilityPage = $pdf('#readability-page-1'); 346 | // 评论的div块 347 | htmlReadabilityPage.after('
留言×
'); 348 | fs.writeFile(path.join(savePath, 'pdf.html'), $pdf.html(), () => { 349 | resp(NwrEnum.SUCCESS, `【${article.title}】保存pdf的html文件完成`); 350 | const articleId = md5(articleInfo.contentUrl); 351 | // 通知main线程,保存pdf 352 | resp(NwrEnum.PDF, '保存pdf', new PdfInfo(articleId, article.title, savePath, articleInfo.fileName)); 353 | // 保存回调钩子,等待main线程保存pdf完成 354 | PDF_RESOLVE_MAP.set(articleId, resolve); 355 | }); 356 | }) 357 | ); 358 | } 359 | 360 | // 判断是否保存到数据库 361 | if (1 == downloadOption.dlMysql && CONNECTION_STATE && saveToDb) { 362 | proArr.push( 363 | new Promise((resolve) => { 364 | // 是否要清洗markdown并保存数据库(这是个人需求) 365 | let markdownStr: string = ''; 366 | if (1 == downloadOption.cleanMarkdown) { 367 | const cleanHtml = service.getTmpHtml($.html()); 368 | 369 | markdownStr = turndownService.turndown(cleanHtml); 370 | } 371 | 372 | const modSqlParams = [articleInfo.title, articleInfo.html, articleInfo.author, articleInfo.contentUrl, articleInfo.datetime, articleInfo.copyrightStat, JSON.stringify(articleInfo.commentList), JSON.stringify(articleInfo.replyDetailMap), articleInfo.digest, articleInfo.cover, articleInfo.metaInfo?.jsName, markdownStr, articleInfo.title, articleInfo.datetime]; 373 | CONNECTION.query(INSERT_SQL, modSqlParams, function (err) { 374 | if (err) { 375 | logger.error('mysql插入失败', err); 376 | } else { 377 | resp(NwrEnum.SUCCESS, `【${article.title}】保存Mysql完成`); 378 | } 379 | resolve(); 380 | }); 381 | }) 382 | ); 383 | } 384 | 385 | for (const pro of proArr) { 386 | await pro; 387 | } 388 | resp(NwrEnum.SUCCESS, `【${article.title}】下载完成,共${imgCount}张图,url:${url}`); 389 | } 390 | 391 | const picListRegex = /window.picture_page_info_list\s*=\s(\[[\s\S]*\])\.slice/; 392 | const cdnUrlRegex = /cdn_url:\s?'(.*)',/g; 393 | // 解析海报格式页面:https://mp.weixin.qq.com/s/00XdizbDQtKRWxFv6iGqIA https://mp.weixin.qq.com/s/fzPkvEyECe-MYE_OTHLS-w 394 | function parsePostHtml(articleInfo: ArticleInfo, $) { 395 | // const $ = cheerio.load(articleInfo.html); 396 | // 获取内容 397 | const contentText = $('meta[name=description]').attr('content'); 398 | // 获取图片 399 | const picArr: unknown[] = []; 400 | const picListmatch = picListRegex.exec(articleInfo.html || ''); 401 | if (picListmatch) { 402 | let cdnUrlMatch; 403 | for (let iii = 0; iii < 20; iii++) { 404 | cdnUrlMatch = cdnUrlRegex.exec(picListmatch[1]); 405 | if (!cdnUrlMatch) { 406 | break; 407 | } 408 | picArr.push(cdnUrlMatch[1]); 409 | } 410 | } 411 | // 标题 412 | let title = $('.rich_media_title').text(); 413 | if (!title) { 414 | title = contentText; 415 | } 416 | if (!title || picArr.length == 0) { 417 | return null; 418 | } 419 | let contentHtml = `

${contentText.replaceAll(/\\x0a|\\n/g, '
')}

`; 420 | for (const pidx in picArr) { 421 | contentHtml = contentHtml + `

`; 422 | } 423 | contentHtml += '
'; 424 | // 获取作者 425 | const nickNameEle = $('.wx_follow_nickname'); 426 | let byline; 427 | if (nickNameEle.length > 1) { 428 | byline = nickNameEle.first().text(); 429 | } else { 430 | byline = nickNameEle.text(); 431 | } 432 | 433 | return { 434 | title: title.replaceAll(/\\x0a|\\n/g, ''), 435 | content: contentHtml, 436 | textContent: '', 437 | length: 0, 438 | excerpt: '', 439 | byline: byline, 440 | dir: '', 441 | siteName: '', 442 | lang: '' 443 | }; 444 | } 445 | 446 | // 解析短文字格式页面:https://mp.weixin.qq.com/s/vLuVL5owS5VdTDmMRzu3vQ 447 | // 这种格式没有内容,只有标题 448 | const shortTitleRegex = /window.msg_title\s?=\s?'(.*?)'/; 449 | function parseShortTextHtml(articleInfo: ArticleInfo, $) { 450 | const shortTitleMatch = shortTitleRegex.exec(articleInfo.html || ''); 451 | let title; 452 | if (shortTitleMatch) { 453 | title = shortTitleMatch[1]; 454 | } else { 455 | return null; 456 | } 457 | 458 | // 获取内容 459 | let contentText = $('meta[name=description]').attr('content'); 460 | if (!contentText) { 461 | contentText = title; 462 | } 463 | let contentHtml = `

${contentText.replaceAll(/\\x0a|\\n/g, '
')}

`; 464 | // 获取作者 465 | const nickNameEle = $('.wx_follow_nickname'); 466 | let byline; 467 | if (nickNameEle.length > 1) { 468 | byline = nickNameEle.first().text(); 469 | } else { 470 | byline = nickNameEle.text(); 471 | } 472 | 473 | contentHtml += '
'; 474 | return { 475 | title: title.replaceAll(/\\x0a|\\n/g, ''), 476 | content: contentHtml, 477 | textContent: '', 478 | length: 0, 479 | excerpt: '', 480 | byline: byline, 481 | dir: '', 482 | siteName: '', 483 | lang: '' 484 | }; 485 | } 486 | 487 | /** 488 | * 解析元数据 489 | * @param articleInfo 文章信息 490 | * @param htmlStr 微信文章网页源码 491 | * @param byline Readability解析出来的作者名 492 | */ 493 | let totalJsName; 494 | function parseMeta(articleInfo: ArticleInfo, $meta: any, byline?: string) { 495 | // 判断是否需要下载元数据 496 | // if (1 != downloadOption.saveMeta) { 497 | // return; 498 | // } 499 | const authorName = articleInfo.author ? articleInfo.author : getEleText($meta('#js_author_name')); 500 | // 缓存公众号名字,防止特殊页面获取不到 501 | let jsName; 502 | if (dlEvent == DlEventEnum.BATCH_WEB) { 503 | if (!totalJsName) { 504 | totalJsName = getEleText($meta('#js_name')); 505 | } 506 | jsName = totalJsName; 507 | } else { 508 | jsName = getEleText($meta('#js_name')); 509 | } 510 | // 封面 511 | let cover: string | undefined; 512 | if (!articleInfo.cover) { 513 | const coverEles = $meta('meta[property=og:image]'); 514 | if (coverEles) { 515 | if (coverEles.length > 1) { 516 | cover = coverEles.first().attr('content'); 517 | } else { 518 | cover = coverEles.attr('content'); 519 | } 520 | } 521 | } 522 | const copyrightFlg = $meta('#copyright_logo')?.text() ? true : false; 523 | const publicTime = articleInfo.datetime ? DateUtil.format(articleInfo.datetime, 'yyyy-MM-dd HH:mm') : ''; 524 | const ipWording = service.matchIpWording($meta.html()); 525 | const articleMeta = new ArticleMeta(); 526 | articleMeta.copyrightFlg = copyrightFlg; 527 | articleMeta.author = authorName ? authorName : byline; 528 | articleMeta.jsName = jsName; 529 | articleMeta.publicTime = publicTime; 530 | articleMeta.ipWording = ipWording; 531 | articleInfo.metaInfo = articleMeta; 532 | articleInfo.cover = cover; 533 | } 534 | 535 | function getEleText(ele: any): string { 536 | if (ele) { 537 | if (ele.length > 1) { 538 | return StrUtil.trim(ele.first().text()); 539 | } else { 540 | return StrUtil.trim(ele.text()); 541 | } 542 | } 543 | return ''; 544 | } 545 | 546 | // 根据过滤规则过滤文章 547 | function doFilter(articleInfo: ArticleInfo): { flgFilter: boolean; filterMsg: string } { 548 | const filterRuleStr = downloadOption.filterRule; 549 | if (!filterRuleStr) { 550 | return { flgFilter: false, filterMsg: '' }; 551 | } 552 | 553 | const filterRule: FilterRuleInfo = parseFilterInfo(filterRuleStr); 554 | if (filterRule.titleInclude.length > 0 && !isInclude(articleInfo.title, filterRule.titleInclude)[0]) { 555 | return { flgFilter: true, filterMsg: '标题未包含关键词' }; 556 | } 557 | 558 | if (filterRule.authInclude.length > 0 && !isInclude(articleInfo.author, filterRule.authInclude)[0]) { 559 | return { flgFilter: true, filterMsg: '作者未包含关键词' }; 560 | } 561 | 562 | const [flgTitleInclude, titleExcludeWord] = isInclude(articleInfo.title, filterRule.titleExclude); 563 | if (flgTitleInclude) { 564 | return { flgFilter: true, filterMsg: `标题包含排除关键词 ${titleExcludeWord}` }; 565 | } 566 | 567 | const [flgAuthInclude, authExcludeWord] = isInclude(articleInfo.author, filterRule.authExclude); 568 | if (flgAuthInclude) { 569 | return { flgFilter: true, filterMsg: `作者包含排除关键词 ${authExcludeWord}` }; 570 | } 571 | 572 | return { flgFilter: false, filterMsg: '' }; 573 | } 574 | /** 575 | * 判断内容是否包含关键词 576 | * @param content 内容 577 | * @param include 包含关键词 578 | */ 579 | function isInclude(content: string | undefined, include: string[]): [boolean, string] { 580 | if (!content) return [false, '']; 581 | for (const includeItem of include) { 582 | if (content.includes(includeItem)) { 583 | return [true, includeItem]; 584 | } 585 | } 586 | return [false, '']; 587 | } 588 | 589 | function parseFilterInfo(filterRuleStr: string): FilterRuleInfo { 590 | const filterRuleInfo = new FilterRuleInfo(); 591 | const filterRule = JSON.parse(filterRuleStr); 592 | if (filterRule.title) { 593 | const titleInclude = filterRule.title.include; 594 | if (titleInclude && titleInclude.length > 0) { 595 | filterRuleInfo.titleInclude = titleInclude; 596 | } 597 | const titleExclude = filterRule.title.exclude; 598 | if (titleExclude && titleExclude.length > 0) { 599 | filterRuleInfo.titleExclude = titleExclude; 600 | } 601 | } 602 | 603 | if (filterRule.auth) { 604 | const authInclude = filterRule.auth.include; 605 | if (authInclude && authInclude.length > 0) { 606 | filterRuleInfo.authInclude = authInclude; 607 | } 608 | const authExclude = filterRule.auth.exclude; 609 | if (authExclude && authExclude.length > 0) { 610 | filterRuleInfo.authExclude = authExclude; 611 | } 612 | } 613 | return filterRuleInfo; 614 | } 615 | 616 | /* 617 | * 下载评论 618 | */ 619 | async function downloadComment(articleInfo: ArticleInfo) { 620 | if (!articleInfo.html) return; 621 | 622 | const gzhInfo = articleInfo.gzhInfo; 623 | // 判断是否需要下载评论 624 | if (1 != downloadOption.dlComment || !gzhInfo) { 625 | return; 626 | } 627 | 628 | const commentId = service.matchCommentId(articleInfo.html); 629 | if (!commentId) { 630 | logger.error('获取精选评论参数失败'); 631 | resp(NwrEnum.FAIL, '获取精选评论参数失败'); 632 | } else if (commentId == '0') { 633 | logger.info(`【${articleInfo.title}】没有评论`); 634 | resp(NwrEnum.FAIL, `【${articleInfo.title}】没有评论`); 635 | } else { 636 | const headers = { 637 | Host: gzhInfo.Host, 638 | Connection: 'keep-alive', 639 | 'User-Agent': gzhInfo.UserAgent, 640 | Cookie: gzhInfo.Cookie, 641 | Referer: articleInfo.contentUrl 642 | }; 643 | // 评论列表 644 | let commentList; 645 | // 评论回复map 646 | let replyDetailMap; 647 | await axios 648 | .get(COMMENT_LIST_URL, { 649 | params: { 650 | __biz: gzhInfo.biz, 651 | key: gzhInfo.key, 652 | uin: gzhInfo.uin, 653 | comment_id: commentId 654 | }, 655 | headers: headers 656 | }) 657 | .then((response) => { 658 | if (response.status != 200) { 659 | logger.error(`获取精选评论失败,状态码:${response.status}`, articleInfo.contentUrl); 660 | resp(NwrEnum.FAIL, `获取精选评论失败,状态码:${response.status}`); 661 | return; 662 | } 663 | const resData = response.data; 664 | if (resData.base_resp.ret != 0) { 665 | logger.error(`【${articleInfo.title}】获取精选评论失败`, resData, response.config.url, response.config.params); 666 | resp(NwrEnum.FAIL, `【${articleInfo.title}】获取精选评论失败:${resData.errmsg}`); 667 | return; 668 | } 669 | if (resData.elected_comment && resData.elected_comment.length > 0) { 670 | commentList = resData.elected_comment; 671 | } 672 | logger.debug(`【${articleInfo.title}】精选评论`, commentList); 673 | }) 674 | .catch((error) => { 675 | logger.error(`【${articleInfo.title}】获取精选评论失败`, error, articleInfo.contentUrl); 676 | }); 677 | 678 | // 处理评论的回复 679 | if (1 == downloadOption.dlCommentReply && commentList) { 680 | replyDetailMap = new Map(); 681 | for (const commentItem of commentList) { 682 | const replyInfo = commentItem.reply_new; 683 | if (replyInfo.reply_total_cnt > replyInfo.reply_list.length) { 684 | await axios 685 | .get(COMMENT_REPLY_URL, { 686 | params: { 687 | __biz: gzhInfo.biz, 688 | key: gzhInfo.key, 689 | uin: gzhInfo.uin, 690 | comment_id: commentId, 691 | content_id: commentItem.content_id, 692 | max_reply_id: replyInfo.max_reply_id 693 | }, 694 | headers: headers 695 | }) 696 | .then((response) => { 697 | if (response.status != 200) { 698 | logger.error(`获取评论回复失败,状态码:${response.status}`, response.config.url, response.config.params); 699 | resp(NwrEnum.FAIL, `获取评论回复失败,状态码:${response.status}`); 700 | return; 701 | } 702 | const resData = response.data; 703 | if (resData.base_resp.ret != 0) { 704 | logger.error(`获取评论回复失败`, resData, response.config.url, response.config.params); 705 | resp(NwrEnum.FAIL, `获取评论回复失败:${resData.errmsg}`); 706 | return; 707 | } 708 | replyDetailMap[commentItem.content_id] = resData.reply_list.reply_list; 709 | }) 710 | .catch((error) => { 711 | logger.error('获取评论回复失败', error); 712 | }); 713 | } 714 | } 715 | } 716 | articleInfo.commentList = commentList; 717 | articleInfo.replyDetailMap = replyDetailMap; 718 | } 719 | } 720 | 721 | /* 722 | * 下载图片并替换src 723 | * $: cheerio对象 724 | * savePath: 保存文章的路径(已区分文章),例如: D://savePath//测试文章1 725 | * tmpPath: 缓存路径(已区分文章),例如:D://tmpPathPath//6588aec6b658b2c941f6d51d0b1691b9 726 | */ 727 | async function downloadImgToHtml($, savePath: string, tmpPath: string, articleInfo: ArticleInfo): Promise<{ imgCount: number }> { 728 | const imgArr = $('img'); 729 | const awaitArr: Promise[] = []; 730 | let imgCount = 0; 731 | // 创建保存图片的文件夹 732 | const imgPath = path.join(savePath, 'img'); 733 | if (imgArr.length > 0 && !fs.existsSync(imgPath)) { 734 | fs.mkdirSync(imgPath, { recursive: true }); 735 | } 736 | 737 | imgArr.each(function (i, elem) { 738 | const $ele = $(elem); 739 | // 文件后缀 740 | let fileSuf = $ele.attr('data-type'); 741 | // 文件url 742 | const fileUrl = $ele.attr('data-src') || $ele.attr('src'); 743 | if (fileUrl) { 744 | if (!fileSuf) { 745 | fileSuf = HttpUtil.getSuffByUrl(fileUrl) || 'jpg'; 746 | } 747 | 748 | imgCount++; 749 | const tmpFileName = `${md5(fileUrl)}.${fileSuf}`; 750 | const fileName = `${i}.${fileSuf}`; 751 | const dlPromise = FileUtil.downloadFile(fileUrl, tmpPath, tmpFileName).then((_fileName) => { 752 | // 图片下载完成之,将图片从缓存文件夹复制到需要保存的文件夹 753 | $ele.attr('src', path.join('img', fileName).replaceAll('\\', '/')); 754 | $ele.attr('tmpsrc', path.join('md', md5(articleInfo.contentUrl), tmpFileName).replaceAll('\\', '/')); 755 | const resolveSavePath = path.join(imgPath, fileName); 756 | if (!fs.existsSync(resolveSavePath)) { 757 | // 复制 758 | fs.copyFile(path.join(tmpPath, _fileName), resolveSavePath, (err) => { 759 | if (err) { 760 | logger.error(err, `复制图片失败,名字:${_fileName}`, 'tmpPath', path.resolve(tmpPath, _fileName), 'resolveSavePath', resolveSavePath); 761 | } 762 | }); 763 | } 764 | }); 765 | awaitArr.push(dlPromise); 766 | } 767 | }); 768 | for (const dlPromise of awaitArr) { 769 | await dlPromise; 770 | } 771 | return { imgCount: imgCount }; 772 | } 773 | 774 | /* 775 | * 下载音乐并替换src 776 | * $: cheerio对象 777 | * savePath: 保存文章的路径(已区分文章),例如: D://savePath//测试文章1 778 | * tmpPath: 缓存路径(已区分文章),例如:D://tmpPathPath//6588aec6b658b2c941f6d51d0b1691b9 779 | */ 780 | async function convertAudio($, savePath: string, tmpPath: string, articleInfo: ArticleInfo) { 781 | let musicArr = $('mp-common-qqmusic'); 782 | if (musicArr.length == 0) { 783 | musicArr = $('qqmusic'); 784 | } 785 | const compvoiceArr = $('mp-common-mpaudio'); 786 | const mpvoiceArr = $('mpvoice'); 787 | 788 | // 创建歌曲的文件夹 789 | const songPath = path.join(savePath, 'song'); 790 | if ((musicArr.length > 0 || mpvoiceArr.length > 0 || compvoiceArr.length > 0) && !fs.existsSync(songPath)) { 791 | fs.mkdirSync(songPath, { recursive: true }); 792 | } 793 | 794 | const awaitArr: Promise[] = []; 795 | // 处理QQ音乐 796 | const gzhInfo = articleInfo.gzhInfo; 797 | for (let i = 0; i < musicArr.length; i++) { 798 | const $ele = $(musicArr[i]); 799 | // 歌名 800 | const musicName = $ele.attr('music_name'); 801 | // 歌手名 802 | const singer = $ele.attr('singer'); 803 | // 歌曲id 804 | const mid = $ele.attr('mid'); 805 | if (gzhInfo) { 806 | // QQ音乐接口获取歌曲信息 807 | await axios 808 | .get(QQ_MUSIC_INFO_URL, { 809 | params: { 810 | __biz: gzhInfo.biz, 811 | key: gzhInfo.key, 812 | uin: gzhInfo.uin, 813 | song_mid: mid 814 | } 815 | }) 816 | .then((resp) => { 817 | const dataObj = resp.data; 818 | const songDesc = JSON.parse(dataObj['resp_data']); 819 | const songInfo = songDesc.songlist[0]; 820 | const songSrc = songInfo['song_play_url_standard']; 821 | if (songSrc) { 822 | const tmpFileName = `${mid}.m4a`; 823 | const fileName = `public_${i}.m4a`; 824 | awaitArr.push(downloadSong($ele, musicName, songSrc, songPath, tmpPath, tmpFileName, fileName, articleInfo, singer)); 825 | } 826 | }) 827 | .catch((error) => { 828 | logger.error(`音频下载失败,mid:${mid}`, error); 829 | }); 830 | } else { 831 | const tmpFileName = `${mid}.m4a`; 832 | const mypath = path.resolve(tmpPath, tmpFileName); 833 | // 文件存在就继续 834 | if (fs.existsSync(mypath)) { 835 | const fileName = `public_${i}.m4a`; 836 | awaitArr.push(downloadSong($ele, musicName, '', songPath, tmpPath, tmpFileName, fileName, articleInfo, singer)); 837 | } 838 | } 839 | } 840 | // 处理作者录制的音频 841 | for (let j = 0; j < mpvoiceArr.length; j++) { 842 | const $ele = $(mpvoiceArr[j]); 843 | // 歌名 844 | const musicName = $ele.attr('name'); 845 | // 歌曲id 846 | const mid = $ele.attr('voice_encode_fileid'); 847 | const songSrc = `https://res.wx.qq.com/voice/getvoice?mediaid=${mid}`; 848 | const tmpFileName = `${mid}.mp3`; 849 | const fileName = `person_${j}.mp3`; 850 | // 下载音频到本地 851 | awaitArr.push(downloadSong($ele, musicName, songSrc, songPath, tmpPath, tmpFileName, fileName, articleInfo)); 852 | } 853 | 854 | for (let j = 0; j < compvoiceArr.length; j++) { 855 | const $ele = $(compvoiceArr[j]); 856 | // 歌名 857 | const musicName = $ele.attr('name'); 858 | // 歌曲id 859 | const mid = $ele.attr('voice_encode_fileid'); 860 | const songSrc = `https://res.wx.qq.com/voice/getvoice?mediaid=${mid}`; 861 | const tmpFileName = `${mid}.mp3`; 862 | const fileName = `person_${j}.mp3`; 863 | // 下载音频到本地 864 | awaitArr.push(downloadSong($ele, musicName, songSrc, songPath, tmpPath, tmpFileName, fileName, articleInfo)); 865 | } 866 | 867 | for (const dlPromise of awaitArr) { 868 | await dlPromise; 869 | } 870 | } 871 | /* 872 | * 下载音频 873 | * $: cheerio对象 874 | * musicName:音乐名 875 | * songSrc:歌曲url 876 | * songPath:歌曲保存路径 877 | * tmpPath:缓存路径 878 | * tmpFileName:缓存歌曲文件名(缓存文件夹中的名字) 879 | * fileName:歌曲文件名(html中的名字) 880 | * singer:歌手 881 | */ 882 | async function downloadSong($ele, musicName: string, songSrc: string, songPath: string, tmpPath: string, tmpFileName: string, fileName: string, articleInfo: ArticleInfo, singer?: string): Promise { 883 | if (1 == downloadOption.dlAudio) { 884 | resp(NwrEnum.SUCCESS, `正在下载歌曲【${musicName}】...`); 885 | await FileUtil.downloadFile(songSrc, tmpPath, tmpFileName).then((_fileName) => { 886 | // 音频下载完成之后,从缓存文件夹复制到需要保存的文件夹 887 | const resolveSavePath = path.join(songPath, fileName); 888 | if (!fs.existsSync(resolveSavePath)) { 889 | // 复制 890 | fs.copyFileSync(path.join(tmpPath, _fileName), resolveSavePath); 891 | } 892 | songSrc = path.join('song', fileName).replaceAll('\\', '/'); 893 | const tmpSongSrc = path.join('md', md5(articleInfo.contentUrl), tmpFileName).replaceAll('\\', '/'); 894 | addSongDiv($ele, musicName, songSrc, tmpSongSrc, singer); 895 | resp(NwrEnum.SUCCESS, `歌曲【${musicName}】下载完成...`); 896 | }); 897 | } else { 898 | addSongDiv($ele, musicName, songSrc, '', singer); 899 | } 900 | } 901 | /* 902 | * 添加音频展示的div 903 | * musicName:音乐名 904 | * songSrc:歌曲url 905 | * tmpSongSrc:缓存路径的歌曲url 906 | * singer:歌手 907 | */ 908 | function addSongDiv($ele, musicName: string, songSrc: string, tmpSongSrc: string, singer?: string) { 909 | $ele.after(` 910 |
911 |
912 |
${musicName}
913 | ${singer ? '
' + singer + '
' : ''} 914 |
915 |
916 | 919 |
920 |
921 | `); 922 | } 923 | 924 | /* 925 | * 批量下载(来源是数据库) 926 | */ 927 | async function batchDownloadFromDb() { 928 | const exeStartTime = performance.now(); 929 | if (!CONNECTION_STATE) { 930 | resp(NwrEnum.BATCH_FINISH, '数据库初始化失败'); 931 | return; 932 | } 933 | 934 | const { startDate, endDate } = service.getTimeScpoe(downloadOption); 935 | const modSqlParams = [startDate, endDate]; 936 | CONNECTION.query(SELECT_SQL, modSqlParams, async function (err, result) { 937 | if (err) { 938 | logger.error('获取数据库数据失败', err.message); 939 | resp(NwrEnum.BATCH_FINISH, '获取数据库数据失败'); 940 | } else { 941 | let articleCount = 0; 942 | const promiseArr: Promise[] = []; 943 | for (const dbObj of result) { 944 | articleCount++; 945 | const articleInfo: ArticleInfo = service.dbObjToArticle(dbObj, downloadOption); 946 | promiseArr.push(dlOne(articleInfo, false)); 947 | // 栅栏,防止一次性下载太多 948 | if (promiseArr.length > DOWNLOAD_LIMIT) { 949 | for (let i = 0; i < DOWNLOAD_LIMIT; i++) { 950 | const p = promiseArr.shift(); 951 | await p; 952 | } 953 | } 954 | } 955 | // 栅栏,等待所有文章下载完成 956 | for (const articlePromise of promiseArr) { 957 | await articlePromise; 958 | } 959 | 960 | const exeEndTime = performance.now(); 961 | const exeTime = (exeEndTime - exeStartTime) / 1000; 962 | resp(NwrEnum.BATCH_FINISH, `批量下载完成,共${articleCount}篇文章,耗时${exeTime.toFixed(2)}秒`); 963 | 964 | finish(); 965 | } 966 | }); 967 | } 968 | 969 | /* 970 | * 批量下载(来源是网络) 971 | */ 972 | async function batchDownloadFromWeb() { 973 | const { startDate, endDate } = service.getTimeScpoe(downloadOption); 974 | const articleArr: ArticleInfo[] = []; 975 | const exeStartTime = performance.now(); 976 | // 获取文章列表 977 | const articleCount: number[] = [0]; 978 | await downList(0, articleArr, startDate, endDate, articleCount); 979 | 980 | // downList中没下载完的,在这处理 981 | const promiseArr: Promise[] = []; 982 | for (const article of articleArr) { 983 | article.gzhInfo = GZH_INFO; 984 | promiseArr.push(axiosDlOne(article)); 985 | // 下载间隔 986 | await sleep(downloadOption.dlInterval); 987 | } 988 | // 栅栏,等待所有文章下载完成 989 | for (const articlePromise of promiseArr) { 990 | await articlePromise; 991 | } 992 | 993 | const exeEndTime = performance.now(); 994 | const exeTime = (exeEndTime - exeStartTime) / 1000; 995 | 996 | resp(NwrEnum.BATCH_FINISH, `批量下载完成,共${articleCount[0]}篇文章,耗时${exeTime.toFixed(2)}秒`); 997 | 998 | finish(); 999 | } 1000 | /* 1001 | * 批量下载选择的文章 1002 | */ 1003 | async function batchDownloadFromWebSelect(articleArr: ArticleInfo[]) { 1004 | const exeStartTime = performance.now(); 1005 | 1006 | const promiseArr: Promise[] = []; 1007 | for (let i = 0; i < articleArr.length; i++) { 1008 | const article = articleArr[i]; 1009 | if (i > 0) { 1010 | await sleep(downloadOption.dlInterval); 1011 | } 1012 | // 单线程下载 1013 | if (downloadOption.threadType == 'single') { 1014 | await axiosDlOne(article); 1015 | } else { 1016 | // 多线程下载 1017 | promiseArr.push(axiosDlOne(article)); 1018 | // 栅栏,防止一次性下载太多 1019 | if (promiseArr.length >= DOWNLOAD_LIMIT) { 1020 | for (let i = 0; i < DOWNLOAD_LIMIT; i++) { 1021 | const p = promiseArr.shift(); 1022 | await p; 1023 | } 1024 | } 1025 | } 1026 | } 1027 | // 栅栏,等待所有文章下载完成 1028 | for (const articlePromise of promiseArr) { 1029 | await articlePromise; 1030 | } 1031 | 1032 | const exeEndTime = performance.now(); 1033 | const exeTime = (exeEndTime - exeStartTime) / 1000; 1034 | 1035 | resp(NwrEnum.BATCH_FINISH, `批量下载完成,共${articleArr.length}篇文章,耗时${exeTime.toFixed(2)}秒`); 1036 | 1037 | finish(); 1038 | } 1039 | 1040 | /* 1041 | * 获取文章列表 1042 | * nextOffset: 微信获取文章列表所需参数 1043 | * articleArr:文章信息 1044 | * startDate:过滤开始时间 1045 | * endDate:过滤结束时间 1046 | * articleCount:文章数量 1047 | */ 1048 | async function downList(nextOffset: number, articleArr: ArticleInfo[], startDate: Date, endDate: Date, articleCount: number[]) { 1049 | let dataObj; 1050 | logger.debug('下载文章列表', `${LIST_URL}&__biz=${GZH_INFO.biz}&key=${GZH_INFO.key}&uin=${GZH_INFO.uin}&pass_ticket=${GZH_INFO.passTicket}&offset=${nextOffset}`); 1051 | await axios 1052 | .get(LIST_URL, { 1053 | params: { 1054 | __biz: GZH_INFO.biz, 1055 | key: GZH_INFO.key, 1056 | uin: GZH_INFO.uin, 1057 | pass_ticket: GZH_INFO.passTicket, 1058 | offset: nextOffset 1059 | }, 1060 | headers: { 1061 | Host: GZH_INFO.Host, 1062 | Connection: 'keep-alive', 1063 | 'User-Agent': GZH_INFO.UserAgent, 1064 | Cookie: GZH_INFO.Cookie, 1065 | Referer: `https://mp.weixin.qq.com/mp/profile_ext?action=home&lang=zh_CN&__biz=${GZH_INFO.biz}&uin=${GZH_INFO.uin}&key=${GZH_INFO.key}&pass_ticket=${GZH_INFO.passTicket}` 1066 | } 1067 | }) 1068 | .then((response) => { 1069 | if (response.status != 200) { 1070 | logger.error(`获取文章列表失败,状态码:${response.status}`, GZH_INFO); 1071 | resp(NwrEnum.FAIL, `获取文章列表失败,状态码:${response.status}`); 1072 | return; 1073 | } 1074 | dataObj = response.data; 1075 | logger.debug('列表数据', dataObj); 1076 | }) 1077 | .catch((error) => { 1078 | logger.error('获取文章列表失败', error, GZH_INFO); 1079 | }); 1080 | if (!dataObj) return; 1081 | const oldArticleLengh = articleArr.length; 1082 | const errmsg = dataObj['errmsg']; 1083 | if ('ok' != errmsg) { 1084 | logger.error('获取文章列表失败', `${LIST_URL}&__biz=${GZH_INFO.biz}&key=${GZH_INFO.key}&uin=${GZH_INFO.uin}&pass_ticket=${GZH_INFO.passTicket}&offset=${nextOffset}`, dataObj); 1085 | resp(NwrEnum.FAIL, `获取文章列表失败,错误信息:${errmsg}`); 1086 | return; 1087 | } 1088 | const generalMsgList = JSON.parse(dataObj['general_msg_list']); 1089 | let flgContinue = true; 1090 | 1091 | for (const generalMsg of generalMsgList['list']) { 1092 | const commMsgInfo = generalMsg['comm_msg_info']; 1093 | const appMsgExtInfo = generalMsg['app_msg_ext_info']; 1094 | 1095 | const dateTime = new Date(commMsgInfo['datetime'] * 1000); 1096 | // 判断,如果小于开始时间,直接退出 1097 | if (dateTime < startDate) { 1098 | flgContinue = false; 1099 | break; 1100 | } 1101 | // 如果大于结束时间,则不放入 1102 | if (dateTime > endDate) continue; 1103 | 1104 | service.objToArticle(appMsgExtInfo, dateTime, articleArr); 1105 | 1106 | if (appMsgExtInfo['is_multi'] == 1) { 1107 | for (const multiAppMsgItem of appMsgExtInfo['multi_app_msg_item_list']) { 1108 | service.objToArticle(multiAppMsgItem, dateTime, articleArr); 1109 | } 1110 | } 1111 | } 1112 | articleCount[0] = articleCount[0] + articleArr.length - oldArticleLengh; 1113 | resp(NwrEnum.SUCCESS, `正在获取文章列表,目前数量:${articleCount[0]}`); 1114 | // 单线程下载 1115 | if (downloadOption.threadType == 'single') { 1116 | for (let i = 0; i < articleArr.length; i++) { 1117 | const article = articleArr.shift(); 1118 | if (article) { 1119 | article.gzhInfo = GZH_INFO; 1120 | await axiosDlOne(article); 1121 | // 下载间隔 1122 | await sleep(downloadOption.dlInterval); 1123 | } 1124 | } 1125 | } else { 1126 | // 多线程下载 1127 | // 文章数量超过限制,则开始下载详情页 1128 | while (articleArr.length >= DOWNLOAD_LIMIT) { 1129 | const promiseArr: Promise[] = []; 1130 | for (let i = 0; i < DOWNLOAD_LIMIT; i++) { 1131 | const article = articleArr.shift(); 1132 | if (article) { 1133 | article.gzhInfo = GZH_INFO; 1134 | promiseArr.push(axiosDlOne(article)); 1135 | // 下载间隔 1136 | await sleep(downloadOption.dlInterval); 1137 | } 1138 | } 1139 | // 栅栏,等待所有文章下载完成 1140 | for (const articlePromise of promiseArr) { 1141 | await articlePromise; 1142 | } 1143 | } 1144 | } 1145 | 1146 | if (flgContinue && dataObj['can_msg_continue'] == 1) { 1147 | await downList(dataObj['next_offset'], articleArr, startDate, endDate, articleCount); 1148 | } 1149 | } 1150 | 1151 | function resp(code: NwrEnum, message: string, data?) { 1152 | logger.info('resp', code, message, data); 1153 | if (port) port.postMessage(new NodeWorkerResponse(code, message, data)); 1154 | } 1155 | 1156 | /* 1157 | * 创建mysql数据库连接 1158 | */ 1159 | async function createMysqlConnection(): Promise { 1160 | if (1 != downloadOption.dlMysql && 'db' != downloadOption.dlSource) return CONNECTION; 1161 | if (CONNECTION) { 1162 | CONNECTION.end(); 1163 | } 1164 | 1165 | CONNECTION = mysql.createConnection(connectionConfig); 1166 | // 这里是想阻塞等待连接成功 1167 | return new Promise((resolve) => { 1168 | const sql = 'show tables'; 1169 | CONNECTION.query(sql, (err) => { 1170 | if (err) { 1171 | resp(NwrEnum.FAIL, 'mysql连接失败'); 1172 | logger.error('mysql连接失败', err); 1173 | } else { 1174 | resp(NwrEnum.SUCCESS, 'mysql连接成功'); 1175 | logger.info('连接成功'); 1176 | CONNECTION_STATE = true; 1177 | } 1178 | resolve(CONNECTION); 1179 | }); 1180 | }); 1181 | } 1182 | /* 1183 | * 收尾方法 1184 | */ 1185 | function finish() { 1186 | // 关闭数据库连接 1187 | if (CONNECTION) { 1188 | CONNECTION.end(); 1189 | } 1190 | // 通知主线程关闭此线程 1191 | resp(NwrEnum.CLOSE, ''); 1192 | } 1193 | -------------------------------------------------------------------------------- /src/preload/index.d.ts: -------------------------------------------------------------------------------- 1 | import { ElectronAPI } from '@electron-toolkit/preload'; 2 | 3 | declare global { 4 | interface Window { 5 | electron: ElectronAPI; 6 | api: { 7 | /*** render->main ***/ 8 | // 安装证书 9 | installLicence(); 10 | // 以桌面的默认方式打开给定的文件 11 | openPath(path: string): Promise; 12 | // 打开日志文件的文件夹 13 | openLogsDir(); 14 | // 选择路径 15 | showOpenDialog(options: OpenDialogOptions, callbackMsg: string); 16 | // 下载详情页数据 17 | downloadOne(url: string); 18 | // 开启公号文章监测(获取用户参数) 19 | monitorArticle(); 20 | // 开启公号文章监测(历史接口被封使用,获取文章地址) 21 | monitorLimitArticle(); 22 | stopMonitorLimitArticle(); 23 | // 测试mysql连接 24 | testConnect(); 25 | // 消息弹框 26 | showMessageBox(options: MessageBoxOptions); 27 | // electron-store的api 28 | store: { 29 | get: (key: string) => any; 30 | set: (key: string, val: any) => void; 31 | // any other methods you've defined... 32 | }; 33 | // 加载初始化数据 34 | loadInitInfo: () => string; 35 | // 检查更新 36 | checkForUpdate(); 37 | // 生成epub 38 | createEpub(options: any); 39 | 40 | /*** main->render ***/ 41 | // 用于打开文件夹之后接收打开的路径 42 | openDialogCallback(callback: (event: IpcRendererEvent, callbackMsg: string, path: string) => void); 43 | /* 44 | * 输出日志到主页面 45 | * msg:输出的消息 46 | * append:是否追加 47 | * flgHtml:消息是否是html 48 | */ 49 | outputLog(callback: (event: IpcRendererEvent, msg: string, flgAppend = false, flgHtml = false) => void); 50 | // 输出日志到生成Epub页面 51 | outputEpubLog(callback: (event: IpcRendererEvent, msg: string, flgAppend = false, flgHtml = false) => void); 52 | // 下载完成后做的处理 53 | downloadFnish(callback: (event: IpcRendererEvent) => void); 54 | /* 55 | * 发送更新信息 56 | * msg:输出的消息 57 | */ 58 | updateMsg(callback: (event: IpcRendererEvent, msg: any) => void); 59 | /* 60 | * 发送下载进度 61 | */ 62 | downloadProgress(callback: (event: IpcRendererEvent, msg: ProgressInfo) => void); 63 | }; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/preload/index.ts: -------------------------------------------------------------------------------- 1 | import { contextBridge, shell, ipcRenderer, OpenDialogOptions, MessageBoxOptions } from 'electron'; 2 | import { electronAPI } from '@electron-toolkit/preload'; 3 | 4 | // Custom APIs for renderer 5 | const api = { 6 | /*** render->main ***/ 7 | // 安装证书 8 | installLicence: () => ipcRenderer.send('install-licence'), 9 | // 以桌面的默认方式打开给定的文件 10 | openPath: (path: string) => shell.openPath(path), 11 | // 打开日志文件的文件夹 12 | openLogsDir: () => ipcRenderer.send('open-logs-dir'), 13 | // 选择路径 14 | showOpenDialog: (options: OpenDialogOptions, callbackMsg: string) => ipcRenderer.send('show-open-dialog', options, callbackMsg), 15 | // 下载详情页数据 16 | downloadOne: (url: string) => ipcRenderer.send('download-one', url), 17 | // 开启公号文章监测(获取用户参数) 18 | monitorArticle: () => ipcRenderer.send('monitor-article'), 19 | // 开启公号文章监测(历史接口被封使用,获取文章地址) 20 | monitorLimitArticle: () => ipcRenderer.send('monitor-limit-article'), 21 | stopMonitorLimitArticle: () => ipcRenderer.send('stop-monitor-limit-article'), 22 | // 测试mysql连接 23 | testConnect: () => ipcRenderer.send('test-connect'), 24 | // 消息弹框 25 | showMessageBox: (options: MessageBoxOptions) => ipcRenderer.send('show-message-box', options), 26 | // electron-store的api 27 | store: { 28 | get(key) { 29 | return ipcRenderer.sendSync('electron-store-get', key); 30 | }, 31 | set(property, val) { 32 | ipcRenderer.send('electron-store-set', property, val); 33 | } 34 | // Other method you want to add like has(), reset(), etc. 35 | }, 36 | // 加载初始化数据 37 | loadInitInfo() { 38 | return ipcRenderer.sendSync('load-init-info'); 39 | }, 40 | // 检查更新 41 | checkForUpdate: () => ipcRenderer.send('check-for-update'), 42 | // 生成epub 43 | createEpub: (options: any) => ipcRenderer.send('create-epub', options), 44 | /*** main->render ***/ 45 | // 用于打开文件夹之后接收打开的路径 46 | openDialogCallback: (callback) => ipcRenderer.on('open-dialog-callback', callback), 47 | // 输出日志 48 | outputLog: (callback) => ipcRenderer.on('output-log', callback), 49 | // 输出Epub日志 50 | outputEpubLog: (callback) => ipcRenderer.on('output-log', callback), 51 | // 下载完成 52 | downloadFnish: (callback) => ipcRenderer.on('download-fnish', callback), 53 | // 发送更新信息 54 | updateMsg: (callback) => ipcRenderer.on('update-msg', callback), 55 | // 发送下载进度 56 | downloadProgress: (callback) => ipcRenderer.on('download-progress', callback) 57 | }; 58 | 59 | // Use `contextBridge` APIs to expose Electron APIs to 60 | // renderer only if context isolation is enabled, otherwise 61 | // just add to the DOM global. 62 | if (process.contextIsolated) { 63 | try { 64 | contextBridge.exposeInMainWorld('electron', electronAPI); 65 | contextBridge.exposeInMainWorld('api', api); 66 | } catch (error) { 67 | console.error(error); 68 | } 69 | } else { 70 | // @ts-ignore (define in dts) 71 | window.electron = electronAPI; 72 | // @ts-ignore (define in dts) 73 | window.api = api; 74 | } 75 | -------------------------------------------------------------------------------- /src/renderer/auto-imports.d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | /* prettier-ignore */ 3 | // @ts-nocheck 4 | // noinspection JSUnusedGlobalSymbols 5 | // Generated by unplugin-auto-import 6 | export {} 7 | declare global { 8 | const ElMessage: typeof import('element-plus/es')['ElMessage'] 9 | } 10 | -------------------------------------------------------------------------------- /src/renderer/components.d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | /* prettier-ignore */ 3 | // @ts-nocheck 4 | // Generated by unplugin-vue-components 5 | // Read more: https://github.com/vuejs/core/pull/3399 6 | export {} 7 | 8 | declare module 'vue' { 9 | export interface GlobalComponents { 10 | ElButton: typeof import('element-plus/es')['ElButton'] 11 | ElCheckbox: typeof import('element-plus/es')['ElCheckbox'] 12 | ElCol: typeof import('element-plus/es')['ElCol'] 13 | ElConfigProvider: typeof import('element-plus/es')['ElConfigProvider'] 14 | ElContainer: typeof import('element-plus/es')['ElContainer'] 15 | ElDatePicker: typeof import('element-plus/es')['ElDatePicker'] 16 | ElFooter: typeof import('element-plus/es')['ElFooter'] 17 | ElForm: typeof import('element-plus/es')['ElForm'] 18 | ElFormItem: typeof import('element-plus/es')['ElFormItem'] 19 | ElHeader: typeof import('element-plus/es')['ElHeader'] 20 | ElInput: typeof import('element-plus/es')['ElInput'] 21 | ElInputNumber: typeof import('element-plus/es')['ElInputNumber'] 22 | ElMain: typeof import('element-plus/es')['ElMain'] 23 | ElMenu: typeof import('element-plus/es')['ElMenu'] 24 | ElMenuItem: typeof import('element-plus/es')['ElMenuItem'] 25 | ElProgress: typeof import('element-plus/es')['ElProgress'] 26 | ElRadioButton: typeof import('element-plus/es')['ElRadioButton'] 27 | ElRadioGroup: typeof import('element-plus/es')['ElRadioGroup'] 28 | ElRow: typeof import('element-plus/es')['ElRow'] 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/renderer/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | wechatDownload 6 | 7 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/renderer/src/App.vue: -------------------------------------------------------------------------------- 1 | 14 | 15 | 35 | 49 | -------------------------------------------------------------------------------- /src/renderer/src/assets/base.css: -------------------------------------------------------------------------------- 1 | /* :root { 2 | --ev-c-white: #ffffff; 3 | --ev-c-white-soft: #f8f8f8; 4 | --ev-c-white-mute: #f2f2f2; 5 | 6 | --ev-c-black: #1b1b1f; 7 | --ev-c-black-soft: #222222; 8 | --ev-c-black-mute: #282828; 9 | 10 | --ev-c-gray-1: #515c67; 11 | --ev-c-gray-2: #414853; 12 | --ev-c-gray-3: #32363f; 13 | 14 | --ev-c-text-1: rgba(255, 255, 245, 0.86); 15 | --ev-c-text-2: rgba(235, 235, 245, 0.6); 16 | --ev-c-text-3: rgba(235, 235, 245, 0.38); 17 | 18 | --ev-button-alt-border: transparent; 19 | --ev-button-alt-text: var(--ev-c-text-1); 20 | --ev-button-alt-bg: var(--ev-c-gray-3); 21 | --ev-button-alt-hover-border: transparent; 22 | --ev-button-alt-hover-text: var(--ev-c-text-1); 23 | --ev-button-alt-hover-bg: var(--ev-c-gray-2); 24 | } 25 | 26 | :root { 27 | --color-background: var(--ev-c-black); 28 | --color-background-soft: var(--ev-c-black-soft); 29 | --color-background-mute: var(--ev-c-black-mute); 30 | 31 | --color-text: var(--ev-c-text-1); 32 | } */ 33 | 34 | *, 35 | *::before, 36 | *::after { 37 | box-sizing: border-box; 38 | margin: 0; 39 | /* font-weight: normal; */ 40 | } 41 | 42 | ul { 43 | list-style: none; 44 | } 45 | 46 | html { 47 | width: 100%; 48 | height: 100%; 49 | } 50 | 51 | body { 52 | height: 100%; 53 | color: var(--color-text); 54 | background: var(--color-background); 55 | line-height: 1.6; 56 | font-family: 57 | Inter, 58 | -apple-system, 59 | BlinkMacSystemFont, 60 | 'Segoe UI', 61 | Roboto, 62 | Oxygen, 63 | Ubuntu, 64 | Cantarell, 65 | 'Fira Sans', 66 | 'Droid Sans', 67 | 'Helvetica Neue', 68 | sans-serif; 69 | text-rendering: optimizeLegibility; 70 | -webkit-font-smoothing: antialiased; 71 | -moz-osx-font-smoothing: grayscale; 72 | } 73 | 74 | body::-webkit-scrollbar { 75 | /*滚动条整体样式*/ 76 | width: 8px; 77 | /*高宽分别对应横竖滚动条的尺寸*/ 78 | height: 1px; 79 | } 80 | 81 | body::-webkit-scrollbar-thumb { 82 | /*滚动条里面小方块*/ 83 | border-radius: 10px; 84 | box-shadow: inset 0 0 5px rgba(97, 184, 179, 0.1); 85 | background: #6a7979; 86 | } 87 | 88 | body::-webkit-scrollbar-track { 89 | /*滚动条里面轨道*/ 90 | box-shadow: inset 0 0 5px rgba(87, 175, 187, 0.1); 91 | border-radius: 10px; 92 | background: #ededed; 93 | } -------------------------------------------------------------------------------- /src/renderer/src/assets/main.css: -------------------------------------------------------------------------------- 1 | @import './base.css'; 2 | 3 | /* body { 4 | display: flex; 5 | align-items: center; 6 | justify-content: center; 7 | overflow: hidden; 8 | background-image: url('./wavy-lines.svg'); 9 | background-size: cover; 10 | user-select: none; 11 | } */ 12 | 13 | 14 | #app { 15 | display: flex; 16 | /* align-items: center; */ 17 | justify-content: center; 18 | flex-direction: column; 19 | height: 100%; 20 | width: 100%; 21 | padding-top: 40px; 22 | } -------------------------------------------------------------------------------- /src/renderer/src/env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | 3 | declare module '*.vue' { 4 | import type { DefineComponent } from 'vue'; 5 | // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types 6 | const component: DefineComponent<{}, {}, any>; 7 | export default component; 8 | } 9 | -------------------------------------------------------------------------------- /src/renderer/src/main.ts: -------------------------------------------------------------------------------- 1 | import './assets/main.css'; 2 | // import 'element-plus/theme-chalk/el-loading.css' 3 | // import 'element-plus/theme-chalk/el-message.css' 4 | // import 'element-plus/theme-chalk/el-notification.css' 5 | 6 | import { createApp } from 'vue'; 7 | import App from './App.vue'; 8 | 9 | const app = createApp(App); 10 | 11 | app.mount('#app'); 12 | -------------------------------------------------------------------------------- /src/renderer/src/views/EpubCreator.vue: -------------------------------------------------------------------------------- 1 | 39 | 40 | 115 | 116 | 125 | -------------------------------------------------------------------------------- /src/renderer/src/views/Home.vue: -------------------------------------------------------------------------------- 1 | 40 | 41 | 143 | 144 | 174 | -------------------------------------------------------------------------------- /src/renderer/src/views/Setting.vue: -------------------------------------------------------------------------------- 1 | 152 | 153 | 331 | 332 | 372 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "files": [], 3 | "references": [{ "path": "./tsconfig.node.json" }, { "path": "./tsconfig.web.json" }] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.node.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@electron-toolkit/tsconfig/tsconfig.node.json", 3 | "include": ["electron.vite.config.*", "src/main/**/*", "src/preload/**/*"], 4 | "compilerOptions": { 5 | "composite": true, 6 | "types": ["electron-vite/node"] 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tsconfig.web.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@electron-toolkit/tsconfig/tsconfig.web.json", 3 | "include": [ 4 | "src/renderer/src/env.d.ts", 5 | "src/renderer/src/**/*", 6 | "src/renderer/src/**/*.vue", 7 | "src/preload/*.d.ts", 8 | "src/renderer/auto-imports.d.ts" 9 | ], 10 | "compilerOptions": { 11 | "composite": true, 12 | "baseUrl": ".", 13 | "paths": { 14 | "@renderer/*": [ 15 | "src/renderer/src/*" 16 | ] 17 | } 18 | } 19 | } 20 | --------------------------------------------------------------------------------