use discord instead of sendgrid

This commit is contained in:
Matthew Tran
2025-05-06 03:31:31 -07:00
parent 8bd3def755
commit 516a02060c
5 changed files with 25 additions and 26 deletions
+19 -20
View File
@@ -1,45 +1,44 @@
#!/usr/bin/env python3
import requests
import time
import urllib.request
from ipaddress import ip_network
from pathlib import Path
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
if __name__ == "__main__":
sg = SendGridAPIClient(Path("sendgrid.key").read_text())
link = Path("discord.txt").read_text()
old_ipv4, old_ipv6 = None, None
while True:
# get current ips
try:
ipv4 = urllib.request.urlopen("https://v4.ident.me").read().decode("utf8")
ipv6 = urllib.request.urlopen("https://v6.ident.me").read().decode("utf8")
ipv4 = requests.get("https://v4.ident.me").text
ipv6 = requests.get("https://v6.ident.me").text
ipv6 = str(ip_network(ipv6 + "/64", strict=False).network_address) # xfinity gives /64
except Exception as e:
print(e)
print("Error while getting IP", e)
time.sleep(60)
continue
# send message if either changed
if ipv4 != old_ipv4 or ipv6 != old_ipv6:
msg = Mail(
from_email="mtran319@gmail.com",
to_emails="mtran319@gmail.com",
subject="pls update ip",
html_content=(
f"<p>old ipv4: {old_ipv4}</p>"
f"<p>old ipv6: {old_ipv6}</p>"
f"<p>new ipv4: {ipv4}</p>"
f"<p>new ipv6: {ipv6}</p>"
))
try:
print(f"IP changed to {ipv4} and {ipv6}")
resp = sg.send(msg)
resp = requests.post(link, json={
"content": "\n".join([
f"old ipv4: {old_ipv4}",
f"old ipv6: {old_ipv6}",
f"new ipv4: {ipv4}",
f"new ipv6: {ipv6}",
])
})
assert(resp.status_code == 204)
except Exception as e:
print(e)
sg = SendGridAPIClient(Path("sendgrid.key").read_text())
print("Error while sending update", e)
time.sleep(60)
continue
# retry every hour
print("Retrying in 1 hour...")
old_ipv4, old_ipv6 = ipv4, ipv6
time.sleep(3600)