Fork me on GitHub

On My Tv: With Trakt.tv

On My Tv: With Trakt.tv

By now you’ve probably discovered I’m a big fan of trakt.tv, well scrobbling in general. I wan’t a way to show what I’m watching on my own blog in a way that isn’t the preformatting jpg’s trakt.tv provide (which is pretty cool in it’s own right).

So with my trakt.tv api key I decided to write up a quick ruby script to show what’s currently on my tv:

require "net/http"
require "uri"
require 'json'
require 'yaml'

uri = URI.parse("http://api.trakt.tv/user/watching.json/your_api_key/your_username")
response = Net::HTTP.get_response(uri)
result = JSON.parse response.body

if result.is_a?(Hash)
	
	if result["show"]
		title = "<div style="padding-left: 20px;"><h3>ON MY TV</h3><h5 style="margin-bottom: 10px;">#{result['show']['title']} - #{result['episode']['title']} (s#{result['episode']['season']}e#{result['episode']['number']})</h5>"
		poster = result['show']['images']['poster'].gsub('.jpg', '-138.jpg')
		title << "<img src="#{poster}" /></div>"
	elsif result["movie"]
		title = "<div style="padding-left: 20px;"><h3>ON MY TV</h3><h5 style="margin-bottom: 10px;">#{result['movie']['title']} (#{result['movie']['year']})</h5>"
		poster = result['movie']['images']['poster'].gsub('.jpg', '-138.jpg')
		title << "<img src="#{poster}" /></div>"
	end

	trakt = File.open("/path/to/text_file/playing.txt", "w")
	trakt.write(title)
	trakt.close

end

Three things to note here. You will need to update your api-key, username and the path to where you want the text file to be written.

Next up I poll trakt’s api every 5mins via:

crontab -e
*/5 * * * * ruby trakt.rb

Which updates the playlist.txt file. I then bring that into my blog with a simple:

echo file_get_contents('playlist.txt');

Let me know if you use this so I can checkout some new things to watch!

Posted by voidet

Categorised under Programming
Bookmark the permalink or leave a trackback.

2 Comments

  1. If you’re using trakt.tv, maybe you’re interested in TraktTV WordPress Widget. It’s free -check it out! If you like it, maybe write a review here

    November 6, 2012 @ 1:45 am
  2. It’s a shame you don’t have a donate button! I’d certainly donate to this superb blog! I suppose for now i’ll settle for bookmarking and
    adding your RSS feed to my Google account. I look forward to fresh updates and will talk
    about this website with my Facebook group.

    Talk soon!

    January 11, 2013 @ 11:26 am

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

or