From e7974f78565de54165d13e6fbb06654a01bfffcd 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 | 14 ++++++-------- scripts/new_article.py | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 8 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..68b7d4a 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -72,18 +72,12 @@ jobs: - name: 🚀 Upload run: | rsync --archive --stats --verbose --delete ./build/blog/* ${{ secrets.CI_USER_NAME }}@iscsc.fr:${{ secrets.STATIC_WEBSITE_PATH}} - - notify: - 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 +91,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..9adda8f --- /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("http://iscsc.fr:8001/new-blog", json=data) + print(file_path, file_name, data)