Skip to content

How to use NowViewingItem in SessionInfoDto interface #1001

Description

@MathiasPetey

Hi,

I was wondering how to use the NowViewingItem field in the SessionInfoDto interface ?

Basically, I want to access the information of the item being viewed by the Jellyfin user before starting an episode (e.g. I go to the Non Non Biyori page on Jellyfin to display information about the show, I want to find in real time the id of Non Non Biyori (6f5590620a1527ef4f16c611fd264f4d) using the typescript jellyfin sdk).

I can access all the information of the show while playing its episodes with NowPlayingItem, but I don't find how to extract this information from the show presentation page.

I thought it could have been with NowViewingItem but its content is always empty unless I manually report viewing a given item with the reportViewing method.

I don't have much knowledge in typescript (first time developping in ts/js actually), I hope my questions are understandable enough.

Bellow is the code I currently use.

Thanks

import { Jellyfin } from "@jellyfin/sdk/lib/jellyfin.js";
import { getSessionApi, getItemsApi, getUserApi, getSystemApi } from "@jellyfin/sdk/lib/utils/api/index.js";

const JELLYFIN_URL = "JELLYFIN_URL";
const USERNAME = "USERNAME";
const PASSWORD = "PASSWORD";

async function main() {
  // Créer le client Jellyfin
  const jellyfin = new Jellyfin({
    clientInfo: { name: "session_info_extractor", version: "1.0.0" },
    deviceInfo: { name: "PC", id: "session_info_extractor" },
  });

  // Créer une API sur le serveur
  const api = jellyfin.createApi(JELLYFIN_URL);

  // Authentification
  const auth = await getUserApi(api).authenticateUserByName({
    authenticateUserByName: { Username: USERNAME, Pw: PASSWORD },
  });

  if (!auth.data.AccessToken) throw new Error("Login failed");
  // Mettre à jour l'API avec le token
  api.accessToken = auth.data.AccessToken;

  console.log("Connecté à Jellyfin");
  
  let lastState = new Map<string, any>();
  async function checkSessions() {
    const sessions = await getSessionApi(api).getSessions();

    for (const s of sessions.data) {
      if (!s.Id) continue;
      const id = s.Id;
      const state = {
        series: s.NowPlayingItem?.SeriesName,
        episode: s.NowPlayingItem?.Name,
        paused: s.PlayState?.IsPaused,
        genres: s.NowViewingItem?.Genres,
        now_viewing_item: s.NowViewingItem?.Name, // always empty
      };

      const previous = lastState.get(id);

      if (JSON.stringify(previous) !== JSON.stringify(state)) {
        console.log("---- changement détecté ----");
        console.log("Utilisateur :", s.UserName ?? "N/A");
        console.log("Série :", state.series ?? "N/A");
        console.log("Episode :", state.episode ?? "N/A");
        console.log("Pause :", state.paused ?? "N/A");
        console.log("now_viewing_item :", state.now_viewing_item ?? "N/A");
        lastState.set(id, state);
      }
    }
  }

  setInterval(checkSessions, 500); 

}

main().catch(console.error);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions