From bdae9893c462e6f5290eaffd3c373b7f091769bc Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Wed, 11 Sep 2024 05:16:53 +0200 Subject: [PATCH] Curating and fetching images from imgur. Crudely saving to disk. --- .gitignore | 3 ++- app.js | 30 +++++++++++++++++++++++++----- package-lock.json | 10 ++++++++++ package.json | 2 ++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 9779bb8..2b22a9a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules/ config/*.js !config/example.js -dist/* +/* +output/ diff --git a/app.js b/app.js index 3290958..601db8a 100644 --- a/app.js +++ b/app.js @@ -1,22 +1,42 @@ 'use strict'; const config = require('config'); +const util = require('util'); +const note = require('note-log'); const yargs = require('yargs').argv; const snoowrap = require('snoowrap'); - -console.log(yargs.user); +const methods = require('./methods/methods.js'); +const dissectLink = require('./dissectLink.js'); +const fetchContent = require('./fetchContent.js'); const reddit = new snoowrap(config.api); reddit.getUser(yargs.user).getSubmissions({ sort: 'top' }).then(submissions => { - const details = submissions.map(submission => { + const curatedPosts = submissions.map(submission => { return { + id: submission.id, title: submission.title, - url: submission.url + permalink: submission.permalink, + url: submission.url, + host: dissectLink(submission.url) }; }); - console.log(details); + return Promise.all(curatedPosts.reduce((acc, post) => { + if(post.host && methods[post.host.method]) { + acc = acc.concat(methods[post.host.method](post).then(content => { + post.content = content; + + return post; + })); + } else { + note('fetch', 1, `"${post.title}": '${post.url}' not supported :(`); + } + + return acc; + }, [])); +}).then(fetchContent).catch(error => { + note(error); }); diff --git a/package-lock.json b/package-lock.json index 98b34b0..dc8a573 100644 --- a/package-lock.json +++ b/package-lock.json @@ -556,6 +556,11 @@ "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.0.0.tgz", "integrity": "sha1-yobR/ogoFpsBICCOPchCS524NCw=" }, + "node-fetch": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz", + "integrity": "sha1-q4hOjn5X44qUR1POxwb3iNF2i7U=" + }, "note-log": { "version": "2.1.11", "resolved": "https://registry.npmjs.org/note-log/-/note-log-2.1.11.tgz", @@ -847,6 +852,11 @@ "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.1.tgz", "integrity": "sha1-+nG63UQ3r0wUiEHjs7Fl+enlkLc=" }, + "url-pattern": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/url-pattern/-/url-pattern-1.0.3.tgz", + "integrity": "sha1-BAkpJHGyTyPFDWWkeTF5PStaz8E=" + }, "uuid": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.2.1.tgz", diff --git a/package.json b/package.json index 49b60bb..650fa4f 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,10 @@ "dependencies": { "config": "^1.30.0", "fs-extra": "^5.0.0", + "node-fetch": "^2.1.2", "note-log": "^2.1.11", "snoowrap": "^1.15.2", + "url-pattern": "^1.0.3", "yargs": "^11.0.0" } }