50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
import json
|
|
import redis
|
|
import fangjia
|
|
import config
|
|
from datetime import datetime, date
|
|
|
|
|
|
class CJsonEncoder(json.JSONEncoder):
|
|
def default(self, obj):
|
|
if isinstance(obj, datetime):
|
|
return obj.strftime('%Y-%m-%d %H:%M:%S')
|
|
elif isinstance(obj, date):
|
|
return obj.strftime('%Y-%m-%d')
|
|
else:
|
|
return json.JSONEncoder.default(self, obj)
|
|
|
|
|
|
QUEUE = "calc_cmd"
|
|
redisPool = redis.ConnectionPool(host=config.get_redis_host(), port=6379, db=config.get_redis_db())
|
|
client = redis.Redis(connection_pool=redisPool)
|
|
|
|
|
|
def get_district():
|
|
seaweeds = []
|
|
keys = {"city": 1, "region": 1, "name": 1, "_id": 0}
|
|
for dis in fangjia.get_collection("fangjia", "seaweed").find({"cat": "district", "status": 0, "city": "上海"}, keys):
|
|
dis["weekend"] = datetime.strptime("2018-03-11", "%Y-%m-%d")
|
|
seaweeds.append(dis)
|
|
|
|
return seaweeds
|
|
|
|
|
|
def send_cmd(seaweed):
|
|
"""
|
|
cmd = {}
|
|
cmd["city"] = seaweed["city"]
|
|
cmd["region"] = seaweed["region"]
|
|
cmd["name"] = seaweed["name"]
|
|
"""
|
|
|
|
json_cmd = json.dumps(seaweed, ensure_ascii=False, cls=CJsonEncoder)
|
|
client.rpush(QUEUE, json_cmd)
|
|
print("命令发送成功:" + json_cmd)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
for seaweed in get_district():
|
|
if len(seaweed) > 2:
|
|
send_cmd(seaweed)
|