2018-01-14 07:02:41 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-01-12 12:54:17 +08:00
|
|
|
const sql = require('../services/sql');
|
|
|
|
const notes = require('../services/notes');
|
|
|
|
const axios = require('axios');
|
|
|
|
const log = require('../services/log');
|
|
|
|
const utils = require('../services/utils');
|
|
|
|
const unescape = require('unescape');
|
2018-01-14 04:25:09 +08:00
|
|
|
const attributes = require('../services/attributes');
|
2018-01-14 11:51:39 +08:00
|
|
|
const sync_mutex = require('../services/sync_mutex');
|
|
|
|
const config = require('../services/config');
|
2018-01-25 12:08:14 +08:00
|
|
|
const date_notes = require('../services/date_notes');
|
2018-01-17 09:11:50 +08:00
|
|
|
|
|
|
|
// "reddit" date note is subnote of date note which contains all reddit comments from that date
|
|
|
|
const REDDIT_DATE_ATTRIBUTE = 'reddit_date_note';
|
2018-01-14 04:25:09 +08:00
|
|
|
|
|
|
|
async function createNote(parentNoteId, noteTitle, noteText) {
|
|
|
|
return (await notes.createNewNote(parentNoteId, {
|
|
|
|
note_title: noteTitle,
|
|
|
|
note_text: noteText,
|
|
|
|
target: 'into',
|
|
|
|
is_protected: false
|
|
|
|
})).noteId;
|
|
|
|
}
|
|
|
|
|
|
|
|
function redditId(kind, id) {
|
|
|
|
return kind + "_" + id;
|
|
|
|
}
|
2018-01-12 12:54:17 +08:00
|
|
|
|
2018-01-13 11:44:55 +08:00
|
|
|
async function getDateNoteIdForReddit(dateTimeStr, rootNoteId) {
|
|
|
|
const dateStr = dateTimeStr.substr(0, 10);
|
|
|
|
|
2018-01-17 09:11:50 +08:00
|
|
|
let redditDateNoteId = await attributes.getNoteIdWithAttribute(REDDIT_DATE_ATTRIBUTE, dateStr);
|
2018-01-13 11:44:55 +08:00
|
|
|
|
|
|
|
if (!redditDateNoteId) {
|
2018-01-25 12:08:14 +08:00
|
|
|
const dateNoteId = await date_notes.getDateNoteId(dateTimeStr, rootNoteId);
|
2018-01-13 11:44:55 +08:00
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
redditDateNoteId = await createNote(dateNoteId, "Reddit");
|
2018-01-13 11:44:55 +08:00
|
|
|
|
2018-01-17 09:11:50 +08:00
|
|
|
await attributes.createAttribute(redditDateNoteId, REDDIT_DATE_ATTRIBUTE, dateStr);
|
2018-01-13 11:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return redditDateNoteId;
|
|
|
|
}
|
|
|
|
|
2018-01-17 09:11:50 +08:00
|
|
|
async function importComments(rootNoteId, accountName, afterId = null) {
|
2018-01-13 11:44:55 +08:00
|
|
|
let url = `https://www.reddit.com/user/${accountName}.json`;
|
|
|
|
|
|
|
|
if (afterId) {
|
|
|
|
url += "?after=" + afterId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await axios.get(url);
|
2018-01-12 12:54:17 +08:00
|
|
|
const listing = response.data;
|
|
|
|
|
|
|
|
if (listing.kind !== 'Listing') {
|
2018-01-14 04:25:09 +08:00
|
|
|
log.info(`Reddit: Unknown object kind ${listing.kind}`);
|
2018-01-12 12:54:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const children = listing.data.children;
|
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
let importedComments = 0;
|
|
|
|
|
2018-01-12 12:54:17 +08:00
|
|
|
for (const child of children) {
|
|
|
|
const comment = child.data;
|
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
let commentNoteId = await attributes.getNoteIdWithAttribute('reddit_id', redditId(child.kind, comment.id));
|
2018-01-12 12:54:17 +08:00
|
|
|
|
|
|
|
if (commentNoteId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dateTimeStr = utils.dateStr(new Date(comment.created_utc * 1000));
|
|
|
|
|
2018-01-14 12:24:40 +08:00
|
|
|
const permaLink = 'https://reddit.com' + comment.permalink;
|
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
const noteText =
|
2018-01-14 12:24:40 +08:00
|
|
|
`<p><a href="${permaLink}">${permaLink}</a></p>
|
|
|
|
<p>author: <a href="https://reddit.com/u/${comment.author}">${comment.author}</a>,
|
|
|
|
subreddit: <a href="https://reddit.com/r/${comment.subreddit}">${comment.subreddit}</a>,
|
|
|
|
karma: ${comment.score}, created at ${dateTimeStr}</p><p></p>`
|
2018-01-12 12:54:17 +08:00
|
|
|
+ unescape(comment.body_html);
|
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
let parentNoteId = await getDateNoteIdForReddit(dateTimeStr, rootNoteId);
|
2018-01-12 12:54:17 +08:00
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
commentNoteId = await createNote(parentNoteId, comment.link_title, noteText);
|
2018-01-13 11:44:55 +08:00
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
log.info("Reddit: Imported comment to note " + commentNoteId);
|
|
|
|
importedComments++;
|
2018-01-13 11:44:55 +08:00
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
await attributes.createAttribute(commentNoteId, "reddit_kind", child.kind);
|
|
|
|
await attributes.createAttribute(commentNoteId, "reddit_id", redditId(child.kind, comment.id));
|
|
|
|
await attributes.createAttribute(commentNoteId, "reddit_created_utc", comment.created_utc);
|
|
|
|
});
|
2018-01-13 11:44:55 +08:00
|
|
|
}
|
2018-01-12 12:54:17 +08:00
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
// if there have been no imported comments on this page, there shouldn't be any to import
|
|
|
|
// on the next page since those are older
|
|
|
|
if (listing.data.after && importedComments > 0) {
|
2018-01-17 09:11:50 +08:00
|
|
|
importedComments += await importComments(rootNoteId, accountName, listing.data.after);
|
2018-01-14 04:25:09 +08:00
|
|
|
}
|
2018-01-12 12:54:17 +08:00
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
return importedComments;
|
2018-01-12 12:54:17 +08:00
|
|
|
}
|
|
|
|
|
2018-01-14 11:51:39 +08:00
|
|
|
let redditAccounts = [];
|
|
|
|
|
|
|
|
async function runImport() {
|
2018-01-25 12:08:14 +08:00
|
|
|
const rootNoteId = await date_notes.getRootNoteId();
|
2018-01-17 09:11:50 +08:00
|
|
|
|
2018-01-14 11:51:39 +08:00
|
|
|
// technically mutex shouldn't be necessary but we want to avoid doing potentially expensive import
|
|
|
|
// concurrently with sync
|
|
|
|
await sync_mutex.doExclusively(async () => {
|
|
|
|
let importedComments = 0;
|
|
|
|
|
|
|
|
for (const account of redditAccounts) {
|
2018-01-17 09:11:50 +08:00
|
|
|
importedComments += await importComments(rootNoteId, account);
|
2018-01-14 11:51:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`Reddit: Imported ${importedComments} comments.`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-12 12:54:17 +08:00
|
|
|
sql.dbReady.then(async () => {
|
2018-01-14 12:03:17 +08:00
|
|
|
if (!config['Reddit'] || config['Reddit']['enabled'] !== true) {
|
2018-01-12 12:54:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-14 11:51:39 +08:00
|
|
|
const redditAccountsStr = config['Reddit']['accounts'];
|
2018-01-12 12:54:17 +08:00
|
|
|
|
2018-01-14 11:51:39 +08:00
|
|
|
if (!redditAccountsStr) {
|
2018-01-14 04:25:09 +08:00
|
|
|
log.info("Reddit: No reddit accounts defined in option 'reddit_accounts'");
|
2018-01-12 12:54:17 +08:00
|
|
|
}
|
|
|
|
|
2018-01-14 11:51:39 +08:00
|
|
|
redditAccounts = redditAccountsStr.split(",").map(s => s.trim());
|
2018-01-13 11:44:55 +08:00
|
|
|
|
2018-01-17 09:11:50 +08:00
|
|
|
const pollingIntervalInSeconds = config['Reddit']['pollingIntervalInSeconds'] || (4 * 3600);
|
2018-01-13 11:44:55 +08:00
|
|
|
|
2018-01-14 11:51:39 +08:00
|
|
|
setInterval(runImport, pollingIntervalInSeconds * 1000);
|
2018-01-17 09:11:50 +08:00
|
|
|
setTimeout(runImport, 10000); // 10 seconds after startup - intentionally after initial sync
|
2018-01-12 12:54:17 +08:00
|
|
|
});
|