Code Monkey home page Code Monkey logo

Comments (14)

ssnjr2002 avatar ssnjr2002 commented on May 26, 2024 1

Hmm, are you sure the credentials line is intact? You have to paste the code below it. Like:

var credentials = {blah blah};

addEventListener.... etc

from stremio-gdrive.

AlyafeiAli avatar AlyafeiAli commented on May 26, 2024 1

that was the issue, it's working now😂
I'll test a few movies and report back

from stremio-gdrive.

AlyafeiAli avatar AlyafeiAli commented on May 26, 2024 1

I should be the one thanking you for taking out the time to fix these issues, so thank you 😊

This will force stremio to use the stremio server running in the bg instead of directly playing the link.

I forgot to mention that when Stremio crashes and I quickly switch back to it I can see that its server is offline, so I'm confident in fix 2 as well !

Looking forward to test them both lol

from stremio-gdrive.

ssnjr2002 avatar ssnjr2002 commented on May 26, 2024 1

Well this is it, fix 2, if it doesn't work we have run out of options.... Hopefully it does work lol

from stremio-gdrive.

ssnjr2002 avatar ssnjr2002 commented on May 26, 2024 1

btw, should I revert back to the old cf_proxy.js script before testing fix 2?

Nah, stick to the new one.

from stremio-gdrive.

AlyafeiAli avatar AlyafeiAli commented on May 26, 2024 1

Guess what, @ssnjr2002
The problem is finally fixed! No more crashes.
Finally lol
Thank you so much 🤩

from stremio-gdrive.

ssnjr2002 avatar ssnjr2002 commented on May 26, 2024 1

https://www.youtube.com/watch?v=z-JRdRXiNv4

from stremio-gdrive.

ssnjr2002 avatar ssnjr2002 commented on May 26, 2024

So after some poking around I still haven't figured out what exactly is the problem. I have made some changes to cf_proxy.js and I want you to test it (code below). Go to cf workers -> your proxy worker -> quick edit -> select everything below the first line (var credentials line) -> paste the code below -> hit save and deploy.

addEventListener("fetch", (event) => {
    event.respondWith(handleRequest(event.request));
});

let KVNAMESPACE;

try {
    KVNAMESPACE = STREMIO;
} catch {
    KVNAMESPACE = false;
}

let accessTokenObj = {};
let gapihost = "https://www.googleapis.com";

async function handleRequest(request) {
    console.log("handleRequest:", request.url);

    let url = new URL(request.url);
    let path_split = url.pathname.split("/");
    if (path_split[1] == "load")
        return await streamFile(request.headers.get("Range"), path_split[2]);
    else if (url.pathname == "/")
        return new Response("200 Online!", { status: 200 });
    else return new Response("404 Not Found!", { status: 404 });
}

async function streamFile(range = "", fileId) {
    console.log(`streamFile: ${fileId}, range: ${range}`);

    let fileUrl = `${gapihost}/drive/v3/files/${fileId}?alt=media`;
    let fileInit = {};
    fileInit.headers = {};
    fileInit.headers.Range = range;
    fileInit.headers.Authorization = "Bearer " + (await fetchAccessTokenObj());
    fileInit.method = "GET";

    return fetchRetry(fileUrl, fileInit);
}

async function fetchAccessTokenObj() {
    console.log("fetchAccessTokenObj");

    if (KVNAMESPACE) {
        accessTokenObj.expires_in = await KVNAMESPACE.get("expires_in");
        accessTokenObj.access_token = await KVNAMESPACE.get("access_token");
    }

    if (
        !accessTokenObj.access_token ||
        accessTokenObj.expires_in < Date.now()
    ) {
        accessTokenObj = await getAccessTokenObj();
        let expSecondsFromNow = accessTokenObj.expires_in;
        accessTokenObj.expires_in =
            Date.now() + accessTokenObj.expires_in * 1000;
        if (KVNAMESPACE) {
            KVNAMESPACE.put("expires_in", accessTokenObj.expires_in);
            KVNAMESPACE.put("access_token", accessTokenObj.access_token, {
                expirationTtl: expSecondsFromNow,
            });
        }
    }
    return accessTokenObj.access_token;
}

async function getAccessTokenObj() {
    console.log("getAccessTokenObj");

    let jsonBody = {};
    jsonBody.client_id = credentials.client_id;
    jsonBody.client_secret = credentials.client_secret;
    jsonBody.refresh_token = credentials.refresh_token;
    jsonBody.grant_type = "refresh_token";

    let oauthInit = {};
    oauthInit.method = "POST";
    oauthInit.body = JSON.stringify(jsonBody);

    let oauthUrl = gapihost + "/oauth2/v4/token";
    let response = await fetch(oauthUrl, oauthInit);
    return response.json();
}

async function fetchRetry(request, init, tries = 3, delay = 500) {
    let response = await fetch(request, init);
    console.log("fetchRetry", tries, response.status);

    if (!response.ok && tries > 1) {
        await sleep(delay);
        return fetchRetry(request, init, tries - 1);
    }
    return response;
}

async function sleep(delay) {
    return new Promise((r) => setTimeout(r, delay));
}

from stremio-gdrive.

AlyafeiAli avatar AlyafeiAli commented on May 26, 2024

I'll try asap and report back, thanks 👍🏼

from stremio-gdrive.

AlyafeiAli avatar AlyafeiAli commented on May 26, 2024

Whenever I play anything, it says "Oops, Player Error. Stremio encountered an error with this media." The worker is alive (200 Online!)
@ssnjr2002

from stremio-gdrive.

AlyafeiAli avatar AlyafeiAli commented on May 26, 2024

Sadly, Stremio still crashes. ( I tried all HW modes again ).
Looks like this is an issue with Stremio then, not the addon... Thankfully, everything works fine without a proxy.

from stremio-gdrive.

ssnjr2002 avatar ssnjr2002 commented on May 26, 2024

Ah, thanks for confirming that doesn't solve the issue. This means the proxy code is fine. I have two more possible fixes. I will be pushing a commit later for the heroku side of things. You can test that out and report back whenever you have got the time.

Once again thank you for bringing these issues to light. This bug has been annoying me for a while and I always thought it was the app's issue.

Here is the possible problem and fixes if you are interested in reading it: 😉

Fix 1: In the stremio addon documentation here, in the notWebReady flag, it's said that non mp4 urls needs to have notWebReady set to true. For the non proxy version I did this but for the proxy version I did not. So this might be the problem.

Fix 2: If that doesn't work I will add proxyHeaders to make the behaviourHints exactly like how it is for the non proxy version. This will force stremio to use the stremio server running in the bg instead of directly playing the link. Non proxy version pipes the stream through the stremio server and that might be why the it works without crashing but the proxy version struggles since its directly streamed by the player instead of being piped through the stremio server. As to why the player might not be able to play the stream directly, without crashing, I am not sure. Maybe its a bug in the app.

I am more confident in fix 2 since this is how the non proxy version works but for the sake of pinpointing the issue we have to test fix 1 and then fix 2.

from stremio-gdrive.

AlyafeiAli avatar AlyafeiAli commented on May 26, 2024

I tried fix 1, it worked well for ~30 minutes, but it crashed after that.
@ssnjr2002

from stremio-gdrive.

AlyafeiAli avatar AlyafeiAli commented on May 26, 2024

Well this is it, fix 2, if it doesn't work we have run out of options.... Hopefully it does work lol

Hopefully lol
btw, should I revert back to the old cf_proxy.js script before testing fix 2?

from stremio-gdrive.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.