dramaqu.js

by Shannz
39 Raw
const axios = require('axios');
const cheerio = require('cheerio');

async function dramaqu(page = 1) {
  try {
    if (page < 1 || page > 40) {
      throw new Error('Halaman harus antara 1 dan 40');
    }

    const url = `https://dramaqu.hair/drama/page/${page}/`;
    
    const response = await axios.get(url);
    const html = response.data;

    const $ = cheerio.load(html);

    const dramas = [];
    $('.items .item.tvshows').each((index, element) => {
      const $element = $(element);
      const drama = {
        title: $element.find('.serie-title').text().trim(),
        image: $element.find('.poster img').attr('src'),
        type: $element.find('.features-type b').text(),
        year: $element.find('.features-status b').text(),
        link: $element.find('a').attr('href')
      };
      dramas.push(drama);
    });

    const result = {
      page: page,
      data: dramas
    };

    return(JSON.stringify(result, null, 2));

  } catch (error) {
    const errorResult = {
      status: 'error',
      message: 'Terjadi kesalahan saat scraping',
      page: page,
      error: error.message
    };

    return(JSON.stringify(errorResult, null, 2));
  }
}

module.exports = { dramaqu };