Skip to content

Commit 7203dae

Browse files
Add files via upload
0 parents  commit 7203dae

File tree

6 files changed

+242
-0
lines changed

6 files changed

+242
-0
lines changed
696 Bytes
Binary file not shown.
2.78 KB
Binary file not shown.

app.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
import os
3+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
4+
5+
from flask import Flask, jsonify, send_from_directory, request
6+
from selenium_script import get_trending_topics
7+
import threading
8+
import logging
9+
10+
app = Flask(__name__)
11+
12+
@app.route('/')
13+
def index():
14+
return send_from_directory('.', 'index.html')
15+
16+
@app.route('/run_script')
17+
def run_script():
18+
result = {}
19+
use_sample_data = request.args.get('use_sample_data', 'false').lower() == 'true'
20+
21+
def selenium_task():
22+
nonlocal result
23+
result = get_trending_topics(use_sample_data=use_sample_data)
24+
app.logger.info(f"Fetched topics: {result}")
25+
26+
thread = threading.Thread(target=selenium_task)
27+
thread.start()
28+
thread.join()
29+
30+
return jsonify(result)
31+
32+
if __name__ == '__main__':
33+
logging.basicConfig(level=logging.INFO)
34+
app.run(debug=True)

fetch_proxy.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
4+
def get_free_proxy():
5+
url = "https://www.free-proxy-list.net/"
6+
response = requests.get(url)
7+
soup = BeautifulSoup(response.text, 'html.parser')
8+
table = soup.find(id='proxylisttable')
9+
rows = table.tbody.find_all('tr')
10+
for row in rows:
11+
cols = row.find_all('td')
12+
if cols[4].text.strip() == 'elite proxy' and cols[6].text.strip() == 'yes':
13+
proxy = f"{cols[0].text.strip()}:{cols[1].text.strip()}"
14+
return proxy
15+
return None

index.html

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Twitter Trending Topics</title>
7+
<style>
8+
body {
9+
font-family: Arial, sans-serif;
10+
background-color: #f4f4f9;
11+
margin: 0;
12+
padding: 0;
13+
display: flex;
14+
flex-direction: column;
15+
align-items: center;
16+
}
17+
h1 {
18+
color: #333;
19+
margin-top: 1rem;
20+
}
21+
button {
22+
background-color: #1da1f2;
23+
color: white;
24+
border: none;
25+
padding: 10px 20px;
26+
margin: 10px;
27+
border-radius: 5px;
28+
cursor: pointer;
29+
transition: background-color 0.3s ease;
30+
}
31+
button:hover {
32+
background-color: #0c85d0;
33+
}
34+
h2 {
35+
color: #555;
36+
margin-top: 20px;
37+
}
38+
ul {
39+
list-style-type: none;
40+
padding: 0;
41+
}
42+
li {
43+
background-color: #fff;
44+
margin: 5px 0;
45+
padding: 10px;
46+
border-radius: 5px;
47+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
48+
}
49+
p {
50+
color: #777;
51+
}
52+
span {
53+
font-weight: bold;
54+
}
55+
</style>
56+
<script>
57+
function fetchTrendingTopics(useSampleData) {
58+
const url = useSampleData ? '/run_script?use_sample_data=true' : '/run_script';
59+
fetch(url)
60+
.then(response => response.json())
61+
.then(data => {
62+
document.getElementById('trend1').innerText = data.trend1 || 'undefined';
63+
document.getElementById('trend2').innerText = data.trend2 || 'undefined';
64+
document.getElementById('trend3').innerText = data.trend3 || 'undefined';
65+
document.getElementById('trend4').innerText = data.trend4 || 'undefined';
66+
document.getElementById('trend5').innerText = data.trend5 || 'undefined';
67+
document.getElementById('fetched_at').innerText = data.date_time || 'undefined';
68+
document.getElementById('ip_address').innerText = data.ip_address || 'undefined';
69+
})
70+
.catch(error => {
71+
console.error('Error fetching trending topics:', error);
72+
});
73+
}
74+
</script>
75+
</head>
76+
<body>
77+
<h1>Twitter Trending Topics</h1>
78+
<button onclick="fetchTrendingTopics(false)">Fetch Trending Topics</button>
79+
<button onclick="fetchTrendingTopics(true)">Fetch Sample Data</button>
80+
<h2>Trending Topics</h2>
81+
<ul>
82+
<li id="trend1">undefined</li>
83+
<li id="trend2">undefined</li>
84+
<li id="trend3">undefined</li>
85+
<li id="trend4">undefined</li>
86+
<li id="trend5">undefined</li>
87+
</ul>
88+
<p>Fetched at: <span id="fetched_at">undefined</span></p>
89+
<p>IP Address: <span id="ip_address">undefined</span></p>
90+
</body>
91+
</html>

selenium_script.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)