-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtags.py
52 lines (45 loc) · 1.86 KB
/
hashtags.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
#!/usr/bin/env python3
import tweepy
import logging
import json
import csv
from config import create_api
logging.basicConfig(level=logging.INFO,filemode='a', format='%(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger()
# Creating a stream Listener
class favRetweetListener(tweepy.StreamListener):
def __init__(self, api):
self.api = api
self.me = api.me()
def on_status(self, tweet):
'''Process tweets of the stream'''
logger.info(f'Processing the tweet id {tweet.id}')
# Open CSV file and set up csv writer
csv_file = open('csv/data.csv', 'a')
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
if tweet.in_reply_to_status_id is not None or tweet.user.id == self.me.id:
return
if not tweet.favorited:
try:
tweet.favorite()
except Exception:
logger.exception(f'Error while liking tweet of id {tweet.id}')
if not tweet.retweeted:
try:
tweet.retweet()
csv_writer.writerow([f'{tweet.id}',f'{tweet.user.name}', f'twitter.com/{tweet.user.screen_name}/status/{tweet.id}', f'{tweet.created_at}', f'tweet.status.text'])
except Exception:
logger.exception(f'Error while Retweeting tweet of id {tweet.id}')
csv_file.close()
def on_error(self, status_code):
logger.error(status_code)
# If stream exceeds the rate limit
if status_code == 420:
return False
def main():
api = create_api()
tweets_stream_listener = favRetweetListener(api)
stream = tweepy.Stream(api.auth, tweets_stream_listener)
stream.filter(track=['#ikokazike', '#ikokazikenya', '#IkoKaziKE', '#IkoKaziKe', '#IkoKaziKenya'], is_async=True)
if __name__ == '__main__':
main()