Spaces:
Paused
Paused
| import express from "express"; | |
| import axios from "axios"; | |
| import * as cheerio from "cheerio"; | |
| import cors from "cors"; | |
| const app = express(); | |
| app.use(cors()); | |
| app.get("/", (req, res) => { | |
| res.json({ | |
| status: "TikTok Standalone API by Muhammad Farel", | |
| usage: "/api?url=LINK_TIKTOK" | |
| }); | |
| }); | |
| app.get("/api", async (req, res) => { | |
| const url = req.query.url; | |
| if (!url) return res.json({ error: "Masukkan parameter ?url=" }); | |
| try { | |
| const html = await axios.get(url, { | |
| headers: { "User-Agent": "Mozilla/5.0" } | |
| }); | |
| const $ = cheerio.load(html.data); | |
| const raw = $("#SIGI_STATE").html(); | |
| if (!raw) return res.json({ error: "Gagal membaca SIGI_STATE" }); | |
| const data = JSON.parse(raw); | |
| const videoId = Object.keys(data.ItemModule)[0]; | |
| const item = data.ItemModule[videoId]; | |
| if (!item) return res.json({ error: "Video tidak ditemukan" }); | |
| const withWM = item.video.downloadAddr; | |
| const noWM = item.video.downloadAddr.replace("watermark=1", "watermark=0"); | |
| const music = item.music.playUrl; | |
| res.json({ | |
| status: "success", | |
| author: item.author, | |
| username: item.author, | |
| title: item.desc, | |
| cover: item.cover, | |
| duration: item.video.duration, | |
| resolution: `${item.video.height}x${item.video.width}`, | |
| video: { | |
| no_watermark: noWM, | |
| watermark: withWM | |
| }, | |
| audio: music, | |
| raw: item | |
| }); | |
| } catch (err) { | |
| res.json({ | |
| error: "Terjadi kesalahan saat scrape", | |
| detail: err.message | |
| }); | |
| } | |
| }); | |
| const PORT = 7860; | |
| app.listen(PORT, () => console.log("API berjalan di port", PORT)); |