Last active
August 29, 2015 14:06
-
-
Save bussiere/a3003d1140d38d90cf3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
A script to notify about Steam game prices | |
If the specified game is on sale it will email you. | |
If the game is on sale below a specified amount it will | |
tell you to buy the game because GabeN owns your wallet | |
''' | |
import argparse | |
import sys | |
import requests | |
from mailer import Mailer, Message | |
URL = 'http://store.steampowered.com/api/appdetails/?appids=' | |
FROM_ADDR = 'your_email_goes_here' | |
TO_ADDR = ['[email protected]'] | |
def get_args(): | |
''' | |
Returns an ArgumentParser | |
''' | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--appid', type=str) | |
parser.add_argument('--target', type=float, default=0.00) | |
return parser.parse_args() | |
def send_email(msg): | |
''' | |
Sends the provided message to global recipients | |
''' | |
# change the server and port to match what you want | |
sender = Mailer('smtp.mail.yahoo.com', port=587, use_tls=True) | |
sender.login(FROM_ADDR, 'password_goes_here') | |
sender.send(msg) | |
def create_message(subject, body): | |
''' | |
Returns a MIME message for sending | |
''' | |
msg = Message() | |
msg.From = FROM_ADDR | |
msg.To = TO_ADDR | |
msg.Subject = subject | |
msg.Body = body | |
return msg | |
def convert_cents_to_usd(amount): | |
''' | |
Returns the price in dollars and cents | |
The API provides the price in cents so this | |
handles conversion to dollars | |
''' | |
return amount / 100.0 | |
def get_game_info(appid): | |
''' | |
Returns a dictionary of info about specified game | |
''' | |
info = {} | |
req = requests.get(URL + appid) | |
final_price = req.json()[appid]['data']['price_overview']['final'] | |
info['final'] = convert_cents_to_usd(final_price) | |
initial_price = req.json()[appid]['data']['price_overview']['initial'] | |
info['initial'] = convert_cents_to_usd(initial_price) | |
info['name'] = req.json()[appid]['data']['name'] | |
info['link'] = 'http://store.steampowered.com/app/{}'.format(appid) | |
return info | |
def main(): | |
''' | |
The main function obviously | |
''' | |
args = get_args() | |
if not args.appid: | |
print 'Please specify a game ID' | |
sys.exit(1) | |
info = get_game_info(args.appid) | |
print args.target | |
if info['final'] <= args.target: | |
subject = '{0} is below your target price!'.format(info['name']) | |
body = 'Go buy it now!\n\n{}'.format(info['link']) | |
msg = create_message(subject, body) | |
send_email(msg) | |
elif info['final'] < info['initial']: | |
subject = 'Sale on {}'.format(info['name']) | |
body = '{0} on sale from {1} to {2}\n\n{3}'.format(info['name'], | |
info['initial'], | |
info['final'], | |
info['link']) | |
msg = create_message(subject, body) | |
send_email(msg) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment