1const Apify = require('apify');
2const request = require('request-promise');
3const cheerio = require('cheerio');
4
5
6Apify.main(async () => {
7
8 const input = await Apify.getValue('INPUT');
9 if (!input || typeof(input.url) !== 'string') {
10 throw new Error("Invalid input, it needs to contain 'url' field.");
11 }
12
13
14 console.log(`Opening ${input.url}`);
15 const html = await request(input.url);
16
17 const $ = cheerio.load(html);
18
19 const meta = {};
20 $('head meta').each(function () {
21 const name = $(this).attr('name');
22 const content = $(this).attr('content');
23 if (name) meta[name] = content ? content.trim() : null;
24 });
25
26 const result = {
27 url: input.url,
28 title: ($('head title').text() || '').trim(),
29 meta,
30 }
31
32
33 console.log('Result:');
34 console.dir(result);
35 await Apify.setValue('OUTPUT', result);
36});