Posts tagged code

River Level Twitter Bot

With all the bad weather, and with moving to opposite a river I had been keeping an eye on the river on my way to work and found the Environment Agency site has real time (sort of) data for most of the rivers in the UK.

So I decided to knock up a quick script to scrape the page for the river level and tweet to a newly created account.

Link: @RiverAireLeeds

Image Overlay CSS

My friend wanted a simple way of overlaying an image (or text) on an image, ideally using CSS. After a quick google I found a nice looking example of what he was after. Juts going to post it here so I have it for future reference.

null

HTML:

<div class="image">
 <img src="http://farm8.staticflickr.com/7151/6707334701_af50daf3c5.jpg"/>
 <h2><span>Roseberry Topping</span></h2>
</div>

 
CSS:

.image {
 position: relative;
 width: 100%; /* for IE 6 */
}

h2 {
 position: absolute;
 top: 200px;
 left: 0;
 width: 100%;
}

h2 span {
 color: white;
 font: bold 24px/45px Helvetica, Sans-Serif;
 letter-spacing: -1px;
 background: rgb(0, 0, 0); /* fallback color */
 background: rgba(0, 0, 0, 0.7);
 padding: 10px;
}

Demo: http://imjam.es/sam/test.html
Source: http://css-tricks.com/text-blocks-over-image/

Kindle – Unofficial Guardian Subscription

Having received a new 4th gen Kindle for christmas I signed up for the Guardian paper 2 week trial from Amazon. I’m quite impressed at how good the newspaper reading experience is like on the Kindle. The paper gets delivered everyday at around 6am, so when I get on the train, todays issue is sat at the top of my home page ready to be read. The subscription currently costs £10 per month, which isnt too bad, they charge £1 per issue if you don’t want to subscribe.

Guardian Kindle

I came across this blog post from Mark Longair, he has used the Guardian’s own open api to create a Kindle version of the paper, in fact he did this before the Guardian had their official Kindle version to download. Mark has the most recent edition on a page on his site, this is created ‘early’ each day. Now having compared the paid and this ‘free’ version, there isnt much difference, the majority of articles are there, including the magazines etc. I wanted a way to get this delivered to my Kindle every morning, much the same as my current paid subscription works.

Amazon allow you to email your kindle with attachments, which are then automatically downloaded to your device. So I set about creating a simple PHP script that will allow me to download the latest issue and email it to my kindle. Below is the code I used, I couldn’t get the attachment to work if the file was on a remote server, so the first thing the script does, is go and grab the todays issue from blah blahs website. Then the newly grabbed file is then attached to an email and sent to the specified email address.

Simply replace the To, From, Subject and Message variables with which ever values you require. Make sure to add the from address your going to use to your Amazon/Kindle account, to authorise this email address for delivery to your device.

<?php
 
 $filename = "gdn-".date("Y")."-".date("m")."-".date("d").".mobi"; 
  
 $handle = fopen("http://mythic-beasts.com/~mark/random/guardian-for-kindle/".$filename, "rb");
if($handle){
  $somecontent = stream_get_contents($handle);
  fclose($handle);

  $handle = fopen($filename, 'wb');
  if($handle){
    if (fwrite($handle, $somecontent) === FALSE) {       
       exit;
    }    
    fclose($handle);
  }else{     
     exit;
  }
}
 
// array with filenames to be sent as attachment
$files = array($filename);
 
// email fields: to, from, subject, and so on
$to = "hello@email.com";
$from = "me@me.com"; 
$subject ="subject"; 
$message = "message";
$headers = "From: $from";
 
// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
 
// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
 
// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$mime_boundary}\n";
 
// preparing attachments
for($x=0;$x<count($files);$x++){
	$file = fopen($files[$x],"rb");
	$data = fread($file,filesize($files[$x]));
	fclose($file);
	$data = chunk_split(base64_encode($data));
	$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
	"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
	$message .= "--{$mime_boundary}\n";
}
 
// send
 
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
	echo "<p>mail sent to $to!</p>"; 
} else { 
	echo "<p>mail could not be sent!</p>"; 
} 
 
?>

If there is any interest I may provide a service where you provide me with your kindle email address and I’ll add you to the list to receive the unofficial version as well :).

Sources: PHP email with attachments

Xbox Achievements

As i’m now tracking all of my Xbox achievements, see previous post*, i thought it’d be good to get some sort of viewing page going. So now on the right > you’ll see my last few achievements that I have unlocked. This is simply hardcoded into the sidebar as learning and writing a WordPress plugin was overkill for what I wanted.

Secondly i’ve added a bigger viewing page, click the link in the menu above. You’ll be greeted with a drop down, that lists every game i’ve played on the Xbox. Select a game and every achievement for that game is displayed via some ajax. I need to tweak it a bit as its a bit ugly atm, its breaking the sidebar :/.

Edit. *Realised I haven’t done a blog post on my Xbox Achievement tracking. Better get that done….

Torrent Status via Twitter

I’ve been wanting to use twitter to interact with my uTorrent downloads for a while. I already have a system set up that tweets me when a download has finished, but I wanted more functionality, ideally the ability to add torrents and get the overall status of my downloads.

Thankfully uTorrent have created a nice little api that lets you do pretty much everything you would want, I used this uTorrent Python library, that lets you interact with the uTorrent api. I then made a simple c# win forms app (needs changing into a command line app really) that would make various calls to the Python library. Initially the only functionality is the current status of downloads, but now I have the basics in place I can add features fairly easily.

At the moment the application reads my latest tweets, I have already created some code todo this for my Home Automation System, and then when a tweet matches the ‘status’ command, I fire off the call to the api to get the status of every download in my client. The returned list is then parsed, and any active downloads are extracted and returned to the main method. This then processes the returned strings and replies back to my original tweet with the status of my downloads.

The next step is to add the ability to add new torrent files straight from a tweet, this should be fairly straightforward, the tweet would contain the location of the .torrent file, the system would then make the call to the uTorrent api with the location of the file to be downloaded.

Text to Voice Calling

This weekend me and @Bonxy spent some time adding some features to the Bonxy SMS site, that I posted about recently. My aim was to add in voice calling as well, so users could write out prank messages or reminders and send them to friends or family. To do this I used Tropo.com, they provide a cloud based SMS/call api, allowing you to make calls, receive calls, conferences and of course send text message.

text to calling

The actual integration was pretty straightforward, use their PHP library’s, set up an application and hey presto you have voice calling and answering. I’m thinking of using the service to create a cheap conference system, all users call into the local rate number, therefore using your own contract minutes.

code

I’m also hopefully going to transfer the technology to a few other sites, to provider reminders and wake up calls/alarms etc.