Last active
August 29, 2015 14:04
-
-
Save andmatand/ceb11b42557dca159567 to your computer and use it in GitHub Desktop.
Send e-mail notifications for Steam trade offers, to be run as a cron job
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
#!/usr/bin/env python3 | |
# Purpose: Send an e-mail notification when a Steam trade offer is accepted or | |
# received. | |
# Documentation URL: | |
# https://developer.valvesoftware.com/wiki/Steam_Web_API/IEconService | |
# Set these before running this script: | |
STEAM_API_KEY = 'your steam API Key' | |
STEAM_PROFILE = 'your public steam profile name' | |
SMTP_SERVER = 'SMTP server' | |
SMTP_PORT = 465 | |
SMTP_USERNAME = 'SMTP username' | |
SMTP_PASSWORD = 'SMTP password' | |
RECIPIENT_ADDRESS = 'your e-mail address' | |
import json | |
import smtplib | |
import time | |
import urllib.request | |
DEBUG = False | |
def call_method(service, method, version, key, userId=None, params=None): | |
url = 'https://api.steampowered.com/' + \ | |
'{service}/{method}/v{v}/?key={key}'.format( | |
service=service, | |
method = method, | |
v = version, | |
key = key) | |
# If a User ID was given | |
if userId != None: | |
# Append the user ID | |
url += '&steamid=' + userId | |
# Specify JSON as the format | |
url += '&format=json' | |
if params != None: | |
# Add each parameter | |
for k, v in params.items(): | |
url += '&' + str(k) + '=' + str(v) | |
if DEBUG: | |
print(url) | |
# Get the HTTP response | |
response = urllib.request.urlopen(url) | |
# Decode the binary response into text | |
encoding = response.headers.get_content_charset() | |
responseText = response.read().decode(encoding) | |
return json.loads(responseText)['response'] | |
def create_email_body(response): | |
numAccepted = response['newly_accepted_sent_count'] | |
numReceived = response['new_received_count'] | |
body = '' | |
if numReceived > 0: | |
body += '{} new offer'.format(numReceived) | |
if numReceived != 1: | |
body += 's' | |
body += '\r\n' | |
if numAccepted > 0: | |
body += numAccepted | |
body += ' offer' | |
if numAccepted == 1: | |
body += ' was' | |
else: | |
body += 's were' | |
body += ' accepted\r\n' | |
if body != '': | |
body += '\r\n' | |
body += 'https://steamcommunity.com/id/' + STEAM_PROFILE + \ | |
'/tradeoffers/' | |
return body | |
else: | |
return None | |
def send_notification_email(to, subject, body): | |
session = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) | |
session.ehlo() | |
session.login(SMTP_USERNAME, SMTP_PASSWORD) | |
headers = '\r\n'.join( | |
['from: ' + SMTP_USERNAME, | |
'subject: ' + subject, | |
'to: ' + to, | |
'mime-version: 1.0', | |
'content-type: text/plain']) | |
content = headers + '\r\n\r\n' + body | |
session.sendmail(SMTP_USERNAME, to, content) | |
# Get the timestamp from 6 minutes ago (this script must be run more often | |
# than every 6 minutes or we will miss updates) | |
timestamp = int(time.time()) - (6 * 60) | |
response = call_method('IEconService', | |
'GetTradeOffersSummary', '1', | |
STEAM_API_KEY, | |
None, | |
{'time_last_visit': timestamp}) | |
if DEBUG: | |
print(response) | |
body = create_email_body(response) | |
if body != None: | |
send_notification_email(RECIPIENT_ADDRESS, | |
'Steam Trade Notification', body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment