wxapi/lib/db.py
2020-07-08 12:16:41 +08:00

131 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pymongo import MongoClient
from bson import ObjectId, json_util
import re
# 主环境 (生产环境为production开发环境为development)
setting = 'production'
# 获取数据集
def col(arg):
conn = MongoClient('mongodb://wxapi:uJ5hB9mJ0gR2@mongo:27017/wxapi')
if setting == 'development':
arg += '_test'
if arg == 'keyword':
return conn.wxapi.keyword
elif arg == 'access_token':
return conn.wxapi.access_token
elif arg == 'keyword_test':
return conn.wxapi.keyword_test
elif arg == 'access_token_test':
return conn.wxapi.access_token_test
else:
return False
# 获取access_token
def getAccessToken():
try:
access_token = col('access_token').find_one({"name": 'access_token'},{'content': 1})
if access_token:
access_token = access_token['content']
return {'errcode': 200, 'access_token': access_token, 'errmsg': 'ok'}
else:
return {'errcode': 100, 'errmsg': 'access_token不存在'}
except Exception as e:
# id不合法
return {'errcode': 101, 'errmsg': 'access_token获取失败', 'errdetail': e}
# 设置access_token
def setAccessToken(access_token):
try:
res = col('access_token').update({"name": 'access_token'},
{'$set': {'content': access_token}})
# 判断有没有access_token
if(res['updatedExisting']):
return {'errcode': 200, 'errmsg': 'access_token覆写成功'}
else:
return insertAccessToken(access_token)
except Exception as e:
return {'errcode': 102, 'errmsg': 'access_token覆写失败','errdetail': e}
# 插入access_token
def insertAccessToken(access_token):
try:
col('access_token').insert_one({'name': 'access_token', 'content': access_token})
except Exception as e:
# 插入失败
return {'errcode': 103, 'errmsg': 'access_token插入失败','errdetail': e}
return {'errcode': 200, 'errmsg': 'access_token插入成功'}
# 插入新关键词
def insertKeyword(keyword):
try:
col('keyword').insert_one(keyword)
except Exception as e:
# 插入失败
return {'errcode': 301, 'errmsg': 'keyword插入失败'}
return {'errcode': 200, 'errmsg': 'keyword插入成功'}
# 删除特定关键词
def delKeyword(id):
try:
col('keyword').remove({"_id" : ObjectId(id)})
except Exception as e:
return {'errcode': 401, 'errmsg': '数据库操作失败id不合法'}
return {'errcode': 200, 'errmsg': 'ok'}
# 获取整个表
def getAllKeyword():
arr = []
try:
for i in col('keyword').find().sort("date",-1):
arr.append(i)
except Exception as e:
return {'errcode': 401, 'errmsg': 'keyword表获取失败'}
return {'errcode': 200, 'arr': json_util.dumps(arr), 'errmsg': 'ok'}
# 模糊搜索
def fuzzySearch(keyword):
arr = []
try:
for i in col('keyword').find({'keywords': re.compile(keyword)}):
arr.append(i)
except Exception as e:
return {'errcode': 501, 'errmsg': '模糊搜索keyword获取失败', 'arr': [],}
return {'errcode': 200, 'arr': arr, 'errmsg': 'ok'}
# 增加查询数
def addCount(id):
try:
res = col('keyword').update_one({"_id": ObjectId(id)},{"$inc": {"num": 1}})
except Exception as e:
return {'errcode': 502, 'errmsg': 'num自增失败',}
return {'errcode': 200, 'errmsg': 'ok'}
# 更新关键词
def updateKeyword(id, keywords, content, date_now, news):
try:
col('keyword').update({"_id": ObjectId(id)},
{'$set': {'keywords': keywords, 'content': content, 'date': date_now, 'news': news}})
except Exception as e:
# 失败
return {'errcode': 601, 'errmsg': '更新keyword数据库修改失败'}
return {'errcode': 200, 'errmsg': 'ok'}
# 删库
def clearKeyword():
try:
col('keyword').remove({})
except Exception as e:
# 失败
return {'errcode': 801, 'errmsg': 'keyword数据库清空失败'}
return {'errcode': 200, 'errmsg': 'ok'}
# 获取整个表excel用
def getAllKeywordList():
arr = []
try:
for i in col('keyword').find():
arr.append(i)
except Exception as e:
return {'errcode': 901, 'errmsg': 'keyword表获取失败'}
return {'errcode': 200, 'arr': arr, 'errmsg': 'ok'}