Day 1 - Tweepybot

It’s the first day of the 180 Day Coding challenge, and most of what I’ve been doing is preparing myself for this journey. Getting my github account squared away, making sure that I’ve got a decent way to link to everything, and etc. I setup the hustleandcode180 repo on github, which just captures all the work in one central location.

Coding-wise, there were two things that I concentrated on today- Getting down with the basics, starting to capture all of the chapter exercises in the Udemy Ultimate Python Class (more work to do there), and coding up a pretty simple Twitter bot that uses the tweepy python module to send out tweets. I decided that even though doing exercises to learn is cool, I think that completing at least a bite-sized tool is enough to count towards the challenge.

Here’s the code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy, sys, time
from random import randint
from keys import keys

CONSUMER_KEY = keys['consumer_key']
CONSUMER_SECRET = keys['consumer_secret']
ACCESS_TOKEN = keys['access_token']
ACCESS_TOKEN_SECRET = keys['access_token_secret']

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)

handles = sys.argv[1]
f = open(handles, "r")
h = f.readlines()
f.close()

for handle in h:
  handle = handle.rstrip()
  message = handle + " " + sys.argv[2]
  send = api.update_status(message)
  nap = randint(1,60)
  time.sleep(nap)

To secure my api keys, I created a separate file that stores a dict with my key data. Other than that, it’s pretty straightforward- I store a bunch of twitter handles in a text file, and send a message to them (provided as a command-line argument). Cool for a first project!

BTW, this blog might be filled with other stuff that I blog about, so if you want to see a running list of everything related to the 180 Day Coding Challenge, go . Shouts out to dototot for the tutorial!

Jason T Clark

Jason T Clark

Father. Musician. Gamer. Coder.

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin