send data from script

This commit is contained in:
Bhupesh-V 2024-01-07 20:03:12 +05:30
parent a9cb37dbd3
commit f7a559aeb7
3 changed files with 14 additions and 16 deletions

View File

@ -23,12 +23,9 @@ jobs:
REDDIT_CLIENT_SECRET: ${{ secrets.REDDIT_CLIENT_SECRET }} REDDIT_CLIENT_SECRET: ${{ secrets.REDDIT_CLIENT_SECRET }}
REDDIT_PASSWORD: ${{ secrets.REDDIT_PASSWORD }} REDDIT_PASSWORD: ${{ secrets.REDDIT_PASSWORD }}
REDDIT_USERNAME: ${{ secrets.REDDIT_USERNAME }} REDDIT_USERNAME: ${{ secrets.REDDIT_USERNAME }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
run: | run: |
cd user-flair-usage cd user-flair-usage
python_output=$(python main.py) python -m pip install --upgrade pip
echo "::set-output name=python_output::$python_output" if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Send Discord message python main.py
env:
WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
run: |
curl -X POST -H "Content-Type: application/json" -d "{\"content\":\"$python_output\"}" $WEBHOOK_URL

View File

@ -2,11 +2,13 @@ import praw
from collections import defaultdict from collections import defaultdict
import os import os
import json import json
import requests
client_id = os.environ["REDDIT_CLIENT_ID"] client_id = os.environ["REDDIT_CLIENT_ID"]
client_secret = os.environ["REDDIT_CLIENT_SECRET"] client_secret = os.environ["REDDIT_CLIENT_SECRET"]
reddit_pass = os.environ["REDDIT_PASSWORD"] reddit_pass = os.environ["REDDIT_PASSWORD"]
username = os.environ["REDDIT_USERNAME"] username = os.environ["REDDIT_USERNAME"]
webhook_url = os.environ["DISCORD_WEBHOOK_URL"]
user_agent = 'User Flair Usage' user_agent = 'User Flair Usage'
sub = "developersIndia" sub = "developersIndia"
@ -63,9 +65,6 @@ for flair, count in sorted_flairs:
else: else:
old_available_flair_count[flair] += count 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()) total_count = sum(available_flair_count.values())
old_flairs_total_count = sum(old_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}") # print(f"Total count of user-flairs: {total_count + emoji_flair_count + old_flairs_total_count}")
data = { data = {
'Users with un-supported (old) text flairs': old_flairs_total_count, 'Users with un-supported (old) text flairs': f"**{old_flairs_total_count}**",
'Users with supported text flairs': total_count, 'Users with supported text flairs': f"**{total_count}**",
'Users with emoji only flairs': emoji_flair_count, 'Users with emoji only flairs': f"**{emoji_flair_count}**",
'Total count of user-flairs': total_count + emoji_flair_count + old_flairs_total_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) requests.post(webhook_url, json={"content": formatted_data})

View File

@ -0,0 +1,2 @@
praw
requests