-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathworker.js
95 lines (90 loc) · 2.99 KB
/
worker.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
import { shortcutURL, supportedVersions } from './modules/global-variables';
import tiktokAPIClient from './modules/tiktok-api-client';
import jsonBuilder from './modules/json-builder';
import landingPage from './modules/landing-page';
// Utilities methods, used for various things
const jsonResponseBuilder = (body, options = {}) => {
return new Response(JSON.stringify(body), {
...options,
headers: {
'Content-Type': 'application/json; charset=UTF-8'
}
});
};
// Check params
const objectValidator = (object) => {
// Check if installed version is a supported one
if (!object.version || !supportedVersions.includes(object.version)) {
object.message = `Your current version is outdated. Please download the latest one on ${shortcutURL}.`;
return object;
}
// Check for valid URL
if (!object.url) {
object.message = 'The URL field is required and cannot be left blank.';
return object;
}
// Check for valid tiktok ID
if (!(/^https:\/\/www\.tiktok\.com\/@[^/]*\/(video|photo)\/\d{8,}/.test(object.url))) {
object.message = `The provided URL (${object.url}) is not valid.`;
return object;
}
// Save the id on params object
[object.id] = object.url.match(/\d{8,}/);
return object;
};
const formToObject = async (request) => {
try {
const objectForm = Object.fromEntries(await request.formData());
return objectValidator(objectForm);
} catch (error) {
return { message: 'There was an error processing the form data.' };
}
};
const handlePostRequest = async (request, env) => {
if (env.MAINTENANCE === 'true') {
return jsonResponseBuilder(
{ error: 'DTikTok is under maintenance. Find more at @heismauri on Twitter/X.' },
{ status: 503 }
);
}
const params = await formToObject(request);
if (params.message) {
return jsonResponseBuilder({ error: params.message }, { status: 400 });
}
const tiktokJSON = await tiktokAPIClient(params.id, env);
// Check if the API gave any errors
if (tiktokJSON.error) {
switch (tiktokJSON.error.message) {
case 'NotFound':
return jsonResponseBuilder(
{
error: 'Sorry, the download might have failed as the video is flagged as sensitive content. Access to this '
+ 'content is restricted and may vary by country or due to other reasons.'
},
{ status: 404 }
);
default:
return jsonResponseBuilder(
{
error: "TikTok's API does not seem to be working right now. Please try again later. "
+ 'Contact @heismauri on Twitter/X if the problem persists.'
},
{ status: 503 }
);
}
}
// Success
return jsonResponseBuilder(jsonBuilder(tiktokJSON, params.selector), { status: 200 });
};
export default {
fetch(request, env) {
if (request.method === 'POST') {
return handlePostRequest(request, env);
}
return new Response(landingPage(), {
headers: {
'Content-Type': 'text/html; charset=UTF-8'
},
});
}
};