-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitterClient.py
More file actions
106 lines (70 loc) · 2.43 KB
/
twitterClient.py
File metadata and controls
106 lines (70 loc) · 2.43 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
import tweepy
import os
import sqlite3
from tweepy import OAuthHandler
from os.path import join, dirname
from dotenv import find_dotenv, load_dotenv
from StreamListener import StreamListener
from User import User
from Tweet import Tweet
# TwitterClient handles the connection to the Twitter API using Tweepy and processes the streaming API
class TwitterClient:
def __init__(self):
# Load Environment Variables
envPath = join(dirname(__file__), '.env')
load_dotenv(envPath)
# Initialize API keys
consumer_key = os.environ["consumer_key"]
consumer_secret = os.environ["consumer_secret"]
access_token = os.environ["access_token"]
access_secret = os.environ["access_secret"]
self.db_file = os.environ["db_file"]
# Attempt Connection to Twitter API
try:
self.auth = OAuthHandler(consumer_key, consumer_secret)
self.auth.set_access_token(access_token, access_secret)
self.api = tweepy.API(self.auth)
except:
print("Connection To Twitter Failed")
def create_stream(self, filters=[]):
stream_listener = StreamListener(self.db_file)
stream = tweepy.Stream(auth=self.api.auth, listener=stream_listener)
stream.filter(track=filters)
def sample_tweets(self, query, amount):
tweets = []
for tweet in tweepy.Cursor(self.api.search,
q=query,
lang="en").items(amount):
tweets.append(tweet)
return tweets
# Note the Twitter API only allows a max of 100 tweets to be looked up at a time
def find_tweets(self, tweet_ids):
return self.api.statuses_lookup(tweet_ids)
# Looks up all tweets in the DB and updates their favorite and retweet counts
def backfill_tweets(self):
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
cursor.execute("SELECT * FROM tweets")
rows = cursor.fetchall()
STATUS_ID = 1
count = 0
search = []
for row in rows:
search.append(row[STATUS_ID])
count += 1
if count == 100:
self.update_tweets(self.find_tweets(search), conn)
search = []
count = 0
if count > 0:
self.update_tweets(self.find_tweets(search), conn)
def update_tweets(self, tweets, conn):
cursor = conn.cursor()
for status in tweets:
cursor.execute("""UPDATE tweets SET
favorites = {favorite_count},
retweets = {retweet_count}
WHERE id = {status_id}""".format(favorite_count=status.favorite_count,
retweet_count=status.retweet_count,
status_id=status.id))
conn.commit()