-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathdiscuz.py
169 lines (147 loc) · 6.1 KB
/
discuz.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
import random
import login
import time
import logging
import re
from random import randint
from bs4 import BeautifulSoup
import requests
import sys
import config
logging.basicConfig(level=logging.INFO, filename='info.log', format="%(asctime)s %(filename)s %(funcName)s:line %(lineno)d %(levelname)s %(message)s")
class Discuz:
def __init__(self, hostname, username, password, chatgpt_key, questionid='0', answer=None, cookies_flag=True, pub_url=''):
self.chatgpt_key = chatgpt_key
self.hostname = hostname
if pub_url != '':
self.hostname = self.get_host(pub_url)
self.discuz_login = login.Login(self.hostname, username, password, questionid, answer, cookies_flag)
def login(self):
self.discuz_login.main()
self.session = self.discuz_login.session
self.formhash = self.discuz_login.post_formhash
def get_host(self, pub_url):
res = requests.get(pub_url)
res.encoding = "utf-8"
url = re.search(r'a href="https://(.+?)/".+?>.+?入口</a>', res.text)
if url != None:
url = url.group(1)
logging.info(f'获取到最新的论坛地址:https://{url}')
return url
else:
logging.error(f'获取失败,请检查发布页是否可用{pub_url}')
return self.hostname
def go_home(self):
return self.session.get(f'https://{self.hostname}/forum.php').text
def go_hot(self):
return self.session.get(f'https://{self.hostname}/forum-45-1.html').text
def get_reply_tid_list(self):
tids = []
soup = BeautifulSoup(self.go_hot(), features="html.parser")
replys = []
reply = soup.select_one('#threadlisttableid')
replys.append(reply)
pattern = re.compile(r'thread-')
for reply in replys:
for a in reply.find_all("a", href=pattern):
if '机器人' in str(a) or '测试' in str(a) or '封号' in str(a):
continue
url = a['href']
match = re.search(r'thread-(\d+)', url)
if match:
tids.append(match.group(1))
return tids
def get_reply_tid(self):
tids = self.get_reply_tid_list()
if len(tids) > 0:
return tids[randint(0, len(tids) - 1)]
else:
logging.error('tid获取失败,退出')
sys.exit()
def chat_with_gpt(self, prompt):
url = config.chatgpt_api_url + "/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + self.chatgpt_key,
}
data = {
"model": config.chatgpt_model,
"messages": [
{"role": "system", "content": config.chatgpt_prompt},
{"role": "user", "content": prompt}
],
"max_tokens": 500,
"temperature": 0.7
}
response = requests.post(url, headers=headers, json=data)
response_json = response.json()
print("chatgpt response -->" + str(response_json))
if "choices" in response_json:
choices = response_json["choices"]
if len(choices) > 0 and "message" in choices[0] and "content" in choices[0]["message"]:
return choices[0]["message"]["content"]
return None
def generate_random_numbers(self, start, end, count):
random_numbers = []
for _ in range(count):
random_number = random.randint(start, end)
random_numbers.append(random_number)
return random_numbers
def signin(self):
signin_url = f'https://{self.hostname}'
self.session.get(signin_url)
def visit_home(self):
start = 1 # 起始数字
end = 50000 # 结束数字
count = 10 # 随机数字的数量
random_numbers = self.generate_random_numbers(start, end, count)
for number in random_numbers:
time.sleep(5)
signin_url = f'https://{self.hostname}/space-uid-{number}.html'
self.session.get(signin_url)
def reply(self, tid, message=''):
topic_url = f'https://{self.hostname}/thread-{tid}-1-1.html'
res = self.session.get(topic_url).text
prompt = "你好,请直接回复两句古诗"
pattern = r'<meta\s+name="description"\s+content="([^"]+)"\s*/>'
match = re.search(pattern, res)
if match:
prompt = match.group(1)
response = self.chat_with_gpt(prompt)
if response:
reply_url = f'https://{self.hostname}/forum.php?mod=post&action=reply&tid={tid}&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1'
data = {
'file': '',
'message': response,
'posttime': int(time.time()),
'formhash': self.formhash,
'usesig': 1,
'subject': '',
}
res = self.session.post(reply_url, data=data).text
if 'succeed' in res:
url = re.search(r'succeedhandle_fastpost\(\'(.+?)\',', res).group(1)
logging.info(f'回复发送成功,tid:{tid},回复:{response},链接:{"https://" + self.hostname + "/" + url}')
else:
logging.error('回复发送失败\t' + res)
else:
logging.error('ChatGPT未能成功获取回复\t')
if __name__ == '__main__':
# 循环执行每对用户名、密码和ChatGPT密钥的组合
for credentials in config.user_credentials:
hostname = 'hostloc.com'
username = credentials['username']
password = credentials['password']
# 随机选择一个ChatGPT密钥
chatgpt_key = random.choice(config.chatgpt_keys)
discuz = Discuz(hostname, username, password, chatgpt_key)
discuz.login()
discuz.signin()
discuz.visit_home()
if config.auto_replay:
# 循环执行50次
for i in range(50):
# 执行方法
discuz.reply(discuz.get_reply_tid())
# 等待5分钟 , 5分钟 = 5 * 60秒 = 300秒
time.sleep(300)