Skip to content

Instantly share code, notes, and snippets.

@chipolux
Last active December 19, 2015 16:09
Show Gist options
  • Save chipolux/5981665 to your computer and use it in GitHub Desktop.
Save chipolux/5981665 to your computer and use it in GitHub Desktop.
Quick and dirty python script to get current prices and discounts from all apps in the steam store. No keys required and multithreaded, change thread_limit to adjust how many living threads you'd like at once.
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 11 23:06:53 2013
@author: chipolux
"""
import json
import urllib2
import threading
import Queue
thread_limit = 100
games_url = 'http://api.steampowered.com/ISteamApps/GetAppList/v0002'
store_url = 'http://store.steampowered.com/api/appdetails/?appids={app_id}'
header = u'"App","RegularPrice","CurrentPrice","Currency","Discount"\n'
template = u'"%s","%s","%s","%s","%s"\n'
price_queue = Queue.Queue()
class GenericThread(threading.Thread):
def __init__(self, function, *args, **kwargs):
threading.Thread.__init__(self)
self.function = function
self.args = args
self.kwargs = kwargs
def run(self):
self.function(*self.args, **self.kwargs)
return
def get_price_info(app_id, name):
app_id = unicode(app_id)
name = unicode(name)
price_data = {'name': name,
'initial_price': 'FAILED TO LOAD',
'final_price': '',
'currency': '',
'discount': ''}
url = store_url.format(app_id = app_id)
try:
page = urllib2.urlopen(url)
response = json.load(page)
page.close()
except:
price_data['initial_price'] = 'FAILED PAGE LOAD'
price_queue.put(price_data)
return
if response[app_id]['success'] == False:
price_data['initial_price'] = 'SERVER FAILED REQUEST'
price_queue.put(price_data)
return
try:
data = response[app_id]['data']
price_data = {'name': name,
'currency': data['price_overview']['currency'],
'initial_price': data['price_overview']['initial'] * .01,
'final_price': data['price_overview']['final'] * .01,
'discount': data['price_overview']['discount_percent']}
except KeyError:
price_data['initial_price'] = 'BAD DATA'
price_queue.put(price_data)
return
price_queue.put(price_data)
def get_apps():
try:
page = urllib2.urlopen(games_url)
response = json.load(page)
page.close()
except:
print 'Unable To Load App Ids'
return []
return response['applist']['apps']
def writer():
results_file = open('PriceScan.csv', 'w')
results_file.write(header)
while True:
price_info = price_queue.get()
if price_info == 'EXIT':
break
try:
results_file.write(template % (price_info['name'],
price_info['initial_price'],
price_info['final_price'],
price_info['currency'],
price_info['discount']))
except:
print 'Failed To Write: %r' % price_info['name']
results_file.close()
if __name__ == '__main__':
apps = get_apps()
writer_thread = GenericThread(writer)
writer_thread.start()
threads = []
for i in xrange(len(apps)):
while True:
thread_statuses = [x.isAlive() for x in threads]
if thread_statuses.count(True) < thread_limit:
break
threads.append(GenericThread(get_price_info,
apps[i]['appid'],
apps[i]['name']))
threads[-1].start()
while True:
thread_statuses = [x.isAlive() for x in threads]
if thread_statuses.count(True) == 0:
break
price_queue.put('EXIT')
while writer_thread.isAlive():
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment