{"size": 1637, "ext": "js", "max_stars_repo_path": "modules/foodpantries_org.js", "max_stars_repo_name": "eebbesen/scrape_food_pantry", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "modules/foodpantries_org.js", "max_issues_repo_name": "eebbesen/scrape_food_pantry", "max_issues_repo_issues_event_min_datetime": "2020-09-26T01:21:02.000Z", "max_issues_repo_issues_event_max_datetime": "2021-01-06T02:23:49.000Z", "max_forks_repo_path": "modules/foodpantries_org.js", "max_forks_repo_name": "eebbesen/scrape_food_pantry", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 29.2321428571, "max_line_length": 136, "alphanum_fraction": 0.7049480757}
const axios = require('axios');
const cheerio = require('cheerio');

const MAIN_SELECTOR = 'h2 a';
const DETAIL_SELECTOR = '.container.content .row .span8 script';

async function getProviderData(url) {
  const res = await axios.get(url);
  const $ = cheerio.load(res.data);
  const entries = $(MAIN_SELECTOR);
  const providers = [];

  entries.map(i => {
    const entry = entries[i].attribs;
    providers.push({provider: entry.title, link: entry.href});
  });

  return providers;
};

async function getDetails(provider) {
  const res = await axios.get(provider.link);
  const $ = cheerio.load(res.data);
  const entries = $(DETAIL_SELECTOR);

  let detailsJson = 'error';
  try {
    detailsJson = JSON.parse(entries[0].children[0].data.replace(/\n/g, '').replace("\\'", ''));
  } catch (err) {
    console.log('Error parsing JSON for\n', entries[0].children[0].data);
    console.log(err);
  }

  const fullAddress = detailsJson.address;
  const address = [fullAddress.streetAddress, fullAddress.addressLocality, fullAddress.addressRegion, fullAddress.postalCode].join(',');

  return Object.assign({address: address, phone: detailsJson.telephone, description: detailsJson.description}, provider);
};

async function decorateProviders(providers) {
  const promises = providers.map(async p => {
    const updatedProvider = await getDetails(p);
    return updatedProvider;
  });

  const resolved = await Promise.all(promises);
  return resolved;
};

exports.scrape = async (url) => {
  const providers = await getProviderData(url);
  const decorated_providers = await decorateProviders(providers);

  return decorated_providers;
};
