using requests instead of discord.py

This commit is contained in:
Camille Frantz 2024-11-22 13:03:13 -06:00
parent 40cacc2020
commit de018b443b
Signed by: fyrfli
SSH Key Fingerprint: SHA256:cyNGncrLxKXAgYC96dYNROnQKikLInzrpiMraZdqyhY

47
main.py
View File

@ -1,6 +1,6 @@
from dotenv import load_dotenv
from requests import get,post,put
from discord import SyncWebhook
# from discord import SyncWebhook
from datetime import datetime as dt, timezone as tz
from time import mktime
from feedparser import parse
@ -22,7 +22,7 @@ except:
load_dotenv()
discord_user = getenv('DISCORD_USERNAME')
discord_webhook_url = getenv('DISCORD_WEBHOOK')
webhook = SyncWebhook.from_url(getenv('DISCORD_WEBHOOK'))
# webhook = SyncWebhook.from_url(getenv('DISCORD_WEBHOOK'))
github_token = getenv('GITHUB_TOKEN')
# list of repos to check
@ -47,25 +47,48 @@ headers = {
def get_latest_releases():
for repo in repos:
release_url = f'{github_api}/{repo}/releases/latest'
# debug
# print(release_url)
latest_release = get(release_url, headers=headers)
if latest_release.status_code == 200:
release_details = loads(latest_release.text)
# debugging
# print(
# release_details['name'],
# release_details['published_at'],
# old_ts,
# mktime(old_ts.timetuple()),
# mktime(dt.strptime(release_details['published_at'], "%Y-%m-%dT%H:%M:%SZ").timetuple())
# )
if mktime(dt.strptime(release_details['published_at'], '%Y-%m-%dT%H:%M:%SZ').timetuple()) > mktime(old_ts.timetuple()):
print(
release_details['tag_name'],
release_details['name'],
release_details['published_at'],
release_details['html_url']
)
webhook.send(f"{release_details['name']}: {release_details['tag_name']}\n{release_details['html_url']}")
else:
print(latest_release.status_code, latest_release.reason)
# debug
# print(
# release_details['tag_name'],
# release_details['name'],
# release_details['published_at'],
# release_details['html_url']
# )
payload = {
'username': discord_user,
'content': f"{release_details['name']}: {release_details['tag_name']}\n{release_details['html_url']}"
}
webhook_put = post(discord_webhook_url, headers=headers, json=payload)
# return if something goes wrong
if webhook_put.status_code != 204:
print(f"whoops... status_code is {webhook_put.status_code}")
# debug
# print(f"call was post({discord_webhook_url}, headers={headers}, json={payload})")
return False
# webhook.send(f"{release_details['name']}: {release_details['tag_name']}\n{release_details['html_url']}")
# else:
# print(latest_release.status_code, latest_release.reason)
else:
print(f"whoops ... got a {latest_release.status_code} on that last one")
return False
## return true if the function didn't terminate abnormally before
return True
# print("all good - finish clean")
return True
if __name__ == "__main__":
results_sent = get_latest_releases()