Skip to content

Commit

Permalink
Merge pull request #31 from slkzgm/spaces
Browse files Browse the repository at this point in the history
feat: add Spaces support (creation, Janus integration, STT/TTS pipeline)
  • Loading branch information
Freytes authored Dec 28, 2024
2 parents 7b9f1fa + 83c8cee commit b9d6711
Show file tree
Hide file tree
Showing 15 changed files with 2,999 additions and 4 deletions.
143 changes: 140 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"test": "jest"
},
"dependencies": {
"@roamhq/wrtc": "^0.8.0",
"@sinclair/typebox": "^0.32.20",
"headers-polyfill": "^3.1.2",
"json-stable-stringify": "^1.0.2",
Expand All @@ -35,7 +36,8 @@
"tough-cookie": "^4.1.2",
"tslib": "^2.5.2",
"twitter-api-v2": "^1.18.2",
"undici": "^7.1.1"
"undici": "^7.1.1",
"ws": "^8.18.0"
},
"devDependencies": {
"@commitlint/cli": "^17.6.3",
Expand All @@ -46,6 +48,7 @@
"@types/node": "^22.9.1",
"@types/set-cookie-parser": "^2.4.2",
"@types/tough-cookie": "^4.0.2",
"@types/ws": "^8.5.13",
"@typescript-eslint/eslint-plugin": "^5.59.7",
"@typescript-eslint/parser": "^5.59.7",
"dotenv": "^16.4.5",
Expand Down
100 changes: 100 additions & 0 deletions src/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ import {
sendDirectMessage,
SendDirectMessageResponse,
} from './messages';
import {
fetchAudioSpaceById,
fetchAuthenticatePeriscope,
fetchBrowseSpaceTopics,
fetchCommunitySelectQuery, fetchLiveVideoStreamStatus, fetchLoginTwitterToken
} from './spaces';
import {AudioSpace, Community, LiveVideoStreamStatus, LoginTwitterTokenResponse, Subtopic} from './types/spaces';

const twUrl = 'https://twitter.com';
const UserTweetsUrl =
Expand Down Expand Up @@ -899,4 +906,97 @@ export class Scraper {

return res.value;
}

/**
* Retrieves the details of an Audio Space by its ID.
* @param id The ID of the Audio Space.
* @returns The details of the Audio Space.
*/
public async getAudioSpaceById(id: string): Promise<AudioSpace> {
const variables = {
id,
isMetatagsQuery: false,
withReplays: true,
withListeners: true,
};

return await fetchAudioSpaceById(variables, this.auth);
}

/**
* Retrieves available space topics.
* @returns An array of space topics.
*/
public async browseSpaceTopics(): Promise<Subtopic[]> {
return await fetchBrowseSpaceTopics(this.auth);
}

/**
* Retrieves available communities.
* @returns An array of communities.
*/
public async communitySelectQuery(): Promise<Community[]> {
return await fetchCommunitySelectQuery(this.auth);
}

/**
* Retrieves the status of an Audio Space stream by its media key.
* @param mediaKey The media key of the Audio Space.
* @returns The status of the Audio Space stream.
*/
public async getAudioSpaceStreamStatus(
mediaKey: string,
): Promise<LiveVideoStreamStatus> {
return await fetchLiveVideoStreamStatus(mediaKey, this.auth);
}

/**
* Retrieves the status of an Audio Space by its ID.
* This method internally fetches the Audio Space to obtain the media key,
* then retrieves the stream status using the media key.
* @param audioSpaceId The ID of the Audio Space.
* @returns The status of the Audio Space stream.
*/
public async getAudioSpaceStatus(
audioSpaceId: string,
): Promise<LiveVideoStreamStatus> {
const audioSpace = await this.getAudioSpaceById(audioSpaceId);

const mediaKey = audioSpace.metadata.media_key;
if (!mediaKey) {
throw new Error('Media Key not found in Audio Space metadata.');
}

return await this.getAudioSpaceStreamStatus(mediaKey);
}

/**
* Authenticates Periscope to obtain a token.
* @returns The Periscope authentication token.
*/
public async authenticatePeriscope(): Promise<string> {
return await fetchAuthenticatePeriscope(this.auth);
}

/**
* Logs in to Twitter via Proxsee using the Periscope JWT.
* @param jwt The JWT obtained from AuthenticatePeriscope.
* @returns The response containing the cookie and user information.
*/
public async loginTwitterToken(
jwt: string,
): Promise<LoginTwitterTokenResponse> {
return await fetchLoginTwitterToken(jwt, this.auth);
}

/**
* Orchestrates the flow: get token -> login -> return Periscope cookie
*/
public async getPeriscopeCookie(): Promise<string> {
const periscopeToken = await this.authenticatePeriscope();

const loginResponse = await this.loginTwitterToken(periscopeToken);

return loginResponse.cookie;
}
}
Loading

0 comments on commit b9d6711

Please sign in to comment.