用户:
lsk666666查看:0 回复:5 评论:0 创建时间:2021-11-03T21:37:53
半年没更新了出来冒个泡awa
如题,这个是我去年做的编程猫消息提醒的2.0,链接一会放评论区里。这次更新主要就是重构了一下代码(周日一下午一晚上写的,懒得分模块了awa),删掉了没太大用的东西,加上了调用dbus发送系统通知的代码,把信息抽象到配置文件里,安装为系统服务(只做了linux的,windows的忘了怎么做了,有会的请给我写段,谢谢),以及吧做了个配置的GUI(这个过段时间在更,众所周知,我恨GUI开发,我只是吧GUI用Qt画出来了。代码框架包给@Albert钟写,等他完事了就发布GUI模块)
就这样,下面是代码,不长。就100多行
import sys
sys.path.append("lib")
sys.path.append("notifcation")
from pyLog import Logger
from pyBases import *
from pyProperties import parse
import requests
import time
from requests.cookies import RequestsCookieJar
from json import dumps,loads
from bs4 import BeautifulSoup
configFile = parse("main.properties")
username = configFile.get("login.username")
password = configFile.get("login.password")
ignoreSystem = bool(configFile.get("message.ignoreSystem"))
ignoreLike = bool(configFile.get("message.ignoreLike"))
updatesPerHour = configFile.get("message.updatePerHour")
updateSpace = 60 / int(updatesPerHour)
enableDebug = bool(configFile.get("log.enableDebug"))
logFile = configFile.get("log.file")
notificationImplementer = configFile.get("notification.implementer")
notify = getattr(__import__(notificationImplementer), "notify")
logger = Logger("listener", enableDebug=enableDebug, logFile=logFile)
cache = {
"authorization" : ""
}
class Response:
def __init__(self,text,cookies):
self.text = text
self.cookies = cookies
def getCookie(self,key):
return self.cookies[key]
def getText(self):
return self.text
def cookieDict(self):
return self.cookies
def login():
ses = requests.session()
headers = {"Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; ) AppleWebKit/537.36 (KH喵L, like Gecko) Chrome/81.0.4044.138 Safari/537.36"}
shequ = requests.get('https://shequ.codemao.cn', headers = headers)
soup = BeautifulSoup(shequ.text, 'html.parser')
pid = loads(soup.find_all("script")[0].string.split("=")[1])['pid']
logger.debug("pid : "+str(pid))
a = ses.post('https://api.codemao.cn/tiger/v3/web/accounts/login', headers = headers,data = dumps({"identity": username, "password": password, "pid": pid}))
cookies = a.cookies.get_dict()
logger.debug(cookies)
logger.info("authorization : "+cookies["authorization"])
loginResp = Response(a.text,cookies)
shequResp = Response(shequ.text,shequ.cookies.get_dict())
return loginResp,shequResp
def privateMessageCount(authorization):
cookie_jar = RequestsCookieJar()
cookie_jar.set("authorization",authorization)
resp = requests.get("https://api.codemao.cn/web/message-record/count",cookies = cookie_jar)
logger.debug(resp)
respJson = resp.json()
logger.debug(respJson)
commentJson = respJson[0]
comment = commentJson["count"]
likeJson = respJson[1]
like = likeJson["count"]
systemJson = respJson[2]
system = systemJson["count"]
logger.info("comment : "+str(comment))
logger.info("like : "+str(like))
logger.info("system : "+str(system))
return comment,like,system
def messageCount():
if cache["authorization"] == "":
loginResp,shequResp = login()
cache["authorization"] = loginResp.getCookie("authorization")
return messageCount()
else:
logger.info("Using cached authorization data : "+cache["authorization"])
try:
logger.info("Getting message count......")
return privateMessageCount(cache["authorization"])
except Exception as e:
logger.error("Get message count failed. Retrying...",e=e)
cache["authorization"] = ""
return messageCount()
def main():
try:
while True:
comment, like, system = messageCount()
notification = ""
if (comment == 0) and (like == 0) and (system == 0):
time.sleep(updateSpace)
continue
if not comment == 0:
notification += "你喵 + int(comment) + "条未读回复\r\n"
if (not like == 0) and not ignoreLike:
notification += int(like) + "人赞或购买了你的作品\r\n"
if (not system == 0) and not ignoreSystem:
notification += "你收到了" + int(system) + "条系统通知"
notify(notification)
time.sleep(updateSpace)
except Exception as e:
logger.error("Exception: ", e)
logger.fp.close()
sys.exit(1)
except KeyboardInterrupt:
logger.info("Ctrl+C pushed. Exiting...")
logger.fp.close()
sys.exit(0)
if __name__ == "__main__":
main()
文件结构如图:

其中,lib目录是我自己写的库,在我github/gitee能找到,notifcation目录用了放通知插件的py文件(先咕一会,过会写个libnotify通知的插件和一个从网上抄到的java提示的通知插件),main.properties是配置文件,下面是他的内容
login.username=用户名(不是昵称!!)
login.password=密码
message.updatePerHour=一小时刷新几次(不建议设得太频繁,我怕你被反爬虫)
message.ignoreSystem=忽略系统消息,true为是,false为否
message.ignoreLike=忽略点赞和购买,true为是,false为否
log.enableDebug=详细日志输出,true为是,false为否
log.file=日志文件名
notification.implementer=通知插件名,就是插件的py文件名去掉.py后缀
有问题,有Bug评论区问。预发布版,我也没时间太调试,bug可能有点多
那就这样,2.0Release见!