Zavier Optima
Back to Dashboard

Tiktok Downloader

Open Source

Script Tiktok Donwloader

JAVASCRIPT & ESM
import axios from 'axios'

const handler = async (m, { text, usedPrefix, command, conn }) => {
  const tiktokRegex = /(https?:\/\/(www\.)?(tiktok\.com|vm\.tiktok\.com|vt\.tiktok\.com)\/[^\s]+)/
  const match = text.match(tiktokRegex)
  if (!match) return m.reply(`Contoh penggunaan: ${usedPrefix + command} https://vt.tiktok.com/example`)
  const url = match[0]
  try {
    m.reply(wait)
    const res = await tiktok(url)
   
    const { data, stats, music_info, author, title, id } = res
    let no = 1

    if (data.some(d => d.type === "photo")) {
      conn.reply(m.chat, '*_Foto telah terkirim di private chat!_*', m)
      for (const img of data.filter(d => d.type === "photo")) {
        await conn.sendFile(m.sender, img.url, '', `*乂 TikTok Slide - Downloader*\n\nImage Slide *(${no++}/${data.length})*\n\n*─ Author:* ${author.nickname}`, m)
        await conn.delay(500)
      }
    } else {
      const video = data.find(d => d.type === "nowatermark") || data.find(d => d.type === "nowatermark_hd")
      if (video) {
        const reyz = await conn.sendFile(m.chat, video.url, '', `*乂 TikTok Video - Downloader*\n\n*• Author :* ${author.nickname}\n*• Title :* ${title}\n*• ID :* [ ${id} ]\n*• Views :* ${stats.views}\n*• Like :* ${stats.likes}\n*• Comment :* ${stats.comment}\n*• Share :* ${stats.share}\n*• Profile :* https://tiktok.com/@${author.fullname}`, m)
        await conn.sendMessage(m.chat, {
          audio: {
            url: music_info.url
          },
          mimetype: 'audio/mpeg',
          fileName: `${music_info.url}.mp3`,
        }, { quoted: reyz })
      } else {
        throw new Error('Video tidak ditemukan')
      }
    }
  } catch (e) {
    console.error(e)
    m.reply('Terjadi kesalahan: ' + e.message)
  }
}
handler.help = ['tiktok']
handler.tags = ['downloader']
handler.command = /^(tt|tiktok|ttdl)$/i
handler.limit = true

export default handler

async function tiktok(url) {
    return new Promise(async (resolve) => {
        try {
            function formatNumber(integer) {
                let numb = parseInt(integer)
                return Number(numb).toLocaleString().replace(/,/g, ".")
            }

            function formatDate(n, locale = "id") {
                let d = new Date(n * 1000)
                return d.toLocaleDateString(locale, {
                    weekday: "long",
                    day: "numeric",
                    month: "long",
                    year: "numeric",
                    hour: "numeric",
                    minute: "numeric",
                    second: "numeric"
                })
            }

            let domain = "https://www.tikwm.com/api/"
            let res = await (
                await axios.post(
                    domain, {}, {
                        headers: {
                            Accept: "application/json, text/javascript, */*; q=0.01",
                            "Accept-Language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7",
                            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                            Origin: "https://www.tikwm.com",
                            Referer: "https://www.tikwm.com/",
                            "Sec-Ch-Ua": '"Not)A;Brand";v="24", "Chromium";v="116"',
                            "Sec-Ch-Ua-Mobile": "?1",
                            "Sec-Ch-Ua-Platform": "Android",
                            "Sec-Fetch-Dest": "empty",
                            "Sec-Fetch-Mode": "cors",
                            "Sec-Fetch-Site": "same-origin",
                            "User-Agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36",
                            "X-Requested-With": "XMLHttpRequest"
                        },
                        params: {
                            url: url,
                            count: 12,
                            cursor: 0,
                            web: 1,
                            hd: 1
                        }
                    }
                )
            ).data.data

            if (!res.play)
                return resolve({ status: false })

            let data = []
            if (!res.size) {
                res.images.map((v) => {
                    data.push({ type: "photo", url: v })
                })
            } else {
                data.push(
                    { type: "watermark", url: "https://www.tikwm.com" + res.wmplay },
                    { type: "nowatermark", url: "https://www.tikwm.com" + res.play },
                    { type: "nowatermark_hd", url: "https://www.tikwm.com" + res.hdplay }
                )
            }

            let json = {
                status: true,
                title: res.title,
                taken_at: formatDate(res.create_time),
                region: res.region,
                id: res.id,
                duration: res.duration + " detik",
                cover: "https://www.tikwm.com" + res.cover,
                data: data,
                music_info: {
                    id: res.music_info.id,
                    title: res.music_info.title,
                    author: res.music_info.author,
                    album: res.music_info.album ? res.music_info.album : "Tidak diketahui",
                    url: "https://www.tikwm.com" + res.music || res.music_info.play
                },
                stats: {
                    views: formatNumber(res.play_count),
                    likes: formatNumber(res.digg_count),
                    comment: formatNumber(res.comment_count),
                    share: formatNumber(res.share_count),
                    download: formatNumber(res.download_count)
                },
                author: {
                    id: res.author.id,
                    fullname: res.author.unique_id,
                    nickname: res.author.nickname,
                    avatar: "https://www.tikwm.com" + res.author.avatar
                }
            }
            return resolve(json)
        } catch (e) {
            console.log(e)
            return resolve({ status: false, msg: e.message })
        }
    })
}

© 2024 ZavierOptima Archive.