diff --git a/.env.example b/.env.example index e05042e..ff42a12 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ -URL="http://127.0.0.1:5000" # URL for rss/atom feed \ No newline at end of file +URL="http://127.0.0.1:5000" # URL for rss/atom feed +CHANNEL_LIST_TOKEN="xoxb-" # Used in channels.py \ No newline at end of file diff --git a/.gitignore b/.gitignore index 399446e..57e3136 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +channels.json templates/articles/ !templates/articles/loremipsum.html diff --git a/channels.py b/channels.py new file mode 100644 index 0000000..bf7341a --- /dev/null +++ b/channels.py @@ -0,0 +1,46 @@ +""" +Create a dict of channel_id: channel_name for scrapbook +""" + +from dotenv import load_dotenv +import requests +import json +import os + +load_dotenv() + +ACCESS_TOKEN = os.getenv("CHANNEL_LIST_TOKEN") + +headers = { + "Authorization": f"Bearer {ACCESS_TOKEN}", + "Content-Type": "application/json" +} + +params = { + "types": "public_channel", + "limit": 1000 +} + +channels_map = {} + +while True: + response = requests.get("https://slack.com/api/conversations.list", headers=headers, params=params) + data = response.json() + + if not data.get("ok"): + print("Error:", data.get("error")) + break + + for channel in data.get("channels", []): + channels_map[channel["id"]] = channel["name"] + + cursor = data.get("response_metadata", {}).get("next_cursor") + if cursor: + params["cursor"] = cursor + else: + break + +print(f"{len(channels_map)} public channels found") + +with open("channels.json", "w+") as f: + json.dump(channels_map, f) \ No newline at end of file diff --git a/main.py b/main.py index 31bd9d1..3df9f04 100644 --- a/main.py +++ b/main.py @@ -3,19 +3,25 @@ import re import os from flask import Flask, render_template, request, send_file, make_response from feedgen.feed import FeedGenerator +from flask_moment import Moment from dotenv import load_dotenv from routes.editor import editor_routes +from routes.scrapbook import sp_routes from classes import * load_dotenv() app = Flask(__name__) +moment = Moment(app) + app.register_blueprint(editor_routes) +app.register_blueprint(sp_routes) with open("articles.json", "r", encoding="utf-8") as f: articles_data = json.load(f) categories = {} # Category name:str -> [Article] articles = {} # ID:str = Article +# RSS Feed fg = FeedGenerator() fg.title("Mathias") fg.id("Mathias") @@ -24,6 +30,7 @@ fg.language("en") fg.link(href=os.getenv("URL")) fg.description("Blog RSS feed") +# Register articles for article_data in articles_data: category_name = article_data.get("category", "Uncategorized") category_list = categories.get(category_name, []) @@ -86,4 +93,4 @@ def article(article_id:str): return render_template(f"articles/{article.template}.html") if __name__ == "__main__": - app.run() + app.run(debug=True) diff --git a/requirements.txt b/requirements.txt index 32f5093..a75be84 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ flask feedgen +requests +Flask-Moment python-dotenv \ No newline at end of file diff --git a/routes/scrapbook.py b/routes/scrapbook.py new file mode 100644 index 0000000..df1d531 --- /dev/null +++ b/routes/scrapbook.py @@ -0,0 +1,61 @@ +""" +Display my hackclub scrapbook +see https://scrapbook.hackclub.com/about +""" + +from flask import Blueprint, render_template +from datetime import datetime +import requests +import time +import json +import re + +sp_routes = Blueprint('scrapbook', __name__, template_folder='templates') + +lastScrapbookUpdate = 0 +scrapbookPosts = [] + +try: + channels_maps = json.load(open('channels.json', 'r', encoding="utf-8")) +except: + print("channels.json not found") + channels_maps = {} + +@sp_routes.app_template_filter('formatDate') +def format_date(date_str): + date_obj = datetime.fromisoformat(date_str.replace('Z', '+00:00')) + return date_obj.strftime('%d/%m/%Y @ %Hh%M') + +@sp_routes.app_template_filter('formatContent') +def convert_slack_references(text): + # Convert channel references + channel_pattern = r'<#(C[A-Z0-9]+)\|>' + + def channel_replacement(match): + channel_id = match.group(1) + channel_name = channels_maps.get(channel_id, channel_id) + return f'#{channel_name}' + + url_pattern = r'<(http(?:|s):\/\/[a-zA-Z0-9\.\/_-]+)>' + def url_replacement(match): + url = match.group(1) + return f'{url}' + + result = re.sub(url_pattern, url_replacement, text) + result = re.sub(channel_pattern, channel_replacement, result) + + return result + +@sp_routes.route("/scrapbook") +def scrapbook(): + """Page linking to my scrapbook articles""" + global lastScrapbookUpdate, scrapbookPosts + if lastScrapbookUpdate+3600 < time.time(): + try: + r = requests.get("https://scrapbook.hackclub.com/api/users/mathias") + data = r.json() + scrapbookPosts = data["posts"] + lastScrapbookUpdate = time.time() + except: pass + + return render_template("scrapbook.html", posts=scrapbookPosts) \ No newline at end of file diff --git a/static/base.css b/static/base.css index 7171eda..dd32a1d 100644 --- a/static/base.css +++ b/static/base.css @@ -1,6 +1,5 @@ @import url("typography.css"); - /* Reset */ * { margin: 0; @@ -52,6 +51,13 @@ header h1 { background-color: #03396C; padding: 5px 3em 5px 0; text-align: right; + min-width: 100%; + color: #ffffff; +} + +#navbar a + a::before { + content: "// "; + padding: 0 0.1em; } /* Main Content */ @@ -60,25 +66,71 @@ header h1 { flex: 1; } +#main details div { + padding-left: 1em; + line-height: 115%; +} + +#main details:open { + padding-bottom: 1em; +} + +#main details summary { + cursor: pointer; + padding-bottom: 0.25em; +} + /* Footer */ #footer { margin-top: auto; + text-align: center; } + #footer-title { width: 100%; + min-width: 100%; background-color: #03396C; + color: #ffffff; text-align: center; padding: 0.5em 0; } + +#footer p { + padding-top: 0.5em; + padding-bottom: 0.5em; + line-height: 1.25; +} + +hr { + margin-bottom: 1em; +} + +/* Links */ +a:hover { + text-decoration: underline; +} + +nav a:hover { + text-decoration: none; +} + +a { + text-decoration: none; + color: #005B96; +} + +/* Media Queries */ @media (max-width: 768px) { html, body { min-height: 100dvh; } + #wrap { width: 100%; max-width: none; } - footer p{ + + footer p { margin-bottom: 0.25em; } diff --git a/static/typography.css b/static/typography.css index e93b18b..7409202 100644 --- a/static/typography.css +++ b/static/typography.css @@ -19,11 +19,12 @@ h1 a, h2 a, h3 a, h4 a, h5 a, h6 a { } /* Heading Hover Effects */ -h2 a:hover::before, -h3 a:hover::before, -h4 a:hover::before, -h5 a:hover::before, -h6 a:hover::before { +h1.chapter a:hover::before, +h2.chapter a:hover::before, +h3.chapter a:hover::before, +h4.chapter a:hover::before, +h5.chapter a:hover::before, +h6.chapter a:hover::before { content: "#"; position: absolute; left: -0.75em; @@ -36,26 +37,23 @@ h6 a:hover::before { margin-bottom: 0.75em; } +summary > small { + font-size: 0.5em; +} + time { font-family: "Montserrat", sans-serif; font-weight: bold; font-size: 75%; } -hr { - margin-bottom: 1em; -} - -/* Links */ -a { - text-decoration: none; - color: #005B96; +#main details summary { + font-family: "Montserrat", sans-serif; + font-weight: 500; } /* Navigation */ #navbar { - min-width: 100%; - color: #ffffff; text-transform: uppercase; font-family: "Montserrat", sans-serif; } @@ -64,25 +62,7 @@ a { color: inherit; } -#navbar a + a::before { - content: "// "; - padding: 0 0.1em; -} - /* Footer */ #footer-title { - padding: 5px 0; - min-width: 100%; - color: #ffffff; font-family: "Montserrat", sans-serif; -} - -#footer { - text-align: center; -} - -#footer p { - padding-top: 0.5em; - padding-bottom: 0.5em; - line-height: 1.25; } \ No newline at end of file diff --git a/templates/articles/loremipsum.html b/templates/articles/loremipsum.html index 7a2b118..8374a8b 100644 --- a/templates/articles/loremipsum.html +++ b/templates/articles/loremipsum.html @@ -5,11 +5,12 @@ {% endblock %} {% block content %} -
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla mattis, est et laoreet efficitur, nisi leo volutpat nibh, et rutrum erat urna vel nisl. Aenean sodales lorem quis dolor sodales, at convallis purus convallis. Nam interdum tincidunt nunc, sed molestie lectus. Integer a rhoncus enim, eu cursus lacus.
Donec dignissim consequat augue mattis ornare. Vivamus feugiat odio in sagittis consectetur. Proin nec suscipit dolor, ut consectetur nisi. Aliquam ut ex dapibus, volutpat justo sit amet, dignissim tellus. Curabitur placerat tempor neque congue sollicitudin. Praesent elementum in ligula id semper.
+ {% endblock %} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index ce673df..40e333b 100644 --- a/templates/base.html +++ b/templates/base.html @@ -6,6 +6,7 @@ {% block head %}{% endblock %} + {{ moment.include_moment() }}URL does not appear to be an image or video: {{ attachment }}
+ {% endif %} + {% endfor %} +