· 6 years ago · Sep 11, 2019, 11:36 PM
1#!/usr/bin/env python3
2
3# A functional twitter bot that will perform various tasks
4# for me such as fetching the price of certain cryptocurrencies (cont)
5
6import sys
7import tweepy
8import json
9from credentials import *
10from tweepy.auth import OAuthHandler
11from time import sleep
12from contextlib import suppress
13
14# Setting FollowerMode = True to stream tweets from
15# one account (@lostgoyard).
16FollowerMode = True
17
18twitter_ids = {'@lostgoyard': 1111679644628107264
19 }
20
21# Searching tweets from @lostgotyard by keywords.
22search = ['!btcprice']
23
24# Connecting to the Twitter API.
25auth = OAuthHandler(consumer_key, consumer_secret)
26auth.set_access_token(access_token, access_token_secret)
27api = tweepy.API(auth)
28
29print('Listening for tweets.\n')
30
31# Gets all of the Twitter IDs from the 'twitter_ids' dictionary
32# and converts them to strings
33if FollowerMode == True:
34 ids = [str(i) for i in list(twitter_ids.values())]
35
36
37class MyStreamListener(tweepy.StreamListener):
38 global ids
39 global FollowerMode
40 global search
41
42 tweets = 0
43
44
45 def on_status(self, status):
46 with suppress(Exception):
47 userid = str(status.user.id)
48 if FollowerMode == True and userid in ids:
49 print('-' * 80)
50 # Prints the name for each ID that's defined
51 # in 'twitter_ids'
52 with suppress(Exception):
53 print(list(twitter_ids.keys())[list(twitter_ids.values()).index(int(userid))])
54 print('User: ' + status.user.screen_name)
55 # Attempt to display location and/or country if it exists.
56 with suppress(Exception):
57 if status.user.location != None and status.user.location != None:
58 print('Location: ' + status.user.location)
59 with suppress(Exception):
60 print('Country: ' + status.place.country)
61 # Checks to see if tweet is extended/long. If it is,
62 # It will display the full tweet.
63 try:
64 text = status.extended_tweet['full_text']
65 except AttributeError:
66 text = status.text
67 print('Tweet: ' + text)
68 elif FollowerMode == False:
69 print('-' * 80)
70 print('User: ' + status.user.screen_name)
71 with suppress(Exception):
72 if status.user.location != None and status.user.location != 'None':
73 ('Location: ' + status.user.location)
74 with suppress(Exception):
75 print('Country: ' + status.place.country)
76 try:
77 text = status.extended_tweet['full_text']
78 except AttributeError:
79 text = status.text
80 print('Tweet: ' + text)
81
82 # Allowing bot to auto-respond to key phrases.
83 if search[0] in text:
84 try:
85 api.update_status(f"@{status.user.screen_name} testing complete {status.id}", status.user.id)
86 print(f'Replied to {status.user.screen_name}')
87 print(text)
88 except Exception as e:
89 print(f'Couldn\'t reply. \n Reason: {e}')
90 sleep(0.015)
91 # Prevents the display from hiccups.
92 sleep(0.016)
93
94# Define the listener
95listener = MyStreamListener()
96stream = tweepy.Stream(auth, listener)
97
98if FollowerMode == True:
99 stream.filter(follow=ids, track = search)
100else:
101 stream.filter(languages=["en"], track = search)