-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
128 lines (112 loc) · 4.25 KB
/
app.py
File metadata and controls
128 lines (112 loc) · 4.25 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
import streamlit as st
import asyncio
import datetime
import pandas as pd
from scrape import scrape_tweets
from db_utils import load_df, setup_database, clear_database
from sentiment_utils import classify_sentiment
import nltk
# Configure the page
st.set_page_config(
page_title="Twitter Scraping & Sentiment Analysis",
page_icon="🐦",
layout="wide",
initial_sidebar_state="expanded",
)
# Custom CSS with updated dark blue buttons, gradient blue main container and black-blue gradient for the sidebar
st.markdown(
"""
<style>
/* Set a neutral background for the entire app */
body {
background-color: #f0f2f6;
}
/* Main container styling with gradient blue background */
.main {
background: linear-gradient(135deg, #4facfe, #00f2fe);
padding: 2rem;
border-radius: 10px;
color: #333;
}
/* Style for dark blue buttons */
.stButton>button {
background-color: #001f3f;
color: white;
border: none;
border-radius: 5px;
padding: 10px 20px;
font-size: 16px;
transition: background-color 0.3s ease;
}
.stButton>button:hover {
background-color: #00264d;
}
/* Sidebar headers and expanders with black-blue gradient */
[data-testid="stSidebar"] .css-1d391kg {
background: linear-gradient(135deg, #000428, #004e92) !important;
border: 2px solid #004e92 !important;
border-radius: 5px !important;
padding: 10px;
margin-bottom: 10px;
color: white;
}
</style>
""", unsafe_allow_html=True
)
def main():
# Initialize the database
setup_database()
# Main title and description
st.title("🐦 Twitter (X) Scraping & Sentiment Analysis")
st.markdown("""
Welcome to the **Twitter Scraping & Sentiment Analysis Dashboard**. This application allows you to:
- Log into Twitter (X)
- Scrape tweets by topic
- Perform sentiment analysis
- Store and view the results
Enjoy exploring Twitter data with a sleek design!
""")
# Sidebar for configuration
st.sidebar.header("Configuration")
with st.sidebar.expander("🔐 Twitter Login Details", expanded=True):
name_or_email = st.text_input("Username/Email for Twitter", "")
password = st.text_input("Twitter Password", "", type="password")
phone = st.text_input("Phone (optional)", "")
with st.sidebar.expander("🔍 Twitter Search Parameters", expanded=True):
base_topic = st.text_input("Base Topic", "nvidia")
start_date = st.date_input("Start Date", datetime.date(2024, 1, 1))
end_date = st.date_input("End Date", datetime.date(2025, 1, 28))
num_tweets = st.number_input("Number of Tweets to Scrape", min_value=1, value=100, step=1)
st.write(f"**Twitter Date Range**: {start_date} to {end_date}")
# Button to start scraping tweets
if st.button("🚀 Start Twitter Scraping"):
if not name_or_email or not password:
st.warning("Please enter your Twitter username/email and password.")
else:
final_query = (
f"{base_topic} until:{end_date.strftime('%Y-%m-%d')} "
f"since:{start_date.strftime('%Y-%m-%d')} lang:en"
)
st.write(f"**Twitter Final Query**: `{final_query}`")
with st.spinner("🕵️♂️ Scraping tweets, please wait..."):
try:
asyncio.run(scrape_tweets(name_or_email, password, phone, final_query, num_tweets))
st.success("✅ Twitter scraping completed. Tweets stored in the database.")
except Exception as e:
st.error(f"Error during Twitter scraping: {e}")
st.markdown("---")
# Section to view scraped tweets
st.subheader("📄 View Scraped Tweets")
if st.button("🔄 Load Tweets Data"):
with st.spinner("Loading tweets data..."):
df_tweets = load_df()
if df_tweets.empty:
st.warning("No tweets found in the database.")
else:
st.dataframe(df_tweets)
if st.button("Clear tweets"):
clear_database()
st.success("Tweets database cleared.")
st.markdown("---")
if __name__ == "__main__":
main()