-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowse.py
352 lines (293 loc) · 13.2 KB
/
browse.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
import time
import os
from playwright.sync_api import sync_playwright, expect
import sys, multiprocessing
import os
import json
import requests
from PyQt5.QtWidgets import (QApplication, QWidget, QLabel, QVBoxLayout, QHBoxLayout, QFrame, QMessageBox, QTextEdit, QSizePolicy, QPushButton, QScrollArea)
from PyQt5.QtGui import QFont, QIcon, QCursor, QPixmap, QColor
from PyQt5.QtCore import Qt, QSize
import logging
from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class TweetFrame(QFrame):
def __init__(self, text, is_original=False, parent=None):
super().__init__(parent)
self.setObjectName("tweetFrame")
layout = QVBoxLayout()
# Tweet text
self.tweet_text = QTextEdit(text)
self.tweet_text.setReadOnly(True)
self.tweet_text.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.tweet_text.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.tweet_text.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.tweet_text.setStyleSheet(f"color: #000000; font-size: 16px; background-color: transparent; border: none;")
layout.addWidget(self.tweet_text)
self.setLayout(layout)
self.setStyleSheet(f"""
QFrame#tweetFrame {{
background-color: #FFFFFF;
border: 1px solid #E1E8ED;
border-radius: 12px;
padding: 10px;
margin-bottom: 10px;
}}
""")
def sizeHint(self):
return self.tweet_text.document().size().toSize() + QSize(20, 20)
class TweetResponder(QWidget):
def __init__(self, original_post, reply, suggestions, page):
super().__init__()
self.reply = reply
self.original_post = original_post
self.suggestions = suggestions
self.page = page
self.setWindowTitle("Tweet and Suggestions")
self.resize(800, 1000)
self.setWindowIcon(QIcon('icon.png'))
self.setStyleSheet("""
QWidget {
background-color: #F5F8FA;
color: #14171A;
font-family: "Helvetica Neue", Arial, sans-serif;
}
QLabel {
color: #14171A;
}
QPushButton {
background-color: #1DA1F2;
color: #FFFFFF;
border: none;
border-radius: 20px;
padding: 10px;
font-size: 14px;
}
QPushButton:hover {
background-color: #1A91DA;
}
""")
self.init_ui()
def init_ui(self):
main_layout = QVBoxLayout()
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
scroll_content = QWidget()
scroll_layout = QVBoxLayout(scroll_content)
# Original Tweet Display
scroll_layout.addWidget(QLabel("Original Tweet:"))
self.tweet_frame = TweetFrame(self.original_post, is_original=True)
scroll_layout.addWidget(self.tweet_frame)
# Reply Display
scroll_layout.addWidget(QLabel("Reply:"))
if self.reply != "no replies":
self.reply_frame = TweetFrame(self.reply)
scroll_layout.addWidget(self.reply_frame)
else:
no_reply_label = QLabel("No replies yet")
no_reply_label.setStyleSheet("font-style: italic;")
scroll_layout.addWidget(no_reply_label)
# Suggestions Display
if self.suggestions:
scroll_layout.addWidget(QLabel("Suggested Replies:"))
for suggestion in self.suggestions:
suggestion_frame = QFrame()
suggestion_layout = QVBoxLayout(suggestion_frame)
suggestion_text = QTextEdit(suggestion)
suggestion_text.setReadOnly(True)
suggestion_layout.addWidget(suggestion_text)
use_reply_button = QPushButton("Use This Reply")
use_reply_button.clicked.connect(lambda _, s=suggestion: self.use_reply(s))
suggestion_layout.addWidget(use_reply_button)
scroll_layout.addWidget(suggestion_frame)
# No Response Button
no_response_button = QPushButton("No Response")
no_response_button.clicked.connect(self.no_response)
scroll_layout.addWidget(no_response_button)
scroll_layout.addStretch(1)
scroll_area.setWidget(scroll_content)
main_layout.addWidget(scroll_area)
self.setLayout(main_layout)
def use_reply(self, suggestion):
try:
logger.info("Starting reply process")
# Wait for the page to load completely
# self.page.wait_for_load_state('networkidle')
# Find all tweet articles
tweet_articles = self.page.query_selector_all('article[data-testid="tweet"]')
if len(tweet_articles) < 2:
raise Exception("Couldn't find the comment to reply to")
# Select the second tweet article (the comment)
comment_article = tweet_articles[1]
# Click on the comment to focus it
comment_article.click()
logger.info("Clicked on the comment")
# Wait for the reply input to appear
reply_input_selector = 'div[data-testid="tweetTextarea_0"]'
self.page.wait_for_selector(reply_input_selector, state="visible", timeout=5000)
logger.info("Reply input is visible")
# Fill in the reply
self.page.fill(reply_input_selector, suggestion)
logger.info("Filled in the reply")
# Find and click the Reply button
reply_button_selector = 'button[data-testid="tweetButtonInline"]'
self.page.wait_for_selector(reply_button_selector, state="visible", timeout=5000)
self.page.click(reply_button_selector)
logger.info("Clicked the Reply button")
# Wait for the reply to be posted
self.page.wait_for_timeout(2000)
QMessageBox.information(self, "Reply Sent", "Your reply has been posted successfully!")
self.close()
except PlaywrightTimeoutError as e:
logger.error(f"Timeout error: {str(e)}")
QMessageBox.critical(self, "Error", f"Timeout error: {str(e)}")
except Exception as e:
logger.error(f"Failed to post reply: {str(e)}")
QMessageBox.critical(self, "Error", f"Failed to post reply: {str(e)}")
def no_response(self):
QMessageBox.information(self, "No Response", "You have chosen not to respond.")
self.close()
CLAUDE_API_URL = 'https://api.anthropic.com/v1/messages' # Example Claude API URL
API_KEY = os.getenv('CLAUDE_API_KEY')
headers = {
'Content-Type': 'application/json',
'X-API-Key': API_KEY,
'anthropic-version': '2023-06-01' # Replace with the correct version
}
def generate_claude_replies(prompt):
"""Calls Claude API to generate replies based on the prompt."""
payload = {
'model': 'claude-3-5-sonnet-20240620',
'max_tokens': 1024,
'messages': [
{
'role': 'user',
'content': f"{prompt}\n\nPlease provide 3 distinct reply suggestions, each on a new line."
}
]
}
try:
response = requests.post(CLAUDE_API_URL, headers=headers, json=payload)
response.raise_for_status() # This will raise an exception for HTTP errors
result = response.json()
content = result.get('content', [])
if content and isinstance(content[0], dict):
text = content[0].get('text', '')
suggestions = [line.strip() for line in text.split('\n') if line.strip()]
return suggestions[:3]
else:
print("Unexpected response structure:", result)
return []
except requests.exceptions.RequestException as e:
print(f"API request failed: {e}")
if response:
print(f"Response status code: {response.status_code}")
print(f"Response content: {response.text}")
return []
except Exception as e:
print(f"An unexpected error occurred: {e}")
return []
# Replace these with your own X credentials
USERNAME = ''
PASSWORD = ''
def login_x(page):
print("Navigating to login page...")
page.goto("https://x.com/login", wait_until="networkidle")
print("Page loaded.")
# Wait for and fill in the username
username_selector = 'input[name="text"]'
print("Waiting for username input...")
page.wait_for_selector(username_selector, state='visible', timeout=60000)
print("Filling username...")
page.fill(username_selector, USERNAME)
# Press Enter instead of clicking "Next"
print("Pressing Enter after username...")
page.press(username_selector, 'Enter')
# Wait for and fill in the password
password_selector = 'input[name="password"]'
print("Waiting for password input...")
page.wait_for_selector(password_selector, state='visible', timeout=60000)
print("Filling password...")
page.fill(password_selector, PASSWORD)
# Press Enter to log in instead of clicking the button
print("Pressing Enter to log in...")
page.press(password_selector, 'Enter')
# Wait for navigation to complete
print("Waiting for navigation to complete...")
page.wait_for_timeout(5000) # Additional wait to ensure everything is loaded
def get_most_recent_tweet_and_reply(page):
print(f"Navigating to {USERNAME}'s profile...")
page.goto(f"https://x.com/{USERNAME}")
print("Scrolling to load more tweets...")
page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
page.wait_for_timeout(2000) # Wait for content to load
print("Locating the most recent tweet...")
tweet_selector = 'article[data-testid="tweet"]'
page.wait_for_selector(tweet_selector)
tweets = page.query_selector_all(tweet_selector)
print(tweets if tweets else "NO TWEETS LOCATED BRUH")
if tweets:
most_recent_tweet = tweets[0]
print("Clicking on the most recent tweet...")
most_recent_tweet.click()
page.wait_for_timeout(2000) # Wait for content to load
print("Extracting post URL...")
post_url = page.url
print("Extracting original post...")
original_post = page.query_selector('div[data-testid="tweetText"]')
original_post_text = original_post.inner_text() if original_post else "No original post found"
print("Checking for replies...")
replies = page.query_selector_all('div[data-testid="cellInnerDiv"] article')
if len(replies) > 1:
reply_text = replies[1].query_selector('div[data-testid="tweetText"]').inner_text()
print("Reply found.")
else:
reply_text = "no replies"
print("No replies found.")
return post_url, original_post_text, reply_text
else:
print("No tweets found")
return None, None, None
def save_to_file(content, filename):
downloads_folder = os.path.expanduser("~/Downloads")
file_path = os.path.join(downloads_folder, filename)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
print(f"Saved to {file_path}")
def main():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context(viewport={'width': 1280, 'height': 720})
page = context.new_page()
page.set_default_timeout(60000) # Set default timeout to 60 seconds
try:
login_x(page)
# Save a screenshot for debugging
page.screenshot(path="after_login.png")
# Retrieve most recent tweet URL, original post, and reply
post_url, original_post, reply = get_most_recent_tweet_and_reply(page)
if post_url and original_post:
print(f"Most recent tweet URL: {post_url}")
print(f"Original tweet: {original_post}")
print(f"Reply: {reply}")
prompt = f"Original Tweet: {original_post}\n Response Tweet: {reply}\nDraft a witty and engaging unique response to the response tweet. Show a bit of personality, but don't be overly formal or stiff. Keep it real. Don't be too enthusiastic or too negative. Just be chill and friendly. Do not say anything other than the 3 responses. No numbers, no heading intro, just the responses and nothing else."
suggestions = generate_claude_replies(prompt)
print("Generated suggestions:", suggestions)
if not suggestions:
print("No suggestions were generated. Proceeding with empty list.")
app = QApplication(sys.argv)
window = TweetResponder(original_post, reply, suggestions, page)
window.show()
sys.exit(app.exec_())
else:
print("Couldn't retrieve the tweet or URL.")
except Exception as e:
print(f"An error occurred: {str(e)}")
# Save a screenshot when an error occurs
page.screenshot(path="error_screenshot.png")
finally:
browser.close()
if __name__ == "__main__":
main()