还是老题目,如下:
这次使用google的firebase接口,来做相关处理。(增删改查)
关于firebase,之前有用过:https://www.toutiao.com/article/7084441585076683278/
这里直接把代码放进来即可,
import redis
from redis import StrictRedis,ConnectionPool
import time
import re,requests
from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession
import os,sys,json,datetime
def get_input():
#输入键盘数据,返回键盘的输入数据
instructions = """
Please provide a task which you want input,
if you wanna write a task,with the format like this:
new notes go to school today
if you wanna list the task, with the format like this:
list notes
if you wanna delete some note, you can use list task to check the notes id ,
then use this command to delete the note
delete note id 1
if you wanna stop writing
just enter a blank ,then the loop will not continue
"""
print(instructions)
values = input("Please provide the information")
return values
def judge_input(values):
#判断获取到的字符串的含义,如果是插入则插入,如果是删除则删除,如果是显示,则显示,如果是空白则退出循环
if re.match("new notes",values) !=None:
hanyi = 0
value = values.replace("new notes ",'',1)
if re.match("list notes",values) !=None:
hanyi = 2
value = values.replace("list notes ",'',1)
if re.match("delete note id",values) !=None:
hanyi = 1
value = values.replace("delete note id ",'',1)
if re.match(" ",values) != None:
hanyi = 3
value = " "
return hanyi,value
def store_info(note):
today = datetime.date.today().strftime('%y%m%d')
diary = {'date':today, 'note':note}
# Define the required scopes
scopes = [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/firebase.database"
]
# Authenticate a credential with the service account
credentials = service_account.Credentials.from_service_account_file(
"**/serviceAccountKey.json", scopes=scopes)
authed_session = AuthorizedSession(credentials)
# Use the credentials object to authenticate a Requests session.
# authed_session = AuthorizedSession(credentials)
response1= authed_session.post("https://lukechengtest-default-rtdb.firebaseio.com/notes.json",json=json.dumps(diary))
print("You note is saved")
def drop_redis(value):
#如果含义是删除,则做删除操作
pool = ConnectionPool(host='localhost', port=6379, db=0, decode_responses=True)
r = StrictRedis(connection_pool=pool)
r.hdel('note',value)
print("the date {} has been removed".format(value))
def display_info():
print("infomation displayed")
scopes = [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/firebase.database"
]
# Authenticate a credential with the service account
credentials = service_account.Credentials.from_service_account_file(
"**/serviceAccountKey.json", scopes=scopes)
authed_session = AuthorizedSession(credentials)
response = authed_session.get(
"https://lukechengtest-default-rtdb.firebaseio.com/notes.json")
dic=json.loads(response.content)
#print(dic)
for i in dic.values():
#print("date: {0}".format(i['date']),end="")
#print("note: {0}".format(i['note']))
print(i)
if __name__ == "__main__":
while 1:
values = get_input()
hanyi,notes = judge_input(values)
if hanyi == 0:
#如果是插入,则做插入操作
store_info(notes)
if hanyi == 1:
#如果是删除,则做删除操作
drop_redis(notes)
if hanyi == 2:
#如果是展示,则做展示操作
display_info()
if hanyi == 3:
#如果含义是退出,则跳出循环
break
上一次只用了增加和查询的接口,直接放进去,测试下增加和查询能力,目前看是OK的
增加功能
查询功能:
在后台查询数据库
这里要做删除,没看到描述,google了一下,youtube 里面给了一个:
印度妹子介绍,用
https://www.youtube.com/watch?v=H9cR1eoAtYM
看了下,用的是pyrebase 这个接口,
https://github.com/thisbejim/Pyrebase
改天用pyrebase 的增删改查弄一下,先写到这里。技术不难实现,还是比较简单的,这里就不再写了。
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved