| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import logging
- import json
- import time
- import sys
- import requests
- import db
- import util.util
- import util.dbutil
- _BASE_URL = 'https://api.cryptokitties.co/%s'
- _API_REQ_HEADERS = {
- 'accept': 'application/json',
- 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/600.5.17 (KHTML, like Gecko) Version/8.0.5 Safari/600.5.17'
- }
- _CURRENT_AUCTIONS = {
- 'sale': db.load_current_auctions('sale'),
- 'sire': db.load_current_auctions('sire'),
- }
- for k, v in _CURRENT_AUCTIONS.iteritems():
- print 'Loaded %i %s auctions.' % (len(v), k)
- _KNOWN_KITTIES = util.dbutil.get_set('select id from kittens')
- print 'Loaded %i known kitties' % len(_KNOWN_KITTIES)
- def _update_kitten(id, clear):
- kitten = _get_kitten(id)
- db.update_kitten(kitten, clear)
- logging.info('Inserted kitten %i', kitten['id'])
- return kitten
- def _get_all_kittens():
- offset = 0
- data = {'kitties': 1}
- while data.get('kitties'):
- params = {
- 'offset': offset,
- 'limit': 100,
- }
- resp = requests.get(_BASE_URL % 'kitties', params, headers=_API_REQ_HEADERS)
- if resp.status_code != 200:
- logging.error('getting auction data: %i: %s', resp.status_code, resp.content)
- return
- data = resp.json()
- kitties = [k for k in data['kitties'] if k['id'] not in _KNOWN_KITTIES]
- db.clear_data(kitties)
- for kitten in kitties:
- _update_kitten(kitten['id'], False)
- offset += len(data['kitties'])
- def _process_closed_auctions(auctions):
- completed = 0
- cancelled = 0
- if not auctions:
- return
- type = auctions[0]['type']
- for a in auctions:
- kitten = _update_kitten(a['kitty']['id'], True)
- # if type == 'sire':
- # print json.dumps([
- # a,
- # kitten
- # ])
- if type == 'sale' and kitten['owner']['address'] != a['seller']['address']:
- db.record_transaction(type, kitten['id'], a['current_price'], a['seller']['address'], kitten['owner']['address'], a)
- completed += 1
- elif type == 'sire' and a['kitty']['status']['is_ready'] and not kitten['status']['is_ready']:
- db.record_transaction(type, kitten['id'], a['current_price'], a['seller']['address'], None, a)
- completed += 1
- else:
- cancelled += 1
- del _CURRENT_AUCTIONS[type][a['id']]
- db.clear_auctions(auctions)
- logging.info('Found %i completed and %i cancelled %s auctions.', completed, cancelled, type)
- def _check_auctions(type):
- offset = 0
- data = {'auctions': 1}
- seen_auctions = set()
- while data.get('auctions'):
- params = {
- 'offset': offset,
- 'limit': 100,
- 'type': type,
- 'status': 'open',
- }
-
- resp = requests.get(_BASE_URL % 'auctions', params, headers=_API_REQ_HEADERS)
- if resp.status_code != 200:
- logging.error('Error getting auction data: %i: %s', resp.status_code, resp.content)
- return
- data = resp.json()
- if not data.get('auctions'):
- break
- unseen_auctions = [a for a in data['auctions'] if a['id'] not in _CURRENT_AUCTIONS[type]]
- db.update_auctions(unseen_auctions)
- seen_auctions.update([a['id'] for a in data['auctions']])
- _CURRENT_AUCTIONS[type].update({a['id']: a for a in data['auctions']})
- offset += len(data['auctions'])
- logging.info('Processed %i new %s auctions. Currently known: %i. Progress: %i/%i.', len(unseen_auctions), type, len(_CURRENT_AUCTIONS[type]), offset, data['total'])
-
- closed_auctions = [a for a in _CURRENT_AUCTIONS[type].itervalues() if a['id'] not in seen_auctions]
- if closed_auctions:
- _process_closed_auctions(closed_auctions)
- def _get_kitten(id):
- resp = requests.get(_BASE_URL % ('kitties/%i' % id), headers=_API_REQ_HEADERS)
- if resp.status_code != 200:
- logging.error('getting auction data: %i: %s', resp.status_code, resp.content)
- return
- return resp.json()
- def run():
- # print json.dumps(_get_kitten(19184))
- # _get_all_kittens()
- # print json.dumps([
- # _get_kitten(17610),
- # _get_kitten(12324),
- # _get_kitten(15348),
- # ])
- # return
- if sys.argv[1] == 'monitor':
- while True:
- logging.info('Downloading...')
- _check_auctions('sale')
- _check_auctions('sire')
- time.sleep(30)
-
- elif sys.argv[1] == 'kittens':
- _get_all_kittens()
- else:
- print 'commands: monitor, kittens'
-
- if __name__ == '__main__':
- util.util.init_logging()
- run()
|