App下載

Python怎么實(shí)現(xiàn)京東云無線寶消息推送功能

猿友 2021-07-23 10:48:17 瀏覽數(shù) (2503)
反饋

不知道小伙伴們有沒有了解過京東云無線寶?曾經(jīng)在各種廣告中號(hào)稱閑置路由器能賺錢的就是他。雖然現(xiàn)實(shí)中京東云無線寶的收益并不是那么可觀,但聊勝于無。不過路由器在那運(yùn)行很安靜,不會(huì)自己給我們報(bào)備它的工作信息這點(diǎn)令小編有點(diǎn)不滿意,在翻閱了大佬的代碼后設(shè)計(jì)了一個(gè)python實(shí)現(xiàn)京東云無線寶的消息推送的python代碼,有了它你就能更了解你的京東云無線寶的工作情況了。

配置文件

注釋里面有寫,懶得用配置文件了

代碼

# 京東云無線路由寶推送
import requests
import json
import time
import hmac
import hashlib
import base64
import urllib.parse
from datetime import datetime
# 發(fā)送釘釘消息
def sendinfo_ding(token,secret,data):
    dic=get_timestamp_and_sign_by_secret(secret)
    timestamp=dic['timestamp']
    sign=dic['sign']
    url = 'https://oapi.dingtalk.com/robot/send?access_token=%s&timestamp=%s&sign=%s' %(token,timestamp,sign)  #你的機(jī)器人webhook地址
    headers = {'Content-Type': 'application/json'}
    f = requests.post(url, data=json.dumps(data), headers=headers)
# 獲取密簽
def get_timestamp_and_sign_by_secret(secret):
    timestamp = str(round(time.time() * 1000))
    secret_enc = secret.encode('utf-8')
    string_to_sign = '{}
{}'.format(timestamp, secret)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    return {"timestamp":timestamp,"sign":sign}
# 獲取可用積分
def get_jd_total_avail_point(wskey):
    url='https://router-app-api.jdcloud.com/v1/regions/cn-north-1/pinTotalAvailPoint'
    headers = {'Content-Type': 'application/json','wskey':wskey}
    rsp=requests.get(url,headers=headers)
    data=(json.loads(rsp.text)['result']['totalAvailPoint'])
    return {'totalAvailPoint':data}
# 獲取積分詳情
def get_jd_detail(wskey):
    url='https://router-app-api.jdcloud.com/v1/regions/cn-north-1/todayPointDetail?sortField=today_point&sortDirection=DESC&pageSize=15&currentPage=1'
    headers = {'Content-Type': 'application/json','wskey':wskey}
    rsp=requests.get(url,headers=headers)
    data=(json.loads(rsp.text)['result'])
    items=data['pointInfos']
    total_today_point=0
    total_all_point=0
    dic={}
    dic['todayDate']=data['todayDate']
    dic['items']=[]
    for item in items:
        mac=item['mac']
        today_point=item['todayPointIncome']
        all_point=item['allPointIncome']
        total_today_point+=int(today_point)
        total_all_point+=int(all_point)
        dic['items'].append(item)
    dic['total_today_point']=total_today_point
    dic['total_all_point']=total_all_point
    return dic
# 發(fā)送京東路由寶日?qǐng)?bào)
def send_jd_router(wskey):
    dic=get_jd_detail(wskey)
    msg='# 京東路由寶日?qǐng)?bào)
'
    msg+='## %s 
' %(dic['todayDate'])
    msg+=('> 今日獲取總積分為**%d**分,對(duì)應(yīng)金錢為 **%.2f**元
' %(dic['total_today_point'],float(dic['total_today_point'])/100))
    for item in dic['items']:
        msg+=('>> 設(shè)備**%s** 
' %(item['mac']))
        msg+=('>>> 今日獲取積分為**%s**分,對(duì)應(yīng)金錢為 **%.2f**元 

' %(item['todayPointIncome'],float(item['todayPointIncome'])/100))
    msg+=('> 累計(jì)總積分為 **%s** 分,對(duì)應(yīng)金錢 **%.2f** 元 

' %(dic['total_all_point'],float(dic['total_all_point'])/100))
    dic=get_jd_total_avail_point(wskey)
    msg+=('> 目前可用積分為 **%s** 分,對(duì)應(yīng)金錢 **%.2f** 元' %(dic['totalAvailPoint'],float(dic['totalAvailPoint'])/100))

    # token=""
    # secret=""
    # data = {
    #     "msgtype": "markdown",
    #     "markdown": {
    #         "title":"京東云路由寶日?qǐng)?bào)",
    #         "text": msg
    #         },
    # }
    # sendinfo_ding(token,secret,data)
    
    # 更改server醬發(fā)送
    server_send_wechat('京東路由寶日?qǐng)?bào)',msg)

#Server醬發(fā)送微信
def server_send_wechat(text,desp=''):
    # Server醬 Key
    key=''
    url=('https://sc.ftqq.com/%s.send' %key)
    params={
        'text':text,
        'desp':desp
    }
    requests.get(url,params)

# 更改為配置文件獲取 todo
wskey=''
while True:
    now = datetime.now()
    print("任務(wù)執(zhí)行的時(shí)間為:"+datetime.strftime(now,"%Y-%m-%d:%H:%M:%S"))
    if now.hour==8 and now.minute==0:
        send_jd_router(wskey)
    time.sleep(60)


以上就是python實(shí)現(xiàn)京東云無線寶的消息推送的詳細(xì)內(nèi)容,更多Python學(xué)習(xí)資料請(qǐng)關(guān)注W3Cschool



0 人點(diǎn)贊