-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTiktokScript.js
More file actions
266 lines (232 loc) · 6.96 KB
/
TiktokScript.js
File metadata and controls
266 lines (232 loc) · 6.96 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const PLATFORM = "Tiktok";
const PYTHON_SERVER_ADDRESS = "http://192.168.1.108:3002";
const URL_SEARCH_CHANNEL = "/search/";
const URL_CHANNEL_INFO = "/user/info/";
const URL_CHANNEL_VIDEOS = "/user/videos";
const URL_VIDEO_COMMENTS = "/video/comments";
const URL_VIDEO_INFO = "/video/info";
const URL_VIDEO_STREAM = "/video/stream";
const REGEX_VIDEO_CHANNEL_URL = /^(https:\/\/)?(www\.|m\.)?tiktok\.com\/@([\w.-]+)(\?.*)?$/
const REGEX_EMBED_VIDEO_URL = /^(https:\/\/)?(www\.|m\.)?tiktok\.com\/@([\w.-]+)\/video\/\d+(\?.*)?$/
const REGEX_VIDEO_ID_URL = /@[\w.-]+\/video\/(\d+)/
var config = {};
let local_settings;
//Source Methods
source.enable = function(conf, settings, savedState){
config = conf ?? {};
local_settings = settings;
log(settings);
log(config);
}
source.getHome = function() {
return new ContentPager([], false);
};
source.searchSuggestions = function(query) {
return [];
};
source.getSearchCapabilities = () => {
return {
types: [Type.Feed.Mixed],
sorts: [Type.Order.Chronological],
filters: [ ]
};
};
source.search = function (query, type, order, filters) {
return new ContentPager([]. false);
};
source.getSearchChannelContentsCapabilities = function () {
return {
types: [Type.Feed.Mixed],
sorts: [Type.Order.Chronological],
filters: []
};
};
source.searchChannelContents = function (channelUrl, query, type, order, filters) {
throw new ScriptException("This is a sample");
};
source.searchChannels = function (query) {
const results = []
const res = http.GET(PYTHON_SERVER_ADDRESS+URL_SEARCH_CHANNEL+query, {});
if (!res.isOk) {
return [];
}
const data = JSON.parse(res.body);
console.log(data);
data.forEach(c => {
results.push(new PlatformAuthorLink(
new PlatformID(PLATFORM, c.uid, config.id),
c.unique_id,
"https://www.tiktok.com/@"+c.unique_id,
c.avatar_thumb.url_list[0],
c.follower_count
))
});
return new TiktokChannelPager(results)
};
//Channel
source.isChannelUrl = function(url) {
return REGEX_VIDEO_CHANNEL_URL.test(url);
};
source.getChannel = function(url) {
const match = url.match(REGEX_VIDEO_CHANNEL_URL);
if (!(match && match[3])) {
throw new ScriptException("Channel regex doesn't match")
}
const username = match[3];
const res = http.GET(PYTHON_SERVER_ADDRESS+URL_CHANNEL_INFO+username, {});
if(!res.isOk){
throw new ScriptException(`Failed to get channel (${res.status}).`);
}
const data = JSON.parse(res.body);
const c = data.userInfo;
return new PlatformChannel({
id: new PlatformID(PLATFORM, c.user.id, config.id),
name: c.user.uniqueId,
thumbnail: c.user.avatarThumb,
banner: "",
subscribers: c.stats.followerCount,
description: c.user.signature,
url: url,
links: {}
})
};
source.getChannelContents = function(url, type, order, filters) {
console.log("get contents: "+url);
const match = url.match(REGEX_VIDEO_CHANNEL_URL);
console.log(match);
if (!(match && match[3])) {
throw new ScriptException("Channel regex doesn't match");
}
const username = match[3];
const res = http.POST(PYTHON_SERVER_ADDRESS+URL_CHANNEL_VIDEOS+"?username="+username, '{}', {}, false);
if(!res.isOk){
throw new ScriptException(`Failed to get channel (${res.body}).`);
}
const data = JSON.parse(res.body);
const videos = [];
data.forEach(v=>{
console.log(v);
videos.push(new PlatformVideo({
id: new PlatformID(PLATFORM, v.id, config.id),
name: v.desc,
// thumbnails: new Thumbnails([new Thumbnail(v.video.cover, 0)]),
thumbnails: new Thumbnails([new Thumbnail(v.video.zoomCover["720"], 0)]),
author: new PlatformAuthorLink(
new PlatformID(PLATFORM, v.author.id, config.id),
v.author.uniqueId,
"https://www.tiktok.com/@"+v.author.uniqueId,
v.author.avatarThumb,
),
uploadDate: parseInt(v.createTime),
duration: v.video.duration,
viewCount: v.stats.playCount,
url: `https://www.tiktok.com/@${v.author.uniqueId}/video/${v.id}`,
isLive: false
}))
});
return new VideoPager(videos, false, {});
};
//Video
source.isContentDetailsUrl = function(url) {
return REGEX_EMBED_VIDEO_URL.test(url);
};
source.getContentDetails = function(url) {
const res = http.GET(`${PYTHON_SERVER_ADDRESS+URL_VIDEO_INFO}?url=${url}`, {});
if (!res.isOk) {
throw new ScriptException(`Failed to get video details (${res.body}).`);
}
const data = JSON.parse(res.body);
console.log(data);
const author = data.author;
const video = data.video;
const sources = [];
sources.push(new VideoUrlSource({
name: "Original 540P",
url: `${PYTHON_SERVER_ADDRESS+URL_VIDEO_STREAM}?url=${url}`,
container: "video/mp4",
width: 540,
duration: video.duration
}));
return new PlatformVideoDetails({
id: new PlatformID(PLATFORM, data.id, config.id),
name: data.desc,
thumbnails: new Thumbnails([new Thumbnail(video.zoomCover["720"])]),
author: new PlatformAuthorLink(
new PlatformID(PLATFORM, author.id, config.id),
author.uniqueId,
"https://www.tiktok.com/@"+author.uniqueId,
author.avatarThumb,
),
datetime: parseInt(data.createTime),
duration: data.duration,
viewCount: data.stats.playCount,
url: url,
isLive: false,
description: data.desc,
rating: new RatingLikes(data.stats.diggCount),
video: new VideoSourceDescriptor(sources),
live: null,
});
};
//Comments
source.getComments = function (url) {
const match = url.match(REGEX_VIDEO_ID_URL);
if (!(match && match[1])) {
throw new ScriptException(`Failed to get video id from url. ${url}`);
}
const video_id = match[1];
const res = http.POST(`${PYTHON_SERVER_ADDRESS+URL_VIDEO_COMMENTS}?video_id=${video_id}`, '{}', {}, false);
if(!res.isOk){
throw new ScriptException(`Failed to get video comments (${res.body}).`);
}
const data = JSON.parse(res.body);
const comments = [];
data.forEach(comment=>{
comments.push(new TiktokComment({
contextUrl: url,
author: new PlatformAuthorLink(
new PlatformID(PLATFORM, comment.author.id, config.id),
comment.author.username,
"https://www.tiktok.com/@"+comment.author.username,
comment.author.thumbnail
),
message: comment.text,
date: comment.create_time,
replyCount: comment.reply_total,
replies: [],
rating: new RatingLikes(comment.likes_count),
}))
})
return new TiktokCommentPager(comments);
}
source.getSubComments = function (comment) {
if(typeof comment === 'string'){
comment = JSON.parse(comment);
}
return new TiktokCommentPager([]);
}
class TiktokCommentPager extends CommentPager {
constructor(results){
super(results, false, {});
}
nextPage() {
return this;
}
}
class TiktokComment extends Comment {
constructor(obj) {
super(obj);
}
getReplies() {
return new TiktokCommentPager([], 20);
}
}
class TiktokChannelPager extends ChannelPager {
constructor(results) {
super(results, false);
}
nextPage() {
return this;
}
}
log("LOADED");