updated git and svn scripts
[xrzperl.git] / discuz_exp
blobe7ed5b7eeed6bf0072823ee1c5eb0f0d9f04c930
1 #!/usr/bin/perl -w
2 ###APPNAME: discuz_exp
3 ###APPAUTHOR: duel
4 ###APPDATE: 2009-01-19 01:53:48
5 ###APPVER: 0.1
6 ###APPDESC: discuz_exp
7 ###APPUSAGE:
8 ###APPEXAMPLE: discuz_exp
9 ###APPOPTION:
10 use strict;
12 #ENV variable MUST be defined somewhere,
13 #FOR perl to search modules from,
14 #OR nothing will work
15 use lib $ENV{XR_PERL_MODULE_DIR};
17 use MyPlace::Script::Usage qw/help_required help_even_empty/;
18 exit 0 if(help_required($0,@ARGV));
19 #exit 0 if(help_even_empty($0,@ARGV));
21 use MyPlace::Discuz::Forum;
22 no strict 'refs';
23 use URI;
25 my $g_url;
26 my $g_page;
27 my @g_history;
29 my $g_url_up;
30 my $g_url_prev;
31 my $g_url_next;
33 my %g_cmd_map = (
34 "l"=>"loadurl",
35 "f"=>"forums",
36 "t"=>"threads",
37 "p"=>"pages",
38 "q"=>"quit",
39 "g"=>"goto",
40 "v"=>"view",
41 "d"=>"download",
42 "txt"=>"text",
43 "img"=>"images",
44 "pt"=>"posts",
47 my %g_type_map = (
48 "f"=>"forum",
49 "t"=>"thread",
50 "p"=>"page",
51 "img"=>"image",
52 "pt"=>"pt",
53 'txt'=>"text",
56 sub cmd_loadurl {
57 my $new_url=shift;
58 unless($new_url) {
59 print STDERR "Usage:loadurl (url)\n";
60 return undef;
62 $new_url=URI->new_abs($new_url,$g_url) if($g_url);
63 print STDERR "Loading $new_url...";
64 my @data = `netcat_autologin '$new_url' $user $password | gb2utf`;
65 return undef unless(@data);
66 print STDERR "\t[Done]\n";
67 print STDERR "Loading page...";
68 my $page = MyPlace::Discuz::Forum->new(@data);
69 print STDERR "\t[Done]\n";
70 $g_url=$new_url;
71 $g_page=$page;
72 return 1;
75 sub fix_print {
76 my $max_num=shift;
77 my $num=0;
78 foreach(@_) {
79 $num++;
80 #printf "%3d.%-30s\t",$num,$_;
81 print "$num.$_";
82 unless($num % $max_num) {
83 print "\n";
85 else {
86 print "\t\t";
89 print "\n" if($num % $max_num);
92 sub cmd_forums {
93 if($g_page->{forums}) {
94 print "Forums:\n";
95 &fix_print(4,map $_->[1],@{$g_page->{forums}});
97 else {
98 print "No forums\n";
102 sub cmd_threads {
103 if($g_page->{threads}) {
104 print "Threads:\n";
105 &fix_print(1,map $_->[1],@{$g_page->{threads}});
107 else {
108 print "No thread\n";
112 sub cmd_pages {
113 if($g_page->{pages}) {
114 print "Pages:",scalar(@{$g_page->{pages}}),"\n";
116 else {
117 print "No page\n";
121 sub cmd_text {
122 if($g_page->{post}) {
123 print $g_page->{post_text};
124 print "\n";
126 else {
127 print "No text\n";
131 sub cmd_images {
132 my $index=shift;
133 if($g_page->{post} and $g_page->{post}->[1]) {
134 my $url = get_select("image",$index) if(defined $index);
135 if($url) {
136 eval {system("feh '$url'&");};
138 else {
139 print "Images:\n";
140 &fix_print(1,@{$g_page->{post_images}});
143 else {
144 print "No images\n";
148 sub get_select {
149 my $type=shift;
150 my $idx=shift;
151 return undef unless($type);
152 $idx = 1 unless($idx);
153 $type = $g_type_map{$type} if($g_type_map{$type});
154 my $url;
155 if($type =~ /^forum$/) {
156 $url = $g_page->{forums}->[$idx-1]->[0];
158 elsif($type =~ /^thread$/) {
159 $url = $g_page->{threads}->[$idx-1]->[0];
161 elsif($type =~ /^page$/) {
162 $url = $g_page->{pages}->[$idx-1];
164 elsif($type =~ /^image$/) {
165 $url = $g_page->{post_images}->[$idx-1];
167 else {
168 return undef;
170 $url = URI->new_abs($url,$g_url) if($url and $g_url);
171 return $url;
174 sub cmd_goto {
175 my $url = get_select(@_);
176 unless($url) {
177 print STDERR "Usage:goto type index\n";
178 print STDERR "\ttype = forum,page,thread\n";
180 else {
181 cmd_loadurl($url);
185 sub cmd_download {
186 my $type=shift;
187 unless($type) {
188 print STDERR "Usage:download type index\n";
189 print STDERR "\ttype = forum,page,thread,image,text\n";
190 return undef;
192 if($type =~ /text|txt/) {
193 my $filename=$g_page->{title} . ".txt";
194 $filename =~ s/[\/\\]//g;
195 print "Saving text content to $filename...";
196 if (open FO,">",$filename) {
197 print FO $g_page->{post_text};
198 close FO;
200 else {
201 print "\t[Failed]\n";
204 else {
205 my $url = get_select($type,@_);
206 if($url) {
207 my $filename = $url;
208 $filename =~ s/^.*\///g;
209 $filename =~ s/[\?\/\\]//g;
210 print "Saving url $url to $filename...";
211 system("netcat_autologin '$url'>$filename");
212 print "\t[Done]\n";
214 else {
215 print "Nothing to download for $type @_\n";
220 sub cmd_view {
221 my $url = get_select(@_);
222 unless($url) {
223 print STDERR "Usage:view type index\n";
224 print STDERR "\ttype = forum,page,thread\n";
226 else {
227 print "Open $url through firefox\n";
228 system("firefox '$url' 2>&1 1>/dev/null&");
232 sub cmd_help {
233 print "I can't hear,louder please!\n";
236 sub get_data {
237 my $type=shift;
238 return $g_page->{$type} ? $g_page->{$type} : $g_page->{$type . "s"} ? $g_page->{$type . "s"} : undef;
241 sub get_data_type {
242 my $data = shift;
243 my $index = shift;
244 my $type = shift;
245 return undef unless($data);
246 if(ref $data->[0]) {
247 return $data->[$index][$type];
249 else {
250 return $data->[$index];
254 sub cmd_list {
255 my $type=shift;
256 my $idx=shift;
257 return undef unless($type);
258 if($type =~ /^text$/) {
259 return "text",$g_page->{post_text};
261 my $array;
262 $array = $g_page->{$type} ? $g_page->{$type} : $g_page->{$type . "s"} ? $g_page->{$type . "s"} : undef;
263 return undef unless($array);
264 my $is_ref = ref $array->[0] ? 1 : 0;
265 my $ubound = @{$array};
266 if(defined $idx) {
267 $idx++;
268 if($idx >$ubound) {
269 return "url",undef;
271 my $url = $is_ref ? $array->[$idx][0] : $array->[$idx];
272 $url = URI->new_abs($url,$g_url) if($url and $g_url);
273 return "url",$url;
275 else {
276 my @urls = $is_ref ? map $_->[0],@{$array} : @{$array};
277 map URI->new_abs($_,$g_url),@urls if(@urls and $g_url);
278 return "url",@urls;
282 sub convert_cmdline {
283 my $cmd = shift;
284 my $type = shift;
285 my $index = shift;
286 return undef unless($cmd);
287 return $cmd unless($type);
288 if($index and $index=~ /[0-9]+/) {
290 else {
291 unshift @_,$index if($index);
292 $index = undef;
294 $cmd = $g_cmd_map{$cmd} if($g_cmd_map{$cmd});
295 $type = $g_type_map{$type} if($g_type_map{$type});
296 my @arg = cmd_list($type,$index);
297 if(@arg) {
298 shift @arg;
299 return $cmd,@arg;
301 else {
302 return $cmd,$type,@_;
305 sub prompt {
306 print "Discuz> ";
307 while(<>) {
308 chomp;
309 goto next_label unless($_);
310 my @cmd=split(/\s+/,$_);
311 goto next_label unless(@cmd);
312 @cmd = &convert_cmdline(@cmd);
313 my $cmd_name=shift @cmd;
314 if (defined *{"cmd_$cmd_name"}) {
315 *{"cmd_$cmd_name"}->(@cmd);
317 else {
318 # print "Pass to shell $cmd_name,@cmd\n";
319 eval {system "$cmd_name @cmd";};
321 next_label:
322 print "Discuz> ";
324 return 1;
327 sub cmd_quit {
328 print "Saving...";
329 open FO,">",$ENV{HOME} . "/.discuz_exp";
330 print FO $g_url;
331 close FO;
332 print "\t[Done]\n";
333 exit 0;
336 my $url=shift;
337 unless($url) {
338 if(open FI,"<",$ENV{HOME} . "/.discuz_exp") {
339 $url=<FI>;
340 close FI;
343 &cmd_loadurl($url) if($url);
344 push @g_history,$url;
345 &prompt;