{"id":848,"date":"2020-06-18T21:33:03","date_gmt":"2020-06-18T21:33:03","guid":{"rendered":"http:\/\/jackofalltradesmasterofsome.com\/blog\/?p=848"},"modified":"2020-06-18T22:17:31","modified_gmt":"2020-06-18T22:17:31","slug":"automate-your-twitter-with-api-and-python-robotic-python-automation","status":"publish","type":"post","link":"https:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/06\/18\/automate-your-twitter-with-api-and-python-robotic-python-automation\/","title":{"rendered":"Automate your Twitter with API and Python &#8211; Robotic Python Automation"},"content":{"rendered":"\n<p>Sometimes you do not want to spend money to do things are are really easy. Tools like Hootsuite or Buffer can run you 100&#8217;s a month for something you can roll out yourself for free and a hour or so worth of work. Let&#8217;s learn how to Automate your Twitter with API and Python<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting Up Twitter <\/h2>\n\n\n\n<p>First we will want to create a Twitter Account and Handle and register for a developer account. This may take a few days for you to get approved. <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a Twitter Account<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"491\" height=\"471\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image.png\" alt=\"\" class=\"wp-image-850\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image.png 491w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-300x288.png 300w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\"><li><\/li><\/ol>\n\n\n\n<ul class=\"wp-block-list\"><li>After a few more steps you will have a working Twitter Account<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"569\" height=\"422\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-1.png\" alt=\"\" class=\"wp-image-851\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-1.png 569w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-1-300x222.png 300w\" sizes=\"auto, (max-width: 569px) 100vw, 569px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Apply for a developer tool. For this we are going to build a hobbyist bot.<ul><li><a href=\"https:\/\/developer.twitter.com\/en\/docs\/basics\/developer-portal\/overview\">https:\/\/developer.twitter.com\/en\/docs\/basics\/developer-portal\/overview<\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"294\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-2.png\" alt=\"\" class=\"wp-image-852\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-2.png 624w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-2-300x141.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>If you have not verified a phone number, it will make you do that now.<\/li><li>The next few screens will have you fill out why you want to use the portal and review and sign a developers agreement.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"256\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-3.png\" alt=\"\" class=\"wp-image-853\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-3.png 624w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-300x123.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>You will than need to wait for your account to be confirmed<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"618\" height=\"481\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-11.png\" alt=\"\" class=\"wp-image-863\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-11.png 618w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-11-300x233.png 300w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Creating an Twitter App<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Once you app is Confirmed, you can create an app to get an API key<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"128\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-5.png\" alt=\"\" class=\"wp-image-855\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-5.png 624w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-5-300x62.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Obtain your consumer keys and your access tokens, once the app is created<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"340\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-6.png\" alt=\"\" class=\"wp-image-856\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-6.png 624w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-6-300x163.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"291\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-7.png\" alt=\"\" class=\"wp-image-857\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-7.png 570w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-7-300x153.png 300w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Create a CSV for Data<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a CVS File called \u201cpost.csv\u201d with your post message, hashtags and urls. If this is not in the same folder as your Python script this will need to be updated in the next steps<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"88\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-8.png\" alt=\"\" class=\"wp-image-858\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-8.png 624w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-8-300x42.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Python Application <\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a new python application and run the command \u201cpip install tweepy\u201d to install the library<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install tweepy<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Import your excel file into a dataframe and use the sample command to select a random row. Place each of these values in their own variables.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\ndf = pd.read_csv('post.csv')\n\n#print (df)\n\ndf2 = df.sample()\n\ntitle = df2.iloc[0]['Title']\nhashtags = df2.iloc[0]['hashtags']\nurl = df2.iloc[0]['url']\n\nprint (title)\nprint (hashtags)\nprint (url)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"130\" src=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-10.png\" alt=\"\" class=\"wp-image-860\" srcset=\"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-10.png 500w, https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-content\/uploads\/2020\/06\/image-10-300x78.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>The following code can now be use to auto post your first twitter message once you replace your keys!<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import tweepy\n\ndef main():\n    twitter_auth_keys = { \n        \"consumer_key\"        : \"REPLACE_THIS_WITH_YOUR_CONSUMER_KEY\",\n        \"consumer_secret\"     : \"REPLACE_THIS_WITH_YOUR_CONSUMER_SECRET\",\n        \"access_token\"        : \"REPLACE_THIS_WITH_YOUR_ACCESS_TOKEN\",\n        \"access_token_secret\" : \"REPLACE_THIS_WITH_YOUR_ACCESS_TOKEN_SECRET\"\n    }\n \n    auth = tweepy.OAuthHandler(\n            twitter_auth_keys&#91;'consumer_key'],\n            twitter_auth_keys&#91;'consumer_secret']\n            )\n    auth.set_access_token(\n            twitter_auth_keys&#91;'access_token'],\n            twitter_auth_keys&#91;'access_token_secret']\n            )\n    api = tweepy.API(auth)\n \n    tweet = title + ' ' + hashtags + ' ' + url\n    status = api.update_status(status=tweet) \n \nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<p>Interested in learning more? Check out our blog post on an <a href=\"http:\/\/jackofalltradesmasterofsome.com\/blog\/2020\/05\/12\/robotic-python-automation-automating-working-with-files-with-python-cleaning-sorting-and-archiving\/\">Introduction to Python Class<\/a>.<\/p>\n\n\n\n<p>Automate your Twitter with API and Python &#8211; Robotic Python Automation<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you do not want to spend money to do things are are really easy. Tools like Hootsuite or Buffer can run you 100&#8217;s a month for something you can roll out yourself for free and a hour or so worth of work. Let&#8217;s learn how to Automate your Twitter with API and Python Setting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":861,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97,94],"tags":[8,98,99],"class_list":["post-848","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-automation","category-python","tag-automation","tag-python","tag-twitter"],"_links":{"self":[{"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts\/848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/comments?post=848"}],"version-history":[{"count":3,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts\/848\/revisions"}],"predecessor-version":[{"id":865,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/posts\/848\/revisions\/865"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/media\/861"}],"wp:attachment":[{"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/media?parent=848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/categories?post=848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jackofalltradesmasterofsome.com\/blog\/wp-json\/wp\/v2\/tags?post=848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}