Remember工具 半成品

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: 前言Mac上有一个自带的备忘录,感觉还挺好用的。然后也想自己动手,做个类似的Remember小工具来玩一下。工具类型:胖服务端,瘦客户端的模式。大致的场景就是客户端只管把自己想让被提醒的事项发给服务器端,然后配合自己的本地扫描,对符合要求的memo进行弹框提醒。

禅心 · 悟道

前言

Mac上有一个自带的备忘录,感觉还挺好用的。然后也想自己动手,做个类似的Remember小工具来玩一下。

工具类型:胖服务端,瘦客户端的模式。大致的场景就是客户端只管把自己想让被提醒的事项发给服务器端,然后配合自己的本地扫描,对符合要求的memo进行弹框提醒。

最近对Redis比较着迷一点,被其优雅高效的设计所打动。虽然对于搜索方面支持的不太好,但是搜索的话使用专业的搜索服务就好了。我个人比较崇尚Unix工具系的宗旨:一个工具只专注于做一件事,这也是Redis目前所体现的。

服务器端

对于服务器端的设计的初衷,是一个“胖胖的”,做了大部分的工作的形式,但是做着做着,发现客户端做的工作其实也蛮多的。目前服务器端的任务是:
- 接受用户注册
- 对memo支持CRUD
- 对请求进行“安全甄选”

目录结构

  server ll *.py
-rw-r--r--  1 changba164  staff   346B Oct 28 14:53 datastructor.py # 常见数据结构bean
-rw-r--r--  1 changba164  staff   2.6K Oct 28 18:22 redishelper.py  # redis操作相关的工具类
-rw-r--r--  1 changba164  staff   4.6K Oct 28 18:42 server.py   # 对客户端提供服务支持

代码比较简单,下面简单来看下具体的内容。

datastructor.py

#!/usr/bin python
# coding: utf8
# file: .py

import sys

reload(sys)
sys.setdefaultencoding('utf8')
import json

class ResponseCode(object):
    """
    服务响应码
    """
    def __init__(self, code, msg):
        self.code = code
        self.msg = msg

    def getcode(self):
        return json.dumps({"code":self.code, "msg":self.msg})

redishelper.py

#!/usr/bin python
# coding: utf8
# file: .py

import sys

reload(sys)
sys.setdefaultencoding('utf8')

import redis
import hashlib
import uuid
import time

class RedisHelper(object):
    """
    redis操作相关的工具类。
    """
    def __init__(self):
        self.rs = redis.Redis(host="localhost", port=6379, db=3, encoding="utf8", charset="utf8")
        # 相关key
        self.uids = "userids:set"
        self.rank = "unfinished:zset:"
        self.unfinished = "unfinished:hash:"
        self.finished = "finished:hash:"

    def check_request_valid(self, uid, securitycode):
        if uid is None:
            return False
        if securitycode == hashlib.md5(uid).hexdigest():
            return True
        return False

    def register(self, uid):
        if uid is None:
            return False
        self.rs.sadd(self.uids, uid)
        return True

    def check_user(self, uid):
        if uid is None:
            return False
        return True if self.rs.sismember(self.uids, uid) else False

    def add_memo(self, uid, memoid, memo=""):
        if uid is None:
            return False
        memoid = memoid
        self.rs.sadd(str(uid), memoid)
        self.rs.hset(self.unfinished+str(uid), memoid, memo)
        self.rs.zadd(self.rank+str(uid), memoid, int(memoid))


    def update_memo(self, uid, memoid, memo):
        if uid is None:
            return False
        if not self.rs.sismember(str(uid), memoid):
            return False
        self.rs.hset(self.unfinished+str(uid), memoid, memo)
        return True

    def delete_memo(self, uid, memoid):
        if uid is None:
            return False
        memo = self.rs.hget(self.unfinished+str(uid), memoid)
        self.rs.hset(self.finished+str(uid), memoid, memo)
        self.rs.zrem(self.rank+str(uid), memoid)
        return True

    def get_memo(self, uid, memoid):
        if uid is None:
            return None
        return self.rs.hget(self.unfinished+str(uid), memoid)

    def get_memos(self, uid, reverse=True):
        if uid is None:
            return None
        memoids = self.get_memoids(uid, reverse)
        print memoids
        memos = []
        for item in memoids:
            memos.append(self.rs.hget(self.unfinished+str(uid), item[0]))
        return memos

    def get_memoids(self, uid, reverse=True):
        if uid is None:
            return []
        if reverse == True:
            return [item[0] for item in self.rs.zrevrange(self.rank+str(uid), 0, -1, withscores=True)]
        else:
            return [item[0] for item in self.rs.zrange(self.rank+str(uid), 0, -1, withscores=True)]

server.py

#!/usr/bin python
# coding: utf8
# file: .py

import sys

reload(sys)
sys.setdefaultencoding('utf8')
from flask import Flask, request
import redishelper
import datastructor
import logging
import json

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s:%(levelname)s %(message)s',
    datafmt="%Y-%m-%d %H:%i:%s",
    filename="/Users/changba164/guo/tools/remeber/server/memo-server.log",
    filemode="a"
)

app = Flask(__name__)
helper = redishelper.RedisHelper()


@app.route("/", methods=['GET', 'POST'])
def home():
    return "It works!"

@app.route("/register", methods=["POST"])
def register():
    uid = request.form["uid"]
    helper.register(uid)
    res = datastructor.ResponseCode(1000, "uid={}".format(uid))
    return res.getcode()

@app.route("/add", methods=["POST"])
def add():
    uid = request.form['uid']
    securitycode = request.form['securitycode']
    memoid = request.form['memoid']
    memo = request.form['memo']

    isuserexists = helper.check_user(uid)
    if isuserexists == False:
        return datastructor.ResponseCode(1001, "该用户还未注册哟!").getcode()
    isvalid = helper.check_request_valid(uid, securitycode)
    logging.info("{}: {}".format(uid, securitycode))
    if isvalid == False:
        return datastructor.ResponseCode(1002, "身份信息不合法!").getcode()
    helper.add_memo(uid, memoid, memo)
    return datastructor.ResponseCode(1000, "memo已经保存啦!").getcode()

@app.route("/update", methods=["POST"])
def update():
    uid = request.form['uid']
    securitycode = request.form['securitycode']
    memoid = request.form['memoid']
    memo = request.form['memo']

    isuserexists = helper.check_user(uid)
    if isuserexists == False:
        return datastructor.ResponseCode(1001, "该用户还未注册哟!").getcode()
    isvalid = helper.check_request_valid(uid, securitycode)
    if isvalid == False:
        return datastructor.ResponseCode(1002, "身份信息不合法!").getcode()
    helper.update_memo(uid, memoid, memo)
    return datastructor.ResponseCode(1000, "memo已经更新啦!").getcode()

@app.route("/delete", methods=["POST"])
def deletememo():
    uid = request.form['uid']
    securitycode = request.form['securitycode']
    memoid = request.form['memoid']

    isuserexists = helper.check_user(uid)
    if isuserexists == False:
        return datastructor.ResponseCode(1001, "该用户还未注册哟!").getcode()
    isvalid = helper.check_request_valid(uid, securitycode)
    if isvalid == False:
        return datastructor.ResponseCode(1002, "身份信息不合法!").getcode()
    helper.delete_memo(uid, memoid)
    return datastructor.ResponseCode(1000, "memo已经删除/完成啦!").getcode()

@app.route("/detail", methods=["POST"])
def detail():
    uid = request.form['uid']
    securitycode = request.form['securitycode']
    memoid = request.form['memoid']

    isuserexists = helper.check_user(uid)
    if isuserexists == False:
        return datastructor.ResponseCode(1001, "该用户还未注册哟!").getcode()
    isvalid = helper.check_request_valid(uid, securitycode)
    if isvalid == False:
        return datastructor.ResponseCode(1002, "身份信息不合法!").getcode()
    detail = helper.get_memo(uid, memoid)
    return datastructor.ResponseCode(1000, detail).getcode()

@app.route("/lists", methods=['POST'])
def lists():
    uid = request.form['uid']
    securitycode = request.form['securitycode']
    reverse = request.form['reverse']
    isuserexists = helper.check_user(uid)
    if isuserexists == False:
        return datastructor.ResponseCode(1001, "该用户还未注册哟!").getcode()
    isvalid = helper.check_request_valid(uid, securitycode)
    if isvalid == False:
        return datastructor.ResponseCode(1002, "身份信息不合法!").getcode()
    memos = helper.get_memos(uid, reverse)
    res = datastructor.ResponseCode(1000, json.dumps((memos))).getcode()
    return res

@app.route("/recent", methods=['POST'])
def recentids():
    uid = request.form['uid']
    securitycode = request.form['securitycode']
    isuserexists = helper.check_user(uid)
    if isuserexists == False:
        return datastructor.ResponseCode(1001, "该用户还未注册哟!").getcode()
    isvalid = helper.check_request_valid(uid, securitycode)
    if isvalid == False:
        return datastructor.ResponseCode(1002, "身份信息不合法!").getcode()
    memoid = helper.get_memoids(uid, False)[0]
    result = {
        "memoid": memoid,
        "memo": helper.get_memo(uid, memoid)
    }
    res = datastructor.ResponseCode(1000, json.dumps((result))).getcode()
    return res


if __name__ == '__main__':
    app.run(host='localhost', port=9999, debug=True)

“瘦客户端”

客户端所要做的无非是:新增memo,获得最需要处理的memo,并进行弹窗提示等。目前客户端的任务还不算完成,也就简单实现了下。

  client ls
testutils.py utils.py
  client

utils.py

#!/usr/bin python
# coding: utf8
# file: .py

import sys

reload(sys)
sys.setdefaultencoding('utf8')

import requests
import json
import hashlib
import time
import datetime
import tkMessageBox
from Tkinter import *

def get_security_code(uid):
    return hashlib.md5(uid).hexdigest()

def time_to_stamp(seed):
    """
    将白话点的时间转换为时间戳,格式为: 00:00:00:00 天:小时:分钟:秒
    """
    curtime = int(time.time())
    d, h, m, s = (int(item) for item in str(seed).split(":"))
    targettime = curtime + (d*86400)+ (h*3600)+(m*60)+s
    return targettime

def stamp_to_time(stamp):
    return datetime.datetime.utcfromtimestamp(float(stamp))

def make_dialog(timestr, msg):
    tkMessageBox.showinfo(title=timestr, message=msg)
    # root = Tk()
    # root.title(timestr)
    # frame = Frame(root)
    # Label(frame, text=msg).pack()
    # frame.pack(side=TOP)
    # root.mainloop()

class UserService(object):
    """
    注册用户
    """
    def __init__(self):
        pass

    @staticmethod
    def register(uid):
        if uid is None:
            return False
        url = "http://localhost:9999/register"
        response = requests.post(url, data={"uid": uid})
        if response.status_code == 200 and response.json()['code'] == 1000:
            return True
        return False




class MemoHelper(object):
    """
    客户端memo工具类
    """
    def __init__(self):
        self.url = "http://localhost:9999/{}"

    def post(self, uid, memoid, memo):
        posturl = self.url.format("add")
        payload = {
            "uid": uid,
            "securitycode": get_security_code(uid),
            "memoid": memoid,
            "memo": memo
        }
        response = requests.post(posturl, data=payload)
        return response.text
    def getmemo(self, uid, memoid):
        url = self.url.format("detail")
        payload = {
            "uid": uid,
            "securitycode": get_security_code(uid),
            "memoid": memoid
        }
        response = requests.post(url, data=payload)
        return response.text
    def getmemos(self, uid, reverse=True):
        url = self.url.format("lists")
        payload = {
            "uid": uid,
            "securitycode": get_security_code(uid),
            "reverse": reverse
        }
        response = requests.post(url, data=payload)
        return response.text
    def getrecent(self, uid):
        url = self.url.format("recent")
        payload = {
            "uid": uid,
            "securitycode": get_security_code(uid)
        }
        response = requests.post(url, data=payload)
        return response.text
    def updatememo(self, uid, memoid, memo):
        url = self.url.format("update")
        payload = {
            "uid": uid,
            "securitycode": get_security_code(uid),
            "memoid": memoid,
            "memo": memo
        }
        response = requests.post(url, data=payload)
        return response.text
    def deletememo(self, uid, memoid):
        url = self.url.format("delete")
        payload = {
            "uid": uid,
            "securitycode": get_security_code(uid),
            "memoid": memoid
        }
        response = requests.post(url, data=payload)
        return response.text


class Emitter(object):
    """
    检测到到期任务,则弹出一个提醒框!
    """
    def __init__(self, uid):
        self.uid = str(uid)
        self.memohelper = MemoHelper()

    def emit_in(self, timestr, memo):
        timestamp = time_to_stamp(timestr)
        self.memohelper.post(self.uid, timestamp, memo)



    def emit_out(self):
        # 如果时间符合要求就emit出来, 找出距离时间最远的memo。
        data = self.memohelper.getrecent(self.uid)
        data = json.loads(data)
        data = data['msg']
        data = json.loads(data)
        targettime = stamp_to_time(data['memoid'])
        memo = data['memo']
        make_dialog(targettime, memo)


if __name__ == '__main__':

    emitter = Emitter(201393260)
    emitter.emit_out()

总结

总体来说,这个小工具没有达到我的预期效果,大致有如下几点:
- 扫描服务可以说没怎么做。
- “安全”这里简单就是限制客户端请求方式为POST,简单的使用securitycode做了下判断(如果被抓包分析的话,那也没什么用,比较好的做法就是模仿Flask使用PIN码,来进一步提高安全性)。
- 客户端弹框效果不是很理想,而且对于发布一个POST而言,还没做对应的GUI支持,目前也就在命令行里面使用类似:

emitin xxxxxxx a:b:c:d
大致的意思是: 记录 在a天b小时c分钟d秒 后要做的xxxxxxxx事项。

有兴趣的可以在目前的代码基础上进行优化,这里权当抛砖引玉吧。

目录
相关文章
|
机器学习/深度学习 人工智能 监控
AI算法分析,智慧城管AI智能识别系统源码
AI视频分析技术应用于智慧城管系统,通过监控摄像头实时识别违法行为,如违规摆摊、垃圾、违章停车等,实现非现场执法和预警。算法平台检测街面秩序(出店、游商、机动车、占道)和市容环境(垃圾、晾晒、垃圾桶、路面不洁、漂浮物、乱堆物料),助力及时处理问题,提升城市管理效率。
488 4
AI算法分析,智慧城管AI智能识别系统源码
|
机器学习/深度学习 JSON JavaScript
图文讲解 Stable Diffusion API 调用教程
Stable Diffusion 是一个先进的深度学习模型,用于创造和修改图像。这个模型能够基于文本描述来生成图像,让机器理解和实现用户的创意。使用这项技术的关键在于掌握其 API,通过编程来操控图像生成的过程。
|
索引 Python
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
220 0
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
|
10月前
|
前端开发 Java 编译器
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
299 36
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
|
机器学习/深度学习 人工智能 自然语言处理
【EMNLP2024】阿里云人工智能平台 PAI 多篇论文入选 EMNLP2024
阿里云人工智能平台 PAI 的多篇论文在 EMNLP2024 上入选。论文成果是阿里云与华南理工大学金连文教授团队、复旦大学王鹏教授团队共同研发。EMNLP 是人工智能自然语言处理领域的顶级国际会议,聚焦于自然语言处理技术在各个应用场景的学术研究,尤其重视自然语言处理的实证研究。该会议曾推动了预训练语言模型、文本挖掘、对话系统、机器翻译等自然语言处理领域的核心创新,在学术和工业界都有巨大的影响力。此次入选标志着阿里云人工智能平台 PAI 在自然语言处理和多模态算法能力方面研究获得了学术界认可。
|
SQL 关系型数据库 MySQL
惊呆:where 1=1 可能严重影响性能,差了10多倍,快去排查你的 sql
老架构师尼恩在读者交流群中分享了关于MySQL中“where 1=1”条件的性能影响及其解决方案。该条件在动态SQL中常用,但可能在无真实条件时导致全表扫描,严重影响性能。尼恩建议通过其他条件或SQL子句命中索引,或使用MyBatis的`<where>`标签来避免性能问题。他还提供了详细的执行计划分析和优化建议,帮助大家在面试中展示深厚的技术功底,赢得面试官的青睐。更多内容可参考《尼恩Java面试宝典PDF》。
|
关系型数据库 分布式数据库 数据库
基于PolarDB Ganos的实时时空计算:电子围栏篇
文章着重介绍了PolarDB Ganos如何应用于实现实时电子围栏计算。这是一种依赖于位置技术来创建虚拟地理边界的解决方案,广泛应用于交通安全、应急管理、营销推广等多个领域。通过与阿里云实时计算Flink版产品的集成,PolarDB Ganos能够高效地进行空间计算和数据分析,显著提高了地理围栏应用的实时性和准确性。文章还提供了使用Ganos进行电子围栏计算的实际步骤、性能测试及优化建议,并强调了PolarDB Ganos在提高数据处理效率和降低成本方面的优势。
|
C# UED
41.C#:Timer控件
41.C#:Timer控件
335 1
|
缓存 新能源 中间件
我看技术人的成长路径
有一句诗词说:宠辱不惊,看庭前花开花落;去留无意,望天上云卷云舒。其实就是讲内心修炼到了一种心境平和,淡泊自然的境界。
5287 70
我看技术人的成长路径
|
Oracle Java 关系型数据库
Linux 安装 Maven
Linux 安装 Maven
9217 1