theanonym

1cloud.pl

Sep 11th, 2013
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.61 KB | None | 0 0
  1. use 5.010;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5.  
  6. use LWP;
  7. use File::Slurp qw/read_file write_file/;
  8.  
  9. #--------------------------------------------------------
  10.  
  11. my $pages   = 100;
  12. my $outfile = "1db.txt";
  13.  
  14. #--------------------------------------------------------
  15.  
  16. my $lwp = new LWP::UserAgent;
  17.  
  18. sub get_board_page {
  19.    my($page) = @_;
  20.    my $res = $lwp->get("http://1chan.ru/news/all/$page/");
  21.    die $res->status_line unless $res->is_success;
  22.    return $res->content;
  23. }
  24.  
  25. sub get_thread_page {
  26.    my($thread) = @_;
  27.    my $res = $lwp->get("http://1chan.ru/news/res/$thread/");
  28.    die $res->status_line unless $res->is_success;
  29.    return $res->content;
  30. }
  31.  
  32. sub parse_threads {
  33.    my($html) = @_;
  34.    my @threads = $html =~ m~<span class="b-blog-entry_b-info_b-link">\s*?<a href="http://1chan.ru/news/res/(\d+)/">~gs;
  35.    return @threads;
  36. }
  37.  
  38. sub parse_comments {
  39.    my($html) = @_;
  40.    my @comments = $html =~ m~<div class="b-comment_b-body g-clearfix">\s*<p>(.*?)</p>\s*?</div>?~gs;
  41.    return @comments;
  42. }
  43.  
  44. sub prepare_comments {
  45.    my @comments = @_;
  46.    return map {
  47.       utf8::decode($_);
  48.       $_ = lc $_;
  49.       tr/ё/е/;
  50.       s~[^а-я\s]~ ~g;
  51.       s~\s+~ ~g;
  52.       s~^\s+|\s+$~~g;
  53.       utf8::encode($_);
  54.       $_;
  55.    } @comments;
  56. }
  57.  
  58. my %used_threads;
  59. my @comments;
  60. for my $page (0 .. $pages) {
  61.    say "Get page '$page'";
  62.    for my $thread (parse_threads get_board_page $page) {
  63.       next if $used_threads{$thread}++;
  64.       say "Get thread '$thread'";
  65.       push @comments, prepare_comments parse_comments get_thread_page $thread;
  66.    }
  67. }
  68.  
  69. write_file($outfile, join " ", @comments);
Advertisement
Add Comment
Please, Sign In to add comment