-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweepy_scrape.py
164 lines (133 loc) · 5.31 KB
/
tweepy_scrape.py
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
import tweepy
import yaml
from flask import Flask, render_template, request, jsonify
from collections import defaultdict
from judge_learning import judge_political_leaning
import logging
from parse_response import parse_political_analysis
from meme import MemeExplainer
from parse_rating import parse_text
app = Flask(__name__)
# Set up logging
logging.basicConfig(level=logging.DEBUG)
# Load the bearer token from the yaml file
try:
with open('/Users/nicholasflores/Documents/Secrets/xai.yaml', 'r') as file:
config = yaml.safe_load(file)
bearer_token = config['x']['bearer']
except Exception as e:
logging.error(f"Error loading config: {str(e)}")
raise
meme_explainer = MemeExplainer(config)
# Authenticate using the bearer token
try:
client = tweepy.Client(bearer_token=config['x']['bearer'])
except Exception as e:
logging.error(f"Error authenticating with Twitter: {str(e)}")
raise
# Function to pull the last n tweets for a single account
def get_last_n_tweets_for_account(account, n):
tweets = []
try:
# Get user ID for the Twitter handle
user = client.get_user(username=account)
user_id = user.data.id
# Pull the last n tweets from that user
response = client.get_users_tweets(
id=user_id,
max_results=n,
tweet_fields=['created_at', 'text']
)
if response.data:
for tweet in response.data:
tweets.append({
'username': account,
'created_at': tweet.created_at,
'text': tweet.text,
'id': tweet.id
})
except Exception as e:
logging.error(f"Error fetching tweets for {account}: {str(e)}")
return tweets
# Function to pull the last n tweets from multiple accounts
def get_last_n_tweets_from_accounts(accounts, n):
all_tweets = []
for account in accounts:
tweets = get_last_n_tweets_for_account(account, n)
all_tweets.extend(tweets)
return all_tweets
@app.route('/')
def home():
return render_template('xaiHackHome.html')
@app.route('/lookup', methods=['GET', 'POST'])
def lookup():
if request.method == 'POST':
try:
accounts = request.form.get('accounts').split(',')
n_tweets = int(request.form.get('n_tweets', 10)) # Default to 10 tweets if not specified
tweets = get_last_n_tweets_from_accounts(accounts, n_tweets)
# Organize tweets by user
tweets_by_user = defaultdict(list)
for tweet in tweets:
tweets_by_user[tweet['username']].append({
'username': tweet['username'],
'created_at': tweet['created_at'].isoformat(),
'text': tweet['text'],
'id': tweet['id']
})
return jsonify(dict(tweets_by_user))
except Exception as e:
logging.error(f"Error in lookup route: {str(e)}")
return jsonify({"error": str(e)}), 500
return render_template('lookup.html')
@app.route('/singletweet', methods=['GET', 'POST'])
def singletweet():
if request.method == 'POST':
try:
tweet_content = request.json.get('tweetContent')
if not tweet_content:
return jsonify({"error": "No tweet content provided"}), 400
analysis = judge_political_leaning(tweet_content)
return jsonify({"analysis": analysis})
except Exception as e:
logging.error(f"Error in singletweet route: {str(e)}")
return jsonify({"error": str(e)}), 500
return render_template('singletweet.html')
from parse_response import parse_political_analysis
@app.route('/analyze_tweet', methods=['POST'])
def analyze_tweet():
try:
tweet_content = request.json.get('tweetContent')
if not tweet_content:
return jsonify({"error": "No tweet content provided"}), 400
analysis_text = judge_political_leaning(tweet_content)
parsed_analysis = parse_political_analysis(analysis_text)
return jsonify({"analysis": parsed_analysis})
except Exception as e:
logging.error(f"Error in analyze_tweet route: {str(e)}")
return jsonify({"error": str(e)}), 500
@app.route('/meme', methods=['GET'])
def meme():
return render_template('meme.html')
@app.route('/analyze_meme', methods=['POST'])
def analyze_meme():
try:
tweet_id = request.json.get('tweetId')
if not tweet_id:
return jsonify({"error": "No tweet ID provided"}), 400
# Use the MemeExplainer to analyze the meme
analysis, base64_image = meme_explainer.explain(tweet_id)
if not analysis:
return jsonify({"error": "Failed to analyze the meme"}), 500
# Parse the rating from the analysis text
rating_text = parse_text(analysis)
return jsonify({
"analysis": analysis,
"image": base64_image,
"rating": rating_text # Return the full rating text
})
except Exception as e:
logging.error(f"Error in analyze_meme route: {str(e)}")
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True, port=8080)