diff options
author | Choom | 2023-01-15 03:12:48 -0300 |
---|---|---|
committer | Choom | 2023-01-16 14:37:04 -0300 |
commit | 4c31093827f57394563de037a21143407586e384 (patch) | |
tree | b16abeb0dc76e48e11f8c0264fa39ce6294458d2 | |
parent | 44f7397cf7a7596edea78864122b78d59983701e (diff) | |
download | weabot-4c31093827f57394563de037a21143407586e384.tar.gz weabot-4c31093827f57394563de037a21143407586e384.tar.xz weabot-4c31093827f57394563de037a21143407586e384.zip |
Autorefresh en home
-rw-r--r-- | cgi/api.py | 11 | ||||
-rw-r--r-- | cgi/post.py | 13 | ||||
-rw-r--r-- | cgi/templates/feed.rss | 25 | ||||
-rw-r--r-- | cgi/templates/home.html | 34 | ||||
-rw-r--r-- | cgi/templates/home.rss | 25 | ||||
-rw-r--r-- | cgi/templates/mobile/latest.html | 4 | ||||
-rw-r--r-- | cgi/templates/mobile/newest.html | 4 | ||||
-rwxr-xr-x | cgi/weabot.py | 7 | ||||
-rw-r--r-- | static/js/autorefresh.js | 4 | ||||
-rw-r--r-- | static/js/home.js | 192 |
10 files changed, 163 insertions, 156 deletions
@@ -93,18 +93,19 @@ def api_process(self, path_split): data_limit = formdata.get('limit') data_time = formdata.get('time', 0) - limit = 30 - if data_limit: try: limit = int(data_limit) except ValueError: raise APIError("Limit must be numeric") - if limit > 50: - raise APIError("Maximum limit is 50") + if limit > 30: + raise APIError("Maximum limit is 30") + + threads = getLastAge(0, limit) + threads += getLastAge(1, limit) + threads = sorted(threads, key=lambda b:b['bumped'], reverse=True) - threads = getLastAge(limit) if threads[0]['bumped'] > int(data_time): values['threads'] = threads else: diff --git a/cgi/post.py b/cgi/post.py index c53e8a1..96b7cab 100644 --- a/cgi/post.py +++ b/cgi/post.py @@ -1080,11 +1080,13 @@ def cut_msg(message, limit): def getLastAge(board_type, limit): threads = [] + sql = "SELECT posts.id, boards.name AS board_name, boards.subname AS board_abr, board_type, boards.dir, timestamp, bumped, length, CASE WHEN posts.subject = boards.subject THEN posts.message ELSE posts.subject END AS content FROM posts INNER JOIN boards ON boardid = boards.id WHERE parentid = 0 AND IS_DELETED = 0 AND boards.secret = 0 AND posts.locked < 3" + if board_type in [0, 1]: - sql = "SELECT posts.id, boards.name AS board_fulln, boards.subname AS board_name, board_type, boards.dir, timestamp, length, CASE WHEN posts.subject = boards.subject THEN posts.message ELSE posts.subject END AS content FROM posts INNER JOIN boards ON boardid = boards.id WHERE parentid = 0 AND IS_DELETED = 0 AND boards.secret = 0 AND posts.locked < 3 AND boards.board_type = %s ORDER BY bumped DESC LIMIT %s" + sql += " AND boards.board_type = %s ORDER BY bumped DESC LIMIT %s" threads = FetchAll(sql, (board_type, limit) ) else: - sql = "SELECT posts.id, boards.name AS board_fulln, boards.subname AS board_name, board_type, boards.dir, timestamp, length, CASE WHEN posts.subject = boards.subject THEN posts.message ELSE posts.subject END AS content FROM posts INNER JOIN boards ON boardid = boards.id WHERE parentid = 0 AND IS_DELETED = 0 AND boards.secret = 0 AND posts.locked < 3 ORDER BY bumped DESC LIMIT %s" + sql += " ORDER BY bumped DESC LIMIT %s" threads = FetchAll(sql, (limit,) ) for post in threads: @@ -1098,7 +1100,7 @@ def getLastAge(board_type, limit): return threads def getNewPosts(limit, rss=False): - sql = "SELECT posts.id, parentid, boardid, boards.name AS board_fulln, board_type, boards.dir, timestamp, posts.message FROM posts INNER JOIN boards ON posts.boardid = boards.id WHERE IS_DELETED = 0 AND boards.secret = 0 ORDER BY timestamp DESC LIMIT %s" + sql = "SELECT posts.id, parentid, boardid, boards.name AS board_name, board_type, boards.dir, timestamp, posts.message FROM posts INNER JOIN boards ON posts.boardid = boards.id WHERE IS_DELETED = 0 AND boards.secret = 0 ORDER BY timestamp DESC LIMIT %s" posts = FetchAll(sql, (limit,)) for post in posts: @@ -1106,6 +1108,7 @@ def getNewPosts(limit, rss=False): if rss: post['timestamp_formatted'] = datetime.datetime.fromtimestamp(post['timestamp']).strftime('%Y-%m-%dT%H:%M:%S%Z') + post['content'] = cut_msg(post['message'], 100) if post['board_type'] == 1: post['url'] = '/%s/read/%d/l10' % (post['dir'], getParentTimestamp(post) or post['timestamp']) @@ -1116,7 +1119,7 @@ def getNewPosts(limit, rss=False): def getNewThreads(limit): threads = [] - sql = "SELECT posts.id, boards.name AS board_fulln, boards.subname AS board_name, board_type, boards.dir, timestamp, CASE WHEN posts.subject = boards.subject THEN posts.message ELSE posts.subject END AS content FROM posts INNER JOIN boards ON boardid = boards.id WHERE parentid = 0 AND IS_DELETED = 0 AND boards.secret = 0 AND boards.id <> 13 AND posts.locked = 0 ORDER BY timestamp DESC LIMIT %s" + sql = "SELECT posts.id, boards.name AS board_name, boards.subname AS board_abr, board_type, boards.dir, timestamp, CASE WHEN posts.subject = boards.subject THEN posts.message ELSE posts.subject END AS content FROM posts INNER JOIN boards ON boardid = boards.id WHERE parentid = 0 AND IS_DELETED = 0 AND boards.secret = 0 AND boards.id <> 13 AND posts.locked = 0 ORDER BY timestamp DESC LIMIT %s" threads = FetchAll(sql, (limit,) ) for post in threads: @@ -1158,7 +1161,7 @@ def regenerateHome(): f.close() if Settings.ENABLE_RSS: - rss_rendered = renderTemplate('home.rss', {'posts': getNewPosts(10, True)} ) + rss_rendered = renderTemplate('feed.rss', {'posts': getNewPosts(10, True)} ) f = open(Settings.HOME_DIR + "bai.rss", "w") try: f.write(rss_rendered) diff --git a/cgi/templates/feed.rss b/cgi/templates/feed.rss new file mode 100644 index 0000000..dfce578 --- /dev/null +++ b/cgi/templates/feed.rss @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8" ?>
+<rss version="2.0">
+ <channel>
+ <title>Bienvenido a Internet BBS/IB</title>
+ <link>https://bienvenidoainternet.org/</link>
+ <description>El BBS/IB más activo de la esfera hispana.</description>
+ <language>es</language>
+ <webMaster>burocracia@bienvenidoainternet.org (Staff ★)</webMaster>
+ <image>
+ <url>https://bienvenidoainternet.org/rss_logo.png</url>
+ <title>Bienvenido a Internet BBS/IB</title>
+ <link>https://bienvenidoainternet.org/</link>
+ <width>144</width>
+ <height>144</height>
+ </image>
+ <?py for post in posts: ?>
+ <item>
+ <title>${post['board_name']}: #{post['content']}</title>
+ <pubDate>${post['timestamp_formatted']}</pubDate>
+ <link>https://bienvenidoainternet.org#{post['url']}</link>
+ <description>${post['content']}</description>
+ </item>
+ <?py #endfor ?>
+ </channel>
+</rss>
diff --git a/cgi/templates/home.html b/cgi/templates/home.html index d60b719..b5d932b 100644 --- a/cgi/templates/home.html +++ b/cgi/templates/home.html @@ -73,25 +73,29 @@ </tr> <tr> <td rowspan="2" class="tab"> - <h2 id="newposts">Hilos activos <div id="counter"></div></h2> - <h4>BBS</h4> - <div id="postlist" class="threads"> + <h2 id="newposts">Hilos activos <a href="bai.rss" target="_blank"><img src="/feed.png" style="width:12px;height:12px;"></a></h2> + <h4>BBS</h4> + <div id="postlistbbs" class="threads"> <?py for thread in latest_age_bbs: ?> - <a href="${thread['url']}" class="thread"><span class="brd">[${thread['board_name']}:${'%03d' % thread['length']}]</span> #{thread['content']}</a> + <a href="${thread['url']}" class="thread"><span class="brd">[${thread['board_abr']}:${'%03d' % thread['length']}]</span> #{thread['content']}</a> <?py #endfor ?> - </div> - <hr /> - <h4>IB</h4> - <div id="postlist" class="threads"> + </div> + <hr /> + <h4>IB</h4> + <div id="postlistib" class="threads"> <?py for thread in latest_age_ib: ?> - <a href="${thread['url']}" class="thread"><span class="brd">[${thread['board_name']}:${'%03d' % thread['length']}]</span> #{thread['content']}</a> + <a href="${thread['url']}" class="thread"><span class="brd">[${thread['board_abr']}:${'%03d' % thread['length']}]</span> #{thread['content']}</a> <?py #endfor ?> + </div> + <hr /> + <div id="ctrl"><a id="refresh" href="/home.html">Actualizar</a> + <span> + <input id="autorefresh" type="checkbox" /><label for="autorefresh">Automático</label> + <span id="counter"></span> + </span> + <span><input id="autosound" type="checkbox" checked="checked" disabled="disabled" /><label for="autosound">Sonido</label><audio id="machina"><source src="msn.ogg" type="audio/ogg" /></audio></span> + <input type="hidden" name="limit" id="limit" value="#{latest_age_num}" /> </div> - <hr /> - <div id="ctrl"><a id="refresh" href="/home.html">Actualizar</a> - <span><input id="autorefresh" type="checkbox" /><label for="autorefresh">Automático</label><audio id="machina"><source src="msn.ogg" type="audio/ogg" /></audio></span> - <span><input id="autosound" type="checkbox" checked="checked" disabled="disabled" /><label for="autosound">Sonido</label><input type="hidden" name="limit" id="limit" value="#{latest_age_num}" /></span> - <a href="bai.rss"><img src="/rss.png" style="width:12px;height:12px;margin-right:4px;">RSS</a></div> </td> <td id="newstab" class="tab"> <h2>Blotter</h2> @@ -107,7 +111,7 @@ <h2>Hilos nuevos</h2> <div id="new_ib" class="threads"> <?py for thread in new_threads: ?> - <a href="${thread['url']}" class="thread"><span class="brd">[${thread['board_name']}]</span> #{thread['content']}</a> + <a href="${thread['url']}" class="thread"><span class="brd">[${thread['board_abr']}]</span> #{thread['content']}</a> <?py #endfor ?> </div> </td> diff --git a/cgi/templates/home.rss b/cgi/templates/home.rss deleted file mode 100644 index 1e5f552..0000000 --- a/cgi/templates/home.rss +++ /dev/null @@ -1,25 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" ?>
-<rss version="2.0">
- <channel>
- <title>Bienvenido a Internet BBS/IB</title>
- <link>https://bienvenidoainternet.org/</link>
- <description>El BBS/IB más activo de la esfera hispana.</description>
- <language>es</language>
- <webMaster>burocracia@bienvenidoainternet.org (Staff ★)</webMaster>
- <image>
- <url>https://bienvenidoainternet.org/rss_logo.png</url>
- <title>Bienvenido a Internet BBS/IB</title>
- <link>https://bienvenidoainternet.org/</link>
- <width>144</width>
- <height>144</height>
- </image>
-<?py for post in posts: ?>
- <item>
- <title>${post['board_fulln']}: #{post['content']}</title>
- <pubDate>${post['timestamp_formatted']}</pubDate>
- <link>https://bienvenidoainternet.org#{post['url']}</link>
- <description>${post['message']}</description>
- </item>
-<?py #endfor ?>
- </channel>
-</rss>
diff --git a/cgi/templates/mobile/latest.html b/cgi/templates/mobile/latest.html index 50fae25..8156e69 100644 --- a/cgi/templates/mobile/latest.html +++ b/cgi/templates/mobile/latest.html @@ -8,13 +8,13 @@ <div class="list"> <h3 style="text-align:center">BBS</h3> <?py for thread in latest_age_bbs: ?> - <a href="/cgi/mobileread${thread['url']}">#{thread['content']}<div>${thread['board_fulln']} <span>R:<span>#{int(thread['length'])-1}</span></span></div></a> + <a href="/cgi/mobileread${thread['url']}">#{thread['content']}<div>${thread['board_name']} <span>R:<span>#{int(thread['length'])-1}</span></span></div></a> <?py #endfor ?> </div> <div class="list"> <h3 style="text-align:center">IB</h3> <?py for thread in latest_age_ib: ?> - <a href="/cgi/mobileread${thread['url']}">#{thread['content']}<div>${thread['board_fulln']} <span>R:<span>#{int(thread['length'])-1}</span></span></div></a> + <a href="/cgi/mobileread${thread['url']}">#{thread['content']}<div>${thread['board_name']} <span>R:<span>#{int(thread['length'])-1}</span></span></div></a> <?py #endfor ?> </div> </body> diff --git a/cgi/templates/mobile/newest.html b/cgi/templates/mobile/newest.html index 37fd67f..76da88b 100644 --- a/cgi/templates/mobile/newest.html +++ b/cgi/templates/mobile/newest.html @@ -7,8 +7,8 @@ <div class="bar"><a href="//m.bienvenidoainternet.org">Secciones</a><a href="/cgi/mobilehome">Hilos activos</a><a href="/cgi/mobilenewest" class="sel">Nuevos hilos</a></div> <div class="list"> <?py for thread in newthreads: ?> - <a href="/cgi/mobileread${thread['url']}">#{thread['content']}<div>${thread['board_fulln']}</div></a> + <a href="/cgi/mobileread${thread['url']}">#{thread['content']}<div>${thread['board_name']}</div></a> <?py #endfor ?> </div> </body> -</html>
\ No newline at end of file +</html> diff --git a/cgi/weabot.py b/cgi/weabot.py index 48f19f5..3ecf0f3 100755 --- a/cgi/weabot.py +++ b/cgi/weabot.py @@ -235,8 +235,8 @@ class weabot(object): self.output = renderTemplate('txt_newthread.html', {}, True) elif path_split[1] == "mobilehome": OpenDb() - latest_age_ib = getLastAge(0, Settings.HOME_LASTPOSTS) - latest_age_bbs = getLastAge(1, Settings.HOME_LASTPOSTS) + latest_age_ib = getLastAge(0, 10) + latest_age_bbs = getLastAge(1, 10) for threads in latest_age_ib: content = threads['url'] content = content.replace('/read/', '/') @@ -261,8 +261,7 @@ class weabot(object): content = content.replace('.html', '') threads['url'] = content caught = True - self.output = renderTemplate( - 'newest.html', {'newthreads': newthreads}, True) + self.output = renderTemplate('newest.html', {'newthreads': newthreads}, True) elif path_split[1] == "mobileread": OpenDb() board = setBoard(path_split[2]) diff --git a/static/js/autorefresh.js b/static/js/autorefresh.js index af61c8b..91ac05c 100644 --- a/static/js/autorefresh.js +++ b/static/js/autorefresh.js @@ -252,7 +252,7 @@ function counter() { loadJSON(); } else { refreshTime--; - document.getElementById("counter").innerHTML = refreshTime + 1; + document.getElementById("counter").textContent = refreshTime + 1; } } @@ -343,7 +343,7 @@ function startCounter() { function stopCounter(str) { clearInterval(refreshInterval); - document.getElementById("counter").innerHTML = str; + document.getElementById("counter").textContent = str; } function autoRefresh(e) { diff --git a/static/js/home.js b/static/js/home.js index 32ade31..e395638 100644 --- a/static/js/home.js +++ b/static/js/home.js @@ -1,35 +1,6 @@ -console.log("%c¡Es calidad BaI!", "font-size: 32px; font-weight: bold;"); - -function set_stylesheet(styletitle) { - opcs.style = styletitle; - parse(); - var links = document.getElementsByTagName("link"); - var found = false; - for (var i = 0; i < links.length; i++) { - var rel = links[i].getAttribute("rel"); - var title = links[i].getAttribute("title"); - if (rel.indexOf("style") != -1 && title) { - links[i].disabled = true; // IE needs this to work. IE needs to die. - if (styletitle == title) { - links[i].disabled = false; - found = true; - } - } - } -} - -function get_active_stylesheet() { - var links = document.getElementsByTagName("link"); - for (var i = 0; i < links.length; i++) { - var rel = links[i].getAttribute("rel"); - var title = links[i].getAttribute("title"); - if (rel.indexOf("style") != -1 && title && !links[i].disabled) return title; - } - return null; -} +console.log("%c¡Es calidad BaI!", "font-size:25px; font-weight:bold; text-decoration:underline; font-style:italic; text-decoration-line:underline overline;"); function check_news() { - console.log("check_news"); var last_t = opcs.last; var items = document.getElementsByClassName("ni"); var dates = document.getElementsByClassName("ni-d"); @@ -39,14 +10,25 @@ function check_news() { dates[i].innerHTML = '<img src="/new.gif" style="width:18px;height:7px;"><br />' + dates[i].innerHTML; + } else { + break; } opcs.last = (Date.now() / 1000) | 0; parse(); } +function zeropad(n, w) { + var n = String(n); + if (n.length >= w) { + return n; + } else { + return (String(0).repeat(w) + n).slice(n.length); + } +} + var lastTime = 0; var refreshInterval; -var refreshMaxTime = 30; +var refreshMaxTime = 10; var refreshTime; var unread = {}; var last_threads = 0; @@ -56,10 +38,8 @@ var http_request = new XMLHttpRequest(); function loadJSON() { stopCounter("..."); var data_file = - "/cgi/api/lastage?time=" + - lastTime + - "&limit=" + - document.getElementById("limit").value; + "/cgi/api/lastage?time=" + lastTime + + "&limit=" + document.getElementById("limit").value; http_request.open("GET", data_file, true); http_request.send(); } @@ -72,11 +52,19 @@ function setRead(threadId) { } function updatePostList(threads, serverTime) { - if (refreshMaxTime <= 120) refreshMaxTime += 5; + if (refreshMaxTime >= 120 && refreshMaxTime <= 270) { + refreshMaxTime += 30; + } else if (refreshMaxTime >= 60 && refreshMaxTime < 120) { + refreshMaxTime += 10; + } else if (refreshMaxTime < 60) { + refreshMaxTime += 5; + } + var arrayLength = threads.length; if (!arrayLength) return; - html = ""; + divbbs = ""; + divib = ""; last_threads = threads; last_serverTime = serverTime; @@ -87,63 +75,76 @@ function updatePostList(threads, serverTime) { for (var i = 0; i < arrayLength; i++) { thread = threads[i]; + if (thread.bumped >= lastTime) { unread[thread.id] = true; - news.push("- " + thread.board_fulln + ": " + thread.content); + news.push(thread.board_name + ": " + thread.content); new_unread = true; } - if (unread[thread.id]) html += '<span class="new">'; - html += - '<a href="' + - thread.url + - '" class="thread" data-brd="' + - thread.board_fulln + - '" data-unix="' + - thread.timestamp + - '" data-last="' + - thread.bumped + - '" data-img="' + - thread.thumb + - '"><span class="brd">[' + - thread.board_name + - ']</span> <span class="cont">' + - thread.content + - '</span> <span class="rep">(' + - thread.length + - ")</span></a>"; + + html = + '<a href="' + thread.url + '" class="thread">' + + '<span class="brd">[' + thread.board_abr + ':' + zeropad(thread.length, 3) + ']</span> ' + + thread.content + '</a>' + if (unread[thread.id]) { - html += "</span>"; + html = '<span class="new">' + html + "</span>"; newposts++; } + + if (thread.board_type == 1) { + divbbs += html; + } else { + divib += html; + } } - if (newposts) newTitle = "(" + newposts + ") " + newTitle; + + if (newposts) { + newTitle = "(" + newposts + ") " + newTitle; + } + if (new_unread) { - document.getElementById("newposts").style = "color:red"; notif( "Bienvenido a Internet BBS/IB", - "Hay nuevos mensajes:\n" + news.join("\n") + "Hay nuevos mensajes en:\n" + news.join("\n") ); + refreshMaxTime = 10; + if (document.getElementById("autosound").checked) { document.getElementById("machina").volume = 0.6; document.getElementById("machina").play(); } } - window.parent.document.title = newTitle; + document.title = newTitle; - document.getElementById("postlist").innerHTML = html; + window.addEventListener('focus', markRead); + + document.getElementById("postlistbbs").innerHTML = divbbs; + document.getElementById("postlistib").innerHTML = divib; +} + +function markRead() { + document.title = bainame; + window.removeEventListener('focus', markRead); } function notif(title, msg) { - var n = new Notification(title, { body: msg }); - setTimeout(n.close.bind(n), 10000); + if (Notification.permission === "granted") { + var n = new Notification(title, { + body: msg, + icon: 'https://bienvenidoainternet.org/favicon.ico', + }); + setTimeout(n.close.bind(n), 5000); + } } function counter() { - if (refreshTime < 1) loadJSON(); - else { + if (refreshTime < 1) { + loadJSON(); + } else { refreshTime--; - document.getElementById("counter").innerHTML = "– " + (refreshTime + 1); + document.getElementById("counter").textContent = "(" + (refreshTime + 1) + ")"; } } @@ -155,18 +156,26 @@ function startCounter() { function stopCounter(str) { clearInterval(refreshInterval); - document.getElementById("counter").innerHTML = str; + document.getElementById("counter").textContent = str; } function autoRefresh(e) { if (chk.checked) { if (chk_snd) chk_snd.disabled = false; - Notification.requestPermission(); + + if (!("Notification" in window)) { + console.log("Alerta: Este navegador no soporta notificaciones."); + } else { + Notification.requestPermission(); + } + lastTime = Math.floor(Date.now() / 1000); refreshTime = refreshMaxTime; startCounter(); } else { - if (chk_snd) chk_snd.disabled = true; + if (chk_snd) { + chk_snd.disabled = true; + } stopCounter(""); } } @@ -177,7 +186,9 @@ http_request.onreadystatechange = function() { if (jsonObj.state == "success") { updatePostList(jsonObj.threads, jsonObj.time); lastTime = jsonObj.time; - if (chk.checked) startCounter(); + if (chk.checked) { + startCounter(); + } } } }; @@ -187,49 +198,38 @@ function parse() { } document.addEventListener("DOMContentLoaded", function() { - console.log("1"); - window.parent.document.title = document.getElementsByTagName( - "title" - )[0].textContent; + var bainame = "Bienvenido a Internet BBS/IB"; if (localStorage.hasOwnProperty("home")) opcs = JSON.parse(localStorage.getItem("home")); else { - opcs = { style: "IB", auto: false, sound: false, last: 0 }; + opcs = { auto: false, sound: false, last: 0 }; parse(); } - //set_stylesheet(opcs.style); - console.log("2"); - - //var css = document.getElementById("change_style").getElementsByTagName("a"); - //for (var j = 0; j < css.length; j++) { - // css[j].addEventListener("click", function(e) { - // e.preventDefault(); - // set_stylesheet(this.textContent); - // }); - //} - console.log("2a"); + + check_news(); + document.getElementById("autorefresh").addEventListener("click", function(e) { opcs.auto = !opcs.auto; autoRefresh(); parse(); }); - console.log("2b"); document.getElementById("autosound").addEventListener("click", function(e) { opcs.sound = !opcs.sound; parse(); }); - console.log("3"); - check_news(); - console.log("4"); chk = document.getElementById("autorefresh"); chk_snd = document.getElementById("autosound"); if (opcs.auto) { chk.checked = true; autoRefresh(); - } else chk.checked = false; - if (opcs.sound) chk_snd.checked = true; - else chk_snd.checked = false; - console.log("5"); + } else { + chk.checked = false; + } + if (opcs.sound) { + chk_snd.checked = true; + } else { + chk_snd.checked = false; + } }); |