自制CSDN博客評論郵件提醒





CSDN 本身是有評論郵件提醒服務的。我知道,但是。。。。。。。。。。。
你們自己看吧。。。。


在這裏插入圖片描述


幹啥啊?郵件轟炸機嘛??

拉黑,拉黑 !!!

自己搞一個了。

上代碼:

# -*- coding:utf-8 -*-
import net
import re
import json
from EmailClass import Email_L

def run():
    try:
        # 舊的評論數
        reviewCount = readReviewCount()
        # 獲取網頁上最新的評論數
        homeUrl = "https://blog.csdn.net/qq_28766327"
        homePage = net.getResponse(homeUrl).encode('utf8').decode('utf8') #注意這個編碼問題
        reviewCountResult = re.search(u'<dt>評論</dt>.*?"count">(\d*?)</span>',homePage,re.S|re.M)
        if reviewCountResult != None:
            pageReviewCount = reviewCountResult.group(1)
        else:
            pageReviewCount = reviewCount
        # 如果網頁上評論數比舊評論數多,證明有新評論
        print reviewCount
        print pageReviewCount
        if str(reviewCount) != str(pageReviewCount):
            # 將新評論數記錄下來
            writeReviewCount(pageReviewCount)
            # 獲取評論信息
            content,userName,postTime = getNewReviewDetail(homePage)
            message = "<b>"+userName+":</b>"+content+"<br>"+postTime
            # 發送提醒郵件
            emailObj = Email_L()
            emailObj.senEmail(["[email protected]"],"【新博客評論】","html",message)
    except Exception as e:
        pass

def getNewReviewDetail(homePage):
    try:
        # 獲取文章 id
        newArticleIdResult = re.search(r'class="newcomment-list".*?https://blog.csdn.net/qq_28766327/article/details/(.*?)#comments">',homePage,re.S|re.M)
        articleId = newArticleIdResult.group(1)
        # 根據文章ID獲取評論列表
        reviewListUrl = "https://blog.csdn.net/qq_28766327/phoenix/comment/list/"+str(articleId)+"?page=1&size=15&tree_type=1"
        reviewDataString = net.getResponse(reviewListUrl)
        reviewData = json.loads(reviewDataString)
        lists = reviewData["data"]["list"]
        content = ""
        userName = ""
        postTime = ""
        commentId = 0
        # 找到最新一條評論
        for item in lists:
            info = item["info"]
            if int(info["CommentId"]) > commentId:
                commentId = info["CommentId"]
                content = info["Content"]
                userName = info["UserName"]
                postTime = info["PostTime"]
            if item.has_key("sub"):
                subArray = item["sub"]
                for sub in subArray:
                    if int(sub["CommentId"]) > commentId:
                        commentId = sub["CommentId"]
                        content = sub["Content"]
                        userName = sub["UserName"]
                        postTime = sub["PostTime"]
        return content,userName,postTime
    except Exception as e:
        return None

def readReviewCount():
    try:
        with open(r'./review_count',"r") as f:
            reviewCount = f.read()
    except Exception as e:
        reviewCount = 0
    return reviewCount

def writeReviewCount(reviewCount):
    with open(r'./review_count',"w") as f:
        f.write(str(reviewCount))

if __name__ == "__main__":
    run()

備註:
1. net 模塊是我用 requets 封裝的網絡請求工具,功能是在子線程中進行網絡請求(我做另一個項目的時候發現,幾十萬次網絡請求可能有一次會卡死進程)。這裏它不重要,不要管他
2. EmailClass 是我封裝的發送郵件的工具,這裏它也不重要。不要管他。如果不知道怎麼發郵件,可以看我的其他文章,我專門寫了一篇

然後就是定時監測了,很簡單 利用 linux的 crontab 一個定時任務搞定

30 9-21 * * * cd /home/xxxxxx/xxxxx; python checkReview.py > checkReview.log 2>&1 &

部署到我的樹莓派上,完成












發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章