Spaces:
Paused
Paused
| import axios from 'axios'; | |
| import cheerio from 'cheerio'; | |
| class Urlebird { | |
| #get = async (param) => { | |
| const html = (await axios.get(`https://urlebird.com/${encodeURI(param)}/`)).data; | |
| const $ = cheerio.load(html); | |
| return $('div.thumb').get().map(x => encodeURI($(x).find('a').eq(2).attr('href'))); | |
| }; | |
| detailVideo = async (url) => { | |
| const { data: html } = await axios.get(url); | |
| const $ = cheerio.load(html); | |
| const obj = { author: {}, video: {} }; | |
| // Extracting video details | |
| const videoElement = $('video'); | |
| const videoContainer = $('.container-fluid'); | |
| // Extracting author details | |
| const authorElement = videoContainer.find('.col-auto.text-left'); | |
| obj.author.username = authorElement.find('h2 a.user-video').text().match(/@(.*)/)?.[1]; | |
| obj.author.followers = authorElement.text().match(/(\d+(\.\d+)?[KkMm]? pengikut)/)?.[0]; | |
| obj.author.avatar = videoContainer.find('.col-auto.p-0.pl-2 img.user').attr('src'); | |
| // Extracting video stats | |
| const statsElements = videoContainer.find('.stats .col-md-3, .stats .col-6'); | |
| obj.video.play = statsElements.eq(0).text().trim(); | |
| obj.video.likes = statsElements.eq(1).text().trim(); | |
| obj.video.comments = statsElements.eq(2).text().trim(); | |
| obj.video.share = statsElements.eq(3).text().trim(); | |
| // Extracting video metadata | |
| obj.video.post = authorElement.find('h6').text().trim(); | |
| obj.video.music = $('.music a').text().trim(); | |
| obj.video.description = $('.info2 h1').text().trim(); | |
| obj.video.url = videoElement.attr('src'); | |
| obj.video.url2 = `https://tiktok.com/${obj.author.username}/video/${url.split('-').pop()}`; | |
| return obj; | |
| }; | |
| latest = async () => { | |
| const arr = []; | |
| for (const data of await this.#get('videos')) { | |
| arr.push(await this.detailVideo(data)); | |
| } | |
| return arr; | |
| }; | |
| popular = async () => { | |
| const arr = []; | |
| for (const data of await this.#get('videos/popular')) { | |
| arr.push(await this.detailVideo(data)); | |
| } | |
| return arr; | |
| }; | |
| trending = async () => { | |
| const arr = []; | |
| for (const data of await this.#get('trending')) { | |
| arr.push(await this.detailVideo(data)); | |
| } | |
| return arr; | |
| }; | |
| user = async (username) => { | |
| const html = (await axios.get(`https://urlebird.com/user/${username}/`)).data; | |
| const $ = cheerio.load(html); | |
| const obj = {}; | |
| const img = $('img.user-image'); | |
| const stats = $('div.content').find('div.row > div').get().map(x => $(x).text()); | |
| obj.name = img.attr('alt').split(' - ')?.[1]; | |
| obj.username = img.attr('alt').split(' - ')?.[0]; | |
| obj.likes = stats?.[0]; | |
| obj.followers = stats?.[1]?.replace(' followers', ''); | |
| obj.following = stats?.[2]?.replace(' following', ''); | |
| obj.description = $('div.content > p').text(); | |
| obj.avatar = img.attr('src'); | |
| obj.videos = []; | |
| for (const x of await this.#get(`user/${username}`)) { | |
| obj.videos.push(await this.detailVideo(x)); | |
| } | |
| return obj; | |
| }; | |
| } | |
| export default new Urlebird(); | |