-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrowser.py
More file actions
500 lines (417 loc) · 20.8 KB
/
browser.py
File metadata and controls
500 lines (417 loc) · 20.8 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import os
import re
import time
import random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from dotenv import load_dotenv
import google.generativeai as genai
import logging
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
class LinkedInBot:
def __init__(self):
self.driver = self.setup_driver()
self.login()
def setup_driver(self):
"""Sets up the Chrome WebDriver with necessary options."""
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument("--headless")
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument(
"user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
)
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.execute_script(
"Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
)
return driver
def random_delay(self, min_delay=1, max_delay=3):
"""Introduce a random delay to mimic human behavior."""
time.sleep(random.uniform(min_delay, max_delay))
def login(self):
"""Logs into LinkedIn using credentials from environment variables."""
self.driver.get("https://www.linkedin.com/login")
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
)
username_field = self.driver.find_element(By.ID, "username")
password_field = self.driver.find_element(By.ID, "password")
# Mimic human typing by sending keys with delays
for char in os.getenv("LINKEDIN_USERNAME"):
username_field.send_keys(char)
self.random_delay(0.1, 0.3)
self.random_delay()
for char in os.getenv("LINKEDIN_PASSWORD"):
password_field.send_keys(char)
self.random_delay(0.1, 0.3)
self.random_delay()
password_field.send_keys(Keys.RETURN)
self.random_delay(5, 7)
# Check for verification code input form
try:
verification_form = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, "email-pin-challenge"))
)
logging.info("Verification code required. Prompting user for input.")
verification_code = input("Enter the verification code sent to your email: ")
# Enter the verification code
code_input = self.driver.find_element(By.ID, "input__email_verification_pin")
code_input.send_keys(verification_code)
# Submit the verification form
submit_button = self.driver.find_element(By.ID, "email-pin-submit-button")
submit_button.click()
# Wait for the process to complete and navigate to the feed section
self.random_delay(10, 12)
self.driver.get("https://www.linkedin.com/feed/")
logging.info("Logged in and navigated to the feed section.")
except Exception as e:
logging.info("Verification code not required or error occurred.")
pass
def remove_markdown(self, text, ignore_hashtags=False):
"""Removes markdown syntax from a given text string."""
patterns = [
r"(\*{1,2})(.*?)\1", # Bold and italics
r"\[(.*?)\]\((.*?)\)", # Links
r"`(.*?)`", # Inline code
r"(\n\s*)- (.*)", # Unordered lists (with `-`)
r"(\n\s*)\* (.*)", # Unordered lists (with `*`)
r"(\n\s*)[0-9]+\. (.*)", # Ordered lists
r"(#+)(.*)", # Headings
r"(>+)(.*)", # Blockquotes
r"(---|\*\*\*)", # Horizontal rules
r"!\[(.*?)\]\((.*?)\)", # Images
]
# If ignoring hashtags, remove the heading pattern
if ignore_hashtags:
patterns.remove(r"(#+)(.*)")
# Replace markdown elements with an empty string
for pattern in patterns:
text = re.sub(
pattern, r" ", text
)
return text.strip()
def generate_post_content(self, topic):
"""Generates post content using Gemini AI based on the given topic."""
logging.info(f"Generating post content for topic: {topic}")
try:
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
client = genai.GenerativeModel("gemini-pro")
messages = [
{
"role": "user",
"parts": [
f"Generate a LinkedIn post with a minimum amount of 1000 characters about the following topic and do not forget to add suitable hastags: {topic}. Start with a captivating introduction that grabs the reader's attention. Develop a compelling thesis statement that clearly articulates the main argument of the post and support it with strong evidence and logical reasoning. Ensure the post is engaging, relatable, and structured with clear sections or headings. Include experts experiences, emotions, and specific scenarios or examples that support the topic. Provide detailed case studies or examples showing the impact of this topic in various contexts or industries. Delve into relevant technical aspects or processes if applicable. Support the claims with statistics or data points. Conclude with a call to action that encourages readers to learn more or take specific steps related to the topic. The post should read like it was written by a human and resonate with the readers."
],
}
]
post_response = client.generate_content(messages)
if post_response.text:
post_text = self.remove_markdown(
post_response.text, ignore_hashtags=True
)
else:
post_text = f"Excited to share some thoughts on {topic}! #technology #leadership"
except Exception as e:
logging.error("Failed to generate post content.", exc_info=True)
post_text = f"Excited to share some thoughts on {topic}! #technology #leadership"
return post_text
def close_overlapping_elements(self):
try:
# Close chat overlay
chat_overlay_close_button = self.driver.find_element(By.XPATH, "//button[contains(@class, 'msg-overlay-bubble-header__control--close')]")
chat_overlay_close_button.click()
self.random_delay()
except Exception as e:
logging.info("No chat overlay to close.")
try:
# Close any other notification or modal
notification_overlay_close_button = self.driver.find_element(By.XPATH, "//button[contains(@class, 'artdeco-modal__dismiss')]")
notification_overlay_close_button.click()
self.random_delay()
except Exception as e:
logging.info("No notification or modal overlay to close.")
def post_to_linkedin(self, post_text):
"""Posts the generated content to LinkedIn."""
logging.info("Posting to LinkedIn.")
try:
# Close overlapping elements
self.close_overlapping_elements()
# Wait for the "Start a post" button to be clickable and click it using JavaScript
start_post_button = WebDriverWait(self.driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Start a post')]"))
)
self.driver.execute_script("arguments[0].click();", start_post_button)
# Wait a moment for animation or modal dialogs to appear
time.sleep(2)
# Assuming the text area for the post becomes visible after clicking the button:
post_text_area = WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "div[role='textbox']"))
)
# Click the text area to focus and start typing a post
post_text_area.click()
self.driver.execute_script(
"arguments[0].innerText = arguments[1];", post_text_area, post_text
)
# Optionally, you can search for the 'Post' button and click it to publish
post_button = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable(
(
By.XPATH,
"//button[contains(@class, 'share-actions__primary-action')]",
)
)
)
self.driver.execute_script("arguments[0].click();", post_button)
logging.info("Post successful.")
return True
except Exception as e:
logging.error("Failed to post to LinkedIn.", exc_info=True)
return False
def process_topics(self):
"""Processes the first topic from Topics.txt, posts it to LinkedIn, and updates the files accordingly."""
try:
with open("Topics.txt", "r") as file:
topics = file.readlines()
if not topics:
logging.info("No topics to process.")
return
# Get the first topic
topic = topics[0].strip()
if not topic:
logging.info("The first topic is empty.")
return
post_text = self.generate_post_content(topic)
if self.post_to_linkedin(post_text):
with open("Topics_done.txt", "a") as done_file:
done_file.write(topic + "\n")
logging.info(f"Topic posted and saved to Topics_done.txt: {topic}")
# Remove the posted topic from Topics.txt
with open("Topics.txt", "w") as file:
file.writelines(topics[1:])
logging.info("First topic removed from Topics.txt.")
else:
logging.info(f"Failed to post topic: {topic}")
self.random_delay(5, 10)
except Exception as e:
logging.error("An error occurred while processing topics.", exc_info=True)
if __name__ == "__main__":
bot = LinkedInBot()
try:
bot.process_topics()
time.sleep(5)
finally:
bot.driver.quit()
logging.info("Driver session ended cleanly.")
# import os
# import re
# import time
# import random
# from selenium import webdriver
# from selenium.webdriver.common.keys import Keys
# from selenium.webdriver.common.by import By
# from selenium.webdriver.chrome.service import Service
# from selenium.webdriver.chrome.options import Options
# from webdriver_manager.chrome import ChromeDriverManager
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from dotenv import load_dotenv
# import google.generativeai as genai
# import logging
# load_dotenv()
# # Configure logging
# logging.basicConfig(
# level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
# )
# class LinkedInBot:
# """
# A class representing a bot for interacting with LinkedIn, capable of liking posts,
# commenting based on sentiment analysis and content relevance, and navigating LinkedIn's interface.
# """
# def __init__(self):
# self.driver = self.setup_driver()
# self.login()
# def setup_driver(self):
# """Sets up the Chrome WebDriver with necessary options."""
# chrome_options = Options()
# chrome_options.add_argument("--no-sandbox")
# chrome_options.add_argument("--disable-dev-shm-usage")
# chrome_options.add_argument("--disable-blink-features=AutomationControlled")
# # chrome_options.add_argument("--headless")
# chrome_options.add_argument("start-maximized")
# chrome_options.add_argument("disable-infobars")
# chrome_options.add_argument("--disable-extensions")
# chrome_options.add_argument(
# "user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
# )
# service = Service(ChromeDriverManager().install())
# driver = webdriver.Chrome(service=service, options=chrome_options)
# driver.execute_script(
# "Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
# )
# return driver
# def random_delay(self, min_delay=1, max_delay=3):
# """Introduce a random delay to mimic human behavior."""
# time.sleep(random.uniform(min_delay, max_delay))
# def login(self):
# """Logs into LinkedIn using credentials from environment variables."""
# self.driver.get("https://www.linkedin.com/login")
# WebDriverWait(self.driver, 10).until(
# EC.presence_of_element_located((By.ID, "username"))
# )
# username_field = self.driver.find_element(By.ID, "username")
# password_field = self.driver.find_element(By.ID, "password")
# # Mimic human typing by sending keys with delays
# for char in os.getenv("LINKEDIN_USERNAME"):
# username_field.send_keys(char)
# self.random_delay(0.1, 0.3)
# self.random_delay()
# for char in os.getenv("LINKEDIN_PASSWORD"):
# password_field.send_keys(char)
# self.random_delay(0.1, 0.3)
# self.random_delay()
# password_field.send_keys(Keys.RETURN)
# self.random_delay(5, 7)
# def remove_markdown(self, text, ignore_hashtags=False):
# """
# Removes markdown syntax from a given text string.
# Args:
# text: The text string potentially containing markdown syntax.
# ignore_hashtags: Boolean flag to ignore hashtags while removing markdown.
# Returns:
# The text string with markdown syntax removed.
# """
# patterns = [
# r"(\*{1,2})(.*?)\1", # Bold and italics
# r"\[(.*?)\]\((.*?)\)", # Links
# r"`(.*?)`", # Inline code
# r"(\n\s*)- (.*)", # Unordered lists (with `-`)
# r"(\n\s*)\* (.*)", # Unordered lists (with `*`)
# r"(\n\s*)[0-9]+\. (.*)", # Ordered lists
# r"(#+)(.*)", # Headings
# r"(>+)(.*)", # Blockquotes
# r"(---|\*\*\*)", # Horizontal rules
# r"!\[(.*?)\]\((.*?)\)", # Images
# ]
# # If ignoring hashtags, remove the heading pattern
# if ignore_hashtags:
# patterns.remove(r"(#+)(.*)")
# # Replace markdown elements with an empty string
# for pattern in patterns:
# text = re.sub(
# pattern, r" ", text
# )
# # Extracts the inner content (group 2) if available
# return text.strip()
# def generate_post_content(self, topic):
# """Generates post content using Gemini AI based on the given topic."""
# logging.info(f"Generating post content for topic: {topic}")
# try:
# genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
# client = genai.GenerativeModel("gemini-pro")
# messages = [
# {
# "role": "user",
# "parts": [
# f"Generate a LinkedIn post with a minimum amount of 1000 characters about the following topic and do not forget to add suitable hastags: {topic}. Start with a captivating introduction that grabs the reader's attention. Develop a compelling thesis statement that clearly articulates the main argument of the post and support it with strong evidence and logical reasoning. Ensure the post is engaging, relatable, and structured with clear sections or headings. Include experts experiences, emotions, and specific scenarios or examples that support the topic. Provide detailed case studies or examples showing the impact of this topic in various contexts or industries. Delve into relevant technical aspects or processes if applicable. Support the claims with statistics or data points. Conclude with a call to action that encourages readers to learn more or take specific steps related to the topic. The post should read like it was written by a human and resonate with the readers."
# ],
# }
# ]
# post_response = client.generate_content(messages)
# if post_response.text:
# post_text = self.remove_markdown(
# post_response.text, ignore_hashtags=True
# )
# else:
# post_text = f"Excited to share some thoughts on {topic}! #technology #leadership"
# except Exception as e:
# logging.error("Failed to generate post content.", exc_info=True)
# post_text = f"Excited to share some thoughts on {topic}! #technology #leadership"
# return post_text
# def post_to_linkedin(self, post_text):
# """Posts the generated content to LinkedIn."""
# logging.info("Posting to LinkedIn.")
# try:
# # Wait for the "Start a post" button to be clickable and click it
# start_post_button = WebDriverWait(self.driver, 20).until(
# EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Start a post')]"))
# )
# start_post_button.click()
# # Wait a moment for animation or modal dialogs to appear
# time.sleep(2)
# # Assuming the text area for the post becomes visible after clicking the button:
# post_text_area = WebDriverWait(self.driver, 10).until(
# EC.visibility_of_element_located((By.CSS_SELECTOR, "div[role='textbox']"))
# )
# # Click the text area to focus and start typing a post
# post_text_area.click()
# self.driver.execute_script(
# "arguments[0].innerText = arguments[1];", post_text_area, post_text
# )
# # Optionally, you can search for the 'Post' button and click it to publish
# post_button = WebDriverWait(self.driver, 10).until(
# EC.element_to_be_clickable(
# (
# By.XPATH,
# "//button[contains(@class, 'share-actions__primary-action')]",
# )
# )
# )
# post_button.click()
# logging.info("Post successful.")
# return True
# except Exception as e:
# logging.error("Failed to post to LinkedIn.", exc_info=True)
# return False
# def process_topics(self):
# """Processes the first topic from Topics.txt, posts it to LinkedIn, and updates the files accordingly."""
# try:
# with open("Topics.txt", "r") as file:
# topics = file.readlines()
# if not topics:
# logging.info("No topics to process.")
# return
# # Get the first topic
# topic = topics[0].strip()
# if not topic:
# logging.info("The first topic is empty.")
# return
# post_text = self.generate_post_content(topic)
# print(post_text)
# if self.post_to_linkedin(post_text):
# with open("Topics_done.txt", "a") as done_file:
# done_file.write(topic + "\n")
# logging.info(f"Topic posted and saved to Topics_done.txt: {topic}")
# # Remove the posted topic from Topics.txt
# with open("Topics.txt", "w") as file:
# file.writelines(topics[1:])
# logging.info("First topic removed from Topics.txt.")
# else:
# logging.info(f"Failed to post topic: {topic}")
# self.random_delay(5, 10)
# except Exception as e:
# logging.error("An error occurred while processing topics.", exc_info=True)
# if __name__ == "__main__":
# bot = LinkedInBot()
# try:
# bot.process_topics()
# time.sleep(5)
# finally:
# time.sleep(50)
# bot.driver.quit() # Ensure the driver is quit properly
# logging.info("Driver session ended cleanly.")