Skip to content

Instantly share code, notes, and snippets.

@imLinguin
Last active March 30, 2024 14:25
Show Gist options
  • Save imLinguin/2c02ebe9c28f408e550054ee1a1386e3 to your computer and use it in GitHub Desktop.
Save imLinguin/2c02ebe9c28f408e550054ee1a1386e3 to your computer and use it in GitHub Desktop.
Discord webhook pulling latest GOG giveaway and pushing the embed message about it
#!/usr/bin/env python3
import requests
import datetime
from math import floor
import json
import os
WEBHOOK_URL = ""
if not WEBHOOK_URL:
print("Provide webhook url")
exit()
if not os.path.exists("last.txt"):
open("last.txt", "x").close()
response = requests.get("https://sections.gog.com/v1/pages/2f?countryCode=PL&locale=en-US&currencyCode=PLN")
data = response.json()
giveaway_section = None
for section in data['sections']:
if section['sectionType'] == 'GIVEAWAY_SECTION':
giveaway_section = section['sectionId']
break
if not giveaway_section:
print("No giveaway")
exit()
response = requests.get(f"https://sections.gog.com/v1/pages/2f/sections/{giveaway_section}?countryCode=PL&locale=en-US&currencyCode=PLN")
giveaway_data = response.json()
giveaway_props = giveaway_data["properties"]
giveaway_product = giveaway_props["product"]
last_giveaway = open("last.txt", 'r').read()
if last_giveaway == giveaway_product["title"]:
print("Already sent notification")
exit()
response = requests.get(f"https://gamesdb.gog.com/platforms/gog/external_releases/{giveaway_product['id']}")
extra_data = response.json()
end_time = datetime.datetime.fromisoformat(giveaway_props['endDate'])
end_ts = floor(end_time.timestamp())
rating = f"⭐ {giveaway_product['reviewsRating'] / 10:0.1f}/5.0"
data = {
'payload_json': json.dumps({
'embeds': [
{
'color': 0x6D3AFE,
'type': 'rich',
'description': extra_data['summary']['*'],
'fields': [
{ 'name': 'Ends', 'value': f"<t:{end_ts + 1}:f>", 'inline': True },
{ 'name': 'Claim directly', 'value': '[Open](https://gog.com/giveaway/claim)', 'inline': True },
{ 'name': 'Reviews rating', 'value': rating, 'inline': True },
{ 'name': 'Supported OS', 'value': ', '.join([os.capitalize() for os in giveaway_product['operatingSystems']]), 'inline': True },
],
'title': giveaway_product['title'],
'image': {'url': giveaway_product['coverHorizontal']},
'url': f'https://gog.com/game/{giveaway_product["slug"]}',
'footer': {
'text': 'Powered by Heroic'
}
}
]})
}
res = requests.post(WEBHOOK_URL, data=data)
open("last.txt", 'w').write(giveaway_product["title"])
@imLinguin
Copy link
Author

Embed looks like follows:
obraz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment