Act

drobnikj/apify-office-menus

  • Builds
  • latest 0.0.16 / 2018-02-26
  • Created 2017-12-14
  • Last modified 2018-02-26
  • grade 4

Description

This act outputs menus near Apify office to slack channel. Just run in your Slack, these commands: /apify login userId token /apify runAct drobnikj/apify-office-menus


API

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

https://api.apify.com/v2/acts/drobnikj~apify-office-menus/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

{ "hello": 123 }

Source code

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

const Apify = require('apify');

Apify.main(async () => {
    const input = await Apify.getValue('INPUT');
    console.log('Launching Puppeteer...');
    const browser = await Apify.launchPuppeteer();
    const restaurants = [];
    
    const page = await browser.newPage();
    // adria
    await page.goto('http://caffeadria.cz/nabidka/poledni-nabidka');
    const adriaMenu = await page.$$eval('[itemprop="articleBody"] tr', (trs) => {
        const results = [];
        trs.forEach((tr) => {
            const tds = tr.querySelectorAll('td');
            results.push({
                meal: tds[0].innerText.trim().replace(/\n/g, ' '),
                price: tds[1].innerText.trim().replace(/\n/g, ' ')
            });
        });
        return results;
    });
    restaurants.push({
        title: 'Adria',
        text: adriaMenu.map(food => `- ${food.meal}\t${food.price}`).join('\n'),
    });
    // U medvidku
    await page.goto('http://umedvidku.cz/denni-menu/');
    const medvidekMenu = await page.$$eval('section[itemprop="blogPost"] tr', (trs) => {
        const results = [];
        trs.forEach((tr) => {
            const tds = tr.querySelectorAll('td');
            if (tds.length > 2) {
                results.push({
                    meal: tds[1].innerText.trim().replace(/\n/g, ' '),
                    price: tds[2].innerText.trim().replace(/\n/g, ' ')
                });
            }
        });
        return results;
    });
    restaurants.push({
        title: 'U medvídků',
        text: medvidekMenu.map(food => `- ${food.meal}\t${food.price}`).join('\n'),
    });
    
    // http://bistroos.cz/
    await page.goto('http://bistroos.cz/');
    const bistroosMenu = await page.$$eval('#post-394 .menu-list__item-title', (trs) => {
        const results = [];
        trs.forEach((tr) => {
            try {
               const price = (!!tr.querySelector('.menu-list__item-price')) ? tr.querySelector('.menu-list__item-price').innerText.trim() : '-';
                const meal = tr.querySelector('.item_title').innerText.trim();
                results.push({
                    meal,
                    price,
                }); 
            } catch(e) {
                console.error(e);
            }
        });
        return results;
    });
    restaurants.push({
        title: 'Bistro Ostrovní',
        text: bistroosMenu.map(food => `- ${food.meal}\t${food.price}`).join('\n'),
    });
    
    const message = {
        response_type: 'in_channel',
        text: 'Today menus:',
        attachments: restaurants,
    }
    
    await Apify.setValue('OUTPUT', message);
});