Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
feat: add method for checking if the current user can start live stre…
Browse files Browse the repository at this point in the history
…ams (#54)
  • Loading branch information
szdc committed Nov 25, 2018
1 parent 543a1bb commit cf110e6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const api = new TikTokAPI(params, { signURL });
* [.listFollowingFeed([params])](#listfollowingfeedparams)
* [.joinLiveStream(id)](#joinlivestreamid)
* [.leaveLiveStream(id)](#leavelivestreamid)
* [.canStartLiveStream()](#canstartlivestream)

#### .loginWithEmail(email, password)

Expand Down Expand Up @@ -484,6 +485,22 @@ api.leaveLiveStream('<room_id>')

See the [live stream types](src/types/live-stream.d.ts) for the complete request/response objects.

#### .canStartLiveStream()

Determines if the current user is allowed to start a live stream.

```javascript
api.canStartLiveStream()
.then(res => console.log(res.data.can_be_live_podcast))
.catch(console.log);

// Outputs:
// true

```

See the [live stream types](src/types/live-stream.d.ts) for the complete request/response objects.

## Resources

* [Reverse engineering the musical.ly API](https://medium.com/@szdc/reverse-engineering-the-musical-ly-api-662331008eb3)
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@ export default class TikTokAPI {
},
})

/**
* Determines if you are allowed to start a live stream. Typically you need 1,000 followers.
*/
canStartLiveStream = () =>
this.request.get<API.CanStartLiveStreamResponse | API.BaseResponseData>('aweme/v1/live/podcast/')

/**
* Transform using JSONBig to store big numbers accurately (e.g. user IDs) as strings.
*
Expand Down
5 changes: 5 additions & 0 deletions src/types/live-stream.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export interface JoinLiveStreamResponse extends BaseResponseData {
room: LiveStream;
}

export interface CanStartLiveStreamResponse extends BaseResponseData {
/** True if your account can start a live stream */
can_be_live_podcast: boolean;
}

export interface LiveStream {
/** The timestamp in seconds when the stream was created */
create_time: number;
Expand Down
27 changes: 26 additions & 1 deletion test/live-stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import MockAdapter from 'axios-mock-adapter';
import { assert } from 'chai';
import { describe, it } from 'mocha';

import TikTokAPI, { BaseResponseData, CommonUserDetails, JoinLiveStreamResponse } from '../src';
import TikTokAPI, {
BaseResponseData,
CanStartLiveStreamResponse,
CommonUserDetails,
JoinLiveStreamResponse,
} from '../src';
import {
loadTestData,
mockConfig,
Expand Down Expand Up @@ -63,3 +68,23 @@ describe('#leaveLiveStream()', () => {
assert.deepStrictEqual(res.data, expected);
});
});

describe('#canStartLiveStream()', () => {
it('a successful response should match the interface', async () => {
const api = new TikTokAPI(mockParams, mockConfig);
const mock = new MockAdapter(api.request);
mock
.onGet(new RegExp('aweme/v1/live/podcast/\?.*'))
.reply(200, loadTestData('canStartLiveStream.json'), {});

const res = await api.canStartLiveStream();
const expected: CanStartLiveStreamResponse = {
extra: {
now: 1000000000000,
},
can_be_live_podcast: true,
status_code: 0,
};
assert.deepStrictEqual(res.data, expected);
});
});
7 changes: 7 additions & 0 deletions test/testdata/canStartLiveStream.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extra": {
"now": 1000000000000
},
"can_be_live_podcast": true,
"status_code": 0
}

0 comments on commit cf110e6

Please sign in to comment.