# (0) 05 Dec 2013, 08:51AM: Fisher-Price's My First Twitter Bot:
On Sunday I wrote my first Twitter bot, with a bit of help from Leonard. (A Hacker School colleague inferred, understandably, that Leonard and I just write Twitter bots on the weekend, to relax.) Then yesterday I helped a peer get her first Twitter bot going, and decided to write it up for y'all/future Pythonistas.
already written other guides to writing good Twitter bots. Those more experienced people are better than I am at writing good, surprising bots, ones that respond to tiresome hashtags or argue with followers or what have you. My guide, in contrast, assumes that you already have some kind of code that spits out strings of 140 characters or fewer, and just want help interfacing with the Twitter API using Python so a Twitter account can say those tweets.
I am posting this guide on 5 December 2013, and undoubtedly Twitter will revamp all this within the next fortnight and switch to OAuth 2038 (OAuth For Workgroups) and turn their developer site into something you manipulate by holding up your mobile phone in front of your Google Glasses. But for right now this works.
- I rely on the python-twitter wrapper around the Twitter API. If you are using a virtual environment that you made with mkvirtualenv or similar, then activate your venv and
pip install python-twitter
If you live on the edge, or if you are a devil-may-care type who loves convenience so much more than safety that you tab-complete your passwords, then command-tab away from Snapchat* long enough to install the package globally:
sudo pip install python-twitter
- Now, go to Twitter.com and create a Twitter account for your bot, e.g., @FreedomBot. Make sure that you give Twitter an email address whose messages you can actually read. Read the end user license agreement and agree to it. Did you know that there's a hotel in San Francisco named the Eula?
- Look for the automatic email from Twitter and follow the instructions to confirm your account. Once you've done that, go to dev.twitter.com and sign in with the username and password of your bot's Twitter account, e.g., your username is FreedomBot.
- Go to https://dev.twitter.com/apps and choose to create a new app. Yes, your "Markov chain parody of the Office of Management and Budget" script is an "app" for our purposes; this is the easiest way to get the relevant keys.
- Go ahead and fill in a reasonable name, description, and link (your website or Gitorious repo would do). These will not be publicly visible or scrutinized by Twitter's gatekeeper gnomes, so don't sweat it. Leave the "callback URL" field empty. Read the Rules of the Road (so much more folksy than "Guidelines for the Walled Garden") and agree.
- Now that you have an "app", go to the Settings tab and change your app type from Read-only to Read/Write, so you can actually post to Twitter from your script. (If you're having trouble navigating back to your app, go to dev.twitter.com/apps to find it.)
- Next, on your app's Details tab, click the button at the bottom to create your API secret key & token. It'll take a minute for Twitter to create those for you; after a minute, go ahead and refresh the page. Now that page gives you the "consumer key" and "consumer secret" as well as the API "access token key" and "access token secret".
(The "consumer key/secret" pair identifies your "app" (the code you are writing) as something that is allowed to interact with the Twitter API; it's sort of a substitute for the User-Agent string in a browser. The "access token key/secret" pair authorizes the Twitter account whose tweets the "app" is gonna write. So conceivably you could write an app like Sycorax that lets your bot roleplay multiple accounts, and it would end up getting multiple access token key/secret pairs so that could work. This is all OAuth stuff that briefly turns you into an octopus when you understand it.)
- So, how can your script use these secret strings (to authenticate and authorize you) without those supersensitive nuclear launch codes falling into the wrong hands, viz., your GitHub repo? I did it this way:
Open up a new file in the same directory as your bot script, called something like "twitterapi.py". Also add "twitterapi.py" to your repository's .gitignore.
#!/wherever/you/put/python # ok, probably /usr/bin/python
api = twitter.Api(consumer_key="KEY",
So now, in your application or in the Python interpreter, you can do:
from twitterapi import api
- And then, to post your awesome string to Twitter as @FreedomBot:
api.PostUpdate("wow\n so Program Assessment Rating Tool\nnice\n very budgetary")
- Optional step! If you want to leave your bot running to tweet at 5-minute intervals, then
import time and stick a
time.sleep(5*60) line and an
api.PostUpdate(awesomestring) into a
while True loop.
* Is there a Snapchat desktop app? Maybe Snapchat is available for the iPad and in my hypothetical you're programming on your tablet? I don't know what people do these days.
: Hacker School
# (0) 30 Nov 2013, 06:37PM: Round Seven of OPW:
Congratulations to all six of the Wikimedia's chosen participants in the current round of FOSS Outreach Program for Women internships. I'm especially glad I was able to help Maria Pacana and Be Birchall, my colleagues via Hacker School, learn more about the program and apply.
Many months ago, Wikimedian Liam Wyatt tweeted:
@hexmode @brainwane prob a dumb question, but do we have anything planned like @codeacademy to help folks learn mediawiki/php/wikimarkup?
The answer: now we do. One OPW intern will make a Codecademy course on the MediaWiki API. (Also, we now have The Wikipedia Adventure and the Visual Editor to help people start editing.)
# (0) 30 Nov 2013, 11:11AM: Colons: A Retrospective:
I am working on another silly project, and it led me to look at winners and finalists for the Pulitzer Prize for General Nonfiction. There are way more colons in the titles of those books than there used to be.
At Leonard's suggestion, I did a stacked graph via LibreOffice.
Here's the original data, and here's the breakdown by year. I went from 1980 to the present because 1980 is the first year the Pulitzer folks released the list of finalists.
I know many people will not find it surprising that we've got a whole lot of colons these days. But look at the old titles: there used to be so few! 1963: Barbara W. Tuchman's The Guns of August. 1975: Annie Dillard's Pilgrim at Tinker Creek. I may ask some publishing-y people what forces and trends change book titles over time, and whether Pulitzer finalists are outliers when it comes to book-naming. Three or four titles per year makes for a ridiculously tiny sample size.
# (0) 27 Nov 2013, 03:45PM: Shiny:
I hereby recommend to you the super-readable, witty, on-point analysis of cosmetics ad claims at "Brightest Bulb In the Box: Beauty for Critical Minds". Much thanks to terriko for the link to BBItB! If you liked Constellation Games, you might imagine Robyn as a genderswapped Ariel Blum. If the aliens show up, she may demand to try and test their cosmetics. I had no idea I wanted to read beauty blogging until I came across Robyn.
I love her perfume reviews, e.g.:
This is the most generic perfume ever. Like, if you didn't care about perfume and just sort of imagined something boring, this is what it would smell like.
If this scent were being worn by a fictional character, it would be Ann Veal from Arrested Development.
Robyn also makes her research available free-as-in-blush, e.g., testing "What Methods of Foundation Application Use the Least Product?" or "How Much Do Your Eyeshadow Brushes Matter?". Most recently, she got out the chi-square to compare two different monthly subscription boxes a few different ways.
But I especially want you to check out her resveratrol and Urban Decay Naked Skin Beauty Balm posts. Her commentary on "light-defusing spheres" especially made me guffaw. Other tidbits:
"DNA repair, optical blurring, oil free"? One of those claims just doesn't belong. (And it is the last one, because it makes sense.)...
First, I want to deal briefly with "reseveratrol". Juice Beauty spelled the name of their supposed active ingredient incorrectly. What they mean to say is resveratrol, which is a phenylpropanoid that is found in the skins of grapes....
If you are a yeast cell, congratulations on your literacy. Maybe check out this resveratrol thing. If you are a human, though, you should know that at the present time, there are NO peer reviewed journal articles that suggest that resveratrol has any effect on people....
Also, Robyn's leitmotif "your face" (e.g., "Your imperfections really would be less noticeable in diffused light, but the solution to that is to avoid uncovered bulbs in your house, not to put this stuff on your face.") reminds me of Danni, who says "your face" a lot and whom I miss.
# (0) 25 Nov 2013, 04:31PM: The Last One You'd Ever Suspect:
Check out this Vienna Teng live set in which she performs a synth-backed "Whatever You Want." I'm just entranced and have been listening to the set over and over. I especially find myself caught by the line
I am the last one you'd ever suspect of setting the fire, of setting the fire
# (0) 23 Nov 2013, 01:59PM: I Cannot Be The First Person To Quip About Quantified Self-Loathing:
After the first week I spent at Hacker School, I worried that I wasn't spending enough time on improving my programming skills. So I started using Project Hamster to track chunks of time that I specifically spent either learning (via coding, pairing, or listening to useful lectures, mostly), versus chunks I spent teaching or helping others.
This past week, I looked at my involvement with Bicho, an open source project that helps people analyze data from bug trackers, and decided there were too many blockers for me to keep on going as I was going. Thriving is a function of a person times their environment, as I learned in my tech management courses, and -- as I wrote in a summary on the metrics-grimoire mailing list -- at my current level of programming proficiency, and given how much refactoring and testing Bicho could use, it's just a bad fit right now. The maintainers responded well, and promise a refactored Bicho is coming, so I hope to restart contributing at some point in the future.
I wondered, after I stopped: how much time had I spent on this project, and what had I learned from it? So I crunched the numbers. Between October 7th and today, I've spent 158 hours on learning activities and 12.9 on teaching/helping activities, which gives me 173.2 hours in total. (I was sometimes rough when inputting my time into Hamster, so take my significant digits with a grain of salt.) Of those, I've spent 55.9 on Bicho, 53.9 on learning and about two on teaching/helping (such as filing bugs and writing that super long email). So that's a little under a third of my Hacker School learning time.
What did I learn? I threw together a rough list:
- read a big giant codebase and understood how parts work together
- got lots more experience with git
- learned how argparse works enough to port something to it from optparse
- used XML-RPC APIs, xmlrpclib
- used launchpadlib module
- learned how packaging works (setup.py, pip)
- grokked __init__ and modules vs packages
- learned some of PEP 8 and used the pep8 and pep8ify modules
- used assert & wrote tests
- used Beautiful Soup
- used some of vars & getattr
- used pdb a little bit
- learned a little bit re Storm and ORMs
- used MySQL & python-MySQLdb
- learned to watch out for old- versus new-style classes
- got comfortable with virtual environments
- wrote a regex
- learned not to put an unquoted argument on the commandline (ampersands!)
That first one is huge. I think it may just take a super long time the first time you try to wrap your head around a codebase fifteen thousand lines long. Then again, now that I've had this experience, I've ordered Michael Feathers's Working Effectively with Legacy Code and may start following Jessica McKellar's advice to Maria Pacana: [Don't] try to understand the whole thing. Understand only as much as you need to know to make the contribution you want to make.
I've now moved on to a different project where I'm making clearer progress, though sometimes it's a slog. In retrospect, I don't really know whether my Bicho work was a good investment of my Hacker School time, or whether I should have stopped a few weeks earlier and learned more and different things. I am trying to remember not to fall prey to the fallacious Fear Of Missing Something. Maybe part of what I learned is a better intuition for "it's time to try a different approach." Argh. So hard, maybe impossible, to assess whether I made good decisions!
: Hacker School
# (0) 23 Nov 2013, 11:40AM: How Comprehensive Are Your Unit Tests? Coverage.py Knows:
I've been writing and maintaining unit tests for my project. But only on Thursday did a colleague's presentation remind me that I could run a code coverage tool to check which code paths my tests are or aren't exercising.
I found it super easy to install and run coverage.py, and it only took marginally more fuss to
--omit="~/.virtualenvs/*". The detailed feedback helped me increase my coverage from 70% to 82%; yay! Thanks, Ned Batchelder & other coverage.py contributors.
: Hacker School