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

Fix Viaplay profile usage #369

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions src/services/viaplay/ViaplayApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ import { Shared } from '@common/Shared';
import { Utils } from '@common/Utils';
import { EpisodeItem, MovieItem, ScrobbleItem, ScrobbleItemValues } from '@models/Item';

export interface ViaplayGlobalObject {
storeState: {
profiles: {
activeProfile: string | null;
profileList: [
{
id: string;
name: string | null;
}
];
};
};
userData: {
firstName: string | null;
};
}

export interface ViaplayAuthResponse {
success: boolean;
userData?: {
Expand All @@ -15,6 +32,14 @@ export interface ViaplayAuthResponse {
};
}

interface ViaplayProfile {
id: string;
name: string;
isOwner: boolean;
}

export interface ViaplayProfilesArray extends Array<ViaplayProfile> {}

export interface ViaplayWatchedTopResponse {
_embedded: {
'viaplay:blocks': [ViaplayHistoryPage];
Expand Down Expand Up @@ -94,11 +119,13 @@ export interface ViaplayProductUserInfo {
}

class _ViaplayApi extends ServiceApi {
INITIAL_URL = 'https://viaplay.com/requirements';
INITIAL_URL = 'https://viaplay.com/';
lindvall marked this conversation as resolved.
Show resolved Hide resolved
HOST_URL = '';
API_BASE_URL = '';
HISTORY_API_URL = '';
AUTH_URL = '';
PROFILES_URL = '';
PROFILES_ID = '';
isActivated = false;

constructor() {
Expand Down Expand Up @@ -130,17 +157,40 @@ class _ViaplayApi extends ServiceApi {
}
this.HOST_URL = `https://content.${host}/`;
this.API_BASE_URL = `${this.HOST_URL}pcdash-${region}/`;
this.HISTORY_API_URL = `${this.API_BASE_URL}watched`;
this.AUTH_URL = `https://login.${host}/api/persistentLogin/v1?deviceKey=pcdash-${region}`;
this.nextHistoryUrl = this.HISTORY_API_URL;
this.PROFILES_URL = `https://${host}/profiles`;

const authResponseText = await Requests.send({
url: this.AUTH_URL,
method: 'GET',
});
const authResponse = JSON.parse(authResponseText) as ViaplayAuthResponse;

const responseProfilesText = await Requests.send({
url: this.PROFILES_URL,
method: 'GET',
});
const profileIdRegex = /"profiles":.*"activeProfile":"(?<activeProfileId>.*?)"/;
const { activeProfileId = '' } = profileIdRegex.exec(responseProfilesText)?.groups ?? {};
this.PROFILES_ID = activeProfileId;

this.HISTORY_API_URL = `${this.API_BASE_URL}watched?profileId=${this.PROFILES_ID}`;
this.nextHistoryUrl = this.HISTORY_API_URL;

const profilesRegex = /"profilesList":(?<profilesArray>\[.*?\])/;
const { profilesArray = '[]' } = profilesRegex.exec(responseProfilesText)?.groups ?? {};

const profiles = JSON.parse(profilesArray) as ViaplayProfilesArray;
const activeProfile = profiles.find(({ id }) => this.PROFILES_ID === id);

let profileName = '';
if (activeProfile) {
profileName = activeProfile.name;
}

this.session = {
profileName: authResponse.userData?.firstName || authResponse.userData?.username || null,
profileName:
profileName || authResponse.userData?.firstName || authResponse.userData?.username || null,
};

this.isActivated = true;
Expand Down Expand Up @@ -172,7 +222,7 @@ class _ViaplayApi extends ServiceApi {
}
const responseItems = historyPage._embedded['viaplay:products'];
const url = historyPage._links?.next?.href;
this.nextHistoryUrl = url;
this.nextHistoryUrl = url + '&profileId=' + this.PROFILES_ID;
this.hasReachedHistoryEnd = !url || responseItems.length === 0;
return responseItems;
}
Expand Down
Loading