Created
September 19, 2013 11:14
-
-
Save m1ari/6621996 to your computer and use it in GitHub Desktop.
Short perl script to show BATC chat in a console window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use LWP::UserAgent; | |
use HTTP::Request::Common qw(GET); | |
# Channel is the feed number we want the chat for. | |
my $channel=1153; | |
my $ua = new LWP::UserAgent; | |
$ua->cookie_jar( {} ); | |
#$ua->agent("User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0"); | |
# Request the main stream page - we need to get a cookie for the chat to work | |
my $req = GET 'http://www.batc.tv/ch_live.php?ch=5&id=' . $channel; | |
my $res = $ua->request($req); | |
# The chat app seems to need an additional header to work | |
$ua->default_header('X-Requested-With' => 'XMLHttpRequest'); | |
# lastmod is an identifier for what chat messages to request. Start at 0 to get the history | |
my $lastmod=0; | |
while (1){ | |
my $req = GET 'http://www.batc.tv/chat/getcontent.php?ch=1' . $channel .'&laststamp=' . $lastmod; | |
my $res = $ua->request($req); | |
if ($res->is_success) { | |
# print $res->content; | |
foreach (split("\n", $res->content)){ | |
if ($_ =~ /^\$\('cmessages'\)\.insert\(El\);$/ ){ | |
} elsif ($_ =~ /^El=new Element\('div'\);$/ ){ | |
} elsif ($_ =~ /^El\.writeAttribute\('class','cmsg'\);$/ ){ | |
} elsif ($_ =~ /^var objDiv = \$\('cmessages'\); objDiv\.scrollTop = objDiv\.scrollHeight;$/ ){ | |
} elsif ($_ =~ /^$/ ){ | |
} elsif ($_ =~ /^<br \/><b>Error: Unable to establish link with mysql: Too many connections<\/b>$/ ){ | |
} elsif ($_ =~ /^<br \/><b>Error: Unable to establish link with mysql: Can't connect to local MySQL server through socket '\/var\/lib\/mysql\/mysql\.sock' \(11\)<\/b>$/ ){ | |
} elsif ($_ =~ /^El\.update\('<div class="ctime">([0-9\/:\ ]+)<\/div> <div class="cauthor">(#sys#)<\/div> : <div class="csys">(.*)<\/div>'\);$/){ | |
printf("%-11.11s %-16.16s %s\n",$1, $2, $3); | |
} elsif ($_ =~ /^El\.update\('<div class="ctime">([0-9\/:\ ]+)<\/div> <div class="cauthor">([a-zA-Z0-9]+)<\/div> : <div class="ctext">(.*)<\/div>'\);$/){ | |
printf("%-11.11s %-16.16s %s\n",$1, $2, $3); | |
} elsif ($_ =~ /^El\.update\('<div class="ctime">([0-9\/:\ ]+)<\/div> <div class="cauthor"><a href="http:\/\/www\.batc\.tv\/bio\.php\?id=[0-9]+">([a-zA-Z0-9]*)<\/a><\/div> : <div class="ctext">(.*)<\/div>'\);$/){ | |
printf("%-11.11s %-16.16s %s\n",$1, $2, $3); | |
} elsif ($_ =~ /lastMod=([0-9]+);/ ){ | |
$lastmod=$1; | |
} else { | |
print "Error: " . $_ . "\n"; | |
} | |
} | |
} else { | |
print $res->status_line . "\n"; | |
} | |
sleep (1); | |
} | |
__END__ | |
Initial commands used in a telnet session | |
GET /chat/getcontent.php?ch=11153&laststamp=706802 HTTP/1.1 | |
Host: www.batc.tv | |
X-Requested-With: XMLHttpRequest | |
Cookie: PHPSESSID=wibblewibblewibblewibblewi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment