RSSHub/lib/routes/twitter/api/mobile-api/token.ts

43 lines
1.2 KiB
TypeScript

import { config } from '@/config';
import login from './login';
import ConfigNotFoundError from '@/errors/types/config-not-found';
let tokenIndex = 0;
let authentication = null;
let first = true;
async function initToken() {
if (config.twitter.username && config.twitter.password && !authentication && first) {
authentication = await login();
first = false;
}
}
function getToken() {
let token;
if (config.twitter.username && config.twitter.password) {
if (authentication) {
token = {
key: authentication.oauth_token,
secret: authentication.oauth_token_secret,
};
}
} else if (config.twitter.oauthTokens?.length && config.twitter.oauthTokenSecrets.length && config.twitter.oauthTokens.length === config.twitter.oauthTokenSecrets.length) {
token = {
key: config.twitter.oauthTokens[tokenIndex],
secret: config.twitter.oauthTokenSecrets[tokenIndex],
};
tokenIndex++;
if (tokenIndex >= config.twitter.oauthTokens.length) {
tokenIndex = 0;
}
} else {
throw new ConfigNotFoundError('Invalid twitter configs');
}
return token;
}
export { initToken, getToken };