Python 京东云无线宝消息推送功能
作者:Alex_Mercer 发布时间:2021-08-19 09:45:57
标签: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 = '{}\n{}'.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='# 京东路由宝日报\n'
msg+='## %s \n' %(dic['todayDate'])
msg+=('> 今日获取总积分为**%d**分,对应金钱为 **%.2f**元\n' %(dic['total_today_point'],float(dic['total_today_point'])/100))
for item in dic['items']:
msg+=('>> 设备**%s** \n' %(item['mac']))
msg+=('>>> 今日获取积分为**%s**分,对应金钱为 **%.2f**元 \n\n' %(item['todayPointIncome'],float(item['todayPointIncome'])/100))
msg+=('> 累计总积分为 **%s** 分,对应金钱 **%.2f** 元 \n\n' %(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编程之微信推送模板消息功能示例
本文实例讲述了Python微信推送模板消息功能。分享给大家供大家参考,具体如下:
具体代码如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import httplib
import json
import MySQLdb
#从数据库中获取access_token
access_token=""
try:
conn=MySQLdb.connect(host='192.168.1.1',user='root',passwd='root',db='db_weixin',port=3306)
cur=conn.cursor()
cur.execute('select access_token from weixin_public')
result=cur.fetchone()
#print result
#print result[0]
access_token=result[0]
cur.close()
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
#根据接口推送消息
if not access_token is None:
conn = httplib.HTTPConnection("api.weixin.qq.com:80")#微信接口链接
headers = {"Content-type":"application/json"} #application/x-www-form-urlencoded
params = ({'touser' : "oEGZ4johnKOtayJbnEVeuaZr6zQ0",#用户openid
'template_id' : 'AtFuydv8k_15UGZuFntaBzJRCsHCkjNm1dcWD3A-11Y',#模板消息ID
'url' : 'https://www.jb51.net',#跳转链接
"topcolor" : "#667F00",#颜色
"data" : {#模板内容
"first" : {"value" : "尊敬的710.so : 您的网站https://www.jb51.net (192.168.1.1) 有异常访问", "color" : "#173177"},
"keyword1" : {"value" : "访问时间 2015-04-05 15:30:59 访问IP 192.168.1.2", "color" : "#173177"},
"keyword2" : {"value" : "访问链接 https://www.jb51.net", "color" : "#173177"},
"remark" : {"value" : "访问频率 10/s", "color" : "#173177"}
}
}
)
conn.request("POST", "/cgi-bin/message/template/send?access_token="+access_token, json.JSONEncoder().encode(params), headers)#推送消息请求
response = conn.getresponse()
data = response.read()#推送返回数据
if response.status == 200:
print 'success'
print data
else:
print 'fail'
conn.close()
希望本文所述对大家Python程序设计有所帮助。
来源:https://www.cnblogs.com/Alex-Mercer/p/14429647.html
0
投稿
猜你喜欢
- 该sql如下:Select /*+ parallel(src, 8) */ distinct src.systemn
- 学习JQUERY就应该从最基本的学起,基本的就应该是语法了,在这里,我们有必要先温习一下JAVASCRIPT的一些知识。语法就不用说了,都是
- 通过学习借鉴朋友的实现方法进行整理,实现了PHP版的微信公共平台消息主动推送,分享给大家供大家参考,具体内容如下此方法是通过模拟登录微信公共
- 问题:我想每日从数据库里导出一些数据,内容基本上都是一样的,只是时间不同,比如导出一张表wjzcreate table wjz(id int
- Access保留字&变量名列表,建表时应避免使用这些词汇和符号。Access 2002/2003-A &nbs
- 由于XML本身的诸多优点,XML技术已被广泛的使用,目前的好多软件技术同XML紧密相关,比如微软的.net 平台对xml提供了强大的支持,提
- 本文实例讲述了PHP采集静态页面并把页面css,img,js保存的方法。分享给大家供大家参考。具体分析如下:这是一个可以获取网页的html代
- 1、首先模拟python类似shell命令行操作的接口:python安装subprocess(本地)、paramiko(SSH远程)#-*-
- 一.前言众所周知:没有对象怎么办?那就new一个!那么在JS中,当我们new一个对象的时候,这个new关键字内部都干了什么呢?现在我们就来剖
- 1、说明拼接文件路径,可以有多个参数。2、语法os.path.join(path1,path2,*)path1 初始路径。path2 需要拼
- python爬虫用mongodb的原因:1、文档结构的存储方式简单讲就是可以直接存json,list2、不要事先定义”表”,随时可以创建3、
- 一、Python的字典在项目的开发过程中,如果遇到有映射关系的内容可以考虑使用Python中的字典进行存储数据,字典中冒号前的数据称为【键】
- 发一个数字拼图游戏,有点小疑问前几天写得,其中一段代码还要感谢“簡簡單單愛妳”的提示,不过我还是不太明白, ,有点笨。 $(&qu
- 我们这里所说的head区域,是指页页html代码的<head>和</head>之间的内容。在以前的文章中,主要介绍了
- 内容摘要:当讨论Request对象内容时,要研究的集合之一就是ServerVariables集合。这个集合包含了两种值的结合体,一种是随同页
- 在近几年Python的呼声越来越高,很多刚开始起步想要学习编程的朋友都会犹豫要不要选择学习Python,毕竟作为人工智能时代的首选语言这个诱
- ModuleNotFoundError: No module named ‘dlib'尝试命令,但没成功pip3 install d
- 从百度查到在django中,使用post方法时,需要先生成随机码,以防止CSRF(Cross-site request forgery)跨站
- 其实所有的死锁最深层的原因就是一个:资源竞争。表现一:一个用户A 访问表A(锁住了表A),然后又访问表B
- “用户体验”作为舶来品在国内风靡已经有几个年头了,而且从目前情况来看仍旧会继续风靡一段时间。当某产品发布会上,发言人张口闭口就