| const express = require('express'); |
| const cors = require('cors'); |
| const mongoose = require('mongoose'); |
| const session = require('express-session'); |
| const MongoStore = require('connect-mongo'); |
| const dotenv = require('dotenv'); |
| const LoveTik = require("./scraper"); |
| const path = require("path"); |
| const lovetik = new LoveTik(); |
| const bodyParser = require("body-parser"); |
|
|
|
|
| |
| dotenv.config(); |
|
|
| const app = express(); |
| const PORT = 7860; |
|
|
| |
| app.use(cors({ |
| origin: true, |
| credentials: true |
| })); |
| app.use(express.json()); |
| app.use(express.urlencoded({ extended: true })); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| function getHDVideo(media = {}) { |
| |
| if (Array.isArray(media.images) && media.images.length) { |
| return { |
| type: "image", |
| url: media.images.map(img => img.url || img) |
| }; |
| } |
|
|
| |
| if (Array.isArray(media.videos) && media.videos.length) { |
| const priority = ["HD Original", "720p", "576p"]; |
|
|
| for (const quality of priority) { |
| const video = media.videos.find(v => |
| typeof v.size === "string" && |
| v.size.toLowerCase().includes(quality.toLowerCase()) |
| ); |
| if (video) { |
| return { |
| type: "video", |
| quality: video.size, |
| url: video.url |
| }; |
| } |
| } |
|
|
| |
| const v = media.videos[0]; |
| return { |
| type: "video", |
| quality: v.size || "unknown", |
| url: v.url |
| }; |
| } |
|
|
| |
| return null; |
| } |
|
|
| app.use(bodyParser.json()); |
| app.use(express.static(path.join(__dirname, "public"))); |
|
|
|
|
| |
| app.get("/", (req, res) => { |
| res.sendFile(path.join(__dirname, "public/index.html")); |
| }); |
|
|
| app.post("/api/tiktok", async (req, res) => { |
| try { |
| const { url } = req.body; |
|
|
| if (!url) { |
| return res.status(400).json({ |
| status: false, |
| message: "Parameter url wajib diisi" |
| }); |
| } |
|
|
| const hehe = await lovetik.download(url); |
|
|
| const result = getHDVideo(hehe.media); |
| |
| |
| res.json({ |
| status: true, |
| result |
| }); |
| } catch (err) { |
| res.status(500).json({ |
| status: false, |
| message: err.message |
| }); |
| } |
| }); |
|
|
|
|
|
|
| app.listen(PORT, '0.0.0.0', () => { |
| console.log(`Server running on port ${PORT}`); |
| }); |
|
|