Created
October 28, 2019 12:41
-
-
Save wadefletch/f959c9778ef456b2e110b94705760140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tweepy | |
from datetime import datetime | |
import requests | |
import re | |
import os | |
CONSUMER_KEY = os.environ.get('TWITTER_CONSUMER_KEY') | |
CONSUMER_SECRET = os.environ.get('TWITTER_CONSUMER_SECRET') | |
ACCESS_TOKEN = os.environ.get('TWITTER_ACCESS_TOKEN') | |
ACCESS_SECRET = os.environ.get('TWITTER_ACCESS_SECRET') | |
HANDLE = os.environ.get('TWITTER_HANDLE') | |
# get authentication info | |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET) | |
# log into the API | |
api = tweepy.API(auth) | |
print('[{}] Logged into Twitter API as @{}\n-----------'.format( | |
datetime.now().strftime("%H:%M:%S %Y-%m-%d"), | |
HANDLE | |
)) | |
# string array of words that will trigger the on_status function | |
trigger_words = [ | |
'@' + HANDLE # respond to @mentions | |
] | |
# override the default listener to add code to on_status | |
class MyStreamListener(tweepy.StreamListener): | |
# listener for tweets | |
# ------------------- | |
# this function will be called any time a tweet comes in | |
# that contains words from the array created above | |
def on_status(self, status): | |
# log the incoming tweet | |
print('[{}] Received: "{}" from @{}'.format( | |
datetime.now().strftime("%H:%M:%S %Y-%m-%d"), | |
status.text, | |
status.author.screen_name | |
)) | |
# get the text from the tweet mentioning the bot. | |
# for this bot, we won't need this since it doesn't process the tweet. | |
# but if your bot does, then you'll want to use this | |
message = status.text | |
extracted_url = re.search("(?P<url>https?://[^\s]+)", message).group("url") | |
r = requests.get('https://api.audd.io', params={'url': extracted_url, 'return': 'spotify', 'api_token': '57c768234dfd8fe931fa7625a4107f6d'}) | |
if r.json()['status'] == 'success': | |
response = '@{} Audio identified! {} - {} ({})'.format(status.author.screen_name, r.json()['result']['title'], r.json()['result']['artist'], r.json()['result']['spotify']['external_urls']['spotify']) | |
else: | |
response = '@{} Audio could not be identified. ;('.format(status.author.screen_name) | |
# respond to the tweet | |
api.update_status( | |
status=response, | |
in_reply_to_status_id=status.id | |
) | |
print('[{}] Responded to @{}'.format( | |
datetime.now().strftime("%H:%M:%S %Y-%m-%d"), | |
status.author.screen_name | |
)) | |
# create a stream to receive tweets | |
try: | |
streamListener = MyStreamListener() | |
stream = tweepy.Stream(auth = api.auth, listener=streamListener) | |
stream.filter(track=trigger_words) | |
except KeyboardInterrupt: | |
print('\nGoodbye') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment