├── .envrc ├── .github ├── dependabot.yml └── workflows │ └── deploy.yml ├── .gitignore ├── LICENSE ├── README.md ├── devenv.lock ├── devenv.nix ├── devenv.yaml ├── package-lock.json ├── package.json ├── src ├── constants.ts ├── index.ts ├── models.ts ├── routes.ts ├── scrapers.ts └── utils.ts ├── tsconfig.json └── wrangler.toml /.envrc: -------------------------------------------------------------------------------- 1 | source_url "https://raw.githubusercontent.com/cachix/devenv/d1f7b48e35e6dee421cfd0f51481d17f77586997/direnvrc" "sha256-YBzqskFZxmNb3kYVoKD9ZixoPXJh1C9ZvTLGFRkauZ0=" 2 | 3 | use devenv -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "npm" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Deploy on workers.dev 2 | 3 | on: 4 | push: 5 | branches: 6 | - develop 7 | workflow_dispatch: 8 | repository_dispatch: 9 | 10 | jobs: 11 | deploy: 12 | runs-on: ubuntu-latest 13 | name: Deploy 14 | steps: 15 | - uses: actions/checkout@v3 16 | - uses: actions/setup-node@v3 17 | with: 18 | node-version: 16 19 | - run: npm install 20 | - name: Publish API 21 | uses: cloudflare/wrangler-action@2.0.0 22 | with: 23 | apiToken: ${{ secrets.CF_API_TOKEN }} 24 | accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} 25 | command: deploy 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Optional stylelint cache 58 | .stylelintcache 59 | 60 | # Microbundle cache 61 | .rpt2_cache/ 62 | .rts2_cache_cjs/ 63 | .rts2_cache_es/ 64 | .rts2_cache_umd/ 65 | 66 | # Optional REPL history 67 | .node_repl_history 68 | 69 | # Output of 'npm pack' 70 | *.tgz 71 | 72 | # Yarn Integrity file 73 | .yarn-integrity 74 | 75 | # dotenv environment variable files 76 | .env 77 | .env.development.local 78 | .env.test.local 79 | .env.production.local 80 | .env.local 81 | 82 | # parcel-bundler cache (https://parceljs.org/) 83 | .cache 84 | .parcel-cache 85 | 86 | # Next.js build output 87 | .next 88 | out 89 | 90 | # Nuxt.js build / generate output 91 | .nuxt 92 | dist 93 | 94 | # Gatsby files 95 | .cache/ 96 | # Comment in the public line in if your project uses Gatsby and not Next.js 97 | # https://nextjs.org/blog/next-9-1#public-directory-support 98 | # public 99 | 100 | # vuepress build output 101 | .vuepress/dist 102 | 103 | # vuepress v2.x temp and cache directory 104 | .temp 105 | .cache 106 | 107 | # Docusaurus cache and generated files 108 | .docusaurus 109 | 110 | # Serverless directories 111 | .serverless/ 112 | 113 | # FuseBox cache 114 | .fusebox/ 115 | 116 | # DynamoDB Local files 117 | .dynamodb/ 118 | 119 | # TernJS port file 120 | .tern-port 121 | 122 | # Stores VSCode versions used for testing VSCode extensions 123 | .vscode-test 124 | 125 | # yarn v2 126 | .yarn/cache 127 | .yarn/unplugged 128 | .yarn/build-state.yml 129 | .yarn/install-state.gz 130 | .pnp.* 131 | 132 | # Devenv 133 | .devenv* 134 | devenv.local.nix 135 | .direnv -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nyaa-Api-Ts 2 | 3 | [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/Yash-Garg/Nyaa-Api-Ts) 4 | 5 | This API is an **Unofficial Nyaa API** rewritten in Typescript. 6 | 7 | Previous Go API - [Yash-Garg/Nyaa-Api-Go](https://github.com/Yash-Garg/Nyaa-Api-Go) 8 | 9 | ## Usage 10 | 11 | - `username` and `id` are required parameters if using `/user/{username}` and `/id/{id}` endpoints. 12 | 13 | - If no parameters are specified in other endpoints like `/anime`, `/manga`, etc. It will return the latest uploaded torrents in the respective category. 14 | 15 | - For Filters, input `filter=1` for _No Remakes_ and `filter=2` for _Trusted Only_. 16 | 17 | - #### Available Endpoints 18 | 19 | | **Arguments** | **Description** | 20 | | ------------------ | ----------------------------------------------------- | 21 | | `q` **(Optional)** | Search query. | 22 | | `s` **(Optional)** | Sorting parameter | 23 | | `p` **(Optional)** | Page number | 24 | | `f` **(Optional)** | Filter option | 25 | | `o` **(Optional)** | Order of sorting. Defaults to **_Descending order_**. | 26 | 27 | - **Endpoints** 28 | | **Category** | **Endpoint** | 29 | |---------|---------| 30 | | All | `/all` | 31 | | Anime | `/anime` | 32 | | Manga | `/manga` | 33 | | Audio | `/audio` | 34 | | Pictures | `/pictures` | 35 | | Live Action | `/live_action` | 36 | | Software | `/software` | 37 | | ID | `/id` | 38 | | User | `/user` | 39 | 40 | - **Sub-Categories** (Not applicable for `/user` and `/id`) 41 | | **Category** | **Sub-Category** | 42 | |------|------| 43 | | Anime | `/amv`, `/eng`, `/non-eng`, `/raw` | 44 | | Manga | `/eng`, `/non-eng`, `/raw` | 45 | | Audio | `/lossy`, `/lossless` | 46 | | Pictures | `/photos`, `/graphics` | 47 | | Live Action | `/promo`, `/eng`, `/non-eng`, `/raw` | 48 | | Software | `/application`, `/games` | 49 | 50 | - **Sorting Parameters** 51 | | **Arguments** | **Methods** | 52 | | ---- | ---- | 53 | | Sort | `size`, `seeders`, `leechers`, `date`, `downloads` | 54 | | Order | `asc`, `desc` | 55 | 56 | - #### Search using ID 57 | 58 | - `https://nyaa-api-ts.yashg.workers.dev/id/{id}` 59 | 60 | - #### Search using category 61 | 62 | - `https://nyaa-api-ts.yashg.workers.dev/{category}?q={search_query}` 63 | - `https://nyaa-api-ts.yashg.workers.dev/{category}?q={search_query}&s={sorting_parameter}` 64 | - `https://nyaa-api-ts.yashg.workers.dev/{category}?q={search_query}&s={sorting_parameter}&p={page_number}` 65 | - `https://nyaa-api-ts.yashg.workers.dev/{category}?q={search_query}&s={sorting_parameter}&p={page_number}&o={order}` 66 | - `https://nyaa-api-ts.yashg.workers.dev/{category}?q={search_query}&s={sorting_parameter}&p={page_number}&o={order}&f={filter}` 67 | 68 | - #### Search using sub category 69 | 70 | - `https://nyaa-api-ts.yashg.workers.dev/{category}/{sub_category}?q={search_query}` 71 | - `https://nyaa-api-ts.yashg.workers.dev/{category}/{sub_category}?q={search_query}&s={sorting_parameter}` 72 | - `https://nyaa-api-ts.yashg.workers.dev/{category}/{sub_category}?q={search_query}&s={sorting_parameter}&p={page_number}` 73 | - `https://nyaa-api-ts.yashg.workers.dev/{category}/{sub_category}?q={search_query}&s={sorting_parameter}&p={page_number}&o={order}` 74 | - `https://nyaa-api-ts.yashg.workers.dev/{category}/{sub_category}?q={search_query}&s={sorting_parameter}&p={page_number}&o={order}&f={filter}` 75 | 76 | - #### Search using username 77 | - `https://nyaa-api-ts.yashg.workers.dev/user/{username}` 78 | - `https://nyaa-api-ts.yashg.workers.dev/user/{username}?q={search_query}` 79 | - `https://nyaa-api-ts.yashg.workers.dev/user/{username}?q={search_query}&s={sorting_parameter}` 80 | - `https://nyaa-api-ts.yashg.workers.dev/user/{username}?q={search_query}&s={sorting_parameter}&p={page_number}` 81 | - `https://nyaa-api-ts.yashg.workers.dev/user/{username}?q={search_query}&s={sorting_parameter}&p={page_number}&o={order}` 82 | - `https://nyaa-api-ts.yashg.workers.dev/user/{username}?q={search_query}&s={sorting_parameter}&p={page_number}&o={order}&f={filter}` 83 | -------------------------------------------------------------------------------- /devenv.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "devenv": { 4 | "locked": { 5 | "dir": "src/modules", 6 | "lastModified": 1682786226, 7 | "narHash": "sha256-VX2ms4dv5CaVJbz+5/Qsbhp4wfdjJYhYarV1+NgnTV4=", 8 | "owner": "cachix", 9 | "repo": "devenv", 10 | "rev": "11fa51d3714c9cf4c71392b39beba1907ce3ff7a", 11 | "type": "github" 12 | }, 13 | "original": { 14 | "dir": "src/modules", 15 | "owner": "cachix", 16 | "repo": "devenv", 17 | "type": "github" 18 | } 19 | }, 20 | "flake-compat": { 21 | "flake": false, 22 | "locked": { 23 | "lastModified": 1673956053, 24 | "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", 25 | "owner": "edolstra", 26 | "repo": "flake-compat", 27 | "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", 28 | "type": "github" 29 | }, 30 | "original": { 31 | "owner": "edolstra", 32 | "repo": "flake-compat", 33 | "type": "github" 34 | } 35 | }, 36 | "flake-utils": { 37 | "locked": { 38 | "lastModified": 1667395993, 39 | "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", 40 | "owner": "numtide", 41 | "repo": "flake-utils", 42 | "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", 43 | "type": "github" 44 | }, 45 | "original": { 46 | "owner": "numtide", 47 | "repo": "flake-utils", 48 | "type": "github" 49 | } 50 | }, 51 | "gitignore": { 52 | "inputs": { 53 | "nixpkgs": [ 54 | "pre-commit-hooks", 55 | "nixpkgs" 56 | ] 57 | }, 58 | "locked": { 59 | "lastModified": 1660459072, 60 | "narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=", 61 | "owner": "hercules-ci", 62 | "repo": "gitignore.nix", 63 | "rev": "a20de23b925fd8264fd7fad6454652e142fd7f73", 64 | "type": "github" 65 | }, 66 | "original": { 67 | "owner": "hercules-ci", 68 | "repo": "gitignore.nix", 69 | "type": "github" 70 | } 71 | }, 72 | "nixpkgs": { 73 | "locked": { 74 | "lastModified": 1682809678, 75 | "narHash": "sha256-jqR8t82mWotOSgnWZvr6xXCO/tc3fCPTLMPvI7Jo5rA=", 76 | "owner": "NixOS", 77 | "repo": "nixpkgs", 78 | "rev": "3dcff817eebb7e4afc4e9eae0ce6f722f4d9e399", 79 | "type": "github" 80 | }, 81 | "original": { 82 | "owner": "NixOS", 83 | "ref": "nixpkgs-unstable", 84 | "repo": "nixpkgs", 85 | "type": "github" 86 | } 87 | }, 88 | "nixpkgs-stable": { 89 | "locked": { 90 | "lastModified": 1678872516, 91 | "narHash": "sha256-/E1YwtMtFAu2KUQKV/1+KFuReYPANM2Rzehk84VxVoc=", 92 | "owner": "NixOS", 93 | "repo": "nixpkgs", 94 | "rev": "9b8e5abb18324c7fe9f07cb100c3cd4a29cda8b8", 95 | "type": "github" 96 | }, 97 | "original": { 98 | "owner": "NixOS", 99 | "ref": "nixos-22.11", 100 | "repo": "nixpkgs", 101 | "type": "github" 102 | } 103 | }, 104 | "pre-commit-hooks": { 105 | "inputs": { 106 | "flake-compat": "flake-compat", 107 | "flake-utils": "flake-utils", 108 | "gitignore": "gitignore", 109 | "nixpkgs": [ 110 | "nixpkgs" 111 | ], 112 | "nixpkgs-stable": "nixpkgs-stable" 113 | }, 114 | "locked": { 115 | "lastModified": 1682596858, 116 | "narHash": "sha256-Hf9XVpqaGqe/4oDGr30W8HlsWvJXtMsEPHDqHZA6dDg=", 117 | "owner": "cachix", 118 | "repo": "pre-commit-hooks.nix", 119 | "rev": "fb58866e20af98779017134319b5663b8215d912", 120 | "type": "github" 121 | }, 122 | "original": { 123 | "owner": "cachix", 124 | "repo": "pre-commit-hooks.nix", 125 | "type": "github" 126 | } 127 | }, 128 | "root": { 129 | "inputs": { 130 | "devenv": "devenv", 131 | "nixpkgs": "nixpkgs", 132 | "pre-commit-hooks": "pre-commit-hooks" 133 | } 134 | } 135 | }, 136 | "root": "root", 137 | "version": 7 138 | } 139 | -------------------------------------------------------------------------------- /devenv.nix: -------------------------------------------------------------------------------- 1 | {pkgs, ...}: { 2 | languages.typescript.enable = true; 3 | 4 | packages = [pkgs.nodejs-18_x]; 5 | 6 | enterShell = '' 7 | node --version 8 | ''; 9 | } 10 | -------------------------------------------------------------------------------- /devenv.yaml: -------------------------------------------------------------------------------- 1 | inputs: 2 | nixpkgs: 3 | url: github:NixOS/nixpkgs/nixpkgs-unstable 4 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nyaa-api-ts", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "nyaa-api-ts", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "cheerio": "^1.0.0-rc.12", 13 | "ts-node": "^10.9.1", 14 | "worktop": "^0.7.3" 15 | }, 16 | "devDependencies": { 17 | "@types/node": "^18.11.17", 18 | "typescript": "^4.9.4" 19 | } 20 | }, 21 | "node_modules/@cspotcode/source-map-support": { 22 | "version": "0.8.1", 23 | "license": "MIT", 24 | "dependencies": { 25 | "@jridgewell/trace-mapping": "0.3.9" 26 | }, 27 | "engines": { 28 | "node": ">=12" 29 | } 30 | }, 31 | "node_modules/@jridgewell/resolve-uri": { 32 | "version": "3.1.0", 33 | "license": "MIT", 34 | "engines": { 35 | "node": ">=6.0.0" 36 | } 37 | }, 38 | "node_modules/@jridgewell/sourcemap-codec": { 39 | "version": "1.4.14", 40 | "license": "MIT" 41 | }, 42 | "node_modules/@jridgewell/trace-mapping": { 43 | "version": "0.3.9", 44 | "license": "MIT", 45 | "dependencies": { 46 | "@jridgewell/resolve-uri": "^3.0.3", 47 | "@jridgewell/sourcemap-codec": "^1.4.10" 48 | } 49 | }, 50 | "node_modules/@tsconfig/node10": { 51 | "version": "1.0.9", 52 | "license": "MIT" 53 | }, 54 | "node_modules/@tsconfig/node12": { 55 | "version": "1.0.11", 56 | "license": "MIT" 57 | }, 58 | "node_modules/@tsconfig/node14": { 59 | "version": "1.0.3", 60 | "license": "MIT" 61 | }, 62 | "node_modules/@tsconfig/node16": { 63 | "version": "1.0.3", 64 | "license": "MIT" 65 | }, 66 | "node_modules/@types/node": { 67 | "version": "18.11.17", 68 | "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.17.tgz", 69 | "integrity": "sha512-HJSUJmni4BeDHhfzn6nF0sVmd1SMezP7/4F0Lq+aXzmp2xm9O7WXrUtHW/CHlYVtZUbByEvWidHqRtcJXGF2Ng==" 70 | }, 71 | "node_modules/acorn": { 72 | "version": "8.8.0", 73 | "license": "MIT", 74 | "bin": { 75 | "acorn": "bin/acorn" 76 | }, 77 | "engines": { 78 | "node": ">=0.4.0" 79 | } 80 | }, 81 | "node_modules/acorn-walk": { 82 | "version": "8.2.0", 83 | "license": "MIT", 84 | "engines": { 85 | "node": ">=0.4.0" 86 | } 87 | }, 88 | "node_modules/arg": { 89 | "version": "4.1.3", 90 | "license": "MIT" 91 | }, 92 | "node_modules/boolbase": { 93 | "version": "1.0.0", 94 | "license": "ISC" 95 | }, 96 | "node_modules/cheerio": { 97 | "version": "1.0.0-rc.12", 98 | "license": "MIT", 99 | "dependencies": { 100 | "cheerio-select": "^2.1.0", 101 | "dom-serializer": "^2.0.0", 102 | "domhandler": "^5.0.3", 103 | "domutils": "^3.0.1", 104 | "htmlparser2": "^8.0.1", 105 | "parse5": "^7.0.0", 106 | "parse5-htmlparser2-tree-adapter": "^7.0.0" 107 | }, 108 | "engines": { 109 | "node": ">= 6" 110 | }, 111 | "funding": { 112 | "url": "https://github.com/cheeriojs/cheerio?sponsor=1" 113 | } 114 | }, 115 | "node_modules/cheerio-select": { 116 | "version": "2.1.0", 117 | "license": "BSD-2-Clause", 118 | "dependencies": { 119 | "boolbase": "^1.0.0", 120 | "css-select": "^5.1.0", 121 | "css-what": "^6.1.0", 122 | "domelementtype": "^2.3.0", 123 | "domhandler": "^5.0.3", 124 | "domutils": "^3.0.1" 125 | }, 126 | "funding": { 127 | "url": "https://github.com/sponsors/fb55" 128 | } 129 | }, 130 | "node_modules/create-require": { 131 | "version": "1.1.1", 132 | "license": "MIT" 133 | }, 134 | "node_modules/css-select": { 135 | "version": "5.1.0", 136 | "license": "BSD-2-Clause", 137 | "dependencies": { 138 | "boolbase": "^1.0.0", 139 | "css-what": "^6.1.0", 140 | "domhandler": "^5.0.2", 141 | "domutils": "^3.0.1", 142 | "nth-check": "^2.0.1" 143 | }, 144 | "funding": { 145 | "url": "https://github.com/sponsors/fb55" 146 | } 147 | }, 148 | "node_modules/css-what": { 149 | "version": "6.1.0", 150 | "license": "BSD-2-Clause", 151 | "engines": { 152 | "node": ">= 6" 153 | }, 154 | "funding": { 155 | "url": "https://github.com/sponsors/fb55" 156 | } 157 | }, 158 | "node_modules/diff": { 159 | "version": "4.0.2", 160 | "license": "BSD-3-Clause", 161 | "engines": { 162 | "node": ">=0.3.1" 163 | } 164 | }, 165 | "node_modules/dom-serializer": { 166 | "version": "2.0.0", 167 | "license": "MIT", 168 | "dependencies": { 169 | "domelementtype": "^2.3.0", 170 | "domhandler": "^5.0.2", 171 | "entities": "^4.2.0" 172 | }, 173 | "funding": { 174 | "url": "https://github.com/cheeriojs/dom-serializer?sponsor=1" 175 | } 176 | }, 177 | "node_modules/domelementtype": { 178 | "version": "2.3.0", 179 | "funding": [ 180 | { 181 | "type": "github", 182 | "url": "https://github.com/sponsors/fb55" 183 | } 184 | ], 185 | "license": "BSD-2-Clause" 186 | }, 187 | "node_modules/domhandler": { 188 | "version": "5.0.3", 189 | "license": "BSD-2-Clause", 190 | "dependencies": { 191 | "domelementtype": "^2.3.0" 192 | }, 193 | "engines": { 194 | "node": ">= 4" 195 | }, 196 | "funding": { 197 | "url": "https://github.com/fb55/domhandler?sponsor=1" 198 | } 199 | }, 200 | "node_modules/domutils": { 201 | "version": "3.0.1", 202 | "license": "BSD-2-Clause", 203 | "dependencies": { 204 | "dom-serializer": "^2.0.0", 205 | "domelementtype": "^2.3.0", 206 | "domhandler": "^5.0.1" 207 | }, 208 | "funding": { 209 | "url": "https://github.com/fb55/domutils?sponsor=1" 210 | } 211 | }, 212 | "node_modules/entities": { 213 | "version": "4.4.0", 214 | "license": "BSD-2-Clause", 215 | "engines": { 216 | "node": ">=0.12" 217 | }, 218 | "funding": { 219 | "url": "https://github.com/fb55/entities?sponsor=1" 220 | } 221 | }, 222 | "node_modules/htmlparser2": { 223 | "version": "8.0.1", 224 | "funding": [ 225 | "https://github.com/fb55/htmlparser2?sponsor=1", 226 | { 227 | "type": "github", 228 | "url": "https://github.com/sponsors/fb55" 229 | } 230 | ], 231 | "license": "MIT", 232 | "dependencies": { 233 | "domelementtype": "^2.3.0", 234 | "domhandler": "^5.0.2", 235 | "domutils": "^3.0.1", 236 | "entities": "^4.3.0" 237 | } 238 | }, 239 | "node_modules/make-error": { 240 | "version": "1.3.6", 241 | "license": "ISC" 242 | }, 243 | "node_modules/nth-check": { 244 | "version": "2.1.1", 245 | "license": "BSD-2-Clause", 246 | "dependencies": { 247 | "boolbase": "^1.0.0" 248 | }, 249 | "funding": { 250 | "url": "https://github.com/fb55/nth-check?sponsor=1" 251 | } 252 | }, 253 | "node_modules/parse5": { 254 | "version": "7.1.1", 255 | "license": "MIT", 256 | "dependencies": { 257 | "entities": "^4.4.0" 258 | }, 259 | "funding": { 260 | "url": "https://github.com/inikulin/parse5?sponsor=1" 261 | } 262 | }, 263 | "node_modules/parse5-htmlparser2-tree-adapter": { 264 | "version": "7.0.0", 265 | "license": "MIT", 266 | "dependencies": { 267 | "domhandler": "^5.0.2", 268 | "parse5": "^7.0.0" 269 | }, 270 | "funding": { 271 | "url": "https://github.com/inikulin/parse5?sponsor=1" 272 | } 273 | }, 274 | "node_modules/regexparam": { 275 | "version": "2.0.1", 276 | "license": "MIT", 277 | "engines": { 278 | "node": ">=8" 279 | } 280 | }, 281 | "node_modules/ts-node": { 282 | "version": "10.9.1", 283 | "license": "MIT", 284 | "dependencies": { 285 | "@cspotcode/source-map-support": "^0.8.0", 286 | "@tsconfig/node10": "^1.0.7", 287 | "@tsconfig/node12": "^1.0.7", 288 | "@tsconfig/node14": "^1.0.0", 289 | "@tsconfig/node16": "^1.0.2", 290 | "acorn": "^8.4.1", 291 | "acorn-walk": "^8.1.1", 292 | "arg": "^4.1.0", 293 | "create-require": "^1.1.0", 294 | "diff": "^4.0.1", 295 | "make-error": "^1.1.1", 296 | "v8-compile-cache-lib": "^3.0.1", 297 | "yn": "3.1.1" 298 | }, 299 | "bin": { 300 | "ts-node": "dist/bin.js", 301 | "ts-node-cwd": "dist/bin-cwd.js", 302 | "ts-node-esm": "dist/bin-esm.js", 303 | "ts-node-script": "dist/bin-script.js", 304 | "ts-node-transpile-only": "dist/bin-transpile.js", 305 | "ts-script": "dist/bin-script-deprecated.js" 306 | }, 307 | "peerDependencies": { 308 | "@swc/core": ">=1.2.50", 309 | "@swc/wasm": ">=1.2.50", 310 | "@types/node": "*", 311 | "typescript": ">=2.7" 312 | }, 313 | "peerDependenciesMeta": { 314 | "@swc/core": { 315 | "optional": true 316 | }, 317 | "@swc/wasm": { 318 | "optional": true 319 | } 320 | } 321 | }, 322 | "node_modules/typescript": { 323 | "version": "4.9.4", 324 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz", 325 | "integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==", 326 | "bin": { 327 | "tsc": "bin/tsc", 328 | "tsserver": "bin/tsserver" 329 | }, 330 | "engines": { 331 | "node": ">=4.2.0" 332 | } 333 | }, 334 | "node_modules/v8-compile-cache-lib": { 335 | "version": "3.0.1", 336 | "license": "MIT" 337 | }, 338 | "node_modules/worktop": { 339 | "version": "0.7.3", 340 | "license": "MIT", 341 | "dependencies": { 342 | "regexparam": "^2.0.0" 343 | }, 344 | "engines": { 345 | "node": ">=12" 346 | } 347 | }, 348 | "node_modules/yn": { 349 | "version": "3.1.1", 350 | "license": "MIT", 351 | "engines": { 352 | "node": ">=6" 353 | } 354 | } 355 | }, 356 | "dependencies": { 357 | "@cspotcode/source-map-support": { 358 | "version": "0.8.1", 359 | "requires": { 360 | "@jridgewell/trace-mapping": "0.3.9" 361 | } 362 | }, 363 | "@jridgewell/resolve-uri": { 364 | "version": "3.1.0" 365 | }, 366 | "@jridgewell/sourcemap-codec": { 367 | "version": "1.4.14" 368 | }, 369 | "@jridgewell/trace-mapping": { 370 | "version": "0.3.9", 371 | "requires": { 372 | "@jridgewell/resolve-uri": "^3.0.3", 373 | "@jridgewell/sourcemap-codec": "^1.4.10" 374 | } 375 | }, 376 | "@tsconfig/node10": { 377 | "version": "1.0.9" 378 | }, 379 | "@tsconfig/node12": { 380 | "version": "1.0.11" 381 | }, 382 | "@tsconfig/node14": { 383 | "version": "1.0.3" 384 | }, 385 | "@tsconfig/node16": { 386 | "version": "1.0.3" 387 | }, 388 | "@types/node": { 389 | "version": "18.11.17", 390 | "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.17.tgz", 391 | "integrity": "sha512-HJSUJmni4BeDHhfzn6nF0sVmd1SMezP7/4F0Lq+aXzmp2xm9O7WXrUtHW/CHlYVtZUbByEvWidHqRtcJXGF2Ng==" 392 | }, 393 | "acorn": { 394 | "version": "8.8.0" 395 | }, 396 | "acorn-walk": { 397 | "version": "8.2.0" 398 | }, 399 | "arg": { 400 | "version": "4.1.3" 401 | }, 402 | "boolbase": { 403 | "version": "1.0.0" 404 | }, 405 | "cheerio": { 406 | "version": "1.0.0-rc.12", 407 | "requires": { 408 | "cheerio-select": "^2.1.0", 409 | "dom-serializer": "^2.0.0", 410 | "domhandler": "^5.0.3", 411 | "domutils": "^3.0.1", 412 | "htmlparser2": "^8.0.1", 413 | "parse5": "^7.0.0", 414 | "parse5-htmlparser2-tree-adapter": "^7.0.0" 415 | } 416 | }, 417 | "cheerio-select": { 418 | "version": "2.1.0", 419 | "requires": { 420 | "boolbase": "^1.0.0", 421 | "css-select": "^5.1.0", 422 | "css-what": "^6.1.0", 423 | "domelementtype": "^2.3.0", 424 | "domhandler": "^5.0.3", 425 | "domutils": "^3.0.1" 426 | } 427 | }, 428 | "create-require": { 429 | "version": "1.1.1" 430 | }, 431 | "css-select": { 432 | "version": "5.1.0", 433 | "requires": { 434 | "boolbase": "^1.0.0", 435 | "css-what": "^6.1.0", 436 | "domhandler": "^5.0.2", 437 | "domutils": "^3.0.1", 438 | "nth-check": "^2.0.1" 439 | } 440 | }, 441 | "css-what": { 442 | "version": "6.1.0" 443 | }, 444 | "diff": { 445 | "version": "4.0.2" 446 | }, 447 | "dom-serializer": { 448 | "version": "2.0.0", 449 | "requires": { 450 | "domelementtype": "^2.3.0", 451 | "domhandler": "^5.0.2", 452 | "entities": "^4.2.0" 453 | } 454 | }, 455 | "domelementtype": { 456 | "version": "2.3.0" 457 | }, 458 | "domhandler": { 459 | "version": "5.0.3", 460 | "requires": { 461 | "domelementtype": "^2.3.0" 462 | } 463 | }, 464 | "domutils": { 465 | "version": "3.0.1", 466 | "requires": { 467 | "dom-serializer": "^2.0.0", 468 | "domelementtype": "^2.3.0", 469 | "domhandler": "^5.0.1" 470 | } 471 | }, 472 | "entities": { 473 | "version": "4.4.0" 474 | }, 475 | "htmlparser2": { 476 | "version": "8.0.1", 477 | "requires": { 478 | "domelementtype": "^2.3.0", 479 | "domhandler": "^5.0.2", 480 | "domutils": "^3.0.1", 481 | "entities": "^4.3.0" 482 | } 483 | }, 484 | "make-error": { 485 | "version": "1.3.6" 486 | }, 487 | "nth-check": { 488 | "version": "2.1.1", 489 | "requires": { 490 | "boolbase": "^1.0.0" 491 | } 492 | }, 493 | "parse5": { 494 | "version": "7.1.1", 495 | "requires": { 496 | "entities": "^4.4.0" 497 | } 498 | }, 499 | "parse5-htmlparser2-tree-adapter": { 500 | "version": "7.0.0", 501 | "requires": { 502 | "domhandler": "^5.0.2", 503 | "parse5": "^7.0.0" 504 | } 505 | }, 506 | "regexparam": { 507 | "version": "2.0.1" 508 | }, 509 | "ts-node": { 510 | "version": "10.9.1", 511 | "requires": { 512 | "@cspotcode/source-map-support": "^0.8.0", 513 | "@tsconfig/node10": "^1.0.7", 514 | "@tsconfig/node12": "^1.0.7", 515 | "@tsconfig/node14": "^1.0.0", 516 | "@tsconfig/node16": "^1.0.2", 517 | "acorn": "^8.4.1", 518 | "acorn-walk": "^8.1.1", 519 | "arg": "^4.1.0", 520 | "create-require": "^1.1.0", 521 | "diff": "^4.0.1", 522 | "make-error": "^1.1.1", 523 | "v8-compile-cache-lib": "^3.0.1", 524 | "yn": "3.1.1" 525 | } 526 | }, 527 | "typescript": { 528 | "version": "4.9.4", 529 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz", 530 | "integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==" 531 | }, 532 | "v8-compile-cache-lib": { 533 | "version": "3.0.1" 534 | }, 535 | "worktop": { 536 | "version": "0.7.3", 537 | "requires": { 538 | "regexparam": "^2.0.0" 539 | } 540 | }, 541 | "yn": { 542 | "version": "3.1.1" 543 | } 544 | } 545 | } 546 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nyaa-api-ts", 3 | "version": "1.0.0", 4 | "description": "Nyaa Web Scraper using Typescript", 5 | "main": "index.js", 6 | "scripts": {}, 7 | "keywords": [], 8 | "author": "", 9 | "license": "ISC", 10 | "devDependencies": { 11 | "@types/node": "^18.11.17", 12 | "typescript": "^4.9.4" 13 | }, 14 | "dependencies": { 15 | "cheerio": "^1.0.0-rc.12", 16 | "ts-node": "^10.9.1", 17 | "worktop": "^0.7.3" 18 | } 19 | } -------------------------------------------------------------------------------- /src/constants.ts: -------------------------------------------------------------------------------- 1 | export class Constants { 2 | static NyaaBaseUrl: string = "https://nyaa.si"; 3 | static NyaaAltUrl: string = "https://nyaa.si"; 4 | static DefaultProfilePic: string = 5 | "https://raw.githubusercontent.com/Yash-Garg/Nyaa-Api-Go/dev/static/default.png"; 6 | 7 | static NyaaEndpoints: Object = { 8 | all: { 9 | all: "0_0", 10 | }, 11 | anime: { 12 | all: "1_0", 13 | amv: "1_1", 14 | eng: "1_2", 15 | "non-eng": "1_3", 16 | raw: "1_4", 17 | }, 18 | audio: { 19 | all: "2_0", 20 | lossless: "2_1", 21 | lossy: "2_2", 22 | }, 23 | manga: { 24 | all: "3_0", 25 | eng: "3_1", 26 | "non-eng": "3_2", 27 | raw: "3_3", 28 | }, 29 | live_action: { 30 | all: "4_0", 31 | eng: "4_1", 32 | promo: "4_2", 33 | "non-eng": "4_3", 34 | raw: "4_4", 35 | }, 36 | pictures: { 37 | all: "5_0", 38 | graphics: "5_1", 39 | photos: "5_2", 40 | }, 41 | software: { 42 | all: "6_0", 43 | applications: "6_1", 44 | games: "6_2", 45 | }, 46 | }; 47 | } 48 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { listen, Router } from "worktop"; 2 | import * as CORS from "worktop/cors"; 3 | import { Handlers } from "./routes"; 4 | 5 | const API = new Router(); 6 | 7 | API.prepare = CORS.preflight({ 8 | origin: "*", 9 | headers: ["Cache-Control", "Content-Type"], 10 | methods: ["GET"], 11 | }); 12 | 13 | API.add("GET", "/", Handlers.Ping); 14 | API.add("GET", "/id/:id", Handlers.GetInfoFromID); 15 | API.add("GET", "/user/:username", Handlers.GetUserUploads); 16 | API.add("GET", "/:category", Handlers.GetCategoryTorrents); 17 | API.add("GET", "/:category/:subcategory", Handlers.GetCategoryTorrents); 18 | 19 | listen(API.run); 20 | -------------------------------------------------------------------------------- /src/models.ts: -------------------------------------------------------------------------------- 1 | export interface Torrent { 2 | id: number; 3 | title: string; 4 | category: string; 5 | uploaded: string; 6 | seeders: number; 7 | leechers: number; 8 | completed: number; 9 | size: string; 10 | file: string; 11 | link: string; 12 | magnet: string; 13 | } 14 | 15 | export interface File { 16 | torrent: Torrent; 17 | description: string; 18 | submittedBy: string; 19 | infoHash: string; 20 | commentInfo: Comments; 21 | } 22 | 23 | export interface Comment { 24 | name: string; 25 | content: string; 26 | image: string; 27 | timestamp: string; 28 | } 29 | 30 | export interface Comments { 31 | count: number; 32 | comments: Comment[]; 33 | } 34 | 35 | export interface QueryParams { 36 | query?: string; 37 | sort?: string; 38 | order?: string; 39 | page?: number; 40 | filter?: number; 41 | } 42 | -------------------------------------------------------------------------------- /src/routes.ts: -------------------------------------------------------------------------------- 1 | import { Handler } from "worktop"; 2 | import * as Scrapers from "./scrapers"; 3 | import { Constants } from "./constants"; 4 | import * as Utils from "./utils"; 5 | 6 | const baseUrl = Constants.NyaaAltUrl; 7 | 8 | export class Handlers { 9 | static Ping: Handler = function (_, res) { 10 | res.send(200, "Nyaa API v2 // Alive"); 11 | }; 12 | 13 | static GetInfoFromID: Handler = async function (req, res) { 14 | try { 15 | const id = req.params.id; 16 | const searchUrl = baseUrl + "/view/" + id; 17 | 18 | await Scrapers.fileInfoScraper(res, searchUrl); 19 | } catch (error) { 20 | res.send(404, "Not Found"); 21 | } 22 | }; 23 | 24 | static GetUserUploads: Handler = async function (req, res) { 25 | try { 26 | const username = req.params.username; 27 | const queryParams = Utils.getSearchParameters(req); 28 | 29 | const searchUrl = `${baseUrl}/user/${username}?q=${queryParams.query.trim()}&p=${ 30 | queryParams.page 31 | }&s=${queryParams.sort}&o=${queryParams.order}&f=${queryParams.filter}`; 32 | 33 | await Scrapers.scrapeNyaa(res, searchUrl); 34 | } catch (error) { 35 | res.send(404, "Not Found"); 36 | } 37 | }; 38 | 39 | static GetCategoryTorrents: Handler = async function (req, res) { 40 | try { 41 | const cat = req.params.category; 42 | const subCat = req.params.subcategory; 43 | 44 | const category = Utils.getCategoryID(cat, subCat); 45 | const queryParams = Utils.getSearchParameters(req); 46 | 47 | const searchUrl = `${baseUrl}?q=${queryParams.query.trim()}&c=${category}&p=${ 48 | queryParams.page 49 | }&s=${queryParams.sort}&o=${queryParams.order}&f=${queryParams.filter}`; 50 | 51 | await Scrapers.scrapeNyaa(res, searchUrl); 52 | } catch (error) { 53 | res.send(404, "Not Found"); 54 | } 55 | }; 56 | } 57 | -------------------------------------------------------------------------------- /src/scrapers.ts: -------------------------------------------------------------------------------- 1 | import { Constants } from "./constants"; 2 | import * as cheerio from "cheerio"; 3 | import type { ServerResponse } from "worktop/response"; 4 | import * as Models from "./models"; 5 | 6 | export async function fileInfoScraper(res: ServerResponse, url: string) { 7 | const response = await fetch(url); 8 | 9 | if (response.status === 200) { 10 | const responseBody = await response.text(); 11 | 12 | const $ = cheerio.load(responseBody); 13 | const container = $("body div.container").last(); 14 | const fileId = Number(url.split("/")[4]) 15 | 16 | const torrentData: Models.Torrent = { 17 | title: container.find("h3.panel-title").first().text().trim(), 18 | file: 19 | Constants.NyaaBaseUrl + 20 | container.find("div.panel-footer a").attr("href"), 21 | link: `${Constants.NyaaBaseUrl}/view/${fileId}`, 22 | id: fileId, 23 | magnet: container.find("div.panel-footer a:nth-child(2)").attr("href")!, 24 | size: container 25 | .find("div.panel-body div.row:nth-child(4) .col-md-5:nth-child(2)") 26 | .text() 27 | .trim(), 28 | category: container 29 | .find("div.panel-body div.row:nth-child(1) .col-md-5:nth-child(2)") 30 | .text() 31 | .trim(), 32 | uploaded: container 33 | .find("div.panel-body div.row:nth-child(1) .col-md-5:nth-child(4)") 34 | .text() 35 | .trim(), 36 | seeders: Number( 37 | container 38 | .find("div.panel-body div.row:nth-child(2) .col-md-5:nth-child(4)") 39 | .text() 40 | .trim() 41 | ), 42 | leechers: Number( 43 | container 44 | .find("div.panel-body div.row:nth-child(3) .col-md-5:nth-child(4)") 45 | .text() 46 | .trim() 47 | ), 48 | completed: Number( 49 | container 50 | .find("div.panel-body div.row:nth-child(4) .col-md-5:nth-child(4)") 51 | .text() 52 | .trim() 53 | ), 54 | }; 55 | 56 | const commentCount = Number( 57 | container.find("div#comments h3.panel-title").text().split("-").at(-1) 58 | ); 59 | 60 | let comments: Models.Comment[] = []; 61 | if (commentCount > 0) { 62 | container 63 | .find("div#comments div.comment-panel div.panel-body") 64 | .each((_, selection) => { 65 | const element = $(selection); 66 | 67 | const comment: Models.Comment = { 68 | name: element.find("a").first().text().trim(), 69 | content: element 70 | .find("div.comment-body div.comment-content") 71 | .text(), 72 | image: 73 | element.find("img.avatar").attr("src") ?? 74 | Constants.DefaultProfilePic, 75 | timestamp: element.find("a").children().first().text(), 76 | }; 77 | 78 | comments.push(comment); 79 | }); 80 | } 81 | 82 | const file: Models.File = { 83 | torrent: torrentData, 84 | description: container.find("div.panel-body#torrent-description").text(), 85 | submittedBy: container 86 | .find("div.panel-body div.row:nth-child(2) .col-md-5:nth-child(2)") 87 | .text() 88 | .trim(), 89 | infoHash: container 90 | .find("div.panel-body div.row:nth-child(5) .col-md-5:nth-child(2)") 91 | .text() 92 | .trim(), 93 | commentInfo: { 94 | count: commentCount, 95 | comments: comments, 96 | }, 97 | }; 98 | 99 | res.send(200, file); 100 | } else { 101 | res.send(404, "Not Found"); 102 | } 103 | } 104 | 105 | export async function scrapeNyaa(res: ServerResponse, url: string) { 106 | const response = await fetch(url); 107 | 108 | if (response.status === 200) { 109 | const responseBody = await response.text(); 110 | 111 | const $ = cheerio.load(responseBody); 112 | const table = $("tbody"); 113 | 114 | let torrents: Models.Torrent[] = []; 115 | table.find("tr").each((_, selection) => { 116 | const row = $(selection); 117 | const torrentPath = row.find("td:nth-child(2) a").last().attr("href"); 118 | const filePath = row.find("td:nth-child(3) a:nth-child(1)").attr("href"); 119 | 120 | const torrent: Models.Torrent = { 121 | id: Number(torrentPath.split("/")[2]), 122 | title: row.find("td:nth-child(2) a").last().text(), 123 | link: Constants.NyaaBaseUrl + torrentPath, 124 | file: Constants.NyaaBaseUrl + filePath, 125 | category: row.find("td:nth-child(1) a").attr("title"), 126 | size: row.find("td:nth-child(4)").text(), 127 | uploaded: row.find("td:nth-child(5)").text(), 128 | seeders: Number(row.find("td:nth-child(6)").text()), 129 | leechers: Number(row.find("td:nth-child(7)").text()), 130 | completed: Number(row.find("td:nth-child(8)").text()), 131 | magnet: row.find("td:nth-child(3) a:nth-child(2)").attr("href"), 132 | }; 133 | 134 | torrents.push(torrent); 135 | }); 136 | 137 | res.send(200, torrents); 138 | } else { 139 | res.send(404, "Not Found"); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/utils.ts: -------------------------------------------------------------------------------- 1 | import { ServerRequest } from "worktop/request"; 2 | import { Constants } from "./constants"; 3 | import { QueryParams } from "./models"; 4 | 5 | export async function checkNyaaUrl(): Promise { 6 | try { 7 | const resp = await fetch(Constants.NyaaBaseUrl); 8 | 9 | console.log("NyaaBaseUrl Status:", resp.statusText); 10 | 11 | if (resp.status === 200) { 12 | return Constants.NyaaBaseUrl; 13 | } else { 14 | return Constants.NyaaAltUrl; 15 | } 16 | } catch (error) { 17 | console.log("NyaaBaseUrl Error:", error ?? "Something went wrong."); 18 | return Constants.NyaaAltUrl; 19 | } 20 | } 21 | 22 | export function getCategoryID(c: string, s: string): string { 23 | if (s === undefined) { 24 | return Constants.NyaaEndpoints[c]["all"]; 25 | } else { 26 | return Constants.NyaaEndpoints[c][s]; 27 | } 28 | } 29 | 30 | export function getSearchParameters(req: ServerRequest): QueryParams { 31 | const q: string | null = (req.query.get("q") ?? "").replaceAll(" ", "+"); 32 | const p: number | null = Number(req.query.get("p")); 33 | const o: string | null = req.query.get("o") ?? ""; 34 | const f: number | null = Number(req.query.get("f")); 35 | let s: string | null = req.query.get("s") ?? ""; 36 | 37 | if (s == "date") { 38 | s = "id"; 39 | } 40 | 41 | return { query: q, page: p, order: o, sort: s, filter: f }; 42 | } 43 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowJs": true, 4 | "rootDir": "src", 5 | "lib": [ 6 | "ES2021.String" 7 | ] 8 | } 9 | } -------------------------------------------------------------------------------- /wrangler.toml: -------------------------------------------------------------------------------- 1 | name = "nyaa" 2 | compatibility_date = "2022-09-11" 3 | workers_dev = true 4 | main = "src/index.ts" 5 | 6 | [dev] 7 | port = 3000 8 | --------------------------------------------------------------------------------