Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- import json
- import time
- import re
- import urllib
- import threading
- import requests
- import grequests
- import random
- from urlparse import urlparse, parse_qs
- from Queue import Queue
- from threading import Thread
- class Worker(Thread):
- """Thread executing tasks from a given tasks queue"""
- def __init__(self, tasks):
- Thread.__init__(self)
- self.tasks = tasks
- self.daemon = True
- self.start()
- def run(self):
- while True:
- func, args, kargs = self.tasks.get()
- try:
- func(*args, **kargs)
- except Exception, e:
- print e
- self.tasks.task_done()
- class ThreadPool:
- """Pool of threads consuming tasks from a queue"""
- def __init__(self, num_threads):
- self.tasks = Queue(num_threads)
- for _ in range(num_threads):
- Worker(self.tasks)
- def add_task(self, func, *args, **kargs):
- """Add a task to the queue"""
- self.tasks.put((func, args, kargs))
- def wait_completion(self):
- """Wait for completion of all the tasks in the queue"""
- self.tasks.join()
- class SteamPrices(object):
- def __init__(self):
- self.tempitems = {
- 'Glock-18 | Water Elemental': 0,
- 'Glock-18 | Dragon Tattoo': 0,
- 'AK-47 | Wasteland Rebel': 0,
- 'AK-47 | Jet Set': 0,
- 'AK-47 | Redline': 0,
- 'AWP | Redline': 0,
- 'M4A4 | \xe9\xbe\x8d\xe7\x8e\x8b (Dragon King)': 0,
- 'M4A4 | Bullet Rain': 0,
- 'USP-S | Caiman': 0,
- 'AWP | Corticera': 0,
- 'P2000 | Corticera': 0,
- 'M4A4 | Modern Hunter': 0,
- 'AK-47 Red Laminate': 0,
- 'Galil AR | Chatterbox': 0,
- 'Tec-9 | Titanium Bit': 0,
- 'AK-47 | First Class': 0,
- 'M4A1-S | Guardian': 0,
- 'M4A1-S | Basilisk': 0,
- 'M4A1-S | Nitro': 0,
- 'P90 | Asiimov': 0,
- 'P2000 | Fire Elemental': 0,
- 'P250 | Muertos': 0,
- 'Desert Eagle | Naga': 0,
- 'StatTrakβ’ Desert Eagle | Naga': 0,
- 'P250 | Cartel': 0,
- 'SCAR-20 | Cardiac': 0,
- 'M4A1-S | Basilisk': 0,
- 'AK-47 | Red Laminate': 0,
- 'M4A4 | Griffin': 0,
- 'USP-S | Caiman': 0,
- 'AK-47 | Blue Laminate': 0,
- 'P250 | Supernova': 0,
- 'P250 | Muertos': 0,
- 'AK-47 | Jaguar': 0,
- 'P250 | Supernova': 0
- }
- self.quality = ['Battle-Scarred', 'Well-Worn', 'Field-Tested', 'Minimal Wear', 'Factory New']
- self.items = {}
- for name, price in self.tempitems.iteritems():
- for q in self.quality:
- self.items['%s (%s)' % (name, q)] = None
- self.items['StatTrak\xe2\x84\xa2 %s (%s)' % (name, q)] = None
- self.pending = self.items.keys()
- self.pool = ThreadPool(15)
- def startjob(self):
- items = list(self.pending)
- for item in items:
- 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})
- self.pending.remove(item)
- def handle(self, response, **kwargs):
- item = parse_qs(urlparse(response.url).query.encode('ASCII'))['market_hash_name'][0]
- if response.status_code == 200:
- data = response.json()
- if 'lowest_price' in data.keys() and 'median_price' in data.keys():
- oldprice = self.items[item]
- newprice = float(data['lowest_price'].split(' ')[0].replace(',', '.'))
- medium = float(data[u'median_price'].split(' ')[0].replace(',', '.'))
- self.items[item] = newprice
- self.pending.append(item)
- if oldprice > newprice:
- difference = (oldprice - newprice)
- commission = (newprice * 15) / 100
- coverscommission = oldprice >= (newprice + commission)
- commissionprice = (newprice + commission)
- print item, oldprice, '->', newprice, '(%s & %s)' % (medium, commissionprice)
- if oldprice >= commissionprice:
- pureprofit = (oldprice - commissionprice)
- print item, 'decreased in price (%s -> %s) +%s profit\nhttp://steamcommunity.com/market/listings/730/%s' % (oldprice, newprice, pureprofit, urllib.quote(item, ''))
- elif medium >= commissionprice:
- pureprofit = (medium - commissionprice)
- print item, 'have lower price than medium (%s -> %s) +%s profit\nhttp://steamcommunity.com/market/listings/730/%s' % (medium, newprice, pureprofit, urllib.quote(item, ''))
- else:
- del self.items[item]
- s = SteamPrices()
- while True:
- s.startjob()
- time.sleep(.6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement