Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch url https://www.youtube.com/iframe_api error #778

Open
4 tasks done
xuya227939 opened this issue Oct 14, 2024 · 1 comment
Open
4 tasks done

fetch url https://www.youtube.com/iframe_api error #778

xuya227939 opened this issue Oct 14, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@xuya227939
Copy link

Describe your suggestion

I use proxy, but get error

Error

Line:40 🍷 url https://www.youtube.com/iframe_api
Fetch error for https://www.youtube.com/iframe_api. Retrying in 2 seconds...

Line:40 🍷 url https://www.youtube.com/sw.js_data
Fetch error for https://www.youtube.com/sw.js_data. Retrying in 2 seconds...

Tell me why? Please, Thank you.

Code

const initializeInnertube = async () => {
  const proxyAgent = new HttpsProxyAgent(
    "https://v2sub.com/xxx"
  );
  return await Innertube.create({
    fetch: async (input, init) => {
      const url =
        typeof input === "string"
          ? input
          : input instanceof URL
          ? input.toString()
          : input.url;

      const modifiedInit = {
        ...init,
        method: init?.method || (init?.body ? "POST" : "GET"),
        agent: proxyAgent,
      };
      if (modifiedInit.method === "POST")
        modifiedInit.headers = {
          ...modifiedInit.headers,
          "Content-Type": "application/json",
        };

      const maxRetries = 5;
      let retryCount = 0;
      const fetchWithRetry = async () => {
        try {
          const response = await fetch(url, modifiedInit);
          if (!response.ok) {
            if (response.status === 429) {
              console.warn(
                `Too Many Requests. Retrying in ${delayTime / 1000} seconds...`
              );
            }
            throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
          }
          return response;
        } catch (error) {
          throw new Error(`Fetch error for ${url}: ${error.message}`);
        }
      };

      return fetchWithRetry();
    },
  });
};

Other details

No response

Checklist

  • I am running the latest version.
  • I checked the documentation and found no answer.
  • I have searched the existing issues and made sure this is not a duplicate.
  • I have provided sufficient information.
@xuya227939 xuya227939 added the enhancement New feature or request label Oct 14, 2024
@qiuqiang1985
Copy link

set proxy like this , it works.

import {setGlobalDispatcher, ProxyAgent} from 'undici'
const proxyh = "http://127.0.0.1:7890";
const socksAgent = new ProxyAgent({uri:proxyh});
setGlobalDispatcher(socksAgent);


const fetchTranscript = async (
  url: string
): Promise<(string | undefined)[] | undefined> => {
  const { Innertube } = await import("youtubei.js");

  const youtube = await Innertube.create({
    lang: "en",
    location: "US",
    retrieve_player: false,
  });

  try {
    const info = await youtube.getInfo(url);
    const transcriptData = await info.getTranscript();
    return transcriptData?.transcript?.content?.body?.initial_segments.map(
      (segment) => segment.snippet.text
    );
  } catch (error) {
    console.error("Error fetching transcript:", error);
    throw error;
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants