diff --git a/process_itch.js b/process_itch.js index 854438dd..26648dd7 100644 --- a/process_itch.js +++ b/process_itch.js @@ -45,10 +45,9 @@ const TAGS_MAP = { embed = await getEmbed(game, uploadsResponse); } - const d = new Date(timestamp); let path; for (timestamp--; path == null || fs.existsSync(path);) { - path = `./site/article/${d.getFullYear()}/${d.getMonth()}/${d.getDate()}/${++timestamp}`; + path = `./site/article/${getTimestampPath(++timestamp)}`; } fs.mkdirSync(path, { recursive: true }); const fd = fs.openSync(path + "/index.md", "w+"); diff --git a/process_posts.js b/process_posts.js index 00529755..a5556362 100644 --- a/process_posts.js +++ b/process_posts.js @@ -53,10 +53,9 @@ const feed = new Feed({ let frontmatter = data.match(/---\n([\S\s]*?\n)---/m)[1]; const { kind, published } = YAML.parse(frontmatter); let timestamp = parseInt(published); - const d = new Date(timestamp); let path; for (timestamp--; path == null || fs.existsSync(path);) { - path = `./site/${kind}/${d.getFullYear()}/${d.getMonth()}/${d.getDate()}/${++timestamp}`; + path = `./site/${kind}/${getTimestampPath(++timestamp)}`; } fs.mkdirSync(path, { recursive: true }); fs.copyFileSync(filePath, path + "/index.md"); @@ -180,8 +179,7 @@ function processPost(dir, file, resolve) { const content = data.match(/---\n[\S\s]*?\n---\n([\S\s]*)/m)[1]; - const d = new Date(timestamp); - const link = `https://www.thepaperpilot.org/${kind}/${d.getFullYear()}/${d.getMonth()}/${d.getDate()}/${timestamp}`; + const link = `https://www.thepaperpilot.org/${kind}/${getTimestampPath(timestamp)}`; feed.addItem({ title: title ?? kind, id: link, @@ -214,8 +212,7 @@ function insertByDate(posts, kind, timestamp) { } function getContentFromTimestamp({ kind, timestamp }) { - const d = new Date(timestamp); - const filePath = `./site/${kind}/${d.getFullYear()}/${d.getMonth()}/${d.getDate()}/${timestamp}/index.md`; + const filePath = `./site/${kind}/${getTimestampPath(timestamp)}/index.md`; const data = fs.readFileSync(filePath).toString(); return data.match(/---\n[\S\s]*?\n---\n([\S\s]*)/m)[1] .replace(//, '') diff --git a/process_reddit.js b/process_reddit.js index 6015cebc..55304223 100644 --- a/process_reddit.js +++ b/process_reddit.js @@ -104,10 +104,9 @@ let createdPosts = 0; const tag = slugify(aliases_map[subreddit.toLowerCase()] ?? subreddit); - const d = new Date(timestamp); let path; for (timestamp--; path == null || fs.existsSync(path);) { - path = `./site/${body ? 'article' : 'bookmark'}/${d.getFullYear()}/${d.getMonth()}/${d.getDate()}/${++timestamp}`; + path = `./site/${body ? 'article' : 'bookmark'}/${getTimestampPath(++timestamp)}`; } fs.mkdirSync(path, { recursive: true }); const fd = fs.openSync(path + "/index.md", "w+"); @@ -225,10 +224,9 @@ tags: [${encodeString(tag, 2)}] })) ?? []); const tag = slugify(aliases_map[subreddit.toLowerCase()] ?? subreddit); - const d = new Date(timestamp); let path; for (timestamp--; path == null || fs.existsSync(path);) { - path = `./site/reply/${d.getFullYear()}/${d.getMonth()}/${d.getDate()}/${++timestamp}`; + path = `./site/reply/${getTimestampPath(++timestamp)}`; } fs.mkdirSync(path, { recursive: true }); const fd = fs.openSync(path + "/index.md", "w+"); @@ -393,10 +391,9 @@ async function like_post(id, permalink, action = "auto") { const author = extractUsername(await submission.author); const subreddit = await extractSubreddit(submission.subreddit); - const d = new Date(timestamp); let path; for (timestamp--; path == null || fs.existsSync(path);) { - path = `./site/${action}/${d.getFullYear()}/${d.getMonth()}/${d.getDate()}/${++timestamp}`; + path = `./site/${action}/${getTimestampPath(++timestamp)}`; } fs.mkdirSync(path, { recursive: true }); const fd = fs.openSync(path + "/index.md", "w+"); @@ -451,10 +448,9 @@ async function like_comment(id, permalink, action = "repost") { const author = extractUsername(await comment.author); const subreddit = await extractSubreddit(submission.subreddit); - const d = new Date(timestamp); let path; for (timestamp--; path == null || fs.existsSync(path);) { - path = `./site/${action}/${d.getFullYear()}/${d.getMonth()}/${d.getDate()}/${++timestamp}`; + path = `./site/${action}/${getTimestampPath(++timestamp)}`; } fs.mkdirSync(path, { recursive: true }); const fd = fs.openSync(path + "/index.md", "w+"); diff --git a/process_youtube.js b/process_youtube.js index df9ed957..f26823a6 100644 --- a/process_youtube.js +++ b/process_youtube.js @@ -214,10 +214,9 @@ async function process(auth) { continue; } - const d = new Date(timestamp); let path; for (timestamp--; path == null || fs.existsSync(path);) { - path = `./site/reply/${d.getFullYear()}/${d.getMonth()}/${d.getDate()}/${++timestamp}`; + path = `./site/reply/${getTimestampPath(++timestamp)}`; } fs.mkdirSync(path, { recursive: true }); const fd = fs.openSync(path + "/index.md", "w+"); @@ -355,10 +354,9 @@ tags: [${encodeString(tag, 2)}] `; - const d = new Date(timestamp); let path; for (timestamp--; path == null || fs.existsSync(path);) { - path = `./site/bookmark/${d.getFullYear()}/${d.getMonth()}/${d.getDate()}/${++timestamp}`; + path = `./site/bookmark/${getTimestampPath(++timestamp)}`; } fs.mkdirSync(path, { recursive: true }); const fd = fs.openSync(path + "/index.md", "w+"); @@ -695,10 +693,9 @@ async function likeVideo({ videoId, auth, kind }) { `; - const d = new Date(timestamp); let path; for (timestamp--; path == null || fs.existsSync(path);) { - path = `./site/${kind}/${d.getFullYear()}/${d.getMonth()}/${d.getDate()}/${++timestamp}`; + path = `./site/${kind}/${getTimestampPath(++timestamp)}`; } fs.mkdirSync(path, { recursive: true }); const fd = fs.openSync(path + "/index.md", "w+"); diff --git a/utils.js b/utils.js index 4be3a6ed..dc79aa9b 100644 --- a/utils.js +++ b/utils.js @@ -10,7 +10,7 @@ const KIND_ACTIONS_MAP = { bookmark: "❤️", favorite: "⭐", reply: "💬", - "": "📝" + [undefined]: "📝" } const KIND_VERBS_MAP = { @@ -42,7 +42,7 @@ function getActionDescription({ kind, timestamp }) { ` on ` : ''; - const path = `/posts/${ts.getFullYear()}/${ts.getMonth()}/${ts.getDate()}/${timestamp}`; + const path = `/${kind}/${getTimestampPath(timestamp)}`; return `
${KIND_ACTIONS_MAP[kind]} The Paper Pilot @@ -239,6 +239,11 @@ function linkify(str) { '$&'); } +function getTimestampPath(timestamp) { + const d = new Date(timestamp); + return `${d.getFullYear()}/${d.getMonth() + 1}/${d.getDate() + 1}/${timestamp}`; +} + function monthString(month) { return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][month]; } @@ -275,5 +280,6 @@ module.exports = { getArchivePreview, getMediaUrl, linkify, - sanitize + sanitize, + getTimestampPath };