找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 17691|回复: 0

[原创教程] Mojo::UserAgent 批量获取 nes 游戏资源

[复制链接]
发表于 2019-1-15 14:40:12 | 显示全部楼层 |阅读模式
本帖最后由 523066680 于 2019-1-15 14:46 编辑

获取midi(背景音乐)、nes、图片、作弊码、以及PDF(通常是手册)
运行环境:Strawberry Perl v5.26
路径设置:our $wdir = "F:/temp/nesgames";
  1. =info
  2.     Mojo::UserAgent 批量获取 nes 游戏资源
  3.     523066680/vicyang
  4.     2018-12
  5. =cut

  6. use File::Slurp;
  7. use File::Path;
  8. use File::Basename;
  9. use Mojo::UserAgent;
  10. use Mojo::DOM;
  11. use Try::Tiny;
  12. STDOUT->autoflush(1);

  13. our $wdir = "F:/temp/nesgames";
  14. our $main = "http://www.nesfiles.com";
  15. our $games = "http://www.nesfiles.com/Games";

  16. mkpath $wdir unless -e $wdir;
  17. chdir $wdir;

  18. our $ua = Mojo::UserAgent->new();
  19. our @headers = ( "User-Agent" => "Firefox/63.0" );

  20. get_games_list($games);

  21. sub get_games_list
  22. {
  23.     our ($ua, $main, @heaee);
  24.     my ($link) = @_;
  25.     my $res = $ua->get( $link, \@headers )->res;
  26.     my $dom = $res->dom;

  27.     for my $e ( $dom->find(".nesfilesTable a")->each )
  28.     {
  29.         #printf "%s %s\n", $e->attr("href"), $e->text;
  30.         get_files( $main .$e->attr("href"), $e->text );
  31.     }
  32. }

  33. sub get_files
  34. {
  35.     our ($main, $ua, @headers);
  36.     my ($link, $name) = @_;
  37.     my $title = basename($link);
  38.     my ($res, $dom);

  39.     my $fname = "${title}.html";
  40.     if ( -e $fname ) {
  41.         my $html = read_file($fname);
  42.         $dom = Mojo::DOM->new( $html );
  43.     } else {
  44.         $res = $ua->get( $link, \@headers )->res;
  45.         $dom = $res->dom;
  46.         write_file( $fname, {binmode=>":raw"}, $res->body );
  47.     }

  48.     # 获取资源明细,略过 Ebay 相关的条目
  49.     mkdir $title unless -e $title;
  50.     my ($head, $list, $res2);
  51.     for my $section ($dom->find(".GameSection")->each)
  52.     {
  53.         $head = $section->at("header")->text;
  54.         last if $head=~/Ebay$/i;

  55.         # 秘籍/代码
  56.         if ($head=~/Codes/i) {
  57.             write_file( $title ."/Codes_Cheats.txt", $section->all_text );
  58.             next;
  59.         }

  60.         if ($head=~/Screenshots/i) {
  61.             # 如果是屏幕截图
  62.             $list = $section->find("img")->map(attr=>"src");
  63.         } else {
  64.             # 其他情况获取 href
  65.             $list = $section->find("a")->map(attr=>"href");
  66.         }

  67.         printf "%s\n", $head;
  68.         for my $href ( $list->each )
  69.         {
  70.             printf "%s\n", $href;
  71.             $fname = $title ."/". basename($href);
  72.             
  73.             next if -e $fname;         # 跳过已经存在的文件
  74.             $res2 = try_to_get( "${main}$href" );
  75.             next unless defined $res2; # 如果获取失败

  76.             write_file( $fname, {binmode=>":raw"}, $res2->body);
  77.         }
  78.     }
  79. }

  80. sub try_to_get
  81. {
  82.     our ($ua, @headers);
  83.     my ($link) = @_;
  84.     my $res;
  85.     my $times = 0;

  86.     while (1)
  87.     {
  88.         try { $res = $ua->get( $link )->result; }
  89.         catch { printf "Error %s, retry: %d\n", $_, $times; };
  90.         $times++;
  91.         last if (defined $res and $res->is_success);
  92.         return undef if ( $times > 5 );
  93.     }
  94.     return $res;
  95. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 00:28 , Processed in 0.017374 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表