UNPKG

@sugarcube/plugin-google

Version:

A plugin for SugarCube to interact with some google services.

77 lines (62 loc) 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.parseReverseImages = exports.parseImages = exports.parseSearches = void 0; var _fp = require("lodash/fp"); var _cheerio = _interopRequireDefault(require("cheerio")); var _url = require("url"); var _utils = require("./utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const parseSearches = (0, _fp.curry)((searchUrl, body) => { const $ = _cheerio.default.load(body); return $("div.srg > div.g").toArray().map(el => { const title = $("h3 > a", el).text(); const href = $("h3 > a", el).attr("href"); const actionItems = $("div.s ol > li", el).toArray(); const cached = $("a", actionItems[0]).attr("href"); const similar = `${searchUrl}${$("a", actionItems[1]).attr("href")}`; const date = (0, _fp.flow)([(0, _fp.trimCharsEnd)(" - "), _utils.maybeDate])($("div.s span.st > span.f", el).text()); const description = $("div.s span.st", el).text(); return { title, href, cached, similar, description, date }; }); }); exports.parseSearches = parseSearches; const parseImages = html => { const $ = _cheerio.default.load(html); return $("#rg a.rg_l").parents().toArray().map(el => { const path = $("a.rg_l", el).attr("href"); const u = new _url.URL(`https://google.com${path}`); return { href: u.searchParams.get("imgurl"), imgHref: u.searchParams.get("imgrefurl") }; }); }; exports.parseImages = parseImages; const parseReverseImages = html => { const $ = _cheerio.default.load(html); // const topstuff = $('#topstuff'); const results = $("#search div.srg div.g").toArray().map(el => { const title = $("h3 > a", el).text(); const href = $("h3 > a", el).attr("href"); return { title, href }; }); return results; }; exports.parseReverseImages = parseReverseImages; var _default = { parseSearches, parseImages, parseReverseImages }; exports.default = _default;