main.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import logging
  2. import json
  3. import time
  4. import sys
  5. import requests
  6. import db
  7. import util.util
  8. import util.dbutil
  9. _BASE_URL = 'https://api.cryptokitties.co/%s'
  10. _API_REQ_HEADERS = {
  11. 'accept': 'application/json',
  12. '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'
  13. }
  14. _CURRENT_AUCTIONS = {
  15. 'sale': db.load_current_auctions('sale'),
  16. 'sire': db.load_current_auctions('sire'),
  17. }
  18. for k, v in _CURRENT_AUCTIONS.iteritems():
  19. print 'Loaded %i %s auctions.' % (len(v), k)
  20. _KNOWN_KITTIES = util.dbutil.get_set('select id from kittens')
  21. print 'Loaded %i known kitties' % len(_KNOWN_KITTIES)
  22. def _update_kitten(id, clear):
  23. kitten = _get_kitten(id)
  24. db.update_kitten(kitten, clear)
  25. logging.info('Inserted kitten %i', kitten['id'])
  26. return kitten
  27. def _get_all_kittens():
  28. offset = 0
  29. data = {'kitties': 1}
  30. while data.get('kitties'):
  31. params = {
  32. 'offset': offset,
  33. 'limit': 100,
  34. }
  35. resp = requests.get(_BASE_URL % 'kitties', params, headers=_API_REQ_HEADERS)
  36. if resp.status_code != 200:
  37. logging.error('getting auction data: %i: %s', resp.status_code, resp.content)
  38. return
  39. data = resp.json()
  40. kitties = [k for k in data['kitties'] if k['id'] not in _KNOWN_KITTIES]
  41. db.clear_data(kitties)
  42. for kitten in kitties:
  43. _update_kitten(kitten['id'], False)
  44. offset += len(data['kitties'])
  45. def _process_closed_auctions(auctions):
  46. completed = 0
  47. cancelled = 0
  48. if not auctions:
  49. return
  50. type = auctions[0]['type']
  51. for a in auctions:
  52. kitten = _update_kitten(a['kitty']['id'], True)
  53. # if type == 'sire':
  54. # print json.dumps([
  55. # a,
  56. # kitten
  57. # ])
  58. if type == 'sale' and kitten['owner']['address'] != a['seller']['address']:
  59. db.record_transaction(type, kitten['id'], a['current_price'], a['seller']['address'], kitten['owner']['address'], a)
  60. completed += 1
  61. elif type == 'sire' and a['kitty']['status']['is_ready'] and not kitten['status']['is_ready']:
  62. db.record_transaction(type, kitten['id'], a['current_price'], a['seller']['address'], None, a)
  63. completed += 1
  64. else:
  65. cancelled += 1
  66. del _CURRENT_AUCTIONS[type][a['id']]
  67. db.clear_auctions(auctions)
  68. logging.info('Found %i completed and %i cancelled %s auctions.', completed, cancelled, type)
  69. def _check_auctions(type):
  70. offset = 0
  71. data = {'auctions': 1}
  72. seen_auctions = set()
  73. while data.get('auctions'):
  74. params = {
  75. 'offset': offset,
  76. 'limit': 100,
  77. 'type': type,
  78. 'status': 'open',
  79. }
  80. resp = requests.get(_BASE_URL % 'auctions', params, headers=_API_REQ_HEADERS)
  81. if resp.status_code != 200:
  82. logging.error('Error getting auction data: %i: %s', resp.status_code, resp.content)
  83. return
  84. data = resp.json()
  85. if not data.get('auctions'):
  86. break
  87. unseen_auctions = [a for a in data['auctions'] if a['id'] not in _CURRENT_AUCTIONS[type]]
  88. db.update_auctions(unseen_auctions)
  89. seen_auctions.update([a['id'] for a in data['auctions']])
  90. _CURRENT_AUCTIONS[type].update({a['id']: a for a in data['auctions']})
  91. offset += len(data['auctions'])
  92. logging.info('Processed %i new %s auctions. Currently known: %i. Progress: %i/%i.', len(unseen_auctions), type, len(_CURRENT_AUCTIONS[type]), offset, data['total'])
  93. closed_auctions = [a for a in _CURRENT_AUCTIONS[type].itervalues() if a['id'] not in seen_auctions]
  94. if closed_auctions:
  95. _process_closed_auctions(closed_auctions)
  96. def _get_kitten(id):
  97. resp = requests.get(_BASE_URL % ('kitties/%i' % id), headers=_API_REQ_HEADERS)
  98. if resp.status_code != 200:
  99. logging.error('getting auction data: %i: %s', resp.status_code, resp.content)
  100. return
  101. return resp.json()
  102. def run():
  103. # print json.dumps(_get_kitten(19184))
  104. # _get_all_kittens()
  105. # print json.dumps([
  106. # _get_kitten(17610),
  107. # _get_kitten(12324),
  108. # _get_kitten(15348),
  109. # ])
  110. # return
  111. if sys.argv[1] == 'monitor':
  112. while True:
  113. logging.info('Downloading...')
  114. _check_auctions('sale')
  115. _check_auctions('sire')
  116. time.sleep(30)
  117. elif sys.argv[1] == 'kittens':
  118. _get_all_kittens()
  119. else:
  120. print 'commands: monitor, kittens'
  121. if __name__ == '__main__':
  122. util.util.init_logging()
  123. run()