mirror of
https://github.com/JonasunderscoreJones/turbo-octo-potato.git
synced 2025-10-25 11:09:18 +02:00
added webhook functionality
This commit is contained in:
parent
3ef4f18f92
commit
fb8a94d97c
2 changed files with 76 additions and 7 deletions
63
rpop_webhook.py
Normal file
63
rpop_webhook.py
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
import os
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import dotenv
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
def send_webhook():
|
||||||
|
# Load the webhook URL from the environment variables
|
||||||
|
dotenv.load_dotenv()
|
||||||
|
|
||||||
|
# Replace with your actual webhook URL
|
||||||
|
webhook_url = os.getenv('DISCORD_WEBHOOK_URL')
|
||||||
|
|
||||||
|
today = datetime.datetime.today().date()
|
||||||
|
|
||||||
|
# Get the date from 7 days ago
|
||||||
|
seven_days_ago = today - datetime.timedelta(days=7)
|
||||||
|
|
||||||
|
# request with the date and http://api.jonasjones.dev/v1/kcomebacks/filter/daterange?start=2023-12-1&end=2023-12-16&limit=50&offset=0
|
||||||
|
response = requests.get(f"http://api.jonasjones.dev/v1/kcomebacks/filter/daterange?start={seven_days_ago}&end={today}&limit=50&offset=0")
|
||||||
|
|
||||||
|
# Check if the request was successful
|
||||||
|
if response.status_code != 200:
|
||||||
|
print(f"Failed to fetch data. Status code: {response.status_code}")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
# Parse the JSON response
|
||||||
|
data = response.json()
|
||||||
|
data = data['results']
|
||||||
|
|
||||||
|
# check if there are 50 or more comebacks
|
||||||
|
if len(data) >= 50:
|
||||||
|
# request with offset 50
|
||||||
|
response = requests.get(f"http://api.jonasjones.dev/v1/kcomebacks/filter/daterange?start={seven_days_ago}&end={today}&limit=50&offset=50")
|
||||||
|
# Check if the request was successful
|
||||||
|
if response.status_code != 200:
|
||||||
|
print(f"Failed to fetch data. Status code: {response.status_code}")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
# Parse the JSON response
|
||||||
|
data2 = response.json()
|
||||||
|
|
||||||
|
# join the lists
|
||||||
|
data += data2['results']
|
||||||
|
|
||||||
|
# Create the message content
|
||||||
|
content = "Here are the comebacks from today:\n"
|
||||||
|
for i in data:
|
||||||
|
content += f"{i['date']} > {i['title']} - {i['artist']}: {i['links'] if i['links'] != [] else '*No Links*'}\n"
|
||||||
|
|
||||||
|
# Message content
|
||||||
|
message = {
|
||||||
|
"content": content,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Make the POST request to the webhook URL
|
||||||
|
response = requests.post(webhook_url, data=json.dumps(message), headers={"Content-Type": "application/json"})
|
||||||
|
|
||||||
|
# Check if the request was successful
|
||||||
|
if response.status_code == 204:
|
||||||
|
print("Webhook sent successfully!")
|
||||||
|
else:
|
||||||
|
print(f"Failed to send webhook. Status code: {response.status_code}")
|
||||||
|
|
@ -6,6 +6,7 @@ import re
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
|
import rpop_webhook
|
||||||
|
|
||||||
def fetch_main_reddit_wiki_page(subreddit_name, page_name):
|
def fetch_main_reddit_wiki_page(subreddit_name, page_name):
|
||||||
|
|
||||||
|
|
@ -247,6 +248,7 @@ def fetch_monthly_page(wiki_link, subreddit_name):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
UPLOAD_TO_CDN = True if "--cdn" in sys.argv else False
|
UPLOAD_TO_CDN = True if "--cdn" in sys.argv else False
|
||||||
|
SEND_WEBHOOK = False if "--no-webhook" in sys.argv else False if "-nwh" in sys.argv else True
|
||||||
|
|
||||||
# reddit infos
|
# reddit infos
|
||||||
subreddit_name = "kpop"
|
subreddit_name = "kpop"
|
||||||
|
|
@ -325,3 +327,7 @@ if content:
|
||||||
elif input("Upload to cdn? [Y/n]") in ["Y", "y", ""]:
|
elif input("Upload to cdn? [Y/n]") in ["Y", "y", ""]:
|
||||||
print("Uploading...")
|
print("Uploading...")
|
||||||
os.system(cdn_upload_cmd)
|
os.system(cdn_upload_cmd)
|
||||||
|
|
||||||
|
|
||||||
|
if SEND_WEBHOOK:
|
||||||
|
rpop_webhook.send_webhook()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue