您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天小编给大家分享一下Python如何实现京东云无线宝消息推送功能的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
注释里面有写,懒得用配置文件了
代码
# 京东云无线路由宝推送
import requests
import json
import time
import hmac
import hashlib
import base64
import urllib.parse
from datetime import datetime
# 发送钉钉消息
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×tamp=%s&sign=%s' %(token,timestamp,sign) #你的机器人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¤tPage=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
# 发送京东路由宝日报
def send_jd_router(wskey):
dic=get_jd_detail(wskey)
msg='# 京东路由宝日报
'
msg+='## %s
' %(dic['todayDate'])
msg+=('> 今日获取总积分为**%d**分,对应金钱为 **%.2f**元
' %(dic['total_today_point'],float(dic['total_today_point'])/100))
for item in dic['items']:
msg+=('>> 设备**%s**
' %(item['mac']))
msg+=('>>> 今日获取积分为**%s**分,对应金钱为 **%.2f**元
' %(item['todayPointIncome'],float(item['todayPointIncome'])/100))
msg+=('> 累计总积分为 **%s** 分,对应金钱 **%.2f** 元
' %(dic['total_all_point'],float(dic['total_all_point'])/100))
dic=get_jd_total_avail_point(wskey)
msg+=('> 目前可用积分为 **%s** 分,对应金钱 **%.2f** 元' %(dic['totalAvailPoint'],float(dic['totalAvailPoint'])/100))
# token=""
# secret=""
# data = {
# "msgtype": "markdown",
# "markdown": {
# "title":"京东云路由宝日报",
# "text": msg
# },
# }
# sendinfo_ding(token,secret,data)
# 更改server酱发送
server_send_wechat('京东路由宝日报',msg)
#Server酱发送微信
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("任务执行的时间为:"+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如何实现京东云无线宝消息推送功能”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。