UNPKG

@sugarcube/plugin-instagram

Version:
45 lines (37 loc) 1.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.feed = void 0; var _fp = require("lodash/fp"); var _dashp = require("dashp"); var _entities = require("./entities"); // FIXME: Using import throws an error. // import rp from "request-promise"; const rp = require("request-promise"); const fetch = (username, maxId) => { const opts = (0, _fp.merge)({ uri: `https://instagram.com/${username}/media`, json: true }, maxId ? { qs: { max_id: maxId } } : {}); return rp(opts).then((0, _fp.getOr)([], "items")).catch(() => []).then((0, _fp.map)((0, _entities.postEntity)(username))); }; const feed = (0, _fp.curry)((count, username) => { const xs = (0, _fp.range)(0, Math.ceil(count / 20)); let lastId = -1; return (0, _dashp.fold)(memo => { const id = (0, _fp.flow)([_fp.last, (0, _fp.get)("postId")])(memo); // There are no more posts. if (id === lastId) return memo; lastId = id; return fetch(username, id).then((0, _fp.concat)(memo)); }, [], xs).then((0, _fp.take)(count)); }); exports.feed = feed; var _default = { feed }; exports.default = _default;