forked from LOX-X/Tiktok-Live-Asteroids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
159 lines (130 loc) · 4.16 KB
/
server.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const fs = require('fs')
const { networkInterfaces } = require('os');
const nets = networkInterfaces();
const results = Object.create(null);
const WebSocket = require('ws')
const server = new WebSocket.Server({ port: '8080' })
const express = require('express')
const bodyParser = require('body-parser');
const { WebcastPushConnection } = require('tiktok-live-connector');
const app = express()
let config = fs.readFileSync('config.json')
config = JSON.parse(config);
const events = ['connected', 'disconnected', 'gift', 'like','share','follow','chat']
const connections = []
let tiktokConnection = null
let tiktokUsername = null
const port = 3000
app.set('view engine', 'ejs')
app.use(express.static(__dirname + '/views'))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.get('/', (req, res) => {
res.render('index')
})
app.post('/start', (req, res) => {
tiktokUsername = req.body.username.toString()
start()
})
app.post('/status', (req, res) => {
let status = 'Disconnected'
try {
if (tiktokConnection != null){
if (tiktokConnection.getState().isConnecting){
status = 'Connecting (@' + tiktokUsername + ')'
}else if (tiktokConnection.getState().isConnected){
status = 'Connected (@' + tiktokUsername + ')'
}
}
} catch (error) {
}
connections.forEach(connection =>{
connection.send(JSON.stringify({
type: 'status',
data: status,
}))
})
})
app.listen(port)
server.on('connection', socket => {
connections.push(socket)
//socket.send(JSON.stringify(g))
socket.send(JSON.stringify(config))
})
async function start(){
if (tiktokConnection != null){
try {
await tiktokConnection.disconnect()
} catch (error) {
console.log(error);
}
}
tiktokConnection = new WebcastPushConnection(tiktokUsername, {
processInitialData: false,
enableRequestPolling: true
}
);
tiktokConnection.getAvailableGifts().then(giftList => {
if (giftList.length === 0) {
// If there are no gifts available, create an empty configuration
const config = {
gifts: []
};
const jsonData = JSON.stringify(config, null, 2);
fs.writeFile('config.json', jsonData, (err) => {
if (err) {
console.error(err);
return;
}
console.log('Empty configuration created successfully!');
});
} else {
const config = {
gifts: giftList.map(gift => ({
name: gift.name,
id: gift.id,
cost: gift.diamond_count
}))
};
const jsonData = JSON.stringify(config, null, 2);
fs.writeFile('config.json', jsonData, (err) => {
if (err) {
console.error(err);
return;
}
console.log('Data saved successfully!');
});
}
});
tiktokConnection.connect().catch(err => {
console.error('Failed to connect', err);
})
events.forEach(eventName => {
tiktokConnection.on(eventName, data => {
connections.forEach(connection => {
connection.send(JSON.stringify({
type: eventName,
data: data
}))
})
})
})
}
function getIP(){
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
if (net.family === familyV4Value && !net.internal) {
if (!results[name]) {
results[name] = [];
}
return net.address
}
}
}
}
console.log('')
console.log('Starting... Please wait')
console.log('')
console.log('Localhost URL (PC): http://localhost:' + port)
console.log('Remote URL (Mobile): http://' + getIP() + ':' + port)