1
0

ttools.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #!/usr/bin/perl
  2. ################################################################################
  3. # $Id: ttools.pl 2823 2007-04-07 08:37:43Z warion $
  4. # $Revision: 2823 $
  5. # $Date: 2007-04-07 03:37:43 -0500 (Sat, 07 Apr 2007) $
  6. ################################################################################
  7. # #
  8. # LICENSE #
  9. # #
  10. # This program is free software; you can redistribute it and/or #
  11. # modify it under the terms of the GNU General Public License (GPL) #
  12. # as published by the Free Software Foundation; either version 2 #
  13. # of the License, or (at your option) any later version. #
  14. # #
  15. # This program is distributed in the hope that it will be useful, #
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of #
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  18. # GNU General Public License for more details. #
  19. # #
  20. # To read the license please visit http://www.gnu.org/copyleft/gpl.html #
  21. # #
  22. # #
  23. ################################################################################
  24. # #
  25. # Requirements : #
  26. # * Digest::SHA1 ( perl -MCPAN -e "install Digest::SHA1" ) #
  27. # * LWP::UserAgent ( perl -MCPAN -e "install LWP::UserAgent" ) #
  28. # #
  29. ################################################################################
  30. use strict;
  31. ################################################################################
  32. # timeout for url-get
  33. my $TIMEOUT = 5;
  34. # Internal Vars
  35. my ($VERSION, $DIR, $PROG, $EXTENSION, $USAGE);
  36. #-------------------------------------------------------------------------------
  37. # Main
  38. #-------------------------------------------------------------------------------
  39. # init some vars
  40. $VERSION =
  41. do { my @r = (q$Revision: 2823 $ =~ /\d+/g); sprintf "%d"."%02d" x $#r, @r };
  42. ($DIR=$0) =~ s/([^\/\\]*)$//;
  43. ($PROG=$1) =~ s/\.([^\.]*)$//;
  44. $EXTENSION=$1;
  45. # main-"switch"
  46. SWITCH: {
  47. $_ = shift @ARGV;
  48. /info|.*-i/ && do { # --- info ---
  49. loadModules();
  50. torrentInfo(shift @ARGV);
  51. exit;
  52. };
  53. /scrape|.*-s/ && do { # --- scrape ---
  54. loadModules();
  55. torrentScrape(shift @ARGV);
  56. exit;
  57. };
  58. /check/ && do { # --- check ---
  59. check();
  60. exit;
  61. };
  62. /.*(version|-v).*/ && do { # --- version ---
  63. printVersion();
  64. exit;
  65. };
  66. /.*(help|-h).*/ && do { # --- help ---
  67. printUsage();
  68. exit;
  69. };
  70. printUsage();
  71. exit;
  72. }
  73. #===============================================================================
  74. # Subs
  75. #===============================================================================
  76. #------------------------------------------------------------------------------#
  77. # Sub: torrentInfo #
  78. # Parameters: string with path to torrent-meta-file #
  79. # Return: null #
  80. #------------------------------------------------------------------------------#
  81. sub torrentInfo {
  82. my $torrentFile = shift;
  83. if (!(defined $torrentFile)) {
  84. printUsage();
  85. exit;
  86. }
  87. if (!(-f $torrentFile)) {
  88. print "Error : ".$torrentFile." is no file\n";
  89. exit;
  90. }
  91. my $torrent = new Net::BitTorrent::File($torrentFile);
  92. if (!(defined $torrent)) {
  93. print "Error loading torrent-meta-file ".$torrentFile."\n";
  94. exit;
  95. }
  96. # hash
  97. my $hash = $torrent->info_hash();
  98. $hash =~ s/(.)/sprintf("%02x",ord($1))/egs;
  99. print "hash : ".lc($hash)."\n";
  100. # name
  101. print "name : ".$torrent->name()."\n";
  102. # announce
  103. print "announce : ".$torrent->announce()."\n";
  104. # files + size
  105. my $info = $torrent->info();
  106. my $torrentSize = 0;
  107. print "file(s) : \n";
  108. if (defined($info->{'files'})) {
  109. foreach my $fileEntry (@{$info->{'files'}}) {
  110. $torrentSize += $fileEntry->{'length'};
  111. if (ref($fileEntry->{'path'}) eq 'ARRAY') {
  112. print " ".$info->{'name'}.'/'.$fileEntry->{'path'}->[0]." (".$fileEntry->{'length'}.")\n";
  113. } else {
  114. print " ".$info->{'name'}.'/'.$fileEntry->{'path'}." (".$fileEntry->{'length'}.")\n";
  115. }
  116. }
  117. } else {
  118. $torrentSize += $info->{'length'},
  119. print " ".$info->{'name'}." (".$info->{'length'}.")\n";
  120. }
  121. print "size : ".$torrentSize."\n";
  122. }
  123. #------------------------------------------------------------------------------#
  124. # Sub: torrentScrape #
  125. # Parameters: string with path to torrent-meta-file #
  126. # Return: null #
  127. #------------------------------------------------------------------------------#
  128. sub torrentScrape {
  129. my $torrentFile = shift;
  130. if (!(defined $torrentFile)) {
  131. printUsage();
  132. exit;
  133. }
  134. if (!(-f $torrentFile)) {
  135. print "Error : ".$torrentFile." is no file\n";
  136. exit;
  137. }
  138. my $torrent = new Net::BitTorrent::File($torrentFile);
  139. if (!(defined $torrent)) {
  140. print "Error loading torrent-meta-file ".$torrentFile."\n";
  141. exit;
  142. }
  143. # get hash
  144. my $hash = $torrent->info_hash();
  145. $hash =~ s/(.)/sprintf("%02x",ord($1))/egs;
  146. $hash = lc($hash);
  147. # get scrape url
  148. my $scrapeUrl;
  149. if ((index($torrent->announce(), "/announce")) > 0) {
  150. $scrapeUrl = $torrent->announce();
  151. $scrapeUrl =~ s#/announce#/scrape#ig;
  152. } else {
  153. print "Error : could not get scrape-url\n";
  154. exit;
  155. }
  156. # get scrape info
  157. my $res = getUrl($scrapeUrl);
  158. if (!($res->is_success)) {
  159. print "Error : could not fetch scrape-infos : ".$res->status_line()."\n";
  160. exit;
  161. }
  162. # decode data
  163. my $info;
  164. eval {
  165. $info = Convert::Bencode::bdecode($res->content);
  166. };
  167. if ($@) {
  168. print "Error : cant decode info-data : ".$@."\n";
  169. exit;
  170. }
  171. # check result
  172. if (!(defined $info)) {
  173. print "Error in tracker-response.\n";
  174. exit;
  175. }
  176. if (!(ref($info) eq 'HASH')) {
  177. print "Error in tracker-response.\n";
  178. exit;
  179. }
  180. if (!(exists($info->{'files'}))) {
  181. print "Error : tracker-response did not contain files.\n";
  182. exit;
  183. }
  184. # process response
  185. foreach my $fileEntry (%{$info->{'files'}}) {
  186. my $t = $info->{'files'}{$fileEntry};
  187. my $fileHash = $fileEntry;
  188. $fileHash =~ s/(.)/sprintf("%02x",ord($1))/egs;
  189. $fileHash = lc($fileHash);
  190. if ($fileHash eq $hash) {
  191. print $t->{'complete'}." seeder(s), ";
  192. print $t->{'incomplete'}." leecher(s).\n";
  193. exit;
  194. }
  195. }
  196. # error
  197. print "Error : tracker-response did not contain requested info.\n";
  198. exit;
  199. }
  200. #------------------------------------------------------------------------------#
  201. # Sub: getUrl #
  202. # Parameters: string with url #
  203. # Return: res #
  204. #------------------------------------------------------------------------------#
  205. sub getUrl() {
  206. my $url = shift;
  207. my $ua = LWP::UserAgent->new(
  208. 'agent' => $PROG.$EXTENSION."/".$VERSION,
  209. 'timeout' => $TIMEOUT
  210. );
  211. return $ua->get($url);
  212. }
  213. #------------------------------------------------------------------------------#
  214. # Sub: loadModules #
  215. # Arguments: null #
  216. # Returns: null #
  217. #------------------------------------------------------------------------------#
  218. sub loadModules {
  219. # load Digest::SHA1
  220. if (eval "require Digest::SHA1") {
  221. Digest::SHA1->import();
  222. } else {
  223. print STDERR "Error : cant load perl-module Digest::SHA1 : ".$@."\n";
  224. exit;
  225. }
  226. # load LWP::UserAgent
  227. if (eval "require LWP::UserAgent") {
  228. LWP::UserAgent->import();
  229. } else {
  230. print STDERR "Error : cant load perl-module LWP::UserAgent : ".$@."\n";
  231. exit;
  232. }
  233. # load Convert::Bencode
  234. if (eval "require Convert::Bencode") {
  235. Convert::Bencode->import();
  236. } else {
  237. print STDERR "Error : cant load perl-module Convert::Bencode : ".$@."\n";
  238. exit;
  239. }
  240. # load Net::BitTorrent::File
  241. if (eval "require Net::BitTorrent::File") {
  242. Net::BitTorrent::File->import();
  243. } else {
  244. print STDERR "Error : cant load perl-module Net::BitTorrent::File : ".$@."\n";
  245. exit;
  246. }
  247. }
  248. #------------------------------------------------------------------------------#
  249. # Sub: check #
  250. # Arguments: Null #
  251. # Returns: info on system requirements #
  252. #------------------------------------------------------------------------------#
  253. sub check {
  254. print "checking requirements...\n";
  255. # 1. perl-modules
  256. print "1. perl-modules\n";
  257. my @mods = ('Digest::SHA1', 'LWP::UserAgent', 'Convert::Bencode', 'Net::BitTorrent::File');
  258. foreach my $mod (@mods) {
  259. if (eval "require $mod") {
  260. print " - ".$mod."\n";
  261. next;
  262. } else {
  263. print "Error : cant load module ".$mod."\n";
  264. # Turn on Autoflush;
  265. $| = 1;
  266. print "Should we try to install the module with CPAN ? (y|n) ";
  267. my $answer = "";
  268. chomp($answer=<STDIN>);
  269. $answer = lc($answer);
  270. if ($answer eq "y") {
  271. exec('perl -MCPAN -e "install '.$mod.'"');
  272. }
  273. exit;
  274. }
  275. }
  276. # done
  277. print "done.\n";
  278. }
  279. #------------------------------------------------------------------------------#
  280. # Sub: printVersion #
  281. # Arguments: Null #
  282. # Returns: Version Information #
  283. #------------------------------------------------------------------------------#
  284. sub printVersion {
  285. print $PROG.".".$EXTENSION." Version ".$VERSION."\n";
  286. }
  287. #------------------------------------------------------------------------------#
  288. # Sub: printUsage #
  289. # Parameters: null #
  290. # Return: null #
  291. #------------------------------------------------------------------------------#
  292. sub printUsage {
  293. print <<"USAGE";
  294. $PROG.$EXTENSION (Revision $VERSION)
  295. Usage: $PROG.$EXTENSION operation path-to-torrent-meta-file
  296. $PROG.$EXTENSION check
  297. $PROG.$EXTENSION version
  298. $PROG.$EXTENSION help
  299. Operations :
  300. -i : decode and print out torrent-info.
  301. -s : get scrape-info and print out seeders + leechers.
  302. Examples:
  303. $PROG.$EXTENSION -i /foo/bar.torrent
  304. $PROG.$EXTENSION -s /foo/bar.torrent
  305. USAGE
  306. }
  307. # EOF