写了一个定时脚本,不想在服务器上安装PHP、nodejs等语言。刚好看每个服务器上都有python,于是用python做。

脚本涉及到下载图片和post请求来自动发朋友圈。测试通过后,在服务器上竟然无法定时启动,原来脚本首行需要加上:

#!/usr/bin/python3

#!/usr/bin/python3
#下载图片到指定位置,并POST到发送朋友圈API
import requests
import urllib.request

sourceUrl = "http://xxxx.herokuapp.com/api/resources/miniature"

rj = requests.get(sourceUrl).json()
imgURL = rj['data']['url']
urllib.request.urlretrieve(imgURL, "/var/www/html/miniature/file.jpg")


postUrl = 'https://wecrm.xxx.net/api/wechat/send'

rj['type'] = 'postImages'
rj['to'] = 'myPost'

url = "http://miniature.xxxx.net/file.jpg"
rj['data']['title'] = "以下使用个人号「定时发送」功能\n所有内容均可自定义,联系我使用\n您可回复【每日一图】给我,获取当日图片\n也可回复【订阅 每日一图】给我,每日自动获取图片\n内容来源: https://miniature-calendar.com"
rj['data']['urls'] = [url]

requests.post(postUrl, json = rj, headers = {"Authorization": "Bearer xxxx","Content-Type":"application/json"})

#!/usr/bin/python3

#请求API获取json数据,并POST到发送朋友圈API
#cat /var/www/html/python/qqMusic.py

import sys
import requests
keyword = sys.argv[1]
comment = sys.argv[2]
postUrl = 'https://xxxxxx/api/wechat/send'
sourceUrl = "http://xxxxx.herokuapp.com/api/resources/"+keyword

rj = requests.get(sourceUrl).json()
rj['type'] = 'postLink'
rj['to'] = 'myPost'

rj['data']['comment'] = "以下使用个人号「定时发送」功能\n所有内容均可自定义,联系我使用\n"+comment+"\n"+rj['data']['description']

requests.post(postUrl, json = rj, headers = {"Authorization": "Bearer Vxxxxx","Content-Type":"application/json"})

crontab:

0 8 * * * python3 /var/www/html/python/qqMusic.py 8点1氪 "8点1氪-互联网人的资讯早餐 每日早报 回复【订阅 8点1氪】给我每日提前接收" https://img.36krcdn.com/20200411/v2_e71e2069fac04da894aefc02d4656231_img_000
0 8 * * * python3 /var/www/html/python/qqMusic.py 华尔街见闻早餐 "回复【订阅 华尔街见闻早餐】给我每日提前接收" https://dn-wscn-avatar.qbox.me/50/0d/32/-logo.png
0 8 * * * python3 /var/www/html/python/postLink.py ifanr "回复【订阅 ifanr】给我每日提前接收 每日早报"

0 7 * * * python3 /var/www/html/python/qqMusic.py odb "Helping you connect with God. Every day. Every way." https://odb.org/ODBM_Logo-small.png
0 7 * * * python3 /var/www/html/python/postMusic.py 620 "" https://txly2.net/images/program_banners/mw_prog_banner_sq.png
0 9 * * * python3 /var/www/html/python/miniature.py