Saturday 14 December 2013

Python Powered SkyCam

A few days ago, I decided to have a play around with the Twitter API, to see if there was any fun to be had. Python has a few extensions that allow the user to post tweets and read timelines. They're all a bit fiddly, mind. I didn't have much luck with Tweepy, and I wasted some time playing with lots of different packages. In the end I went with this one.

Once that was all installed, I had to get authorisation from Twitter to access the API directly. To do this you need to visit their dev pages and create an application. (Log in at https://dev.twitter.com/apps)
When you do this, you get two important keys called a Consumer Key and a Consumer Secret. Your application uses these to log in and access your twitter account.

The code to log into your account and enable access is:

import os
from twitter import *
CONSUMER_KEY="YOUR KEY HERE"
CONSUMER_SECRET="YOUR SECRET HERE"
MY_TWITTER_CREDS = os.path.expanduser('~/.my_app_credentials')
if not os.path.exists(MY_TWITTER_CREDS):
    oauth_dance("YOUR APP NAME HERE", CONSUMER_KEY, CONSUMER_SECRET,MY_TWITTER_CREDS)
oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS)
twitter = Twitter(auth=OAuth(oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))

When you run this, it opens a web page in which you link the app to your Twitter account. This creates a small file on your computer which stores your login details. After that, you can start adding commands which send tweets, read other users' tweets etc. A bog-standard tweet command might be:
twitter.statuses.update(status="Hello World!")
And from there you can start all manner of bizarre auto-tweeting scripts! You can see such things on Twitter all the time, from the classically simple @big_ben_clock to funny and sarcastic meanie bots like these.

I thought I would make something unlike anything I had seen before, so I decided to combine the tweeting script with some of the stuff I had learned when playing with my webcam. Since I can grab a webcam image and analyse the pixels in it, I decided to write a twitter bot that looks out of my window and says what colour the sky is (see here!)

To do this, I needed this videocapture module, which grabs images from a webcam, and this Image processing library to analyse the images. My little script gets a picture from the webcam and crops it down so that it just contains sky (a rectangle of 540*200 pixels). It then samples one pixel in a hundred, and works out the average colour by adding together all the red components, green components and blue components, then dividing each by the number of pixels in the sample (1080, coincidentally).

When it has the RGB average, it does a quick denary-hex conversion to get a 6-character RGB string. Then it uses the handy webpage http://www.colorhexa.com/ to communicate that as a colour in a tweet.


So there you go. Pointless, but fun. It might get a bit repetitive after a while, but I think I might look into getting the program to detect things like sunrise and sunset. Also, I might see if I can get it to generate a colour-chart collage over time and save that as an image. I don't know how you can attach images to a tweet yet, but that would be better than relying on colorhexa.com.

Keep tinkering!


Update: It seems that TweetPony gives me access to an image-uploading API method. I should be able to create an image of the colour block and remove the use of the external website.

Wednesday 11 December 2013

God and death

Version 1.9 is now in the source files folder. It was time for the enemies and the player to interact, so I have now added the code for collisions.

When you hit an enemy, if you are higher than the enemy, they die. If they are higher than you, you die. If you're at roughly the same altitude, you bounce off each other. Eventually, a dying enemy will turn into an egg that must be caught before it hatches into another enemy. At the moment you just disappear.

I have also added a God mode, to make testing easier. Press G to turn it on. When god is active, you cannot die. Also, you can prevent the enemies flapping by holding down 1. Take that, birdies!