This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (52 loc) · 1.94 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from discord import SyncWebhook
import discord
import requests
import os
import json
# Configuration variables
facts_api_url = "https://api.api-ninjas.com/v1/facts?limit=1"
facts_api_key = os.environ["FACTS_API_KEY"]
discord_webhook_url = os.environ["DISCORD_WEBHOOK_URL"]
# Call the fun fact API and return a fact
def get_fun_fact():
print("INFO: Calling Fun fact API")
headers = {"X-Api-Key": facts_api_key}
response = requests.get(f'{facts_api_url}', headers=headers)
if response.ok:
print(f'SUCCESS: Fun fact API request succeeded with status code {response.status_code}')
return response.json()[0]["fact"]
else:
print(f'ERROR: Fun fact API request failed with status code {response.status_code}')
return None
# Check if the fact is a duplicate
def is_duplicate_fact(fact):
with open('history.json') as file:
fact_history = json.loads(file.read())["history"]
print(f'INFO: Fact in history: {fact in fact_history}')
return fact in fact_history
# Add the fact to the history
def add_fact_to_history(fact):
with open('history.json', 'r+', encoding='utf-8') as file:
fact_history = json.loads(file.read())
fact_history["history"].append(fact)
file.seek(0)
file.write(json.dumps(fact_history, ensure_ascii=False, indent=2))
# Post fun facts to Discord webhook
def post_discord_webhook(fact):
print("INFO: Posting to Discord webhook")
webhook = SyncWebhook.from_url(discord_webhook_url)
embed = discord.Embed(color=2695771)
embed.add_field(name='Did You Know?', value=fact, inline=False)
webhook.send(embed=embed)
print("SUCCESS: Posted to Discord webhook")
# Main function
def main():
while(True):
fact = get_fun_fact()
if fact is not None:
if not is_duplicate_fact(fact):
add_fact_to_history(fact)
post_discord_webhook(fact)
break
if __name__ == "__main__":
main()