From f7a559aeb78a5c5dfedfce4df1edfff8a76039b5 Mon Sep 17 00:00:00 2001 From: Bhupesh-V Date: Sun, 7 Jan 2024 20:03:12 +0530 Subject: [PATCH] send data from script --- .github/workflows/flair-usage-stats.yml | 11 ++++------- user-flair-usage/main.py | 17 ++++++++--------- user-flair-usage/requirements.txt | 2 ++ 3 files changed, 14 insertions(+), 16 deletions(-) create mode 100644 user-flair-usage/requirements.txt diff --git a/.github/workflows/flair-usage-stats.yml b/.github/workflows/flair-usage-stats.yml index ca223de..d49eebf 100644 --- a/.github/workflows/flair-usage-stats.yml +++ b/.github/workflows/flair-usage-stats.yml @@ -23,12 +23,9 @@ jobs: REDDIT_CLIENT_SECRET: ${{ secrets.REDDIT_CLIENT_SECRET }} REDDIT_PASSWORD: ${{ secrets.REDDIT_PASSWORD }} REDDIT_USERNAME: ${{ secrets.REDDIT_USERNAME }} + DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} run: | cd user-flair-usage - python_output=$(python main.py) - echo "::set-output name=python_output::$python_output" - - name: Send Discord message - env: - WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} - run: | - curl -X POST -H "Content-Type: application/json" -d "{\"content\":\"$python_output\"}" $WEBHOOK_URL + python -m pip install --upgrade pip + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + python main.py \ No newline at end of file diff --git a/user-flair-usage/main.py b/user-flair-usage/main.py index cde6eae..aa71ce4 100644 --- a/user-flair-usage/main.py +++ b/user-flair-usage/main.py @@ -2,11 +2,13 @@ import praw from collections import defaultdict import os import json +import requests client_id = os.environ["REDDIT_CLIENT_ID"] client_secret = os.environ["REDDIT_CLIENT_SECRET"] reddit_pass = os.environ["REDDIT_PASSWORD"] username = os.environ["REDDIT_USERNAME"] +webhook_url = os.environ["DISCORD_WEBHOOK_URL"] user_agent = 'User Flair Usage' sub = "developersIndia" @@ -63,9 +65,6 @@ for flair, count in sorted_flairs: else: old_available_flair_count[flair] += count -# Print the usage count of each available flair -# for flair, count in available_flair_count.items(): -# print(f"Flair: {flair}, Usage Count: {count}") total_count = sum(available_flair_count.values()) old_flairs_total_count = sum(old_available_flair_count.values()) @@ -76,12 +75,12 @@ old_flairs_total_count = sum(old_available_flair_count.values()) # print(f"Total count of user-flairs: {total_count + emoji_flair_count + old_flairs_total_count}") data = { - 'Users with un-supported (old) text flairs': old_flairs_total_count, - 'Users with supported text flairs': total_count, - 'Users with emoji only flairs': emoji_flair_count, - 'Total count of user-flairs': total_count + emoji_flair_count + old_flairs_total_count + 'Users with un-supported (old) text flairs': f"**{old_flairs_total_count}**", + 'Users with supported text flairs': f"**{total_count}**", + 'Users with emoji only flairs': f"**{emoji_flair_count}**", + 'Total count of user-flairs': f"**{total_count + emoji_flair_count + old_flairs_total_count}**" } -formatted_data = json.dumps(data, indent=4) # Format the data as JSON with indentation +formatted_data = "\n".join([f"{k}: {v}" for k, v in data.items()]) # Format the data as a string with each item on a new line -print(formatted_data) \ No newline at end of file +requests.post(webhook_url, json={"content": formatted_data}) \ No newline at end of file diff --git a/user-flair-usage/requirements.txt b/user-flair-usage/requirements.txt new file mode 100644 index 0000000..51af7fe --- /dev/null +++ b/user-flair-usage/requirements.txt @@ -0,0 +1,2 @@ +praw +requests \ No newline at end of file