|
@@ -0,0 +1,76 @@
|
|
|
+import time as stime
|
|
|
+import redis
|
|
|
+import h5py
|
|
|
+from os import path
|
|
|
+from datetime import timedelta
|
|
|
+
|
|
|
+class Fetcher(object):
|
|
|
+ latest_delta = 2
|
|
|
+
|
|
|
+ def __init__(self, file_name):
|
|
|
+ self._mquit = False
|
|
|
+ self._mRHost = ''
|
|
|
+ self._mRPort = 6379
|
|
|
+ self._file_name = file_name
|
|
|
+
|
|
|
+ def set_redis(self, rhost, rport):
|
|
|
+ self._mRHost = rhost
|
|
|
+ self._mRPort = rport
|
|
|
+
|
|
|
+ def stop(self):
|
|
|
+ self._mquit = True
|
|
|
+ pass
|
|
|
+
|
|
|
+ def redis_name_prefix(self) -> dict:
|
|
|
+ pass
|
|
|
+
|
|
|
+ def run(self):
|
|
|
+ while self._mquit == False:
|
|
|
+ fAllEmpty = True
|
|
|
+ try:
|
|
|
+ pool = redis.ConnectionPool(host=self._mRHost, port=self._mRPort, db=0)
|
|
|
+ r = redis.Redis(connection_pool=pool)
|
|
|
+
|
|
|
+ if path.exists(self._file_name):
|
|
|
+ hfive = h5py.File(self._file_name, 'a')
|
|
|
+ else:
|
|
|
+ hfive = h5py.File(self._file_name, 'w')
|
|
|
+
|
|
|
+ latest_time = int(stime.time()) - self.latest_delta
|
|
|
+ name_prefix = self.redis_name_prefix()
|
|
|
+
|
|
|
+ for name, prefix in name_prefix.items():
|
|
|
+ fEmpty = self.fetch_hset(hfive, r, name, prefix, latest_time)
|
|
|
+ if fEmpty == False:
|
|
|
+ fAllEmpty = False
|
|
|
+ hfive.close()
|
|
|
+ except Exception as ex:
|
|
|
+ print(ex)
|
|
|
+ finally:
|
|
|
+ if fAllEmpty:
|
|
|
+ stime.sleep(1)
|
|
|
+
|
|
|
+ def parase(self, hfive, key, val, prefix, latest_time):
|
|
|
+ pass
|
|
|
+
|
|
|
+ def fetch_hset(self, hfive, redis, name, prefix, latest_time):
|
|
|
+ i = 0
|
|
|
+ fEmpty = True
|
|
|
+ for item in redis.hscan_iter(name):
|
|
|
+ fEmpty = False
|
|
|
+ key = str(item[0], encoding="utf-8")
|
|
|
+ val = str(item[1], encoding="utf-8")
|
|
|
+ print(f'{prefix}:{i}')
|
|
|
+ i += 1
|
|
|
+ fDel = self.parase(hfive, key, val, prefix, latest_time)
|
|
|
+ if fDel:
|
|
|
+ redis.hdel(name, key)
|
|
|
+
|
|
|
+ return fEmpty
|
|
|
+
|
|
|
+ def day_stamp(self, stamp):
|
|
|
+ stamp = int(stamp)
|
|
|
+ x = stime.gmtime(stamp + 8 * 3600)
|
|
|
+ diff = timedelta(hours=x.tm_hour, minutes=x.tm_min, seconds=x.tm_sec)
|
|
|
+ today = stamp - diff.total_seconds()
|
|
|
+ return int(today)
|