-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpythonTryEverything.py
202 lines (165 loc) · 6.87 KB
/
pythonTryEverything.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import os
import logging
import platform
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
from apscheduler.schedulers.background import BackgroundScheduler
import sleep
from threading import Thread
from flask import Flask, request, jsonify
from queue import Queue
from flask import Flask, request, abort, render_template
from wechatpy import parse_message, create_reply
from wechatpy.utils import check_signature
from wechatpy.exceptions import (
InvalidSignatureException,
InvalidAppIdException,
)
import signal
from wechat import push_get_up
# import wechat.push_get_up
q = Queue(100) # 创建一个先进先出的队列
# 定义消费者线程
class Consumer(Thread):
def run(self):
global q
while True:
logging.info('消费者线程开始消费线程了')
msg = q.get() # 默认阻塞
logging.info('消费线程得到了数据:{}'.format(msg))
sleep.send_msg_to_blog(msg)
LOG_FORMAT = "[%(asctime)s][%(levelname)s][%(filename)s:%(funcName)s:%(lineno)d] %(message)s"
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
# https://github.com/zhayujie/bot-on-anything
# https://github.com/zhayujie/chatgpt-on-wechat
# set token or get from environments 三个参数是可以的
TOKEN = os.getenv("WECHAT_TOKEN", "testtest")
AES_KEY = os.getenv("WECHAT_AES_KEY", "Quk4OepZpDVkhWnevUNnOXfoynwT1gg83cYdBpaZXYP")
APPID = os.getenv("WECHAT_APPID", "wx94e03776e64ee600")
app = Flask(__name__)
@app.route("/wechat", methods=["GET", "POST"])
def wechat():
signature = request.args.get("signature", "")
timestamp = request.args.get("timestamp", "")
nonce = request.args.get("nonce", "")
encrypt_type = request.args.get("encrypt_type", "raw")
msg_signature = request.args.get("msg_signature", "")
try:
check_signature(TOKEN, signature, timestamp, nonce)
except InvalidSignatureException:
abort(403)
if request.method == "GET":
echo_str = request.args.get("echostr", "")
return echo_str
# POST request
if encrypt_type == "raw":
# plaintext mode
msg = parse_message(request.data)
if msg.type == "text":
content = " 加油,小王同学,我相信你一定能做到 \n"
content = content + msg.content
logging.info(content)
global q
q.put(content)
reply = create_reply(content, msg)
else:
reply = create_reply("Sorry, can not handle this for now", msg)
return reply.render()
else:
# encryption mode
from wechatpy.crypto import WeChatCrypto
crypto = WeChatCrypto(TOKEN, AES_KEY, APPID)
try:
msg = crypto.decrypt_message(request.data, msg_signature, timestamp, nonce)
except (InvalidSignatureException, InvalidAppIdException):
abort(403)
else:
msg = parse_message(msg)
if msg.type == "text":
reply = create_reply(msg.content, msg)
else:
reply = create_reply("Sorry, can not handle this for now", msg)
return crypto.encrypt_message(reply.render(), nonce, timestamp)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/api/json', methods=['POST'])
def example():
if request.method == 'POST':
# 从 POST 请求的 JSON 数据中获取参数
data = request.json
name = data.get('name')
age = data.get('age')
# 在服务端进行一些处理
# ...
# 返回 JSON 格式的响应
response = {'message': 'Hello, {}, you are {} years old.'.format(name, age)}
return jsonify(response)
class TimeoutException(Exception):
pass
def timeout_handler(signum, frame):
raise TimeoutException("Function execution timed out.")
def markdown_to_wechat():
# 微信公共号文章发布
push_get_up.markdown_to_wechat()
def ai_to_mp4():
""" 自定上传视频内容到不同的平台"""
try:
# 避免熬夜21点到凌晨3点不工作 每周节省3小时时间
if platform.system() == "Windows":
OUT_PATH = r"D:\mp4\output"
BACK_PATH = r"D:\mp4\back"
else:
OUT_PATH = r"/root/mp4/input"
BACK_PATH = r"/root/mp4/bak"
sleep.auto_upload_mp4()
finally:
print("end")
def EeasyHabitSleep():
""" 自定上传视频内容到不同的平台"""
signal.alarm(3600)
try:
sleep.show_sleep()
finally:
# 取消超时设置
signal.alarm(0)
if __name__ == "__main__":
# 设置超时时间为20分钟
signal.signal(signal.SIGALRM, timeout_handler)
# not execute logging fucntion before here
logging.basicConfig(level=logging.DEBUG,
format=LOG_FORMAT,
datefmt=DATE_FORMAT,
filename="./pythonTryEverything.log"
)
logging.info("""
┌──────────────────────────────────────────────────────────────────────┐
│ │
│ • Start Beemove • │
│ │
└──────────────────────────────────────────────────────────────────────┘
""")
job_defaults = {
'coalesce': False,
'max_instances': 1
}
backsched = BackgroundScheduler(job_defaults=job_defaults, timezone='Asia/Shanghai')
# 习惯养成--早睡早起
backsched.add_job(EeasyHabitSleep, CronTrigger.from_crontab("30 6 * * *"), id="do_show_sleep_job")
backsched.add_job(markdown_to_wechat, CronTrigger.from_crontab("10 6 * * *"), id="markdown_to_wechat")
backsched.add_job(ai_to_mp4, CronTrigger.from_crontab("30 7 * * *"), id="auto mp4")
# backsched.add_job(sleep.show_sleep, CronTrigger.from_crontab("0 6 * * *"), id="do_show_sleep_job")
backsched.start()
t2 = Consumer()
t2.start()
# server_address = ("", 8089)
# httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
# httpd.serve_forever()
# host: 绑定的ip(域名)
# port: 监听的端口号
# debug: 是否开启调试模式
app.run(host="0.0.0.0", port=80, debug=True, use_reloader=False)
# export FLASK_APP=xx.py # 指定flask应用所在的文件路径
# export FLASK_ENV=development # 设置项目的环境, 默认是生产环境
# flask run -h 0.0.0.0 -p 8000 # 启动测试服务器并接受请求
# http://127.0.0.1:8000/