-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextblov.py
More file actions
65 lines (47 loc) · 1.84 KB
/
Copy pathTextblov.py
File metadata and controls
65 lines (47 loc) · 1.84 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
import tweepy
import os
from textblob import TextBlob
from jinja2 import Environment, FileSystemLoader
# Set up Twitter API credentials
consumer_key = 'YOUR_CONSUMER_KEY'
consumer_secret = 'YOUR_CONSUMER_SECRET'
access_token = 'YOUR_ACCESS_TOKEN'
access_token_secret = 'YOUR_ACCESS_TOKEN_SECRET'
# Set up Tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def fetch_tweets(brand, count=100):
tweets = api.search(q=brand, count=count, lang='en', tweet_mode='extended')
return tweets
def analyze_sentiment(tweets):
sentiment = {'positive': 0, 'negative': 0, 'neutral': 0}
for tweet in tweets:
text = tweet.full_text
analysis = TextBlob(text)
polarity = analysis.sentiment.polarity
if polarity > 0:
sentiment['positive'] += 1
elif polarity < 0:
sentiment['negative'] += 1
else:
sentiment['neutral'] += 1
return sentiment
# Set up Jinja2 template
def generate_report(brand, sentiment, template='report.html'):
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template(template)
with open(f"{brand}_report.html", 'w') as f:
f.write(template.render(brand=brand, sentiment=sentiment))
# Set up Twitter API credentials and Tweepy
def main():
brand = input("Enter the brand name: ")
tweet_count = int(
input("Enter the number of tweets to analyze (default 100): "))
print(f"Fetching and analyzing {tweet_count} tweets for {brand}...")
tweets = fetch_tweets(brand, count=tweet_count)
sentiment = analyze_sentiment(tweets)
generate_report(brand, sentiment)
print(f"Report generated: {brand}_report.html")
if __name__ == "__main__":
main()