#!/usr/bin/perl -T use strict; use warnings; use GD; use CGI qw(param); use utf8; use Encode; use HTML::Entities; use HTTP::Request::Common; use LWP::UserAgent; my $q = new CGI; my $url = "http://twitter.com/statuses/friends_timeline.xml"; my $u = URI->new($url); my $username = "USERNAME FOR TWITTER"; my $password = "PASSWORD FOR TWITTER"; my $ua = LWP::UserAgent->new; $ua->agent("Mozilla/8.0"); $ua->credentials( $u->host_port , "Twitter API", $username, $password ); #my $res = $ua->request(POST $url, [ status => 'hi, yo.' ] ); my $res = $ua->request(GET $url); my $rss = $res->is_success ? $res->content : ""; my @lines; while ($rss =~ m{(.+?)}gs) { my $item = $1; next if $item =~ m{true}; if ($item =~ m{(.+?).*(.+?)}s) { push @lines, decode_entities("$2: $1"); } } print "Content-Type: image/gif\n\n"; binmode STDOUT; print generate_gif(\@lines); sub generate_gif { my ($lines_ref) = @_; my $width = 320; my $height = 240; my $font_size = 9; my $char_height = int($font_size * 1.5); my $x_start = 3; my $y_start = $char_height; my $font_name = "/home/hitobito/opt/IPAfont00203/ipagui.ttf"; my $im = new GD::Image($width, $height); my $white = $im->colorAllocate(255,255,255); my $black = $im->colorAllocate(0,0,0); my $blue = $im->colorAllocate(0,0,255); my ($x, $y) = ($x_start, $y_start); foreach my $line (@$lines_ref) { my @chars = split(//, $line); my $color = $blue; foreach my $c (@chars) { return $im->gif if $y > $height; if ($c eq ":") { $color = $black; next; } my @bounds = $im->stringFT($color, $font_name, $font_size, 0, $x, $y, $c); if ($bounds[2] > $width - $char_height) { ($x, $y) = ($x_start, $y + $char_height); } else { $x = $bounds[2]; } } ($x, $y) = ($x_start, $y + $char_height) if $x != $x_start; } return $im->gif; }