From 28dc774499648a3eca6c69ce3402955d642a0873 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sun, 15 May 2016 01:41:22 -0700 Subject: [PATCH] Auto-update archive list. #517 --- src/Archive/Redirect.coffee | 74 ++++++++++++++++++++++++++---- src/General/Settings.coffee | 74 ++++++++++++++++++++++-------- src/General/Settings/Advanced.html | 9 +++- src/config/Config.coffee | 5 ++ src/css/style.css | 3 ++ src/main/Main.coffee | 1 + 6 files changed, 139 insertions(+), 27 deletions(-) diff --git a/src/Archive/Redirect.coffee b/src/Archive/Redirect.coffee index 95b329cb1..822935504 100644 --- a/src/Archive/Redirect.coffee +++ b/src/Archive/Redirect.coffee @@ -1,5 +1,17 @@ Redirect = + archives: + `<%= + JSON.stringify(readJSON('archives.json'), null, 2) + .replace(/\n {2,}(?!{)/g, ' ') + .replace(/\n/g, '\n ') + .replace(/`/g, '\\`') + %>` + init: -> + @selectArchives() + @update() if Conf['archiveAutoUpdate'] and Conf['lastarchivecheck'] < Date.now() - 2 * $.DAY + + selectArchives: -> o = thread: {} post: {} @@ -7,8 +19,10 @@ Redirect = report: {} archives = {} - for data in Redirect.archives + for data in Conf['archives'] {uid, name, boards, files, software, withCredentials} = data + boards = [] unless boards instanceof Array + files = [] unless files instanceof Array archives[JSON.stringify(uid ? name)] = data for boardID in boards unless withCredentials @@ -27,13 +41,57 @@ Redirect = Redirect.data = o - archives: - `<%= - JSON.stringify(readJSON('archives.json'), null, 2) - .replace(/\n {2,}(?!{)/g, ' ') - .replace(/\n/g, '\n ') - .replace(/`/g, '\\`') - %>` + update: (cb) -> + urls = [] + responses = [] + nloaded = 0 + for url in Conf['archiveSources'].split('\n') when url[0] isnt '#' + url = url.trim() + urls.push url if url + + load = (i) -> -> + fail = (action, msg) -> new Notice 'warning', "Error #{action} archive data from #{urls[i]}\n#{msg}", 20 + return fail 'fetching', (if @status then "#{@status} #{@statusText}" else 'Connection Error') unless @status is 200 + try + response = JSON.parse @response + catch err + return fail 'parsing', err.message + response = [response] unless response instanceof Array + responses[i] = response + nloaded++ + if nloaded is urls.length + Redirect.parse responses, cb + + if urls.length + for url, i in urls + if url[0] in ['[', '{'] + load(i).call + status: 200 + response: url + else + $.ajax url, + responseType: 'text' + onloadend: load(i) + else + Redirect.parse [], cb + return + + parse: (responses, cb) -> + archives = [] + archiveUIDs = {} + for response in responses + for data in response + uid = JSON.stringify(data.uid ? data.name) + if uid of archiveUIDs + $.extend archiveUIDs[uid], data + else + archiveUIDs[uid] = data + archives.push data + items = {archives, lastarchivecheck: Date.now()} + $.set items + $.extend Conf, items + Redirect.selectArchives() + cb?() to: (dest, data) -> archive = (if dest in ['search', 'board'] then Redirect.data.thread else Redirect.data[dest])[data.boardID] diff --git a/src/General/Settings.coffee b/src/General/Settings.coffee index ee7b20821..ae8440c14 100644 --- a/src/General/Settings.coffee +++ b/src/General/Settings.coffee @@ -430,21 +430,26 @@ Settings = inputs[input.name] = input items = {} - for name in ['captchaLanguage', 'boardnav', 'time', 'backlink', 'fileInfo', 'QR.personas', 'favicon', 'usercss', 'customCooldown'] + for name in ['archiveSources', 'archiveAutoUpdate', 'captchaLanguage', 'boardnav', 'time', 'backlink', 'fileInfo', 'QR.personas', 'favicon', 'usercss', 'customCooldown'] items[name] = Conf[name] input = inputs[name] - event = if name in ['QR.personas', 'favicon', 'usercss'] then 'change' else 'input' - $.on input, event, $.cb.value + event = if name in ['archiveSources', 'archiveAutoUpdate', 'QR.personas', 'favicon', 'usercss'] then 'change' else 'input' + $.on input, event, $.cb[if input.type is 'checkbox' then 'checked' else 'value'] $.on input, event, Settings[name] if name of Settings $.get items, (items) -> for key, val of items input = inputs[key] - input.value = val + input[if input.type is 'checkbox' then 'checked' else 'value'] = val if key of Settings Settings[key].call input return + $.on inputs['archiveSources'], 'change', -> + $.set 'lastarchivecheck', 0 + Conf['lastarchivecheck'] = 0 + $.id('lastarchivecheck').textContent = 'never' + interval = inputs['Interval'] customCSS = inputs['Custom CSS'] applyCSS = $ '#apply-css', section @@ -458,8 +463,41 @@ Settings = $.on customCSS, 'change', Settings.togglecss $.on applyCSS, 'click', -> CustomCSS.update() + itemsArchive = {} + itemsArchive[name] = Conf[name] for name in ['archives', 'selectedArchives', 'lastarchivecheck'] + $.get itemsArchive, (itemsArchive) -> + $.extend Conf, itemsArchive + Settings.addArchiveTable section + + boardSelect = $ '#archive-board-select', section + table = $ '#archive-table', section + updateArchives = $ '#update-archives', section + + $.on boardSelect, 'change', -> + $('tbody > :not([hidden])', table).hidden = true + $("tbody > .#{@value}", table).hidden = false + + $.on updateArchives, 'click', -> + Redirect.update -> + Settings.addArchiveTable section + + addArchiveTable: (section) -> + $('#lastarchivecheck', section).textContent = if Conf['lastarchivecheck'] is 0 + 'never' + else + new Date(Conf['lastarchivecheck']).toLocaleString() + + boardSelect = $ '#archive-board-select', section + table = $ '#archive-table', section + tbody = $ 'tbody', section + + $.rmAll boardSelect + $.rmAll tbody + archBoards = {} - for {uid, name, boards, files, software, withCredentials} in Redirect.archives + for {uid, name, boards, files, software, withCredentials} in Conf['archives'] + boards = [] unless boards instanceof Array + files = [] unless files instanceof Array for boardID in boards o = archBoards[boardID] or= thread: [[], []] @@ -492,24 +530,23 @@ Settings = $.add row, Settings.addArchiveCell boardID, o, item for item in ['thread', 'post', 'file'] rows.push row + if rows.length is 0 + boardSelect.hidden = table.hidden = true + return + + boardSelect.hidden = table.hidden = false + unless g.BOARD.ID of archBoards rows[0].hidden = false - $.add $('tbody', section), rows - - boardSelect = $('#archive-board-select', section) $.add boardSelect, boardOptions - table = $('#archive-table', section) - $.on boardSelect, 'change', -> - $('tbody > :not([hidden])', table).hidden = true - $("tbody > .#{@value}", table).hidden = false + $.add tbody, rows - $.get 'selectedArchives', Conf['selectedArchives'], ({selectedArchives}) -> - for boardID, data of selectedArchives - for type, id of data - if select = $ "select[data-boardid='#{boardID}'][data-type='#{type}']", section - select.value = JSON.stringify id - return + for boardID, data of Conf['selectedArchives'] + for type, id of data + if (select = $ "select[data-boardid='#{boardID}'][data-type='#{type}']", tbody) + select.value = JSON.stringify id + select.value = select.firstChild.value unless select.value return addArchiveCell: (boardID, data, type) -> @@ -544,6 +581,7 @@ Settings = $.get 'selectedArchives', Conf['selectedArchives'], ({selectedArchives}) => (selectedArchives[@dataset.boardid] or= {})[@dataset.type] = JSON.parse @value $.set 'selectedArchives', selectedArchives + Conf['selectedArchives'] = selectedArchives boardnav: -> Header.generateBoardList @value diff --git a/src/General/Settings/Advanced.html b/src/General/Settings/Advanced.html index 70d611b1d..80f23db65 100644 --- a/src/General/Settings/Advanced.html +++ b/src/General/Settings/Advanced.html @@ -1,5 +1,5 @@
- Archiver + Archives
404 Redirect is disabled.
@@ -10,6 +10,13 @@
+
+
+ Each line below should be an archive list in this format or a URL to load an archive list from.
+ Archive properties can be overriden by another item with the same uid (or if absent, its name). +
+ + Last updated:
diff --git a/src/config/Config.coffee b/src/config/Config.coffee index 44c11e7e3..5028af64d 100644 --- a/src/config/Config.coffee +++ b/src/config/Config.coffee @@ -761,6 +761,11 @@ Config = 'Shortcut Icons': true 'Custom Board Navigation': true + archives: + archiveSources: 'https://4chan-x.just-believe.in/json/archives.json' + lastarchivecheck: 0 + archiveAutoUpdate: true + boardnav: """ [ toggle-all ] a-replace diff --git a/src/css/style.css b/src/css/style.css index cf406e511..5efb726c7 100644 --- a/src/css/style.css +++ b/src/css/style.css @@ -602,6 +602,9 @@ div[data-checked="false"] > .suboption-list { .section-advanced textarea { height: 150px; } +.section-advanced textarea[name="archiveSources"] { + height: 75px; +} .section-advanced .archive-cell { min-width: 160px; text-align: center; diff --git a/src/main/Main.coffee b/src/main/Main.coffee index b19490780..612933550 100644 --- a/src/main/Main.coffee +++ b/src/main/Main.coffee @@ -51,6 +51,7 @@ Main = for db in DataBoard.keys Conf[db] = boards: {} + Conf['archives'] = Redirect.archives Conf['selectedArchives'] = {} Conf['cooldowns'] = {} Conf['Index Sort'] = {}