Skip to content

Commit

Permalink
fix failed to retrieve room_id
Browse files Browse the repository at this point in the history
  • Loading branch information
zerodytrash committed Aug 19, 2023
1 parent c45dc5c commit 96a4d4f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,6 @@ dist
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.pnp.*

.tests/*
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiktok-live-connector",
"version": "1.0.3",
"version": "1.0.4",
"description": "Node.js module to receive live stream chat events like comments and gifts from TikTok LIVE",
"main": "index.js",
"types": "./dist/index.d.ts",
Expand Down
19 changes: 16 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,23 @@ class WebcastPushConnection extends EventEmitter {
async #retrieveRoomId() {
try {
let mainPageHtml = await this.#httpClient.getMainPage(`@${this.#uniqueStreamerId}/live`);
let roomId = getRoomIdFromMainPageHtml(mainPageHtml);

this.#roomId = roomId;
this.#clientParams.room_id = roomId;
try {
let roomId = getRoomIdFromMainPageHtml(mainPageHtml);

this.#roomId = roomId;
this.#clientParams.room_id = roomId;
} catch (err) {
// Use fallback method
let roomData = await this.#httpClient.getJsonObjectFromTiktokApi('api-live/user/room/', {
...this.#clientParams,
uniqueId: this.#uniqueStreamerId,
sourceType: 54,
});

this.#roomId = roomData.data.user.roomId;
this.#clientParams.room_id = roomData.data.user.roomId;
}
} catch (err) {
throw new Error(`Failed to retrieve room_id from page source. ${err.message}`);
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/tiktokHttpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class TikTokHttpClient {
let response = await this.#post(`${Config.TIKTOK_URL_WEBCAST}${path}`, params, formData, 'json');
return response.data;
}

async getJsonObjectFromTiktokApi(path, params, shouldSign) {
let url = await this.#buildUrl(Config.TIKTOK_URL_WEB, path, params, shouldSign);
let response = await this.#get(url, 'json');
return response.data;
}
}

module.exports = TikTokHttpClient;

0 comments on commit 96a4d4f

Please sign in to comment.