From 5050ca1d4c3569e91ffb65795a9585990290015a Mon Sep 17 00:00:00 2001 From: SouthFox Date: Mon, 6 Jan 2025 00:03:59 +0800 Subject: [PATCH 1/5] [feat] add utils.hy --- utils.hy | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 utils.hy diff --git a/utils.hy b/utils.hy new file mode 100644 index 0000000..2b248f9 --- /dev/null +++ b/utils.hy @@ -0,0 +1,23 @@ +#!/usr/bin/env hy +(import requests) + +(import sys) +(import os) +(import json) +(import getopt) +(import re) +(import random) +(import datetime [datetime]) + + +(defn get-post-files [] + (setv markdown-files []) + + (for [[root dirs files] (os.walk "source") + -file files] + (let [file (os.path.join root -file)] + (when (and (in "posts" file) (file.endswith ".md")) + (markdown-files.append file)))) + (print markdown-files)) + +(get-post-files) From 1bcd7de0d09c8fa9af78811479a0ac8195ca566e Mon Sep 17 00:00:00 2001 From: SouthFox Date: Mon, 6 Jan 2025 16:56:24 +0800 Subject: [PATCH 2/5] [feat] build font subset file --- utils.hy | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/utils.hy b/utils.hy index 2b248f9..29e9d25 100644 --- a/utils.hy +++ b/utils.hy @@ -1,6 +1,7 @@ #!/usr/bin/env hy (import requests) +(import argparse) (import sys) (import os) (import json) @@ -18,6 +19,24 @@ (let [file (os.path.join root -file)] (when (and (in "posts" file) (file.endswith ".md")) (markdown-files.append file)))) - (print markdown-files)) + (return markdown-files)) -(get-post-files) +(defn subset-font-file [post-files] + (let [str-set #{}] + (for [f post-files] + (with [o (open f "r" :encoding "utf-8")] + (for [w (o.read)] + (str-set.add w)))) + + (print f"{(len str-set)} characters in all files.") + (print (.join "" str-set)) + (with [f (open "strdb.txt" "w")] + (f.write (.join "" str-set))))) + +(setv parser (argparse.ArgumentParser)) +(parser.add_argument "-d" :dest "deploy" :action "store_true") +(setv args (parser.parse_args)) + +(when args.deploy + (let [post-files (get-post-files)] + (subset-font-file post-files))) From de64b77739f59bddcd5b066f02d2e821b179cd99 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Thu, 9 Jan 2025 15:14:17 +0800 Subject: [PATCH 3/5] [feat] add new post github discussion --- utils.hy | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/utils.hy b/utils.hy index 29e9d25..a7de58e 100644 --- a/utils.hy +++ b/utils.hy @@ -10,6 +10,19 @@ (import random) (import datetime [datetime]) +(defmacro -> [head #* args] + (setv ret head) + (for [node args] + (setv ret (if (isinstance node hy.models.Expression) + `(~(get node 0) ~ret ~@(rest node)) + `(~node ~ret)))) + ret) + +(defmacro lfor-> [name head #* rest] + (setv ret head) + (for [node rest] + (setv ret `(lfor ~name ~ret ~node))) + ret) (defn get-post-files [] (setv markdown-files []) @@ -33,10 +46,35 @@ (with [f (open "strdb.txt" "w")] (f.write (.join "" str-set))))) +(defn add-github-discussion [post-files] + (let [repo-id "MDEwOlJlcG9zaXRvcnkyMjg3NDM0MjQ=" + category-id "DIC_kwDODaJZAM4CA7bf" + url "https://api.github.com/graphql" + header {"Authorization" f"Bearer {(os.environ.get "DISCUSSIONS_TOKEN")}"} + query-data {"query" "query{repository(owner: \"southfox-d\", name: \"southfox-d.github.io\"){id discussions(first:100, categoryId: \"DIC_kwDODaJZAM4CA7bf\"){nodes{title}}}}"} + query-request (requests.post url :headers header :json query-data)] + (query-request.raise_for_status) + (let [current-year (str (. (datetime.now) year)) + discussion-post (-> (r.json) :data :repository :discussions :nodes) + discussion-post-titles (lfor x discussion-post (:title x)) + local-post-titles (lfor-> it post-files + (. (it.split "_posts/") [-1]) + (it.strip ".md") + (+ it "/"))] + (for [post-title local-post-titles] + (when (not-in current-year post-title) + (continue)) + (when (not-in post-title discussion-post-titles) + (let [create-data {"query" f"mutation{{createDiscussion(input: {{repositoryId: \"{repo-id}\", categoryId: \"{category-id}\", body: \"{(+ "https://blog.southfox.me/" post-title)}\", title: \"{post-title}\" }}) {{discussion {{id}}}}}}"} + create-request (requests.post url :headers header :json create-data)] + (create-request.raise_for_status) + (print f"Create post {post-title} discussion!"))))))) + (setv parser (argparse.ArgumentParser)) (parser.add_argument "-d" :dest "deploy" :action "store_true") (setv args (parser.parse_args)) (when args.deploy (let [post-files (get-post-files)] - (subset-font-file post-files))) + (subset-font-file post-files) + (add-github-discussion post-files))) From e67942d8c3b3875118bbcf28e9862ecdf319f1aa Mon Sep 17 00:00:00 2001 From: SouthFox Date: Thu, 9 Jan 2025 16:02:05 +0800 Subject: [PATCH 4/5] [feat] backup ipfs media file --- utils.hy | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/utils.hy b/utils.hy index a7de58e..28b066c 100644 --- a/utils.hy +++ b/utils.hy @@ -70,11 +70,48 @@ (create-request.raise_for_status) (print f"Create post {post-title} discussion!"))))))) +(defn download-ipfs-img [img-list] + (let [ipfs-gateway ["https://ipfs.io/ipfs/" "https://dweb.link/ipfs/"]] + (for [img-name img-list] + (when (= "" img-name) + (continue)) + (setv img-url (+ (random.choice ipfs-gateway) img-name)) + (setv r (requests.get img-url)) + (r.raise_for_status) + (with [f (open (+ "./newimg/" img-name) "wb")] + (f.write (. r content)))))) + +(defn backup-ipfs-img [post-files] + (if (os.path.exists "imgList.json") + (setv img-json (with [f (open "imgList.json" "r" :encoding "utf-8")] + (json.load f))) + (setv img-json {"img" []})) + (setv now-img-list []) + + (for [post-file post-files] + (with [f (open post-file "r" :encoding "utf-8")] + (let [post-text (f.read) + img-result (re.findall r"!\[(.*?)\]\((.*?)\)" post-text)] + (for [[_ img-url] img-result] + (when (= "" img-url) + (continue)) + (when (in "ipfs" img-url) + (now-img-list.append (. (img_url.split "/") [-1]))))))) + (setv download-need-img (list (. (set now-img-list) (difference (set (:img img-json)))))) + (when download-need-img + (with [f (open "newimg/imgList.json" "w" :encoding "utf-8")] + (json.dump {"img" (sorted now-img-list)} f)) + (download-ipfs-img download-need-img))) + (setv parser (argparse.ArgumentParser)) (parser.add_argument "-d" :dest "deploy" :action "store_true") +(parser.add_argument "-b" :dest "backup" :action "store_true") (setv args (parser.parse_args)) -(when args.deploy - (let [post-files (get-post-files)] +(let [post-files (get-post-files)] + (when args.deploy (subset-font-file post-files) - (add-github-discussion post-files))) + (add-github-discussion post-files)) + (when args.backup + (os.makedirs "newimg" :exist_ok True) + (backup-ipfs-img post-files))) From 5a7fe1467fe58d39500876ebcf1de03bde810cf2 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Thu, 9 Jan 2025 16:05:19 +0800 Subject: [PATCH 5/5] [ci] use hy utils script --- .github/workflows/backup-img.yml | 11 +++++------ .github/workflows/deploy.yml | 19 +++++++------------ 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/.github/workflows/backup-img.yml b/.github/workflows/backup-img.yml index 608ddd5..8fce352 100644 --- a/.github/workflows/backup-img.yml +++ b/.github/workflows/backup-img.yml @@ -13,23 +13,22 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Set up Python 3.9 - uses: actions/setup-python@v2 + - name: Set up Python 3.13 + uses: actions/setup-python@v5 with: - python-version: 3.9 + python-version: 3.13 - name: Install dependencies run: | sudo apt-get install wget - python -m pip install --upgrade pip - python -m pip install fonttools brotli requests + python -m pip install fonttools brotli requests hy git config --global user.name "SouthFox-D" git config --global user.email "southfoxdreamer@gmail.com" - name: Check Img run: | wget https://raw.githubusercontent.com/SouthFox-D/blog_img/main/imgList.json - python3 utils.py -b + hy utils.hy -b - name: Backup env: diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 95c9fd2..5f3a0a5 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -13,20 +13,15 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Set up Python 3.9 - uses: actions/setup-python@v2 + - name: Set up Python 3.13 + uses: actions/setup-python@v5 with: - python-version: 3.9 - - - name: Set up Node.js - uses: actions/setup-node@v1 - with: - node-version: 17 - + python-version: 3.13 + - name: Install dependencies run: | - python -m pip install --upgrade pip - python -m pip install fonttools brotli requests + sudo apt-get install wget + python -m pip install fonttools brotli requests hy npm install -g hexo-cli npm install git config --global user.name "SouthFox-D" @@ -39,7 +34,7 @@ jobs: run: | hexo clean hexo generate - python3 utils.py -d + hy utils.hy -d pyftsubset Zpix.ttf --text-file=strdb.txt fonttools ttLib.woff2 compress -o Zpix.woff2 Zpix.subset.ttf mv Zpix.woff2 ./public/css/fonts