#!/usr/bin/perl

use strict;

use lib '/www/drudge/v5';
use strict;
use LWP::Simple;
use HTML::TreeBuilder;
use HTTP::Request::Common qw(GET);
use HTTP::Date;
use URI::Escape;
use Encode;
use HTML::Readability;
use Cache::Memcached::Fast;
use CGI;

my $cgi = CGI->new;
my $hop = $cgi->param('hop');
my $type = $cgi->param('type');
if (!$hop) {
   $hop = 'http://www.dinakaran.com/News_detail_2011.asp?Nid=6088';
}
#$hop = 'http://www.businessinsider.com/wynn-ceo-steve-wynn-conference-call-transcript-obama-2011-7';
if ($hop !~ /http/i ) {
   $hop = 'http://drudgereport.com/' .$hop;
}
$hop =~ s/\s//g;
#print STDERR "$hop\n";
#print $cgi->redirect( -URL => $hop);
#exit;

my $memd = new Cache::Memcached::Fast({ servers => [ { address => 'localhost:11211' }], connect_timeout => 0.01, nowait => 1, utf8 => 1, max_size => 512 * 1024 });
my $key = $hop;
$key = $key.$type if ($type eq 'ap');
my $val = $memd->get($key);
my $heredoc = undef;
if ($val) {
   $heredoc = $val->{'v'};
   unless ($heredoc) {
      print $cgi->header(-Referer => 'http://www.drudgereport.com/', -Location => $hop);
      exit;
   }
   #print STDERR "$hop\n";
} else { 
   #print STDERR "$hop\n";
   my $docOff = new HTML::Readability();
   $docOff->url($hop);
   $SIG{ALRM} = sub { 
      $memd->set($key, {'v' => undef}, 1000) ;
      print $cgi->redirect( -URL => $hop);
      exit;
   };

   eval {
      alarm(15);
      $docOff->getHTML();
      $docOff->initDoc();
      alarm(0);
   };

   my @uri = $docOff->uri_split();
   my $base_url = $uri[0].'://'.$uri[1].$uri[2];
   my $content = $docOff->getFinalContent();
   my $title = $docOff->getTitle();
   if (length($content) < 50) {
      $memd->set($key, {'v' => undef}, 1000) ;
      print $cgi->redirect( -URL => $hop);
      exit;
   }
$heredoc = <<END;
<!doctype html> <html> <head>
   <meta name = "viewport" content="width=device-width, initial-scale=1.0,user-scalable=yes">
   <title>$title</title>
   <style>
body {
      font-family:Georgia,serif; 
      font-size:1.0em;
      margin:0 auto;
      width:88%;
      line-height:1.5em;
   }

   h2,h6 {
      font-size:1.1em;
      margin: 0 auto;
      font-weight:normal;color:#69502F;
   }

   img { clear:both; float:left;max-width:100%; height:auto; display:block; margin: 2px 12px 12px 2px; }

   a {color:#000070;text-decoration: none;}

   a.tt {color:#2E2E2E;background-color:#F2F2F2; font-family: arial, serif;font-weight:normal;}
   #h {margin-top:2px; margin-bottom:2px;font-size:1.2em;font-family: "Lucida Grande,tahoma,serif";font-weight:bold;background-color:#F7F7F7}
   #u {margin-top:2px; font-size:0.7em;font-family: "Arial";color:#E77F00;}
   b,strong {font-weight:normal;color:#69502F}
</style>
   <base href="$base_url" />
   </head>
   <body>

<div id="u">$uri[1]</div>
<div id="h" onclick="javascript:location.href='$hop'">
END
$heredoc .= $title;
#$heredoc .= ' | <a class="tt" href="'.$hop.'">View Original</a></div> <hr />';
$heredoc .= '</div> <hr />';
$heredoc .= $content;
if (0 && $type ne 'ap') {
my @ads = ('a149205013dc6b7','a149249895796a2');
my $rand = rand(4);
$heredoc .= <<END;
<script type="text/javascript">
var admob_vars = {
    pubid: '$ads[$rand]', 
    bgcolor: 'FF9119', 
    text: 'FFFFFF', 
    test: false
};
</script>
<script type="text/javascript" src="http://mm.admob.com/static/iphone/iadmob.js"></script>
END
}
$heredoc .= '</body></html>';
if (checkSpecialDomains($hop)) {
   print $cgi->header(-Referer => 'http://www.drudgereport.com/', -Location => $hop);
   exit;
}
$memd->set($key, {'v' => $heredoc}, 1000) ;
}
print $cgi->header(
       -Pragma        => 'no-cache',
       -charset       => 'utf-8',
     );

binmode(STDOUT, ":utf8");
print $heredoc;

sub checkSpecialDomains {
   my $url = shift;
   return ($url =~ /^http:\/\/(youtube|vimeo|video|www\.ft\.com)/i);
}
