-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiktok.js
42 lines (34 loc) · 1.23 KB
/
tiktok.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require('console-stamp')(console, {
format: ':date(dd.mm.yy HH:MM:ss) :label'
} );
module.exports = {
setupTiktok
};
async function setupTiktok(client) {
discordClient = client;
/*twitchAccessToken = await getTwitchToken(process.env.TWITCH_CLIENT_ID, process.env.TWITCH_CLIENT_SECRET);
setInterval(checkTwitchStatus, process.env.TWITCH_CHECK_DELAY_MS);
checkTwitchStatus();*/
console.log('Tiktok finished setting up');
}
const axios = require('axios');
const cheerio = require('cheerio');
async function getLatestTikTokVideo(user) {
const url = `https://www.tiktok.com/@${user}`;
const response = await axios.get(url);
const $ = cheerio.load(response.data);
console.log($)
const script = $('#__NEXT_DATA__');
if (script.length > 0) {
const data = JSON.parse(script[0].children[0].data);
const videos = data.props.pageProps.items;
if (videos && videos.length > 0) {
const latestVideo = videos[0];
const videoUrl = `https://www.tiktok.com/@${user}/video/${latestVideo.id}`;
return videoUrl;
}
}
return null;
}
// Replace 'username' with the TikTok username
getLatestTikTokVideo('grangerval').then(console.log).catch(console.error);