Advertisement
seroperson

TB player info getter

Jul 22nd, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.14 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use open ':std', ':encoding(UTF-8)';
  5. use HTML::Entities; # Чтобы читать русские символы в Location
  6. use LWP::UserAgent;
  7. use HTTP::Cookies;
  8. use HTTP::Request;
  9.  
  10. my %result;
  11. my @max = (0)x4;
  12. my @headline = qw\ Username Clanname CC Location \;
  13. my $login = 'perlbot';
  14. my $pass = 'randompass';
  15.  
  16. my $ua = new LWP::UserAgent;
  17. my $cookie_jar = new HTTP::Cookies;
  18. $ua->agent("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36");
  19. $ua->show_progress('true');
  20. $ua->cookie_jar($cookie_jar);
  21.  
  22. my $req = new HTTP::Request('POST', "http://forum.toribash.com/login.php?do=login");
  23. $req->content_type('application/x-www-form-urlencoded');
  24. $req->content("vb_login_username=$login&cookieuser=1&vb_login_password=$pass&do=login");
  25. $ua->request($req);
  26.  
  27. sub normalize_size {
  28.         my @argv = @_;
  29.         my @result;
  30.         foreach(0..3) {
  31.                 my $max = @argv[$_] > length(@argv[$_+4]) ? @argv[$_] : length(@argv[$_+4]);
  32.                 @result[$_] = $max;#max(@argv[$_], length(@argv[$_+4]));
  33.         }
  34.         return @result;
  35. }
  36.  
  37. @max = normalize_size(@max, @headline);
  38. @ARGV = @ARGV ? @ARGV : <STDIN>;
  39.  
  40. Main: foreach (@ARGV) {
  41.         chomp($_);
  42.         my $username = $_;
  43.         my $code;
  44.         my $location;
  45.         my $clanname;
  46.         my $index = 0;
  47.         my @lines = split /^/, ($ua->get("http://forum.toribash.com/member.php?username=$username"))->content;
  48.         sub parse {
  49.                 sub trim {
  50.                         my $line = @_[0];
  51.                         chomp($line);
  52.                         $line =~ s/^\s+//;
  53.                         $line =~ s/(\s+)$//;
  54.                         return $line;
  55.                 }
  56.                 my $line = $_[$_[-1]];
  57.                 while($line =~ s/<.*?>(.*?)<.*?>/$1/) { }
  58.                 return trim($line);
  59.         }
  60.         foreach(@lines) {
  61.                 $index++;
  62.                 if(/This user has not registered and therefore does not have a profile to view/) {
  63.                         print $username." - ".$&."\n";
  64.                         next Main;
  65.                 }
  66.                 if(/<dt class=\"shade\">Country Code<\/dt>/) {
  67.                         $code = parse(@lines, $index);
  68.                         next;
  69.                 }
  70.                 if(/<dt class=\"shade\">Location<\/dt>/) {
  71.                         $location = decode_entities(parse(@lines, $index));
  72.                         next;
  73.                 }
  74.                 if(/<dd id=\"clan\">/) {
  75.                         $clanname = parse(@lines, $index-1);
  76.                         last;
  77.                 }
  78.         }
  79.         my @data; $data[0] = $username; $data[1] = $clanname; $data[2] = $code; $data[3] = $location;
  80.         @{$result{$username}} = @data;
  81.         @max = normalize_size(@max, @data);
  82. }
  83.  
  84. sub print_line {
  85.         my @argv = @_;
  86.         foreach(0..3) {
  87.                 my $pad = $argv[$_]+4;
  88.                 printf "%${pad}s", $argv[$_+4];
  89.         }
  90.         print "\n";
  91. }
  92.  
  93. print_line(@max, @headline);
  94. foreach(keys(%result)) { print_line(@max, @{$result{$_}}); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement