From f6ef83f4c37e82fdbae7e2c1d05ea5fb1297ff89 Mon Sep 17 00:00:00 2001 From: Tymon Date: Thu, 2 Feb 2023 22:14:06 +0100 Subject: [PATCH] unfinished stuf --- .gitignore | 3 +- buildNode.ts | 42 +++++++++++++++++++++ deno.lock | 84 ++++++++++++++++++++++++++++++++++++++++++ src/classes/Search.ts | 21 +++++------ src/classes/Story.ts | 2 +- src/classes/User.ts | 6 +-- src/classes/Wattpad.ts | 2 - src/utils/http.ts | 83 ++++++++++++++++++++++++++++------------- 8 files changed, 200 insertions(+), 43 deletions(-) create mode 100644 buildNode.ts diff --git a/.gitignore b/.gitignore index 13a2608..c1f207f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ wattpad-app* -test.ts \ No newline at end of file +test.ts +npm/ \ No newline at end of file diff --git a/buildNode.ts b/buildNode.ts new file mode 100644 index 0000000..19c83df --- /dev/null +++ b/buildNode.ts @@ -0,0 +1,42 @@ +import { build, emptyDir } from "https://deno.land/x/dnt@0.33.1/mod.ts"; + +await emptyDir("./npm"); + +await build({ + testPattern: "index.ts", + rootTestDir: "./tests", + entryPoints: ["./mod.ts"], + outDir: "./npm", + scriptModule: false, + shims: { + deno: true, + custom: [{ + package: { + name: "node-fetch", + version: "3.3.0", + }, + globalNames: [{ + name: "fetch", + exportName: "default", + }, { + name: "Headers", + }, { + name: "RequestInit", + typeOnly: true, + }, { + name: "Request", + }, { + name: "RequestInfo", + typeOnly: true, + }], + }], + }, + package: { + name: "wattpad", + version: Deno.args[0], + description: "new air wyatts", + license: "Unlicense", + }, +}); + +Deno.copyFileSync("README.md", "npm/README.md"); diff --git a/deno.lock b/deno.lock index 839011e..0a23696 100644 --- a/deno.lock +++ b/deno.lock @@ -13,6 +13,29 @@ "https://deno.land/std@0.122.0/path/posix.ts": "34349174b9cd121625a2810837a82dd8b986bbaaad5ade690d1de75bbb4555b2", "https://deno.land/std@0.122.0/path/separator.ts": "8fdcf289b1b76fd726a508f57d3370ca029ae6976fcde5044007f062e643ff1c", "https://deno.land/std@0.122.0/path/win32.ts": "11549e8c6df8307a8efcfa47ad7b2a75da743eac7d4c89c9723a944661c8bd2e", + "https://deno.land/std@0.140.0/_util/assert.ts": "e94f2eb37cebd7f199952e242c77654e43333c1ac4c5c700e929ea3aa5489f74", + "https://deno.land/std@0.140.0/_util/os.ts": "3b4c6e27febd119d36a416d7a97bd3b0251b77c88942c8f16ee5953ea13e2e49", + "https://deno.land/std@0.140.0/bytes/bytes_list.ts": "67eb118e0b7891d2f389dad4add35856f4ad5faab46318ff99653456c23b025d", + "https://deno.land/std@0.140.0/bytes/equals.ts": "fc16dff2090cced02497f16483de123dfa91e591029f985029193dfaa9d894c9", + "https://deno.land/std@0.140.0/bytes/mod.ts": "763f97d33051cc3f28af1a688dfe2830841192a9fea0cbaa55f927b49d49d0bf", + "https://deno.land/std@0.140.0/fmt/colors.ts": "30455035d6d728394781c10755351742dd731e3db6771b1843f9b9e490104d37", + "https://deno.land/std@0.140.0/fs/_util.ts": "0fb24eb4bfebc2c194fb1afdb42b9c3dda12e368f43e8f2321f84fc77d42cb0f", + "https://deno.land/std@0.140.0/fs/ensure_dir.ts": "9dc109c27df4098b9fc12d949612ae5c9c7169507660dcf9ad90631833209d9d", + "https://deno.land/std@0.140.0/fs/expand_glob.ts": "0c10130d67c9b02164b03df8e43c6d6defbf8e395cb69d09e84a8586e6d72ac3", + "https://deno.land/std@0.140.0/fs/walk.ts": "117403ccd21fd322febe56ba06053b1ad5064c802170f19b1ea43214088fe95f", + "https://deno.land/std@0.140.0/hash/sha256.ts": "803846c7a5a8a5a97f31defeb37d72f519086c880837129934f5d6f72102a8e8", + "https://deno.land/std@0.140.0/io/buffer.ts": "bd0c4bf53db4b4be916ca5963e454bddfd3fcd45039041ea161dbf826817822b", + "https://deno.land/std@0.140.0/io/types.d.ts": "01f60ae7ec02675b5dbed150d258fc184a78dfe5c209ef53ba4422b46b58822c", + "https://deno.land/std@0.140.0/path/_constants.ts": "df1db3ffa6dd6d1252cc9617e5d72165cd2483df90e93833e13580687b6083c3", + "https://deno.land/std@0.140.0/path/_interface.ts": "ee3b431a336b80cf445441109d089b70d87d5e248f4f90ff906820889ecf8d09", + "https://deno.land/std@0.140.0/path/_util.ts": "c1e9686d0164e29f7d880b2158971d805b6e0efc3110d0b3e24e4b8af2190d2b", + "https://deno.land/std@0.140.0/path/common.ts": "bee563630abd2d97f99d83c96c2fa0cca7cee103e8cb4e7699ec4d5db7bd2633", + "https://deno.land/std@0.140.0/path/glob.ts": "cb5255638de1048973c3e69e420c77dc04f75755524cb3b2e160fe9277d939ee", + "https://deno.land/std@0.140.0/path/mod.ts": "d3e68d0abb393fb0bf94a6d07c46ec31dc755b544b13144dee931d8d5f06a52d", + "https://deno.land/std@0.140.0/path/posix.ts": "293cdaec3ecccec0a9cc2b534302dfe308adb6f10861fa183275d6695faace44", + "https://deno.land/std@0.140.0/path/separator.ts": "fe1816cb765a8068afb3e8f13ad272351c85cbc739af56dacfc7d93d710fe0f9", + "https://deno.land/std@0.140.0/path/win32.ts": "31811536855e19ba37a999cd8d1b62078235548d67902ece4aa6b814596dd757", + "https://deno.land/std@0.140.0/streams/conversion.ts": "712585bfa0172a97fb68dd46e784ae8ad59d11b88079d6a4ab098ff42e697d21", "https://deno.land/std@0.153.0/_deno_unstable.ts": "4ddb8672d49d58b5bbc4a5a7a2f1b3bce4fd06aa4c8b8476728334391667de7b", "https://deno.land/std@0.153.0/_util/assert.ts": "e94f2eb37cebd7f199952e242c77654e43333c1ac4c5c700e929ea3aa5489f74", "https://deno.land/std@0.153.0/async/abortable.ts": "87aa7230be8360c24ad437212311c9e8d4328854baec27b4c7abb26e85515c06", @@ -78,6 +101,25 @@ "https://deno.land/std@0.170.0/node/internal_binding/_winerror.ts": "8811d4be66f918c165370b619259c1f35e8c3e458b8539db64c704fbde0a7cd2", "https://deno.land/std@0.170.0/node/internal_binding/uv.ts": "27922aaec43de314afd99dfca1ce8f4d51ced9f5195e4917b54d387766404f61", "https://deno.land/std@0.170.0/node/perf_hooks.ts": "521454a3e74f0af6a2aad91fa59a22a2c7be19698106a0fbbbcf22ac33764105", + "https://deno.land/std@0.171.0/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462", + "https://deno.land/std@0.171.0/_util/os.ts": "d932f56d41e4f6a6093d56044e29ce637f8dcc43c5a90af43504a889cf1775e3", + "https://deno.land/std@0.171.0/fmt/colors.ts": "938c5d44d889fb82eff6c358bea8baa7e85950a16c9f6dae3ec3a7a729164471", + "https://deno.land/std@0.171.0/fs/_util.ts": "65381f341af1ff7f40198cee15c20f59951ac26e51ddc651c5293e24f9ce6f32", + "https://deno.land/std@0.171.0/fs/empty_dir.ts": "c3d2da4c7352fab1cf144a1ecfef58090769e8af633678e0f3fabaef98594688", + "https://deno.land/std@0.171.0/fs/expand_glob.ts": "536055845aafc32de7e7a46c3b778a741825d5e2ed8580d9851a01ec7a5adf2e", + "https://deno.land/std@0.171.0/fs/walk.ts": "ea95ffa6500c1eda6b365be488c056edc7c883a1db41ef46ec3bf057b1c0fe32", + "https://deno.land/std@0.171.0/path/_constants.ts": "e49961f6f4f48039c0dfed3c3f93e963ca3d92791c9d478ac5b43183413136e0", + "https://deno.land/std@0.171.0/path/_interface.ts": "6471159dfbbc357e03882c2266d21ef9afdb1e4aa771b0545e90db58a0ba314b", + "https://deno.land/std@0.171.0/path/_util.ts": "86c2375a996c1931b2f2ac71fefd5ddf0cf0e579fa4ab12d3e4c552d4223b8d8", + "https://deno.land/std@0.171.0/path/common.ts": "ee7505ab01fd22de3963b64e46cff31f40de34f9f8de1fff6a1bd2fe79380000", + "https://deno.land/std@0.171.0/path/glob.ts": "d479e0a695621c94d3fd7fe7abd4f9499caf32a8de13f25073451c6ef420a4e1", + "https://deno.land/std@0.171.0/path/mod.ts": "4b83694ac500d7d31b0cdafc927080a53dc0c3027eb2895790fb155082b0d232", + "https://deno.land/std@0.171.0/path/posix.ts": "2ecc259e6f34013889b7638ff90339a82d8178f629155761ce6001e41af55a43", + "https://deno.land/std@0.171.0/path/separator.ts": "0fb679739d0d1d7bf45b68dacfb4ec7563597a902edbaf3c59b50d5bcadd93b1", + "https://deno.land/std@0.171.0/path/win32.ts": "99170a0eb0e2b1ce41499c1e86bb55320cb6606299ad947f57ee0a291cdb93d5", + "https://deno.land/std@0.172.0/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462", + "https://deno.land/std@0.172.0/datetime/to_imf.ts": "851d072a1c86c7ec34c753e61b1951ceda50c9da805b170a0ecaea3b024dd266", + "https://deno.land/std@0.172.0/http/cookie.ts": "4afa70654aee951b1b5a82b8939566843bf6eea605a961c7abc8e9705afd0a20", "https://deno.land/std@0.97.0/_util/assert.ts": "2f868145a042a11d5ad0a3c748dcf580add8a0dbc0e876eaa0026303a5488f58", "https://deno.land/std@0.97.0/_util/os.ts": "e282950a0eaa96760c0cf11e7463e66babd15ec9157d4c9ed49cc0925686f6a7", "https://deno.land/std@0.97.0/encoding/base64.ts": "eecae390f1f1d1cae6f6c6d732ede5276bf4b9cd29b1d281678c054dc5cc009e", @@ -117,6 +159,18 @@ "https://deno.land/x/cache@0.2.13/file_fetcher.ts": "5c793cc83a5b9377679ec313b2a2321e51bf7ed15380fa82d387f1cdef3b924f", "https://deno.land/x/cache@0.2.13/helpers.ts": "d1545d6432277b7a0b5ea254d1c51d572b6452a8eadd9faa7ad9c5586a1725c4", "https://deno.land/x/cache@0.2.13/mod.ts": "3188250d3a013ef6c9eb060e5284cf729083af7944a29e60bb3d8597dd20ebcd", + "https://deno.land/x/code_block_writer@11.0.3/mod.ts": "2c3448060e47c9d08604c8f40dee34343f553f33edcdfebbf648442be33205e5", + "https://deno.land/x/code_block_writer@11.0.3/utils/string_utils.ts": "60cb4ec8bd335bf241ef785ccec51e809d576ff8e8d29da43d2273b69ce2a6ff", + "https://deno.land/x/deno_cache@0.4.1/auth_tokens.ts": "5fee7e9155e78cedf3f6ff3efacffdb76ac1a76c86978658d9066d4fb0f7326e", + "https://deno.land/x/deno_cache@0.4.1/cache.ts": "51f72f4299411193d780faac8c09d4e8cbee951f541121ef75fcc0e94e64c195", + "https://deno.land/x/deno_cache@0.4.1/deno_dir.ts": "f2a9044ce8c7fe1109004cda6be96bf98b08f478ce77e7a07f866eff1bdd933f", + "https://deno.land/x/deno_cache@0.4.1/deps.ts": "8974097d6c17e65d9a82d39377ae8af7d94d74c25c0cbb5855d2920e063f2343", + "https://deno.land/x/deno_cache@0.4.1/dirs.ts": "d2fa473ef490a74f2dcb5abb4b9ab92a48d2b5b6320875df2dee64851fa64aa9", + "https://deno.land/x/deno_cache@0.4.1/disk_cache.ts": "1f3f5232cba4c56412d93bdb324c624e95d5dd179d0578d2121e3ccdf55539f9", + "https://deno.land/x/deno_cache@0.4.1/file_fetcher.ts": "07a6c5f8fd94bf50a116278cc6012b4921c70d2251d98ce1c9f3c352135c39f7", + "https://deno.land/x/deno_cache@0.4.1/http_cache.ts": "f632e0d6ec4a5d61ae3987737a72caf5fcdb93670d21032ddb78df41131360cd", + "https://deno.land/x/deno_cache@0.4.1/mod.ts": "ef1cda9235a93b89cb175fe648372fc0f785add2a43aa29126567a05e3e36195", + "https://deno.land/x/deno_cache@0.4.1/util.ts": "8cb686526f4be5205b92c819ca2ce82220aa0a8dd3613ef0913f6dc269dbbcfe", "https://deno.land/x/deno_dom@v0.1.36-alpha/build/deno-wasm/deno-wasm.js": "3fa41dba4813e6d4b024a53a146b76e1afcbdf218fc02063442378c61239ed14", "https://deno.land/x/deno_dom@v0.1.36-alpha/deno-dom-native.ts": "7c350b149092f5ff094faf599c2541373a756ccab929832199958ec0e321f90a", "https://deno.land/x/deno_dom@v0.1.36-alpha/deno-dom-wasm.ts": "bfd999a493a6974e9fca4d331bee03bfb68cfc600c662cd0b48b21d67a2a8ba0", @@ -140,6 +194,27 @@ "https://deno.land/x/deno_dom@v0.1.36-alpha/src/dom/utils-types.ts": "96db30e3e4a75b194201bb9fa30988215da7f91b380fca6a5143e51ece2a8436", "https://deno.land/x/deno_dom@v0.1.36-alpha/src/dom/utils.ts": "ecd889ba74f3ce282620d8ca1d4d5e0365e6cc86101d2352f3bbf936ae496e2c", "https://deno.land/x/deno_dom@v0.1.36-alpha/src/parser.ts": "b65eb7e673fa7ca611de871de109655f0aa9fa35ddc1de73df1a5fc2baafc332", + "https://deno.land/x/deno_graph@0.26.0/lib/deno_graph.generated.js": "2f7ca85b2ceb80ec4b3d1b7f3a504956083258610c7b9a1246238c5b7c68f62d", + "https://deno.land/x/deno_graph@0.26.0/lib/loader.ts": "380e37e71d0649eb50176a9786795988fc3c47063a520a54b616d7727b0f8629", + "https://deno.land/x/deno_graph@0.26.0/lib/media_type.ts": "222626d524fa2f9ebcc0ec7c7a7d5dfc74cc401cc46790f7c5e0eab0b0787707", + "https://deno.land/x/deno_graph@0.26.0/lib/snippets/deno_graph-de651bc9c240ed8d/src/deno_apis.js": "41192baaa550a5c6a146280fae358cede917ae16ec4e4315be51bef6631ca892", + "https://deno.land/x/deno_graph@0.26.0/lib/types.d.ts": "2bbdbf895321d1df8db511fab00160a0211c09c2e7cac56c522dd6e9ed6d2ef7", + "https://deno.land/x/deno_graph@0.26.0/mod.ts": "11131ae166580a1c7fa8506ff553751465a81c263d94443f18f353d0c320bc14", + "https://deno.land/x/dnt@0.33.1/lib/compiler.ts": "dd589db479d6d7e69999865003ab83c41544e251ece4f21f2f2ee74557097ba6", + "https://deno.land/x/dnt@0.33.1/lib/compiler_transforms.ts": "cbb1fd5948f5ced1aa5c5aed9e45134e2357ce1e7220924c1d7bded30dcd0dd0", + "https://deno.land/x/dnt@0.33.1/lib/mod.deps.ts": "6648fb17b4a49677cb0c24f60ffb5067a86ad69ff97712d40fe0d62b281b1811", + "https://deno.land/x/dnt@0.33.1/lib/npm_ignore.ts": "ddc1a7a76b288ca471bf1a6298527887a0f9eb7e25008072fd9c9fa9bb28c71a", + "https://deno.land/x/dnt@0.33.1/lib/package_json.ts": "2d629dbaef8004971e38ce3661f04b915a35342b905c3d98ff4a25343c2a8293", + "https://deno.land/x/dnt@0.33.1/lib/pkg/dnt_wasm.generated.js": "00257fc2f03321bb5f2b9bc23cb85e79fe55eb49a325d5ab925b9fc81b4aa963", + "https://deno.land/x/dnt@0.33.1/lib/pkg/snippets/dnt-wasm-a15ef721fa5290c5/helpers.js": "a6b95adc943a68d513fe8ed9ec7d260ac466b7a4bced4e942f733e494bb9f1be", + "https://deno.land/x/dnt@0.33.1/lib/shims.ts": "7998851b149cb230f5183590d3b66c06f696fefb1c31c24eb5736f1ef12a4de1", + "https://deno.land/x/dnt@0.33.1/lib/test_runner/get_test_runner_code.ts": "2a4e26aa33120f3cc9e03b8538211a5047a4bad4c64e895944b87f2dcd55d904", + "https://deno.land/x/dnt@0.33.1/lib/test_runner/test_runner.ts": "b91d77d9d4b82984cb2ba7431ba6935756ba72f62e7dd4db22cd47a680ebd952", + "https://deno.land/x/dnt@0.33.1/lib/transform.deps.ts": "a6e138e380ebe4479bed1b00ae8dff1d4e6626cc53bc79dd98f907b43745d63a", + "https://deno.land/x/dnt@0.33.1/lib/types.ts": "34e45a3136c2f21f797173ea46d9ea5d1639eb7b834a5bd565aad4214fa32603", + "https://deno.land/x/dnt@0.33.1/lib/utils.ts": "d13b5b3148a2c71e9b2f1c84c7be7393b825ae972505e23c2f6b1e5287e96b43", + "https://deno.land/x/dnt@0.33.1/mod.ts": "691ea4b644cc61123b7beed19e66af301f25985483b81d21cfe49a0be2877fd9", + "https://deno.land/x/dnt@0.33.1/transform.ts": "1b127c5f22699c8ab2545b98aeca38c4e5c21405b0f5342ea17e9c46280ed277", "https://deno.land/x/plug@0.5.2/deps.ts": "0f53866f60dc4f89bbc3be9d096f7501f2068a51923db220f43f0ad284b6b892", "https://deno.land/x/plug@0.5.2/mod.ts": "ca0f207510399c27ed1d38b4c4a2d9389f1d69a0213d836fdd2418779b331251", "https://deno.land/x/plug@0.5.2/plug.ts": "e495c772bc3b19eb30d820bb83f1b75f6047e3009e19d9a5d81dbe68ca642fd9", @@ -148,6 +223,15 @@ "https://deno.land/x/plug@1.0.0-rc.3/mod.ts": "6db7edf982efedf8c9e1002754fbb109c22201fd8fb89ba715fe70171b5e821c", "https://deno.land/x/plug@1.0.0-rc.3/types.ts": "d8eb738fc6ed883e6abf77093442c2f0b71af9090f15c7613621d4039e410ee1", "https://deno.land/x/plug@1.0.0-rc.3/util.ts": "e53018a487292d11036efeae9da81eb82ef47f9e53d0ba83f545b0220350155a", + "https://deno.land/x/ts_morph@17.0.1/bootstrap/mod.ts": "b53aad517f106c4079971fcd4a81ab79fadc40b50061a3ab2b741a09119d51e9", + "https://deno.land/x/ts_morph@17.0.1/bootstrap/ts_morph_bootstrap.d.ts": "607e651c5ae5aa57c2ac4090759a6379e809c0cdc42114742ac67353b1a75038", + "https://deno.land/x/ts_morph@17.0.1/bootstrap/ts_morph_bootstrap.js": "91a954daa993c5acb3361aa5279394f81ea6fe18b3854345c86111b336491cfc", + "https://deno.land/x/ts_morph@17.0.1/common/DenoRuntime.ts": "537800e840d0994f9055164e11bf33eadf96419246af0d3c453793c3ae67bdb3", + "https://deno.land/x/ts_morph@17.0.1/common/mod.ts": "01985d2ee7da8d1caee318a9d07664774fbee4e31602bc2bb6bb62c3489555ed", + "https://deno.land/x/ts_morph@17.0.1/common/ts_morph_common.d.ts": "ee7767b0c68b23c65bb607c94b6cb3512e8237fbcb7d1d8383a33235cde2c068", + "https://deno.land/x/ts_morph@17.0.1/common/ts_morph_common.js": "49a79124b941ba2b35d81ac9eb90fc33c957b2640cdb97569c1941bac5a3bbdb", + "https://deno.land/x/ts_morph@17.0.1/common/typescript.d.ts": "57e52a0882af4e835473dda27e4316cc31149866970210f9f79b940e916b7838", + "https://deno.land/x/ts_morph@17.0.1/common/typescript.js": "5dd669eb199ee2a539924c63a92e23d95df43dfe2fbe3a9d68c871648be1ad5e", "https://deno.land/x/url_join@1.0.0/mod.ts": "d3c7007e3ab15594e54d5b90dce623b3e274a726e0cdf57858e7ecab77a0166c", "https://denopkg.dev/gh/Ruthenic/deno-dom@master/build/deno-wasm/deno-wasm.js": "3fa41dba4813e6d4b024a53a146b76e1afcbdf218fc02063442378c61239ed14", "https://denopkg.dev/gh/Ruthenic/deno-dom@master/deno-dom-native.ts": "69e7714229512f47b2049a2c81e39830499ecb105e9741908a935a24f281618b", diff --git a/src/classes/Search.ts b/src/classes/Search.ts index 7e649fc..92f87e2 100644 --- a/src/classes/Search.ts +++ b/src/classes/Search.ts @@ -50,17 +50,16 @@ export default class Search { async update(pageNum: number) { this.results = []; - const res: SearchResults = - await (await this.#session.get("/v4/stories", false, { - params: new URLSearchParams({ - fields: "stories(id)", - query: this.#opts.query as string, - filter: this.#opts.sort, - limit: this.#opts.limit.toString(), - offset: (pageNum * this.#opts.limit).toString(), - mature: "1", - }), - })).json(); + const res = await (await this.#session.get("/v4/stories", false, { + params: new URLSearchParams({ + fields: "stories(id)", + query: this.#opts.query as string, + filter: this.#opts.sort, + limit: this.#opts.limit.toString(), + offset: (pageNum * this.#opts.limit).toString(), + mature: "1", + }), + })).json() as SearchResults; for (let i = 0; i < res.stories.length; i++) { const work = new Work( diff --git a/src/classes/Story.ts b/src/classes/Story.ts index e081536..69ce221 100644 --- a/src/classes/Story.ts +++ b/src/classes/Story.ts @@ -60,7 +60,7 @@ export default class Story { "id,title,length,createDate,modifyDate,voteCount,readCount,commentCount,url,promoted,sponsor,language,user,description,cover,highlight_colour,completed,isPaywalled,paidModel,categories,numParts,readingPosition,deleted,dateAdded,lastPublishedPart(createDate),tags,copyright,rating,story_text_url(text),,parts(id,title,voteCount,commentCount,videoId,readCount,photoUrl,createDate,modifyDate,length,voted,deleted,text_url(text),dedication,url,wordCount),isAdExempt,tagRankings", }), }, - )).json(); + )).json() as StoryJSON; this.name = this.storyJSON.title; this.tags = this.storyJSON.tags; diff --git a/src/classes/User.ts b/src/classes/User.ts index ca7c783..cf33b07 100644 --- a/src/classes/User.ts +++ b/src/classes/User.ts @@ -28,7 +28,7 @@ export default class User { }), }, )) - .json(); + .json() as UserJSON; this.displayName = this.userJSON.name; this.description = this.userJSON.description; @@ -36,7 +36,7 @@ export default class User { } async updateStories(pageNum: number) { - const res: SearchResults = await (await this.#session.get( + const res = await (await this.#session.get( `/v4/users/${this.username}/stories/published`, false, { @@ -47,7 +47,7 @@ export default class User { mature: "1", }), }, - )).json(); + )).json() as SearchResults; for (let i = 0; i < res.stories.length; i++) { const story = new Story( diff --git a/src/classes/Wattpad.ts b/src/classes/Wattpad.ts index ab51b61..1c24d59 100644 --- a/src/classes/Wattpad.ts +++ b/src/classes/Wattpad.ts @@ -22,7 +22,6 @@ export default class Wattpad { async authenticate(username: string, password: string) { await this.session.post("https://www.wattpad.com/login", { - "credentials": "include", "headers": { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:108.0) Gecko/20100101 Firefox/108.0", @@ -34,7 +33,6 @@ export default class Wattpad { password, }), "method": "POST", - "mode": "cors", }); } diff --git a/src/utils/http.ts b/src/utils/http.ts index d737be8..8b0761b 100644 --- a/src/utils/http.ts +++ b/src/utils/http.ts @@ -1,10 +1,8 @@ import { - CookieJar, - wrapFetch, -} from "https://deno.land/x/another_cookiejar@v5.0.2/mod.ts"; - -const cookieJar = new CookieJar(); -const cookieFetch = wrapFetch({ cookieJar }); + Cookie, + getSetCookies, + setCookie, +} from "https://deno.land/std@0.172.0/http/cookie.ts"; export interface Options { url?: string; @@ -12,12 +10,53 @@ export interface Options { headers?: Record; } +const cookies: Cookie[] = []; + +function isRedirect(status: number) { + return [301, 302, 303, 307, 308].includes(status); +} + +async function cookieFetch(input: RequestInfo, init?: RequestInit) { + const headers = new Headers((input as Request).headers || {}); + + if (init?.headers) { + const initHeaders = new Headers(init.headers); + + for (const entry of initHeaders.entries()) { + headers.set(entry[0], entry[1]); + } + } + + if (cookies) { + for (let i = 0; i < cookies.length; i++) { + const cookie = cookies[i]; + + setCookie(headers, cookie); + } + } + + const res = await fetch(input, init); + const setCookies = getSetCookies(res.headers); + + if (setCookies) { + for (let i = 0; i < setCookies.length; i++) { + const setCookie = setCookies[i]; + // FIXME: this dupe protection is bad out of 10 + if (!cookies.find((cookie) => cookie.name === setCookie.name)) { + cookies.push(setCookie); + } + } + } + + return res; +} + const defaultOptions: Options = { url: "https://wattpad.com", apiUrl: "https://api.wattpad.com", headers: { // lol - "Authorization": (/wattpad\.apiAuthKey = ('|")(.*)('|")/.exec( + Authorization: (/wattpad\.apiAuthKey = ('|")(.*)('|")/.exec( await (await fetch("https://www.wattpad.com")).text(), ) as RegExpExecArray)[2], "User-Agent": @@ -28,33 +67,27 @@ const defaultOptions: Options = { const newSession = (opts: Options) => { opts = Object.assign(defaultOptions, opts); + return { - get: async (path: string, useAPI = false, opts2?: { + get: async (path: string, useAPI = false, opts2: { params?: URLSearchParams; }) => { - const res = await cookieFetch( - (useAPI ? opts.apiUrl : opts.url) + path + "?" + - (opts2?.params ?? "") + - `&wp_token=${cookieJar.getCookie({ - name: "token", - })?.toString().split("=")?.[1]}`, - { - headers: opts.headers, - }, - ); + const url = (useAPI ? opts.apiUrl : opts.url) + path + "?" + + (opts2?.params ?? ""); + + const res = await cookieFetch(url, { + headers: opts.headers, + }); + if (res.status > 300) { - console.log( - (useAPI ? opts.apiUrl : opts.url) + path + "?" + - (opts2?.params ?? "") + - `&wp_token=${cookieJar.getCookie({ - name: "token", - })?.toString().split("=")?.[1]}`, - ); + console.log(url); console.log(await res.json()); throw new Error("Failed request, probably rate-limited"); } + return res; }, + post: async (url: string, opts2: RequestInit) => { const res = await cookieFetch( url,