소스 검색

#1 towards monitoring of auction sales

Martin 9 년 전
부모
커밋
8b2ed33bec
3개의 변경된 파일과 65개의 추가작업 그리고 23개의 파일을 삭제
  1. 23 1
      db.py
  2. 38 22
      main.py
  3. 4 0
      util/util.py

+ 23 - 1
db.py

@@ -1,5 +1,9 @@
 import json
 import util.dbutil
+import util.util
+
+def clear_current_auctions():
+    util.dbutil.execute('truncate current_auctions')
 
 def clear_data(kittens):
     if not kittens:
@@ -17,10 +21,28 @@ def clear_data(kittens):
     for stmt in stmts:
         util.dbutil.execute(stmt, params)
 
+def update_auction(auction):
+    util.dbutil.execute('delete from current_auctions where id=%s', (auction['id']))
+
+    stmt = 'insert into current_auctions (id, type, kitten, start_price, current_price, end_price, start_time, end_time, seller, last_update) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, NOW())'
+    params = (
+        auction['id'],
+        auction['type'],
+        auction['kitty']['id'],
+        int(auction['start_price']) / 1000000000000000000.0,
+        int(auction['current_price']) / 1000000000000000000.0,
+        int(auction['end_price']) / 1000000000000000000.0,
+        util.util.datetime_from_unix_time(int(auction['start_time']) / 1000),
+        util.util.datetime_from_unix_time(int(auction['end_time']) / 1000),
+        auction['seller']['address'],
+    )
+
+    util.dbutil.execute(stmt, params)
+
 def update_kitten(kitten):
 
     util.dbutil.execute(
-        'insert into kittens (id, cooldown_index, name, color, generation, image_url, is_fancy, owner, bio, matron, sire, raw_json) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
+        'insert into kittens (id, cooldown_index, name, color, generation, image_url, is_fancy, owner, bio, matron, sire, raw_json, last_update) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW())',
         (
             kitten['id'],
             kitten['status']['cooldown_index'],

+ 38 - 22
main.py

@@ -1,10 +1,8 @@
-import threading
 import logging
 import json
 
 import requests
 
-
 import db
 import util.util
 import util.dbutil
@@ -20,6 +18,11 @@ _CURRENT_AUCTIONS = {}
 
 _KNOWN_KITTIES = util.dbutil.get_set('select id from kittens')
 
+def _update_kitten(id, clear):
+    kitten = _get_kitten(id)
+    db.update_kitten(kitten)
+    logging.info('Inserted kitten %i', kitten['id'])
+
 def _get_all_kittens():
     offset = 0
     data = {'kitties': 1}
@@ -43,32 +46,41 @@ def _get_all_kittens():
         db.clear_data(kitties)
 
         for kitten in kitties:
-            kitten = _get_kitten(kitten['id'])
+            _update_kitten(kitten['id'], False)
 
-            db.update_kitten(kitten)
+        offset += len(data['kitties'])
 
-            logging.info('Inserted kitten %i', kitten['id'])
+def _check_auctions():
+    offset = 0
+    data = {'auctions': 1}
 
-        offset += len(data['kitties'])
+    while data.get('auctions'):
+        params = {
+            'offset': offset,
+            'limit': 100,
+            'type': 'sale',
+            'status': 'open',
+        }
+        
+        resp = requests.get(_BASE_URL % 'auctions', params, headers=_API_REQ_HEADERS)
 
-def _check_auctions(offset=0):
-    params = {
-        'offset': offset,
-        'limit': 100,
-        'type': 'sale',
-        'status': 'closed',
-    }
-    
-    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
 
-    if resp.status_code != 200:
-        logging.error('getting auction data: %i: %s', resp.status_code, resp.content)
-        return
+        data = resp.json()
 
-    data = resp.json()
+        if not data.get('auctions'):
+            return
+
+        for i, auction in enumerate(data['auctions']):
+            if auction['id'] not in _CURRENT_AUCTIONS:
+                db.update_auction(auction)
+                _CURRENT_AUCTIONS[auction['id']] = auction
+
+                logging.info('Updated auction %i (%i/%i)', auction['id'], offset+i, data['total'])
 
-    for auction in data['auctions']:
-        pass
+        offset += len(data['auctions'])
 
 def _get_kitten(id):
     resp = requests.get(_BASE_URL % ('kitties/%i' % id), headers=_API_REQ_HEADERS)
@@ -81,7 +93,11 @@ def _get_kitten(id):
 
 def run():
     # print json.dumps(_get_kitten(19184))
-    _get_all_kittens()
+    # _get_all_kittens()
+
+
+    db.clear_current_auctions()
+    _check_auctions()
     
 
 if __name__ == '__main__':

+ 4 - 0
util/util.py

@@ -1,3 +1,4 @@
+import datetime
 import sys
 import logging
 
@@ -30,3 +31,6 @@ def init_logging():
     thread	%(thread)d	Thread ID (if available).
     threadName	%(threadName)s	Thread name (if available).
     '''
+
+def datetime_from_unix_time(utc_timestamp):
+    return datetime.datetime.utcfromtimestamp(utc_timestamp)