diff --git a/src/frontend/src/routes/+page.svelte b/src/frontend/src/routes/+page.svelte index 491470d..7a7693e 100644 --- a/src/frontend/src/routes/+page.svelte +++ b/src/frontend/src/routes/+page.svelte @@ -6,21 +6,42 @@ username: "", password: "", }; - let after_download_page = false; let url = ""; + let raw_story_id = ""; + let is_part_id = false; + let button_disabled = false; $: button_disabled = !story_id || (is_paid_story && !(credentials.username && credentials.password)); - $: url = - `/download/${story_id}?om=1` + - (download_images ? "&download_images=true" : "") + - (is_paid_story - ? `&username=${encodeURIComponent(credentials.username)}&password=${encodeURIComponent(credentials.password)}` - : ""); + $: { + is_part_id = false; + if (raw_story_id.includes("wattpad.com")) { + // Originally, I was going to call the Wattpad API (wattpad.com/api/v3/stories/${story_id}), but Wattpad kept blocking those requests. I suspect it has something to do with the Origin header, I wasn't able to remove it. + // In the future, if this is considered, it would be cool if we could derive the Story ID from a pasted Part URL. There isn't a direct API for this. + + if (raw_story_id.includes("/story/")) { + // https://wattpad.com/story/237369078-wattpad-books-presents + story_id = raw_story_id.split("/story/")[1].split("-")[0]; + raw_story_id = story_id; + } else if (raw_story_id.includes("/stories/")) { + // https://www.wattpad.com/api/v3/stories/237369078?fields=... + story_id = raw_story_id.split("/stories/")[1].split("?")[0]; + raw_story_id = story_id; + } else { + // https://www.wattpad.com/939051741-wattpad-books-presents-part-name + is_part_id = true; + raw_story_id = ""; + story_id = ""; + } + } else { + story_id = parseInt(raw_story_id) || ""; // parseInt returns NaN for undefined values. + raw_story_id = story_id; + } + }
@@ -49,20 +70,32 @@