|
| 1 | +from selenium import webdriver |
| 2 | +from selenium.webdriver.common.keys import Keys |
| 3 | +from selenium.webdriver.common.by import By |
| 4 | +from selenium.webdriver.support.ui import WebDriverWait |
| 5 | +from selenium.webdriver.support import expected_conditions as EC |
| 6 | +from pymongo import MongoClient |
| 7 | +from datetime import datetime |
| 8 | +import uuid |
| 9 | +from fetch_proxy import get_free_proxy # Ensure this line correctly references fetch_proxy.py |
| 10 | + |
| 11 | +TWITTER_USERNAME = "Nandhakumar2536" |
| 12 | +TWITTER_PASSWORD = "Nandha@143" |
| 13 | + |
| 14 | +# Specify the path to the ChromeDriver executable |
| 15 | +CHROME_DRIVER_PATH = r"C:\Users\MY-PC\Desktop\chrome-win64\chromedriver.exe" # Ensure this path includes chromedriver.exe |
| 16 | + |
| 17 | +def get_trending_topics(use_sample_data=False): |
| 18 | + if use_sample_data: |
| 19 | + # Sample trending topics |
| 20 | + topics = ["NEET", "Aboard Study", "#NRI", "Election Results", "Zoho"] |
| 21 | + ip_address = "123.45.67.89" # Sample IP address |
| 22 | + unique_id = str(uuid.uuid4()) |
| 23 | + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 24 | + |
| 25 | + try: |
| 26 | + # Store results in MongoDB |
| 27 | + client = MongoClient("mongodb://localhost:27017/") |
| 28 | + db = client["twitter_trends"] |
| 29 | + collection = db["trending_topics"] |
| 30 | + document = { |
| 31 | + "unique_id": unique_id, |
| 32 | + "trend1": topics[0], |
| 33 | + "trend2": topics[1], |
| 34 | + "trend3": topics[2], |
| 35 | + "trend4": topics[3], |
| 36 | + "trend5": topics[4], |
| 37 | + "date_time": timestamp, |
| 38 | + "ip_address": ip_address |
| 39 | + } |
| 40 | + collection.insert_one(document) |
| 41 | + return document |
| 42 | + except Exception as e: |
| 43 | + print(f"Error inserting document into MongoDB: {e}") |
| 44 | + return {"error": str(e)} |
| 45 | + |
| 46 | + proxy = get_free_proxy() |
| 47 | + if not proxy: |
| 48 | + return {"error": "No suitable proxy found"} |
| 49 | + |
| 50 | + options = webdriver.ChromeOptions() |
| 51 | + options.add_argument(f'--proxy-server=http://{proxy}') |
| 52 | + |
| 53 | + driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH, options=options) |
| 54 | + |
| 55 | + try: |
| 56 | + driver.get("https://twitter.com/login") |
| 57 | + username_input = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "session[username_or_email]"))) |
| 58 | + password_input = driver.find_element(By.NAME, "session[password]") |
| 59 | + username_input.send_keys(TWITTER_USERNAME) |
| 60 | + password_input.send_keys(TWITTER_PASSWORD + Keys.RETURN) |
| 61 | + |
| 62 | + # Check for successful login |
| 63 | + WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, "//div[@aria-label='Timeline: Trending now']"))) |
| 64 | + trending_topics = driver.find_elements(By.XPATH, "//div[@aria-label='Timeline: Trending now']//span")[:5] |
| 65 | + topics = [topic.text for topic in trending_topics] |
| 66 | + |
| 67 | + driver.get("https://api.ipify.org?format=text") |
| 68 | + ip_address = driver.find_element(By.TAG_NAME, "body").text |
| 69 | + |
| 70 | + unique_id = str(uuid.uuid4()) |
| 71 | + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 72 | + |
| 73 | + try: |
| 74 | + client = MongoClient("mongodb://localhost:27017/") |
| 75 | + db = client["twitter_trends"] |
| 76 | + collection = db["trending_topics"] |
| 77 | + document = { |
| 78 | + "unique_id": unique_id, |
| 79 | + "trend1": topics[0] if len(topics) > 0 else "", |
| 80 | + "trend2": topics[1] if len(topics) > 1 else "", |
| 81 | + "trend3": topics[2] if len(topics) > 2 else "", |
| 82 | + "trend4": topics[3] if len(topics) > 3 else "", |
| 83 | + "trend5": topics[4] if len(topics) > 4 else "", |
| 84 | + "date_time": timestamp, |
| 85 | + "ip_address": ip_address |
| 86 | + } |
| 87 | + collection.insert_one(document) |
| 88 | + return document |
| 89 | + except Exception as e: |
| 90 | + print(f"Error inserting document into MongoDB: {e}") |
| 91 | + return {"error": str(e)} |
| 92 | + |
| 93 | + except Exception as e: |
| 94 | + print(f"Error during Twitter login: {e}") |
| 95 | + return {"error": str(e)} |
| 96 | + |
| 97 | + finally: |
| 98 | + driver.quit() |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + result = get_trending_topics(use_sample_data=True) |
| 102 | + print(result) |
0 commit comments