Stremio / stremio-addon-sdk

🧙 A Node.js SDK for creating and publishing Stremio add-ons
https://www.stremio.com/addon-sdk
MIT License
656 stars 175 forks source link

Something is wrong with Stremio add-on guide and meta handler #246

Closed lpuglia closed 10 months ago

lpuglia commented 10 months ago

Hello, I just started to develop my first add-on using https://stremio.github.io/stremio-addon-guide. So far so good, but there seems to be a problem with the defineMetaHandler, this is my current code:

const { addonBuilder } = require("stremio-addon-sdk")

// Docs: https://github.com/Stremio/stremio-addon-sdk/blob/master/docs/api/responses/manifest.md
const manifest = {
    "id": "community.helloworld",
    "version": "0.0.1",
    "catalogs": [
        {
            "type": "movie",
            "id": "top"
        }
    ],
    "resources": [
        "catalog",
        {
            "name": "meta",
            "types": ["movie"],
            "idPrefixes": ["hiwrld_"]
        },
        "stream"
    ],
    "types": [
        "movie",
        "series"
    ],
    "name": "he",
    "description": "f"
}

// Populate the catalog from somewhere
function getMoviesCatalog(catalogName) {
    let catalog;

    switch(catalogName) {
        case "top":
            catalog = [
                {
                    id: "tt1254207",
                    type: "movie",
                    name: "The Big Buck Bunny",
                    poster: "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Big_buck_bunny_poster_big.jpg/220px-Big_buck_bunny_poster_big.jpg",
                    genres: [ "Animation", "Short", "Comedy" ]
                },
                {
                    id: "hiwrld_jellyfish",
                    type: "movie",
                    name: "Jellyfish",
                    poster: "https://images.unsplash.com/photo-1496108493338-3b30de66f9be",
                    genres: ["Demo", "Nature"]
                }
            ]
            break
        default:
            catalog = []
            break
    }

    return Promise.resolve(catalog)
}

function getMovieMeta(id) {
    const metas = {
        hiwrld_jellyfish: {
            id: "hiwrld_jellyfish",
            type: "movie",
            name: "Jellyfish",
            poster: "https://images.unsplash.com/photo-1496108493338-3b30de66f9be",
            genres: ["Demo", "Nature"],
            description: "A .mkv video clip useful for testing the network streaming and playback performance of media streamers & HTPCs.",
            cast: ["Some random jellyfishes"],
            director: ["ScottAllyn"],
            logo: "https://b.kisscc0.com/20180705/yee/kisscc0-art-forms-in-nature-jellyfish-recapitulation-theor-jellyfish-5b3dcabcb00692.802484341530776252721.png",
            background: "https://images.unsplash.com/photo-1461783470466-185038239ee3",
            runtime: "30 sec"
        },
    }
    console.log("-----------------------------------")
    console.log(metas)
    console.log("-----------------------------------")
    Promise.resolve(metas[id] || null)
}

function getMovieStreams(id) {
    const streams = {
        tt1254207: [
            { "title": "HTTP location", "yt_ID": "aqz-KE-bpKQ"}
        ],
        hiwrld_jellyfish: [
            { "title": "Web, 3 MBps, HD", "url": "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4" },
            { "title": "Web 15 MBps, HD", "url": "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4" },
            { "title": "Web, 120 MBps, 4K", "url": "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4" }
        ]
    }
    return Promise.resolve(streams[id] || [])
}

// http://192.168.1.100:53558/manifest.json
const builder = new addonBuilder(manifest)

builder.defineStreamHandler(({type, id}) => {
    // Docs: https://github.com/Stremio/stremio-addon-sdk/blob/master/docs/api/requests/defineStreamHandler.md
    let results;

    switch(type) {
        case 'movie':
            results = getMovieStreams(id)
            break
       default:
            results = Promise.resolve( [] )
            break
    }
    return results.then(streams => ({streams}))
})

builder.defineMetaHandler(({type, id}) => {
    // Docs: https://github.com/Stremio/stremio-addon-sdk/blob/master/docs/api/requests/defineMetaHandler.md
    let results;
    console.log("-----------------------------------")
    console.log(id)
    console.log("-----------------------------------")
    switch(type) {
        case 'movie':
            results = getMovieMeta(id)
            break
       default:
            results = null
            break
    }
    return Promise.resolve({ meta: results })
})

// Docs: https://github.com/Stremio/stremio-addon-sdk/blob/master/docs/api/requests/defineCatalogHandler.md
builder.defineCatalogHandler(({type, id, extra}) => {
    let results;

    switch(type) {
        case "movie":
            results = getMoviesCatalog(id)
            break
       default:
            results = Promise.resolve( [] )
            break
    }
    if(extra.search) {
        return results.then(items => {
            metas: items.filter(meta => meta.name
            .toLowercase()
            .includes(extra.search.toLowercase()))
        })
    } else if(extra.genre) {
        return results.then(items => ({
            metas: items.filter(meta => meta.genres
            .includes(extra.genre))
        }))
    }

    const skip = extra.skip || 0;
    return results.then(items => ({
        metas: items.slice(skip, skip + 100)
    }))
 })

module.exports = builder.getInterface()

This is the relevant part of the guide: https://stremio.github.io/stremio-addon-guide/sdk-guide/step3

The problem is that when i click on the jellyfish catalog entry i get this: image This happens on both desktop app and web app. Is there something wrong with the guide? I can see that the console.logs are definitively printed and both defineMetaHandler and getMovieMetagetMovieMeta returns with no error but the meta is still empty.

TRtomasz commented 10 months ago

return Promise.resolve({ meta: results }) this will be { meta: [object Promise] }

lpuglia commented 10 months ago

{ meta: [object Promise] }

SyntaxError: Unexpected identifier 'Promise'
dexter21767-dev commented 10 months ago

in the getMovieMeta function you aren't returning anything. the last line of that function should be return Promise.resolve(metas[id] || null) instead of Promise.resolve(metas[id] || null)

lpuglia commented 10 months ago

That should be fixed in the Tutorial, thanks! will keep this open for now

TRtomasz commented 10 months ago

getMovieMeta is not part of sdk this is your internal function

lpuglia commented 10 months ago

getMovieMeta is not part of sdk this is your internal function

@TRtomasz sure, but it should be fixed here: https://stremio.github.io/stremio-addon-guide/sdk-guide/step3

core1024 commented 10 months ago

Fixed in the tutorial.

lpuglia commented 10 months ago

@core1024 @TRtomasz thanks for the help so far, but, there is still something wrong. Now the "No information about this" error is gone, but the page is still empty: image I can see the the meta promise is being returned correctly though:

{
  meta: Promise {
    {
      id: 'hiwrld_jellyfish',
      type: 'movie',
      name: 'Jellyfish',
      poster: 'https://images.unsplash.com/photo-1496108493338-3b30de66f9be',
      genres: [Array],
      description: 'A .mkv video clip useful for testing the network streaming and playback performance of media streamers & HTPCs.',
      cast: [Array],
      director: [Array],
      logo: 'https://b.kisscc0.com/20180705/yee/kisscc0-art-forms-in-nature-jellyfish-recapitulation-theor-jellyfish-5b3dcabcb00692.802484341530776252721.png',
      background: 'https://images.unsplash.com/photo-1461783470466-185038239ee3',
      runtime: '30 sec'
    }
  }
}

Is this the expected behaviour? should i not see the backgroud image at least?

lpuglia commented 9 months ago

@core1024 There is a second issue in the tutorial that doesn't allow to display the meta, can you please modify this page https://stremio.github.io/stremio-addon-guide/sdk-guide/step3 from this:

builder.defineMetaHandler(({type, id}) => {
    // Docs: https://github.com/Stremio/stremio-addon-sdk/blob/master/docs/api/requests/defineMetaHandler.md
    let results;

    switch(type) {
        case 'movie':
            results = getMovieMeta(id)
            break
       default:
            results = null
            break
    }
    return Promise.resolve({ meta: results })
})

to:

builder.defineMetaHandler(({type, id}) => {
    // Docs: https://github.com/Stremio/stremio-addon-sdk/blob/master/docs/api/requests/defineMetaHandler.md
    let results;

    switch(type) {
        case 'movie':
            results = getMovieMeta(id)
            break
       default:
            results = null
            break
    }
    return results.then(meta => ({meta}))
})
dexter21767-dev commented 9 months ago

@lpuglia that will only work if you have meta; otherwise u won't be able to read .'then' from a null variable.