A #DS106 DailyCreate Twitter Bot

dailycreate-botThe DailyCreate Bot

I’ve always liked random and automated random things. While these are not strictly part of storytelling I’ve managed to bring them into DS106 whenever I can.

A while back I set up @DailyCreateBot for some reason or other. Obviously a Twitter bot of some kind to do with the Daily Create. I do remember having trouble with the OAuth requirements of the more recent Twitter API and giving up.

Last weekend, on a rainy day I blew the dust of my raspberry pi and got it online and set up as a server. I was not too sure what to do with it at the time.

During the week I did revisit a project to use the pi to flash some lights depending on a Twitter search. I don’t have hardware for that but I was interested in how simple the project was. There seems to be plenty of libraries that can sort out Authentication to Twitter for you now. A bit of googling and thinking, mostly googling and I have a Twitter bot set up.

The @DailyCreateBot will reply with a suggestion of a photo challenge of you mention him on Twitter. I am using the same list that Alan Levin provided for me for the photoblitz.

The @DailyCreateBot runs on Python. This is where the pi comes in I would not even know where to begin to find out how to host a python app but the pi lets me do that easily.

I am not proposing to write a step-by-step guide here but it is worth mentioning that several things went wrong or did not work as expected. All were beyond my 2 weeks worth of Python on the mechanical mooc . All were solved by a wee bit of googling and sometime just repeating things till they worked. The delight of working on a pi is that I knew I’d I totally messed up I could just reformat the SD card, install an so again and be back to square two.

I had already:

  • installed one of the basic OS FOR THE PI
  • Set up SSH access so that I can get ‘on’ to the pi from the terminal application on a mac and via SSH apps on iOS.
  • set up the pi as a web server and sorted out the DNS

Next:

*I found a python library and example code that replied.
*I added logic to reply with a random string taken from a list of challenges.
*Tested it a bit.

Then I posted to the DS106 Google + group and a few kind folk tested it a bit. Rochelle asked:

That is cool +John Johnston . It worked for me right out of the bot box. Do we upload to Twitter, tag them DailyCreateBot? I’d like to see what others have done. 🙂

Which got me thinking. A quick google found a php/JavaScript solution to showing tweets with the hashtag #dailycreatebot and I’ve got this up and running.

All very much a work in progress. There are few things to be ironed out:

  • the Python bot falls over every now and again complaining about UTF8 I need to google that some more.
  • the web page showing images just uses the styles used in the demo of the code. I need to tidy it up and perhaps skip tweets with the hashtag but no images.
  • there is also the problem Rochelle pointed out that if you reply to the bot you get another prompt. I wonder if I could turn off replies if there is an image in the tweet?

Anyway if your expectations are low you can join in:

  1. Tweet @DailyCreateBot and get a prompt.
  2. Tweet your photo with the hashtag #DailyCreateBot
  3. See what other folk are doing.
  4. Let me know of any interesting problems.

#DS106 Flickr Daily Creates Pummelvision style

I’ve been playing around with downloading groups of flickr photos and making very quick videos from them. Just blogged some of the process over on my main blog. Being a wee obsessive about this sort of stuff, I’ve created a script that will download all my photos with a particular tag, say dailycreate, and string them together into a movie.

You need to have the commandline ffmpeg application installed and have created a flickr app so that you have an api key. You need to know your flickr id too.

This works for me, copy the code below, replace the bold bits and paste into the terminal. Probably best to run this in a folder. Depending on how many photos you have it cold take a while.
FILELIST=$(curl 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=YOUR-FLICKER-APIKEY&user_id=YOUR-FLICKr_USERNAME&tags=THE-TAG&extras=url_l&per_page=200&format=rest' | sed -ne 's/.*\(http[^"]*\).*/\1/p');echo $FILELIST;for i in $FILELIST ; do curl -O $i ; done;a=1;for i in *.jpg; do
new=$(printf "%04d.jpg" "$a") #04 pad to length of 4
mv -- "$i" "$new"
let a=a+1
done;sips --resampleHeight 768 *.jpg;sips --padToHeightWidth 786 1024 *.jpg;ffmpeg -f image2 -i %04d.jpg -c:v libx264 -pix_fmt yuv420p firstmov.mp4;ffmpeg -i firstmov.mp4 -filter:v "setpts=4.0*PTS" flickr-dailycreate.mp4

This wee bit:

ffmpeg -i flickr-dailycreate.mp4 -i backgroundloop.mp3 -map 0 -map 1 -codec copy -codec:a aac -strict experimental -b:a 192k -shortest flickr-dailycreate-audio.mp4

would add some audio if there is a backgroundloop.mp3 in the same folder.

Gif Scraping and a DS106 Gif API

It started, as so many things do, with a tweet(bark?) from CogDog linking to The Best of ds106 GIF TV. This got me thinking of how to automate the gathering of gifs. I remembered that I had done some gif scraping a while back: Doug’s Gifs.

I wondered about scrapping tumblr in the same manner with a wee bit of php. This turns out to be quite simple: Gifs, Gifs


$url = "http://jjgifs.tumblr.com/rss";

$rss = file_get_contents($url);
$matches = array();
preg_match_all('!http://[^?#\s]+\.(gif)!', $rss,$matches);

I didn’t do the regExpression myself, I must have found it on the internet when I made the page of Doug’s gifs. Anyway at this point you have a list (php array) of urls to all of the gifs on the page. I’ve not worked out the best way to handle this, but currently I am writing it to a cache file and only updating it every now and again. The list is put into a JavaScript Array.

The page Gifs, Gifs, loops through a few ds106naughts tumblrs rather than just the one. It then displays a random gif, clicking on this will display another.

This is pretty much proof of concept rather than a well though out solution, but it works. There are a few interesting things that could be done structurally. Alan has already suggested hiving off the generation to a separate php file and a cron job. We could then have a plain html/javascript page for display. He also suggest a next and previous button(√). I am guessing that it might be better to have the list in a database?

Now I have a text file with a list of ds106 gifs, it is pretty simple to have a
ds106GifAPI! This following image should be a random DS106 gif:

a random ds106 gif

The code used:


<img src="http://johnjohnston.info/oddsandends/ds106gif" alt="a random ds106 gif " />

This should give a random gif every time.

THe DS106 Gif API

Imagine you are blogging (or making any webpage) and want an exciting gif to go with your post, but you don’t just want any old gif, you want a random DS106 gif. Safe for work but not for your mind. Now you casn, simply use http://johnjohnston.info/oddsandends/ds106gif as an image url and you will get a random gif every time the page loads.

Given that my gif store is a list of gif urls the code is simple:


<?php
$cachefile = "Path/To/The/Cache/File.txt";
$gifs = file_get_contents($cachefile); 
$gifArray=explode("\n",$gifs);
$thisgif=$gifArray[rand(0,count($gifArray))];
header('Location: '.$thisgif);
?>

DS106 Random Gif API