-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
155 lines (125 loc) · 4.3 KB
/
main.py
File metadata and controls
155 lines (125 loc) · 4.3 KB
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
import google.generativeai as genai
import asyncio
from metathreads import MetaThreads
from spider import getnews,setn_fetch_url
import random
import json
import re
data = json.load(open("config.json", encoding="utf-8"))
ACCOUNT = data["ACCOUNT"]
PASSWORD = data["PASSWORD"]
API_KEY = data["API_KEY"]
NEWS = data["NEWS"]
MODE = data["MODE"]
# Prompt填這裡
##########################
prompt = """
嗨
"""
##########################
while True:
if MODE in ['setn', 'text', 'manual']: break
MODE = input('不明的模式,模式應為 "setn" 或 "text"\n輸入執行模式: ')
print("正在登入...")
threads = MetaThreads()
threads.login(ACCOUNT,PASSWORD)
genai.configure(api_key=API_KEY)
generation_config = {
'temperature': 1,
'top_p': 0.95,
'top_k': 64,
'max_output_tokens': 2048,
}
safety_settings = [
{
'category': 'HARM_CATEGORY_HARASSMENT',
'threshold': 'block_none'
},
{
'category': 'HARM_CATEGORY_HATE_SPEECH',
'threshold': 'block_none'
},
{
'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
'threshold': 'block_none'
},
{
'category': 'HARM_CATEGORY_DANGEROUS_CONTENT',
'threshold': 'block_none'
},
]
model = genai.GenerativeModel(model_name='gemini-1.0-pro', generation_config=generation_config, safety_settings=safety_settings)
async def text_api(msg: str) -> str | None:
'''
呼叫 api 並回傳我的回應
'''
convo = model.start_chat(history=[
])
if not msg:return '這段訊息是空的'
await convo.send_message_async(msg)
reply_text = convo.last.text
return reply_text
async def text_auto_post():
while True:
reply_text = await text_api(prompt)
print("\n生成結果:" + reply_text)
print("\n\n正在發送貼文")
threads.post_thread(thread_caption = reply_text)
print("成功發送文章!")
await asyncio.sleep(60 + random.randint(10,60))
async def setn_auto_post(url):
while True:
news_url = await setn_fetch_url(url)
try:
with open('cache.txt', 'r') as file:
cached_url = file.read().strip()
except FileNotFoundError:
with open('cache.txt', 'w') as file:
file.write("")
cached_url = ""
if cached_url == f"https://www.setn.com{news_url}":
print("URL 相同,跳過後續流程")
else:
with open('cache.txt', 'w') as file:
file.write(f"https://www.setn.com{news_url}")
news = await getnews(f"https://www.setn.com{news_url}")
print("\n\n新聞內容:", news)
reply_text = await text_api(prompt + " ".join(news))
print("\n生成結果:" + reply_text)
print("\n\n正在發送貼文")
threads.post_thread(thread_caption = reply_text)
print("成功發送文章!")
await asyncio.sleep(60 + random.randint(10,60))
async def manual():
url = input("輸入新聞網址/主題:")
pattern = r'https://[^\s]+'
if re.match(pattern,url):
news = await getnews(url)
print("\n新聞內容:",news)
reply_text = await text_api(prompt + " ".join(news))
print("\n生成結果:" + reply_text)
else:
reply_text = await text_api(prompt + url)
print("\n生成結果:" + reply_text)
yes_or_no = input("\n你要發這篇文嗎: ")
if yes_or_no == "yes" or yes_or_no == "y":
if re.match(pattern,url):
yes_or_no = input("要附連結嗎:")
if yes_or_no == "yes" or yes_or_no == "y":
print("正在發送貼文")
threads.post_thread(thread_caption=f"{reply_text}\n\n{url}")
print("成功發送文章!")
else:
print("正在發送貼文")
threads.post_thread(thread_caption=f"{reply_text}")
print("成功發送文章!")
else:
print("正在發送貼文")
threads.post_thread(thread_caption=f"{reply_text}")
print("成功發送文章!")
await manual()
else:
await manual()
if MODE == "setn": asyncio.run(setn_auto_post(NEWS))
if MODE == "text": asyncio.run(text_auto_post())
if MODE == "manual": asyncio.run(manual())