Skip to content

Commit

Permalink
修改路径问题、新增企业微信通知
Browse files Browse the repository at this point in the history
  • Loading branch information
curtinlv committed Jun 8, 2021
1 parent d20ee43 commit 190c779
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
106 changes: 105 additions & 1 deletion getFollowGifts/jd_getFollowGift.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
PUSH_PLUS_TOKEN = ''
#Bark 推送
BARK = ''
#企业微信推送
QYWX_AM = ''

#######################################
version = 'v1.0.0 Beta'
Expand Down Expand Up @@ -76,10 +78,18 @@
import re, json, base64
from urllib.parse import unquote, quote_plus

# 获取当前工作目录
pwd = repr(os.path.dirname(sys.argv[0]))
pwd = pwd.replace('\'', '')
if pwd:
os.chdir(pwd)
pwd = os.path.abspath('.') + '/'

# 定义一些要用到参数
requests.packages.urllib3.disable_warnings()
scriptHeader = """
════════════════════════════════════════
║ ║
║ JD 关 注 有 礼 ║
Expand All @@ -99,7 +109,7 @@

def getCookie():
global cookies
ckfile = 'JDCookies.txt'
ckfile = pwd + 'JDCookies.txt'
try:
if os.path.exists(ckfile):
cookies = ''
Expand Down Expand Up @@ -159,6 +169,10 @@ def getCookie():
PUSH_PLUS_TOKEN = os.environ["PUSH_PLUS_TOKEN"]
print("已获取并使用Env环境 PUSH_PLUS_TOKEN")
# 获取企业微信应用推送 QYWX_AM
if "QYWX_AM" in os.environ:
if len(os.environ["QYWX_AM"]) > 1:
QYWX_AM = os.environ["QYWX_AM"]
print("已获取并使用Env环境 QYWX_AM")
if "BARK" in os.environ:
if len(os.environ["BARK"]) > 1:
BARK = os.environ["BARK"]
Expand Down Expand Up @@ -224,6 +238,89 @@ def telegram_bot(title, content):
except Exception as e:
print(e)

# 企业微信 APP 推送
def wecom_app(title, content):
try:
if not QYWX_AM:
print("QYWX_AM 并未设置!!\n取消推送")
return
QYWX_AM_AY = re.split(',',QYWX_AM)
if 4 < len(QYWX_AM_AY) > 5:
print("QYWX_AM 设置错误!!\n取消推送")
return
corpid=QYWX_AM_AY[0]
corpsecret=QYWX_AM_AY[1]
touser=QYWX_AM_AY[2]
agentid=QYWX_AM_AY[3]
try:
media_id=QYWX_AM_AY[4]
except :
media_id=''
wx=WeCom(corpid, corpsecret, agentid)
# 如果没有配置 media_id 默认就以 text 方式发送
if not media_id:
message=title + '\n\n' + content
response=wx.send_text(message, touser)
else:
response=wx.send_mpnews(title, content, media_id, touser)
if response == 'ok':
print('推送成功!')
else:
print('推送失败!错误信息如下:\n',response)
except Exception as e:
print(e)

class WeCom:
def __init__(self, corpid, corpsecret, agentid):
self.CORPID = corpid
self.CORPSECRET = corpsecret
self.AGENTID = agentid
def get_access_token(self):
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
values = {'corpid': self.CORPID,
'corpsecret': self.CORPSECRET,
}
req = requests.post(url, params=values)
data = json.loads(req.text)
return data["access_token"]
def send_text(self, message, touser="@all"):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token()
send_values = {
"touser": touser,
"msgtype": "text",
"agentid": self.AGENTID,
"text": {
"content": message
},
"safe": "0"
}
send_msges=(bytes(json.dumps(send_values), 'utf-8'))
respone = requests.post(send_url, send_msges)
respone = respone.json()
return respone["errmsg"]
def send_mpnews(self, title, message, media_id, touser="@all"):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token()
send_values = {
"touser": touser,
"msgtype": "mpnews",
"agentid": self.AGENTID,
"mpnews": {
"articles":[
{
"title": title,
"thumb_media_id": media_id,
"author": "Author",
"content_source_url": "",
"content": message.replace('\n','<br/>'),
"digest": message
}
]
}
}
send_msges=(bytes(json.dumps(send_values), 'utf-8'))
respone = requests.post(send_url, send_msges)
respone = respone.json()
return respone["errmsg"]
#push推送
def pushplus_bot(title, content):
try:
Expand Down Expand Up @@ -292,10 +389,17 @@ def send(title, content):
else:
print('未启用Bark APP应用消息推送')
continue
elif i == 'wecom_app':
if QYWX_AM:
wecom_app(title=title, content=content)
else:
print('未启用企业微信应用消息推送')
continue
else:
print('此类推送方式不存在')



def iscookie():
"""
:return: cookiesList,userNameList,pinNameList
Expand Down
6 changes: 4 additions & 2 deletions getFollowGifts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
##
#########################################
#主脚本路径。默认和主脚本同级目录
scriptPath="${BASH_SOURCE%/*}/jd_getFollowGift.py"
cd `dirname $0`
curpwd=`pwd`
scriptPath="${curpwd}/jd_getFollowGift.py"
################### ↓↓↓【以下需要配置的参数,代替配置文件OpenCradConfig.ini】↓↓↓ ###################
#京东cookie 格式:pt_key=xxx;pt_pin=xxx; & pt_key=xxx;pt_pin=xxx; (多账号&分隔)
export JD_COOKIE=''
Expand Down Expand Up @@ -37,7 +39,7 @@ export BARK=

################### ↑↑↑↑ 你需要填的参数到此结束 ↑↑↑↑ ##############################
######### 以下不用配置,默认就好 ##########################
cd `dirname $0`

_printTime(){
echo "[`date +"%F %T"`]: $1"
}
Expand Down

0 comments on commit 190c779

Please sign in to comment.