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: ytm login #674

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 8 additions & 9 deletions src/components/login/google/YTM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import { Button, Avatar, Text, ActivityIndicator } from 'react-native-paper';
import { WebView } from 'react-native-webview';
import { useFocusEffect } from '@react-navigation/native';
import CookieManager from '@react-native-cookies/cookies';
import { get_option, get_current_user } from 'libmuse';
import { get_current_user } from 'libmuse';
import { useTranslation } from 'react-i18next';

import { saveSecure as saveItem } from '@utils/ChromeStorageAPI';
import { StorageKeys } from '@enums/Storage';
import { User, UseYTMLogin } from './useYTMLogin';
import { museStore } from '@utils/muse';
import useCollapsible from '../useCollapsible';
import { initMuse } from '@utils/muse';

const jsCode = 'window.ReactNativeWebView.postMessage(document.cookie)';
const auth = get_option('auth');

const clearCookies = () => {
museStore.reset();
saveItem(StorageKeys.YTMCOOKIES, null);
};

const checkYTM = async () => console.log(await get_current_user());
const checkYTM = async () => {
await initMuse();
get_current_user().then(console.log).catch(console.log);
};

const Login = () => {
const { t } = useTranslation();
Expand All @@ -35,7 +36,7 @@ const Login = () => {

const onMessage = (event: any) => {
const { data } = event.nativeEvent;
setCookies(data?.split(';'));
setCookies(data?.split('; '));
};

useFocusEffect(
Expand Down Expand Up @@ -125,9 +126,7 @@ const Explore = ({ ytmLogin }: Props) => {
<LoggedInPage
user={user}
logout={() => {
saveItem(StorageKeys.YTMCOOKIES, null);
museStore.set('token', null);
auth.token = null;
saveItem(StorageKeys.YTMCOOKIES, null).then(initMuse);
clear();
}}
/>
Expand Down
4 changes: 2 additions & 2 deletions src/stores/initializeStores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { initialize as initializeAppStore } from './appStore';
import { initialize as initializeRegexStore } from './regexStore';
import { initializeR128Gain } from '../utils/ffmpeg/r128Store';
import { dataSaverPlaylist, initCache } from '../utils/Cache';
import { initialize as initializeMuse } from '@utils/muse';
import { initMuse } from '../utils/muse';
import { useAPM } from './usePersistStore';

const { NoxModule } = NativeModules;
Expand Down Expand Up @@ -42,7 +42,7 @@ export const initializeStores = async ({
default:
break;
}
await initializeMuse();
await initMuse();
await initializeAppStore();
await initializeRegexStore();
await initializeR128Gain();
Expand Down
59 changes: 29 additions & 30 deletions src/utils/muse.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import { setup, MemoryStore } from 'libmuse';
import * as crypto from 'expo-crypto';
import { get_option } from 'libmuse';

import {
getSecure as getItem,
saveSecure as setItem,
} from '@utils/ChromeStorageAPI';
import { getSecure as getItem } from '@utils/ChromeStorageAPI';
import { StorageKeys } from '@enums/Storage';

const MUSE_KEY = 'museStore';
class MyStore extends MemoryStore {
set(key: string, value: unknown) {
super.set(key, value);
setItem(MUSE_KEY, JSON.stringify(Object.fromEntries(this.map)));
}
async init() {
const v = await getItem(MUSE_KEY);
try {
this.map = new Map(Object.entries(JSON.parse(v ?? '{}')));
} catch {
this.map = new Map();
}
}

reset() {
this.map = new Map();
setItem(MUSE_KEY, null);
}
}

export const museStore = new MyStore();
const findCookie = (cookies: string, match: string) => {
const cookieArr = cookies.split('; ').map(v => v.split('='));
return cookieArr.find(cookie => cookie[0] === match)?.[1];
};

export const initialize = async () => {
await museStore.init();
setup({ store: museStore });
export const initMuse = async (
cookies: Promise<string> = getItem(StorageKeys.YTMCOOKIES),
) => {
const sapisid = findCookie(await cookies, 'SAPISID') ?? '';
// https://github.com/sigma67/ytmusicapi/blob/9ce284a7eae9c4cdc04bb098f7549cc5f1c80e22/ytmusicapi/helpers.py#L52
const get_headers = async () => {
const now = new Date();
const timems = now.getTime() + now.getTimezoneOffset() * 60 * 1000;
const timesec = Math.round(timems / 1000);
const SAPISIDHASH = await crypto.digestStringAsync(
crypto.CryptoDigestAlgorithm.SHA1,
`${timesec} ${sapisid} https://music.youtube.com`,
);
return {
Authorization: `SAPISIDHASH ${timesec}_${SAPISIDHASH}`,
Cookie: await cookies,
};
};
const auth = get_option('auth');
auth.get_headers = get_headers;
auth.requires_login = async () => false;
};
Loading