{"size": 1449, "ext": "js", "max_stars_repo_path": "src/sites/agro.js", "max_stars_repo_name": "mykeels/farm-invest-cli", "max_stars_repo_stars_event_min_datetime": "2019-01-28T13:19:09.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-09T11:02:46.000Z", "max_issues_repo_path": "src/sites/agro.js", "max_issues_repo_name": "mykeels/farm-invest-cli", "max_issues_repo_issues_event_min_datetime": "2019-02-05T08:40:19.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-08T05:51:24.000Z", "max_forks_repo_path": "src/sites/agro.js", "max_forks_repo_name": "mykeels/farm-invest-cli", "max_forks_repo_forks_event_min_datetime": "2020-04-17T22:48:25.000Z", "max_forks_repo_forks_event_max_datetime": "2020-06-06T16:18:00.000Z"}
{"max_stars_count": 18, "max_issues_count": 7, "max_forks_count": 2, "avg_line_length": 26.3454545455, "max_line_length": 75, "alphanum_fraction": 0.4989648033}
const { default: axios } = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');
const url = require('url');
const { agroHtml } = require('../utils/create-files-dir');

module.exports = function() {
  return axios.get('https://agropartnerships.co/investments').then(res => {
    const $ = cheerio.load(res.data);

    fs.writeFileSync(agroHtml, res.data);

    const activeProducts = $('a.farm-block.demo-farm').filter(function() {
      return $(this).find('h6.light-green-text.no-padding').length === 0;
    });

    const productList = activeProducts
      .map(function() {
        const title = $(this)
          .find('h2.white-text')
          .text()
          .trim();
        const type = $(this)
          .find('h3.white-text')
          .text()
          .trim();
        const price = $(this)
          .find('h5.white-text')
          .text()
          .trim();
        const returns =
          $(this)
            .find('div.preview-yield-block.main-page > div')
            .text()
            .trim() +
          ' ' +
          $(this)
            .find('div.preview-yield-block.main-page > h5')
            .text()
            .trim();
        const link = url.resolve(
          'https://agropartnerships.co',
          $(this)
            .attr('href')
            .trim()
        );

        return { title, price, link, returns, type };
      })
      .toArray();

    return productList;
  });
};
