|
@@ -1,10 +1,9 @@
|
|
|
import bson
|
|
|
import pymongo
|
|
|
-import redis
|
|
|
from elasticsearch import Elasticsearch
|
|
|
-from utils.title_participle import get_should
|
|
|
|
|
|
-from config.load import mongo_conf, redis_conf, es_conf
|
|
|
+import setting
|
|
|
+from utils.title_participle import get_should
|
|
|
|
|
|
# ---------------------------------- mongo ----------------------------------
|
|
|
MONGO_URI_CLIENTS = {} # a dictionary hold all client with uri as key
|
|
@@ -14,11 +13,12 @@ def mongo_client(cfg=None, host=None, port=None, fork=False, **kwargs):
|
|
|
if host is not None and port is not None:
|
|
|
uri = f'mongodb://{host}:{port}'
|
|
|
else:
|
|
|
- _cfg = (cfg or mongo_conf)
|
|
|
+ _cfg = (cfg or {'host': setting.MONGO_IP, 'port': setting.MONGO_PORT})
|
|
|
uri = f'mongodb://{_cfg["host"]}:{_cfg["port"]}'
|
|
|
|
|
|
if fork:
|
|
|
return pymongo.MongoClient(uri, **kwargs)
|
|
|
+
|
|
|
global MONGO_URI_CLIENTS
|
|
|
matched_client = MONGO_URI_CLIENTS.get(uri)
|
|
|
if matched_client is None:
|
|
@@ -29,13 +29,13 @@ def mongo_client(cfg=None, host=None, port=None, fork=False, **kwargs):
|
|
|
return matched_client
|
|
|
|
|
|
|
|
|
-def mongo_database(name: str, **kw):
|
|
|
- client = mongo_client(**kw)
|
|
|
+def mongo_database(name: str, **kwargs):
|
|
|
+ client = mongo_client(**kwargs)
|
|
|
return client.get_database(name)
|
|
|
|
|
|
|
|
|
-def mongo_table(db: str, name: str, **kw):
|
|
|
- database = mongo_database(db, **kw)
|
|
|
+def mongo_table(db: str, name: str, **kwargs):
|
|
|
+ database = mongo_database(db, **kwargs)
|
|
|
return database.get_collection(name)
|
|
|
|
|
|
|
|
@@ -51,8 +51,16 @@ def object_id(_id: str):
|
|
|
# ---------------------------------- es ----------------------------------
|
|
|
def es_client(cfg=None):
|
|
|
if cfg is None:
|
|
|
- cfg = es_conf
|
|
|
- return Elasticsearch([{"host": cfg['host'], "port": cfg['port']}],http_auth=(cfg['usename'], cfg['pwd']))
|
|
|
+ cfg = {
|
|
|
+ 'host': setting.ES_IP,
|
|
|
+ 'username': setting.ES_USERNAME,
|
|
|
+ 'pwd': setting.ES_PASSWORD,
|
|
|
+ 'port': setting.ES_PORT,
|
|
|
+ }
|
|
|
+
|
|
|
+ hosts = [{'host': cfg['host'], 'port': cfg['port']}]
|
|
|
+ auth = (cfg['username'], cfg['pwd'])
|
|
|
+ return Elasticsearch(hosts, http_auth=auth)
|
|
|
|
|
|
|
|
|
def es_query(title: str, publish_time: int):
|
|
@@ -79,19 +87,6 @@ def es_query(title: str, publish_time: int):
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- result = client.search(index=es_conf['db'], body=query, request_timeout=100)
|
|
|
+ result = client.search(index=setting.ES_INDEX, body=query, request_timeout=100)
|
|
|
total = int(result['hits']['total']['value'])
|
|
|
return total
|
|
|
-
|
|
|
-
|
|
|
-# ---------------------------------- redis ----------------------------------
|
|
|
-def redis_client(cfg=None):
|
|
|
- if cfg is None:
|
|
|
- cfg = redis_conf
|
|
|
- pool = redis.ConnectionPool(
|
|
|
- host=cfg['host'],
|
|
|
- port=cfg['port'],
|
|
|
- password=cfg['pwd'],
|
|
|
- db=cfg['db']
|
|
|
- )
|
|
|
- return redis.Redis(connection_pool=pool, decode_responses=True)
|