From e7ef39a4b2e3b30244d3010d9d4e42a713eb4ec9 Mon Sep 17 00:00:00 2001 From: octospacc Date: Mon, 12 Dec 2022 23:40:40 +0100 Subject: [PATCH] Script per downsync listed --- .gitignore | 4 ++ Scripts/ListedDownsync.js | 128 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100755 Scripts/ListedDownsync.js diff --git a/.gitignore b/.gitignore index 603f9e3..6dc7156 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,10 @@ i18n/* public/* public.gmi/* public.plain/* +Scripts/.bak/* +Scripts/package.json +Scripts/package-lock.json +Scripts/node_modules/* Scripts.out/* *.tmp tmp.* diff --git a/Scripts/ListedDownsync.js b/Scripts/ListedDownsync.js new file mode 100755 index 0000000..02c015c --- /dev/null +++ b/Scripts/ListedDownsync.js @@ -0,0 +1,128 @@ +#!/usr/bin/env -S node --experimental-fetch +const fs = require('fs'); +const JSDOM = require('jsdom').JSDOM; + +const BlogURL = 'https://listed.to/@u8'; +const SiteName = 'sitoctt'; +//const DefaultMode = 'Include' // 'Include' or 'Exclude' | Not implemented +const PostsFileDate = true; // Append date to posts file names +const Replacements = { + " { + if (fs.existsSync(Path)) { + return fs.readFileSync(Path, 'utf8'); + }; +}; +const TryMkdirSync = Path => { + if (!fs.existsSync(Path)) { + return fs.mkdirSync(Path, {recursive:true}); + }; +}; + +const GetPath = URL => { + if (URL.startsWith('http://') || URL.startsWith('https://')) { + URL = URL.split('/').slice(3).join('/'); + }; + return URL; +}; + +const CheckDoDownsync = File => { + let DoDownsync = true; + const TryFile = TryReadFileSync(File); + if (TryFile) { + const Lines = TryFile.trim().toLowerCase().split('\n'); + for (let i=0; i {return e != ''}); + if (Tokens[1] == '%' && Tokens[2] == 'downsync') { + if (['false', 'disabled', 'off', 'no'].includes(Tokens[4])) { + DoDownsync = false; + }; + }; + }; + }; + }; + return DoDownsync; +}; + +const HandlePost = PostSrc => { + let Elem; + let Post = {}; + + Post['Title'] = PostSrc.title; + Post['CreatedOn'] = PostSrc.created_at.split('T')[0]; + console.log(`[I] => [${Post['CreatedOn']}] ${Post['Title']}`); + + const Dom = JSDOM.fragment(PostSrc.rendered_text); + Elem = Dom.querySelector(`.Mirror-${SiteName}`); + if (!Elem) { + Elem = Dom.querySelector(`.Mirror-${SiteName}-Include`); + }; + if (!Elem) { // Post content has no mirror-flagging element, skip it + console.log(`[I] : No mirror flag in source body; Skipping!`); + return; // TODO: Exclusion mode instead of inclusion? Aka automatically handle posts without the element + }; + + const Path = GetPath(JSDOM.fragment(Elem.outerHTML).querySelector('[href]').href); + Post['Categories'] = ''; + const Classes = Elem.classList; + for (let i=0; i { + console.log('[I] Downloading...'); + fetch(`${BlogURL}/all`).then(Response => Response.text()).then(Data => { + console.log('[I] Parsing...'); + const Elem = JSDOM.fragment(Data).querySelector('script[data-component-name="AuthorAll"]'); + const Posts = JSON.parse(Elem.childNodes[0].data).posts; + for (let i=0; i