![]() If all goes well, you’ll see a new tweet with a game of thrones quote on your profile. def tweet_quote(): auth = tweepy.OAuthHandler(consumer_key, consumer_secret_key) t_access_token(access_token, access_token_secret) api = tweepy.API(auth) test_tweet = create_tweet() api.update_status(test_tweet) if _name_ = "_main_": tweet_quote() Then we’ll run python bot.py or whatever the name of your file is from the command line. So after the tweet_quote function, we’ll add if _name_ = "_main_":and then call the tweet_quote() function. We’ll send a test tweet first to verify we connected properly to the Twitter API. consumer_key = credentials.API_KEY consumer_secret_key = credentials.API_SECRET_KEY access_token = credentials.ACCESS_TOKEN access_token_secret = credentials.ACCESS_TOKEN_SECRET Then add the following lines of code a line after it. At the beginning of your file just below the import random line, import the tweepy package. ![]() pip install tweepyĪfter installing it, we can use it to access Twitter’s API via OAuth Authentication. If you don’t have pip, you’ll have to install that as well. If you haven’t already run the command found in the github repository’s README.md, you’ll need to install tweepy using pip. We’ll be needing the tweepy package to interface with the Twitter API. API_KEY=YOUR-API-KEY API_SECRET_KEY=YOUR-API-SECRET-KEY ACCESS_TOKEN=YOUR-ACCESS-TOKEN ACCESS_TOKEN_SECRET=YOUR-ACCESS-TOKEN-SECRET Within the file you’ll add the following details from the ‘keys and tokens’ tab in your new app dashboard. You can store the credentials in a file called credentials.py for local development. This is to enable you to get your credentials to access the Twitter API. If you do not have one, you’ll need to apply for one before creating an application and your application approved. You’ll need to register an application if you have been following along and have a twitter developer account already. This is where the twitter developers account is needed. def create_tweet(): quote = get_random_quote() tweet = """ """.format(quote, quote) return tweetįinally, I need to actually send the tweet to my account. ![]() This is just a function with multi-line string concatenation of variables. Then I need to create the tweet structure in the create_tweet() function using the quote object gotten from get_random_quote() above then returned the tweet. def get_random_quote(): quotes = get_quotes() random_quote = random.choice(quotes) return random_quote Import this package by adding import random just below the json import line. In Python, however, I found a built-in random.choice() function from the built-in random package which allowed me to return a random quote object from the list of quote objects gotten from get_quotes(). When I did something similar in JavaScript here, I had to create a copy of the quotes array using slice, create a random index using JavaScript Math functions and finally select a quote using the randomly generated index. Next, I needed a way to pick random quotes. def get_quotes(): with open('data.json') as f: quotes_json = json.load(f) return quotes_json It parses the json file and returns a list containing objects each consisting of a quote, the id and the character the quote is from. Using the built-in json package from python alongside the built-in open function I created a function called get_quotes(). ![]() ![]() The file contains quotes from GOT characters scrapped off different websites - about 100+ of them.įirst of all, I needed to get the quotes to object from the data.json file. It is going to tweet random quotes from a data.json file I already had. My bot is quite a simple one - not as fancy as the ones I mentioned above. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |