Act

juansgaitan/scheduler-from-spreadsheet

  • Builds
  • latest 0.0.2 / 2018-01-30
  • Created 2018-01-30
  • Last modified 2018-02-14
  • grade 1

Description

Extract the schedule extraction for one or more acts.


API

To run the act, send a HTTP POST request to:

https://api.apify.com/v2/acts/juansgaitan~scheduler-from-spreadsheet/runs?token=<YOUR_API_TOKEN>

The POST payload will be passed as input for the act. For more information, read the docs.


Example input

Content type: application/json; charset=utf-8

{ "spreadsheetId": "1DN3a6YNTjf1K55E-pvGSLDkhZ7uri48csq-5Pnqzyzk" }

Source code

Based on the apify/actor-node-basic Docker image (see docs).

const Apify = require('apify');
const moment = require('moment');
const requestPromise = require('request-promise');

Apify.main(async () => {
  // const { sheetId } = await Apify.getValue('INPUT');
  const sheetId = '1-vpSVo1WjKheduG4YjXoOUfZ6mWaoeSekaiVRVoNV-8';

  if (!sheetId) {
    throw new Error('Invalid input, must be a JSON object with the "sheetId" field!');
  }
  const spreadSheetUrl = `
    https://spreadsheets.google.com/feeds/list/${sheetId}/od6/public/basic?alt=json
  `;
  console.log('Extracting info from:', spreadSheetUrl);

  let json;
  try {
    const response = await requestPromise(spreadSheetUrl);
    json = JSON.parse(response);
  } catch (err) {
    throw new Error('Error while getting spread sheet!', err);
  }
  const now = moment().startOf('hour');
  const afterAnHour = now.clone().add(1, 'hour');
  const defaultTime = moment().startOf('day');

  const { entry } = json.feed;
  const hashtags = entry.map(({ title, content }) => {
    const hashtag = title.$t;
    const [frequency] = [content.$t.match(/\d{1,2}:\d{2}:\d{2} [apm]{2}/i) || defaultTime];
    const time = moment(frequency, 'HH:mm:ss a');
    if (time.isBetween(now, afterAnHour)) {
      console.log('Calling Extractor for', hashtag);
      Apify.call('juansgaitan/instagram-extract-posts', { hashtag });
    }
    return { hashtag, time };
  });
  console.log(hashtags);
  console.log('Done.');
});