pstream is dead; long live pstream taciturnaxolotl.github.io/pstream-ng/
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add retries to token check

Pas a2df05b1 2c9c30af

+77 -24
+77 -24
src/pages/parts/settings/SetupPart.tsx
··· 1 + /* eslint-disable no-console */ 1 2 import classNames from "classnames"; 2 3 import { ReactNode } from "react"; 3 4 import { useTranslation } from "react-i18next"; ··· 21 22 const testUrl = "https://postman-echo.com/get"; 22 23 const febboxApiTestUrl = "https://fed-api.pstream.org/movie/tt15239678"; 23 24 25 + const sleep = (ms: number): Promise<void> => { 26 + return new Promise((resolve) => { 27 + setTimeout(() => { 28 + resolve(); 29 + }, ms); 30 + }); 31 + }; 32 + 24 33 export type Status = "success" | "unset" | "error"; 25 34 26 35 type SetupData = { ··· 48 57 if (!febboxToken) { 49 58 return "unset"; 50 59 } 51 - try { 52 - const response = await fetch(febboxApiTestUrl, { 53 - headers: { 54 - "ui-token": febboxToken, 55 - }, 56 - }); 60 + 61 + let attempts = 0; 62 + const maxAttempts = 3; 63 + 64 + while (attempts < maxAttempts) { 65 + console.log( 66 + `Attempt ${attempts + 1} of ${maxAttempts} to check Febbox token`, 67 + ); 68 + try { 69 + const response = await fetch(febboxApiTestUrl, { 70 + headers: { 71 + "ui-token": febboxToken, 72 + }, 73 + }); 74 + 75 + if (!response.ok) { 76 + console.error("Febbox API test failed with status:", response.status); 77 + attempts += 1; 78 + if (attempts === maxAttempts) { 79 + console.log("Max attempts reached, returning error"); 80 + return "error"; 81 + } 82 + console.log("Retrying after failed response..."); 83 + await sleep(3000); 84 + continue; 85 + } 57 86 58 - if (!response.ok) { 59 - console.error("Febbox API test failed with status:", response.status); 60 - return "error"; 61 - } 87 + const data = (await response.json()) as any; 88 + if (!data || !data.streams) { 89 + console.error("Invalid response format from Febbox API:", data); 90 + attempts += 1; 91 + if (attempts === maxAttempts) { 92 + console.log("Max attempts reached, returning error"); 93 + return "error"; 94 + } 95 + console.log("Retrying after invalid response format..."); 96 + await sleep(3000); 97 + continue; 98 + } 62 99 63 - const data = (await response.json()) as any; 64 - if (!data || !data.streams) { 65 - console.error("Invalid response format from Febbox API:", data); 66 - return "error"; 67 - } 100 + const isVIPLink = Object.values(data.streams).some((link: any) => { 101 + if (typeof link === "string") { 102 + return link.toLowerCase().includes("vip"); 103 + } 104 + return false; 105 + }); 68 106 69 - const isVIPLink = Object.values(data.streams).some((link: any) => { 70 - if (typeof link === "string") { 71 - return link.toLowerCase().includes("vip"); 107 + if (isVIPLink) { 108 + console.log("VIP link found, returning success"); 109 + return "success"; 72 110 } 73 - return false; 74 - }); 75 111 76 - return isVIPLink ? "success" : "error"; 77 - } catch (error: any) { 78 - console.error("Error testing Febbox token:", error); 79 - return "error"; 112 + console.log("No VIP link found in attempt", attempts + 1); 113 + attempts += 1; 114 + if (attempts === maxAttempts) { 115 + console.log("Max attempts reached, returning error"); 116 + return "error"; 117 + } 118 + console.log("Retrying after no VIP link found..."); 119 + await sleep(3000); 120 + } catch (error: any) { 121 + console.error("Error testing Febbox token:", error); 122 + attempts += 1; 123 + if (attempts === maxAttempts) { 124 + console.log("Max attempts reached, returning error"); 125 + return "error"; 126 + } 127 + console.log("Retrying after error..."); 128 + await sleep(3000); 129 + } 80 130 } 131 + 132 + console.log("All attempts exhausted, returning error"); 133 + return "error"; 81 134 } 82 135 83 136 function useIsSetup() {