From 464a97b3f2557958212d47a7f40e6c068379b1b3 Mon Sep 17 00:00:00 2001 From: ctmbl Date: Fri, 17 Nov 2023 15:46:07 +0100 Subject: [PATCH] Add new_article.py script that parse files and send notification --- .github/workflows/build_and_deploy.yml | 9 ++++++--- scripts/new_article.py | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 scripts/new_article.py diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index 16565ec..c6454dc 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -77,13 +77,12 @@ jobs: runs-on: ubuntu-latest steps: # Finally notify of the new article (if any) on the iScsc discord server - - name: 📨 Notify on Discord + - name: 📑 Get changed files uses: jitterbit/get-changed-files@v1 id: files with: format: space-delimited - #token: ${{ secrets.GITHUB_TOKEN }} - - name: Printing + - name: Print changed files... run: | echo "All:" echo "${{ steps.files.outputs.all }}" @@ -97,3 +96,7 @@ jobs: echo "${{ steps.files.outputs.modified }}" echo "Added+Modified:" echo "${{ steps.files.outputs.added_modified }}" + - name: 📨 Notify on Discord + run: | + python3 -m pip install requests PyYAML + python3 ./scripts/new_article.py ${{ steps.files.outputs.added }} diff --git a/scripts/new_article.py b/scripts/new_article.py new file mode 100644 index 0000000..0eb2d89 --- /dev/null +++ b/scripts/new_article.py @@ -0,0 +1,25 @@ +import sys +import re +import requests +import yaml + +for file_path in sys.argv[1:]: + # Check that this is an article file + if re.match("^src/content/posts/.+\.md$", file_path): + # Read YAML Header + with open(file_path, "r") as f: + raw_txt = f.read() + data = yaml.safe_load(raw_txt.split("---")[1]) + + # Get rid of python objects, only keep basic types + for key in data: + if type(data[key]) not in [int, str, float, bool]: + data[key] = str(data[key]) + + # Add URL info + file_name = file_path.split("/")[-1][:-3] + data["url"] = f"https://iscsc.fr/posts/{file_name}" + + # Finally send Data + requests.post("https://iscsc.fr:8001/new-blog", json=data) + print(file_path, file_name, data)