-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (65 loc) · 2.76 KB
/
main.py
File metadata and controls
74 lines (65 loc) · 2.76 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
import os
from google.cloud import language_v1
import tweepy
import pandas as pd
class CryptoSentimentAnalysisBase():
MAX_ID = -1
TWEET_COUNT = 0
HASHTAG = '#Ethereum'
MAX_TWEETS = 100
TWEETS_PER_QUERY = 100
def __init__(self,
consumer_key,
consumer_secret,
access_token,
access_token_secret,
key_word,
tweets_per_query,
tweet_count,
max_tweets,
max_id,
):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.access_token = access_token
self.access_token_secret = access_token_secret
if key_word is None:
self.key_word = CryptoSentimentAnalysisBase.HASHTAG
if tweets_per_query is None:
self.tweets_per_query = CryptoSentimentAnalysisBase.TWEETS_PER_QUERY,
if tweet_count is None:
self.tweet_count = CryptoSentimentAnalysisBase.TWEET_COUNT
if max_tweets is None:
self.max_tweets = CryptoSentimentAnalysisBase.MAX_TWEETS
if max_id is None:
self.max_id = CryptoSentimentAnalysisBase.MAX_ID
def authenticate_user(self):
authenticate = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)
return authenticate
def set_access_token(self):
access_token = self.authenticate_user().set_access_token(self.access_token, self.access_token_secret)
return access_token
def api_call(self):
api = tweepy.API(self.authenticate_user(), wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
return api
def generate_results(self):
twitter_posts = []
while self.tweet_count < self.max_tweets:
if self.max_id <= 0:
new_tweets = self.api_call().search(q=self.key_word,
count=self.tweets_per_query,
result_type='recent',
tweet_mode='extended')
else:
new_tweets = self.api_call().search(q=self.key_word,
count=self.tweets_per_query,
max_id=str(self.max_id -1),
result_type='recent',
tweet_mode='extended')
if not new_tweets:
break
for tweet in new_tweets:
result = {}
result['text'] = tweet.full_text.encode('utf-8')
self.tweet_count += len(new_tweets)
self.max_id = new_tweets[-1].id