Advertisement
devsaider

steam prices

Apr 1st, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.08 KB | None | 0 0
  1. # coding: utf-8
  2. import json
  3. import time
  4. import re
  5. import urllib
  6. import threading
  7. import requests
  8. import grequests
  9. import random
  10. from urlparse import urlparse, parse_qs
  11.  
  12. from Queue import Queue
  13. from threading import Thread
  14.  
  15.  
  16. class Worker(Thread):
  17.  
  18.     """Thread executing tasks from a given tasks queue"""
  19.  
  20.     def __init__(self, tasks):
  21.         Thread.__init__(self)
  22.         self.tasks = tasks
  23.         self.daemon = True
  24.         self.start()
  25.  
  26.     def run(self):
  27.         while True:
  28.             func, args, kargs = self.tasks.get()
  29.             try:
  30.                 func(*args, **kargs)
  31.             except Exception, e:
  32.                 print e
  33.             self.tasks.task_done()
  34.  
  35.  
  36. class ThreadPool:
  37.  
  38.     """Pool of threads consuming tasks from a queue"""
  39.  
  40.     def __init__(self, num_threads):
  41.         self.tasks = Queue(num_threads)
  42.         for _ in range(num_threads):
  43.             Worker(self.tasks)
  44.  
  45.     def add_task(self, func, *args, **kargs):
  46.         """Add a task to the queue"""
  47.         self.tasks.put((func, args, kargs))
  48.  
  49.     def wait_completion(self):
  50.         """Wait for completion of all the tasks in the queue"""
  51.         self.tasks.join()
  52.  
  53.  
  54. class SteamPrices(object):
  55.  
  56.     def __init__(self):
  57.         self.tempitems = {
  58.             'Glock-18 | Water Elemental': 0,
  59.             'Glock-18 | Dragon Tattoo': 0,
  60.             'AK-47 | Wasteland Rebel': 0,
  61.             'AK-47 | Jet Set': 0,
  62.             'AK-47 | Redline': 0,
  63.             'AWP | Redline': 0,
  64.             'M4A4 | \xe9\xbe\x8d\xe7\x8e\x8b (Dragon King)': 0,
  65.             'M4A4 | Bullet Rain': 0,
  66.             'USP-S | Caiman': 0,
  67.             'AWP | Corticera': 0,
  68.             'P2000 | Corticera': 0,
  69.             'M4A4 | Modern Hunter': 0,
  70.             'AK-47  Red Laminate': 0,
  71.             'Galil AR | Chatterbox': 0,
  72.             'Tec-9 | Titanium Bit': 0,
  73.             'AK-47 | First Class': 0,
  74.             'M4A1-S | Guardian': 0,
  75.             'M4A1-S | Basilisk': 0,
  76.             'M4A1-S | Nitro': 0,
  77.             'P90 | Asiimov': 0,
  78.             'P2000 | Fire Elemental': 0,
  79.             'P250 | Muertos': 0,
  80.             'Desert Eagle | Naga': 0,
  81.             'StatTrakβ„’ Desert Eagle | Naga': 0,
  82.             'P250 | Cartel': 0,
  83.             'SCAR-20 | Cardiac': 0,
  84.             'M4A1-S | Basilisk': 0,
  85.             'AK-47 | Red Laminate': 0,
  86.             'M4A4 | Griffin': 0,
  87.             'USP-S | Caiman': 0,
  88.             'AK-47 | Blue Laminate': 0,
  89.             'P250 | Supernova': 0,
  90.             'P250 | Muertos': 0,
  91.             'AK-47 | Jaguar': 0,
  92.             'P250 | Supernova': 0
  93.         }
  94.  
  95.         self.quality = ['Battle-Scarred', 'Well-Worn', 'Field-Tested', 'Minimal Wear', 'Factory New']
  96.  
  97.         self.items = {}
  98.  
  99.         for name, price in self.tempitems.iteritems():
  100.             for q in self.quality:
  101.                 self.items['%s (%s)' % (name, q)] = None
  102.                 self.items['StatTrak\xe2\x84\xa2 %s (%s)' % (name, q)] = None
  103.  
  104.         self.pending = self.items.keys()
  105.  
  106.         self.pool = ThreadPool(15)
  107.  
  108.     def startjob(self):
  109.         items = list(self.pending)
  110.  
  111.         for item in items:
  112.             self.pool.add_task(requests.get, "http://steamcommunity.com/market/priceoverview/", params={'country': 'RU', 'currency': 5, 'appid': 730, 'market_hash_name': item}, hooks={'response': self.handle})
  113.             self.pending.remove(item)
  114.  
  115.     def handle(self, response, **kwargs):
  116.         item = parse_qs(urlparse(response.url).query.encode('ASCII'))['market_hash_name'][0]
  117.  
  118.         if response.status_code == 200:
  119.             data = response.json()
  120.  
  121.             if 'lowest_price' in data.keys() and 'median_price' in data.keys():
  122.                 oldprice = self.items[item]
  123.                 newprice = float(data['lowest_price'].split(' ')[0].replace(',', '.'))
  124.                 medium = float(data[u'median_price'].split(' ')[0].replace(',', '.'))
  125.  
  126.                 self.items[item] = newprice
  127.  
  128.                 self.pending.append(item)
  129.  
  130.                 if oldprice > newprice:
  131.                     difference = (oldprice - newprice)
  132.                     commission = (newprice * 15) / 100
  133.                     coverscommission = oldprice >= (newprice + commission)
  134.                     commissionprice = (newprice + commission)
  135.  
  136.                     print item, oldprice, '->', newprice, '(%s & %s)' % (medium, commissionprice)
  137.  
  138.                     if oldprice >= commissionprice:
  139.                         pureprofit = (oldprice - commissionprice)
  140.                         print item, 'decreased in price (%s -> %s) +%s profit\nhttp://steamcommunity.com/market/listings/730/%s' % (oldprice, newprice, pureprofit, urllib.quote(item, ''))
  141.  
  142.                     elif medium >= commissionprice:
  143.                         pureprofit = (medium - commissionprice)
  144.                         print item, 'have lower price than medium (%s -> %s) +%s profit\nhttp://steamcommunity.com/market/listings/730/%s' % (medium, newprice, pureprofit, urllib.quote(item, ''))
  145.  
  146.             else:
  147.                 del self.items[item]
  148.  
  149. s = SteamPrices()
  150. while True:
  151.     s.startjob()
  152.     time.sleep(.6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement