-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTwit.py
384 lines (322 loc) · 16.1 KB
/
Twit.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# This program is a rough draft, but it works well for current x.com settings. I created this to spread more information about Qelm.
# Currently this program calls your keys from the keys.py file and sends random texts from a text file you include in the same folder.
# Fixed a few repeat issues.
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import datetime, os, random, re, time
import tweepy
import openai
import ssl
import urllib.parse
import threading
import keys
def initialize_tweepy():
client_v2 = tweepy.Client(
consumer_key=keys.api_key,
consumer_secret=keys.api_secret,
access_token=keys.access_token,
access_token_secret=keys.access_token_secret
)
auth = tweepy.OAuthHandler(keys.api_key, keys.api_secret)
auth.set_access_token(keys.access_token, keys.access_token_secret)
api_v1 = tweepy.API(auth)
return client_v2, api_v1
def generate_response(prompt):
openai.api_key = keys.openai_key
model = "gpt-4-1106-preview"
if hasattr(openai, 'ChatCompletion'):
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
temperature=0
)
return response.choices[0].message.content.strip()
else:
fallback_model = "text-davinci-003"
response = openai.Completion.create(
model=fallback_model,
prompt=prompt,
temperature=0,
max_tokens=150
)
return response.choices[0].text.strip()
def get_formatted_date():
return datetime.date.today().strftime("%B %d, %Y")
class AutoTweetApp:
def __init__(self, master):
self.master = master
master.title("Auto Tweet Program")
master.configure(bg='#ADD8E6')
self.scheduled_tweets = []
self.client_v2 = None
# Track file uploads for both standard tweet usage and AI template usage
self.uploaded_file_path = None
self.notebook = ttk.Notebook(master)
self.notebook.pack(fill='both', expand=True)
# Tab 1: Scheduling
self.tab1 = ttk.Frame(self.notebook)
self.notebook.add(self.tab1, text='Scheduling')
self.create_scheduling_tab(self.tab1)
# Tab 2: AI Tweets
self.tab2 = ttk.Frame(self.notebook)
self.notebook.add(self.tab2, text='AI Tweets')
self.create_ai_tweets_tab(self.tab2)
# Tab 3: Progress & Daily
self.tab3 = ttk.Frame(self.notebook)
self.notebook.add(self.tab3, text='Progress & Daily')
self.create_progress_daily_tab(self.tab3)
def create_scheduling_tab(self, parent):
frame = ttk.Frame(parent, padding="5")
frame.pack(fill=tk.BOTH, expand=True)
self.btn_test_tweet = ttk.Button(frame, text="Send Test Tweet", command=self.send_test_tweet)
self.btn_test_tweet.grid(row=0, column=0, pady=3, sticky='ew')
self.btn_upload = ttk.Button(frame, text="Upload File", command=self.upload_file)
self.btn_upload.grid(row=0, column=1, pady=3, sticky='ew')
ttk.Label(frame, text="Tweet Content:").grid(row=1, column=0, sticky=tk.W)
self.text_tweet = tk.Text(frame, height=3, width=40)
self.text_tweet.grid(row=2, column=0, columnspan=2, pady=3, sticky='ew')
self.use_file_var = tk.BooleanVar()
self.check_use_file = ttk.Checkbutton(frame, text="Use random tweet from file", variable=self.use_file_var)
self.check_use_file.grid(row=3, column=0, columnspan=2, pady=3, sticky='w')
ttk.Label(frame, text="Select Days:").grid(row=4, column=0, sticky=tk.W)
days_frame = ttk.Frame(frame)
days_frame.grid(row=5, column=0, columnspan=2, sticky='w')
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
self.days_vars = {}
for d in days:
var = tk.BooleanVar()
cb = ttk.Checkbutton(days_frame, text=d, variable=var)
cb.pack(side=tk.LEFT, padx=2)
self.days_vars[d] = var
ttk.Label(frame, text="Select Hours:").grid(row=6, column=0, sticky=tk.W, pady=(5,0))
self.listbox_hours = tk.Listbox(frame, selectmode='multiple', height=4)
self.listbox_hours.grid(row=7, column=0, columnspan=2, sticky='ew')
hours = [f"{h:02d}:00" for h in range(24)]
for hour in hours:
self.listbox_hours.insert(tk.END, hour)
self.btn_schedule = ttk.Button(frame, text="Schedule Tweet", command=self.schedule_tweet)
self.btn_schedule.grid(row=8, column=0, columnspan=2, pady=5, sticky='ew')
ttk.Label(frame, text="Scheduled Tweets:").grid(row=9, column=0, sticky=tk.W)
self.listbox_scheduled = tk.Listbox(frame, selectmode='extended', height=6)
self.listbox_scheduled.grid(row=10, column=0, columnspan=2, sticky='ew', pady=3)
self.btn_activate = ttk.Button(frame, text="Activate/Deactivate Selected", command=self.toggle_tweet)
self.btn_activate.grid(row=11, column=0, columnspan=2, pady=3, sticky='ew')
self.btn_remove = ttk.Button(frame, text="Remove Selected", command=self.remove_selected_tweets)
self.btn_remove.grid(row=12, column=0, columnspan=2, pady=3, sticky='ew')
def create_ai_tweets_tab(self, parent):
frame = ttk.Frame(parent, padding="5")
frame.pack(fill=tk.BOTH, expand=True)
ttk.Label(frame, text="AI Tweet Template:").grid(row=0, column=0, sticky=tk.W)
# AI template from uploaded file
self.use_file_for_ai_var = tk.BooleanVar()
self.check_use_file_for_ai = ttk.Checkbutton(frame, text="Use file for AI template", variable=self.use_file_for_ai_var)
self.check_use_file_for_ai.grid(row=0, column=1, sticky='w')
self.ai_template = tk.Text(frame, height=3, width=40)
self.ai_template.grid(row=1, column=0, columnspan=3, pady=3, sticky='ew')
self.btn_preview_ai = ttk.Button(frame, text="Generate Preview", command=self.preview_ai_tweet)
self.btn_preview_ai.grid(row=2, column=0, pady=3, sticky='ew')
self.btn_send_ai = ttk.Button(frame, text="Send AI Tweet", command=self.send_ai_custom_tweet)
self.btn_send_ai.grid(row=2, column=1, pady=3, sticky='ew')
ttk.Label(frame, text="Preview:").grid(row=3, column=0, sticky=tk.W)
self.ai_preview = tk.Text(frame, height=4, width=40, state='disabled')
self.ai_preview.grid(row=4, column=0, columnspan=3, pady=3, sticky='ew')
def create_progress_daily_tab(self, parent):
frame = ttk.Frame(parent, padding="5")
frame.pack(fill=tk.BOTH, expand=True)
ttk.Label(frame, text="Progress:").pack(anchor=tk.W)
self.progress = ttk.Progressbar(frame, length=200, mode='determinate')
self.progress.pack(anchor=tk.W)
ttk.Label(frame, text="Today's Schedule:").pack(anchor=tk.W, pady=(5,0))
self.listbox_daily = tk.Listbox(frame, height=10, width=50)
self.listbox_daily.pack(fill=tk.BOTH, expand=True)
def send_test_tweet(self):
client_v2, _ = initialize_tweepy()
try:
with open('tweets.txt', 'r', encoding='utf-8') as f:
content = f.read()
tweets = re.findall(r"\{(.*?)\}", content, re.DOTALL)
if not tweets:
raise ValueError("No tweets found enclosed in curly braces.")
tweet_text = random.choice(tweets).strip()
except Exception as e:
messagebox.showerror("Error", f"Failed to read tweets from file: {e}")
return
try:
response = client_v2.create_tweet(text=tweet_text)
messagebox.showinfo("Success", "Test tweet sent successfully!")
except Exception as e:
messagebox.showerror("Error", f"Failed to send test tweet: {e}")
def preview_ai_tweet(self):
if self.use_file_for_ai_var.get():
if not self.uploaded_file_path:
messagebox.showerror("Error", "No file has been uploaded for AI template.")
return
try:
with open(self.uploaded_file_path, 'r', encoding='utf-8') as f:
prompt = f.read().strip()
except Exception as e:
messagebox.showerror("Error", f"Could not read AI template from file: {e}")
return
else:
prompt = self.ai_template.get("1.0", tk.END).strip()
if not prompt:
messagebox.showerror("Error", "Please provide an AI tweet template.")
return
try:
tweet_text = generate_response(prompt)
self.ai_preview.config(state='normal')
self.ai_preview.delete("1.0", tk.END)
self.ai_preview.insert(tk.END, tweet_text)
self.ai_preview.config(state='disabled')
except Exception as e:
messagebox.showerror("Error", f"AI generation failed: {e}")
def send_ai_custom_tweet(self):
client_v2, _ = initialize_tweepy()
if self.use_file_for_ai_var.get():
if not self.uploaded_file_path:
messagebox.showerror("Error", "No file has been uploaded for AI template.")
return
try:
with open(self.uploaded_file_path, 'r', encoding='utf-8') as f:
prompt = f.read().strip()
except Exception as e:
messagebox.showerror("Error", f"Could not read AI template from file: {e}")
return
else:
prompt = self.ai_template.get("1.0", tk.END).strip()
if not prompt:
messagebox.showerror("Error", "Please provide an AI tweet template.")
return
try:
tweet_text = generate_response(prompt)
except Exception as e:
messagebox.showerror("Error", f"AI generation failed: {e}")
return
try:
response = client_v2.create_tweet(text=tweet_text)
messagebox.showinfo("Success", "AI tweet posted successfully!")
except Exception as e:
messagebox.showerror("Error", f"Failed to send AI tweet: {e}")
def upload_file(self):
file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv"), ("Text files", "*.txt")])
if file_path:
try:
with open(file_path, 'r', encoding='utf-8') as f:
_ = f.read()
self.uploaded_file_path = file_path
messagebox.showinfo("File Loaded", f"Successfully loaded {os.path.basename(file_path)}")
except Exception as e:
messagebox.showerror("Error", f"Failed to read file: {e}")
def schedule_tweet(self):
if self.use_file_var.get():
try:
with open('tweets.txt', 'r', encoding='utf-8') as f:
content = f.read()
tweets = re.findall(r"\{(.*?)\}", content, re.DOTALL)
if not tweets:
raise ValueError("No tweets found enclosed in curly braces.")
except Exception as e:
messagebox.showerror("Error", f"Failed to get tweet from file: {e}")
return
else:
tweet_content = self.text_tweet.get("1.0", tk.END).strip()
if not tweet_content:
messagebox.showerror("Error", "Tweet content cannot be empty.")
return
selected_days = [day for day, var in self.days_vars.items() if var.get()]
selected_hours = [self.listbox_hours.get(i) for i in self.listbox_hours.curselection()]
if not selected_days:
messagebox.showerror("Error", "Please select at least one day.")
return
if not selected_hours:
messagebox.showerror("Error", "Please select at least one hour.")
return
start_date = datetime.date.today()
end_date = start_date + datetime.timedelta(days=30)
current_date = start_date
while current_date <= end_date:
if current_date.strftime('%A') in selected_days:
for hour_str in selected_hours:
hour_int = int(hour_str.split(':')[0])
scheduled_datetime = datetime.datetime.combine(current_date, datetime.time(hour_int, 0))
# Skip scheduling if the scheduled time is now or already in the past
if scheduled_datetime <= datetime.datetime.now():
continue
if self.use_file_var.get():
content_to_schedule = random.choice(tweets).strip()
else:
content_to_schedule = tweet_content
duplicate = any(
t["content"] == content_to_schedule and t["datetime"] == scheduled_datetime
for t in self.scheduled_tweets
)
if duplicate:
continue
tweet_data = {
"content": content_to_schedule,
"datetime": scheduled_datetime,
"active": True
}
self.scheduled_tweets.append(tweet_data)
display_text = f"{content_to_schedule[:30]}... at {scheduled_datetime.strftime('%Y-%m-%d %H:%M')} (Active)"
self.listbox_scheduled.insert(tk.END, display_text)
current_date += datetime.timedelta(days=1)
messagebox.showinfo("Scheduled", "Tweet(s) scheduled successfully!")
def toggle_tweet(self):
selection = self.listbox_scheduled.curselection()
if selection:
index = selection[0]
tweet = self.scheduled_tweets[index]
tweet["active"] = not tweet["active"]
status = "Active" if tweet["active"] else "Inactive"
self.listbox_scheduled.delete(index)
content = tweet["content"]
scheduled_datetime = tweet["datetime"]
display_text = f"{content[:30]}... at {scheduled_datetime.strftime('%Y-%m-%d %H:%M')} ({status})"
self.listbox_scheduled.insert(index, display_text)
else:
messagebox.showerror("Error", "No tweet selected")
def remove_selected_tweets(self):
selected_indices = self.listbox_scheduled.curselection()
if not selected_indices:
messagebox.showerror("Error", "No tweets selected for removal.")
return
for index in reversed(selected_indices):
self.listbox_scheduled.delete(index)
del self.scheduled_tweets[index]
messagebox.showinfo("Removed", "Selected tweets removed from schedule.")
def update_progress(self, value):
self.progress['value'] = value
self.master.update_idletasks()
def run_scheduler(self):
now = datetime.datetime.now()
# Update today's schedule listbox
self.listbox_daily.delete(0, tk.END)
for tweet_dict in self.scheduled_tweets:
if tweet_dict["active"] and tweet_dict["datetime"].date() == now.date():
time_str = tweet_dict["datetime"].strftime("%H:%M")
self.listbox_daily.insert(tk.END, f"{time_str} - {tweet_dict['content'][:30]}...")
# Attempt to post tweets that are due
client_v2, _ = initialize_tweepy()
for tweet_dict in self.scheduled_tweets:
if tweet_dict["active"] and tweet_dict["datetime"] <= now:
try:
posted_tweet = client_v2.create_tweet(text=tweet_dict["content"])
print(f"Tweet Posted: ID {posted_tweet.data['id']}")
tweet_dict["active"] = False
self.update_progress(100)
time.sleep(1) # Throttle requests
except tweepy.TweepyException as e:
messagebox.showerror("Error", f"Error posting tweet: {e}")
# Check again state (edit value in seconds)
self.master.after(60000, self.run_scheduler)
def start(self):
self.run_scheduler()
self.master.mainloop()
if __name__ == "__main__":
root = tk.Tk()
app = AutoTweetApp(root)
app.start()