Nonprogramistan Twitterbot Monstrosity

I wanted a Twitterbot to push out Markov generated stuff from Emily Dickinson’s work. I wanted to do it fairly quickly as it was inspired by an awesome discussion yesterday with Jason Coats who will be teaching one of VCU’s online courses this summer on poetry. One of his goals was to encourage students to put themselves out there and engage with poetry. I thought mechanically created poetry might allow for a certain degree of freedom of analysis and Emily Dickinson’s work was particularly well suited to the Markov genre.

As I already had a Markov generator running with some of the possible texts for Gardner and Jon’s MOOC this summer, it was easy enough to switch out the source material with Emily Dickinson’s work. It never hurts to be able to build what you discuss while still carrying on the discussion. That’s one of the things I love about computers.

So that got me the Markov portion but it was on a web page and would require either a visit or a manual action to feed it into Twitter (which Jason had used previously with his students). To get at Twitter I needed a new Twitter account EmilyMkv which I got by using this GMail alias trick. It’s handy if you run out of unique emails.

As I thought about things what I wanted became a bit more complicated. I decided I had a mechanism to generate the Markov combinations and I could get that to Twitter but I also wanted it copied to a blog in case we wanted to use any of the generated text in a less ephemeral manner. It would also open the text to some more nuance discussion. Twitter, despite claims to the contrary, is not the best place for all conversations.

As I wandered about trying different techniques (and mostly failing) I thought more about the workflow for my current blog – which is blog post –> IFTT –> Twitter.1 It got me thinking.2

Anyway, as I had been wandering around I’d hit a number of things and they combined with a past history of dabbling in AppleScript back around 2007 led to this magnificent monstrosity.

I have the Markov PHP generator running on a webpage. Using AppleScript as a wrapper, I run a curl terminal command3 to get the result of that page and then apply it as a variable in AppleScript to make an email. That email is sent to a WordPress blog which has the JetPack post via email plugin activated. The RSS feed for that blog is tied into IFTTT to post to Twitter. That AppleScript is triggered every 6 hours by tying it to a recurring Calendar event.4

The Devil Inspired Details

To make life easier for me, I stripped down the Markov page to generate just the text. No HTML wrapper, nothing else. Look at the source. You’ll see what I mean.

The Applescript is below.

The first line pulls the content of the URL (the Dickinson Markov stuff in this case) and assigns it to the variable myText.

The next four lines assign variables for the email. In my case I wanted both the subject and the body text to match. You’ll need to put your own information in between the quotes. I left most of mine there as a model.

You shouldn’t have to mess with anything below that.

set myText to do shell script "curl"

set recipientName to "EmilyMark"
set recipientAddress to ""
set theSubject to myText
set theContent to myText

tell application "Mail"
set newMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
tell newMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
end tell
end tell

I am getting better at programming stuff but retain a certain reluctance to spend the time needed to become anything better than semi-literate. It has a lot to do with patience. I know what I want and I can usually cobble together enough pieces to make it happen. I’m not saying this is the a good way to do this but it up and working and could be done by most people (I don’t know how to mimic the AppleScript chunk on a PC but I’m sure it’s doable). In any case, I will revisit this with a more proper programatic view at some point in the near future but for now, it is ALIVE!

1 I also realize now I probably could have done the reverse as well Twitter –> blog.

2 Insert joke here.

3 This sounds far fancier than it is. essentially you write “curl”

4 This is a bit annoying compared to the old days. Read the linked tutorial if you have issues. I chose the Automator path. Plus you can only do repeating full days so you’ll have to make multiple events if you want things to happen on less than 24 hr cycles.

Comments on this post

  1. John Johnston said on February 20, 2014 at 12:48 pm

    Hi Tom,
    Lovely stuff.
    I’ve used an always running apple script to do curls on idle ( you can set the idle time) to fire remote php. But I think launchd is probably a better way to go, there is a nice app for creating launchd items:
    Which works a treat.

    • Tom Woodward said on February 20, 2014 at 1:08 pm

      Thanks John.

      Launchd is new to me and looks all sorts of useful. I initially was going to set it up as cron task but felt the Calendar added an additional element of absurdity. I’ll keep these in mind when I do it properly.

      It’s also nice to know there are a few other souls about still messing with AppleScript.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL