Skip to content

Instantly share code, notes, and snippets.

@proudlygeek
Last active April 19, 2016 20:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save proudlygeek/a677130428cd2181ff3e to your computer and use it in GitHub Desktop.
Save proudlygeek/a677130428cd2181ff3e to your computer and use it in GitHub Desktop.
GOG Insomnia Alerter

GOG Alerts

Those little nifty scripts are useful for monitoring offers on GOG.com's Spring Insomnia Sale.

You can choose between:

  1. A simple Python script which polls an AJAX endpoint every N seconds;
  2. A JavaScript, Client-side poll which uses HTML5 Notifications for the same purposes.

Feel free to use it ;)

Bookmarklet:

Just create a bookmark and replace the address with the following snippet:

javascript:(function(){s = document.createElement('script'); s.src = "https://gist.githubusercontent.com/proudlygeek/a677130428cd2181ff3e/raw/c79671e93d75e68bbd73ef00759e2e7a3f0651d7/gog.js"; document.body.insertBefore(s, document.getElementsByTagName('script')[0]);})()

This is only tested on Chrome and Safari, probably works on other Webkit browsers too.

License

MIT

# coding: utf-8
import os
import re
import time
import requests
GOG_URL="http://www.gog.com/springinsomnia/ajax/getCurrentOffer"
DELAY_SECONDS=5
THRESHOLD=50
def scrape_gog():
"""
Scrapes GOG.com for the current Spring Insomnia Sale every DELAY_SECONDS
(5 by default); when the current title's count reaches the thresold value
(50 by default) the script notifies the user by using Mac OS X Notifier.
"""
reset = False
while True:
page = requests.get(GOG_URL).json()
title = re.findall('"game__title"> (.+) <b', page['html'])[0]
left = int(page['amountLeft'])
if not reset and int(left) <= THRESHOLD:
cmd = "osascript -e \'display notification \"Alert: {} is almost sold out!\" with title \"GOG.COM: {}\"\'".format(title, title)
print cmd
os.system(cmd)
print "[ALERT]: {} is almost sold out!".format(title)
reset = True
if left > THRESHOLD:
reset = False
time.sleep(DELAY_SECONDS)
if __name__ == '__main__':
scrape_gog()
(function() {
'use strict';
var imageUrl = "http://www.userlogos.org/files/logos/educolnago/gog_com.png";
var reset = false;
function getCount() {
return parseInt(document.getElementsByClassName('js-left-count')[0].innerHTML, 10);
}
function getGameTitle() {
return document.getElementsByClassName('game__title')[0].text;
}
function createNotification(title, msg) {
webkitNotifications.createNotification(imageUrl, title, msg).show();
}
function check(threshold) {
if (!reset && getCount() < threshold) {
createNotification("Alert!", getGameTitle() + " is almost sold out!");
reset = true;
}
if (getCount() > threshold) {
reset = false;
}
}
function createActivationButton() {
var activateButton = document.createElement('p');
activateButton.style.position = "fixed";
activateButton.style.top = "20px";
activateButton.style.right = "20px";
activateButton.style.color = "#fff";
activateButton.style.backgroundColor = "rgb(94,94,94)";
activateButton.style.padding = "25px";
activateButton.style.cursor = "pointer";
activateButton.style.borderRadius = "5px";
activateButton.style.border = "1px solid #222";
activateButton.style.zIndex = 999;
activateButton.textContent = "Activate Popup notifications on GOG.COM";
activateButton.setAttribute('onclick', "webkitNotifications.requestPermission();");
if (webkitNotifications.checkPermission()) {
document.body.appendChild(activateButton);
}
}
function main() {
createActivationButton();
setInterval(function() {
check(50);
}, 1000);
}
main();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment