script/MakeHandler.pl untested

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 205 CC 2 SCAR 13.9
CC
2
Cov
0%
Prompt
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 4.0
1 #!/usr/local/bin/perl
2  
3 # use perl -*- mode: Perl; -*-
4  
5 use strict;
6 use File::Cache;
7  
8 my $VERSION = 0.62;
9 my $COMPATIBLE_NEWS_CLIPPER_VERSION = 1.18;
10  
11 my %info;
12 # author_name
13 # author_email
14 # maintainer_name
15 # maintainer_email
16 # name
17 # description
18 # category
19 # url: url for the web page
20 # license
21 # for_news_clipper_version
22 # language
23 # urlcode: code that sets "my $url"
24 # defaults: code to set defaults
25 # attributes: an array of all the attributes and their possible values, as
26 # "attribute:(value1|value2|value3)".
27 # patterncode: code to set the start and end patterns
28 # getfunction: the name of the Get function (GetHtml, e.g.)
29 # getcode: the Get code.
30 # date: code for the GetUpdateTimes function
31 # defaulthandlers: code that contains the GetDefaultHandlers function
32  
33 my $handlerserver = 'handlers.newsclipper.com';
34 #my $handlerserver = 'localhost';
35  
36 sub Prompt;
37 my ($editor,@prompt);
38  
39 print <<EOF;
40 Greetings! This is News Clipper's MakeHandler.pl program, which is designed to
41 help you write handlers. Well, actually, it will help you write *Acquisition*
42 handlers. General filter and output handlers are a bit more involved, and it's
43 not likely that you'll need to write them.
44  
45 This utility will invoke a text editor to allow you to enter responses. The
46 main control will happen here at the script, but most of the data entry will
47 be done in the editor. Just enter the requested information in the space
48 between the prompts, which look like this:
49  
50 vvvvv
51 <THIS IS WHERE YOU ANSWER>
52 ^^^^^
53  
54 Please read the hints for handler writers, at
55 http://www.newsclipper.com/handlers.htm#Handler_Tutorial
56  
57 EOF
58  
59 print "Enter the name of your favorite editor (notepad, vi, emacs): ";
60 $editor = <STDIN>;
61 chomp $editor;
62  
63 #-------------------------------------------------------------------------------
64  
65 print "First, we need a few details...\n\n";
66  
67 push @prompt,<<EOF;
68 What's your name?
69 EOF
70  
71 push @prompt,<<EOF;
72 Where can people reach you by email, in case they have problems with your
73 handler, or want to make modifications to it?
74 EOF
75  
76 push @prompt,<<EOF;
77 Enter the handler's name, in all lower case.
78 EOF
79  
80 push @prompt,<<EOF;
81 Enter a one line description of the handler, so people can understand what it
82 does when it is listed on the handler webpage.
83 EOF
84  
85 push @prompt,<<EOF;
86 Enter a URL where people can surf to in order to get an idea of where the data
87 comes from.
88 EOF
89  
90 push @prompt,<<EOF;
91 What language will the handler be most suited for?
92 EOF
93  
94 push @prompt,<<EOF;
95 Sometimes people like to put a license on their code that gives other people
96 limited rights to modify, copy, sell, etc. it. The GPL is pretty popular, as
97 is the Artistic license. For a summary of the major open source licenses, see
98 http://www.oreilly.com/catalog/opensources/book/perens.html
99  
100 Enter the license type, if you want to license your code:
101 EOF
102  
103 push @prompt,<<EOF;
104 Please choose a category for your handler. If you would like to use a category
105 that is not here, you can enter anything you would like now. However, you will
106 need to email the database maintainer at SubmitHandler\@newsclipper.com and
107 ask that the category be added before you submit the handler to the database.
108  
109 General
110 Tech
111 Business
112 Sports
113 Science
114 Weather
115 Music
116 Local
117 Humor
118 Comics
119 Linux
120 Programming
121 Personal Computers
122 Miscellaneous
123 EOF
124  
125 ($info{author_name},$info{author_email},$info{name},$info{description},
126 $info{url},$info{language},$info{license},$info{category}) = Prompt(@prompt);
127  
128 $info{maintainer_name} = $info{author_name};
129 $info{maintainer_email} = $info{author_email};
130 $info{for_news_clipper_version} = $COMPATIBLE_NEWS_CLIPPER_VERSION;
131  
132 #-------------------------------------------------------------------------------
133  
134 print <<EOF;
135 Some handlers get their information from different URLs, depending on what
136 parameters the user enters. For example, the yahootopstories handler can
137 grab data from several different URLs that all share a common format.
138  
139 Will your URL depend on a parameter?
140 EOF
141  
142 my $yesno;
143 $yesno = <STDIN>;
144 if ($yesno =~ /^y/i)
145 {
146 push @prompt,<<EOF;
147 What is the name of the attribute that you would like the URL to depend on?
148 ("source", for example.)
149 EOF
150  
151 push @prompt,<<EOF;
152 Enter the values (in lower case) and corresponding URLs, like so:
153 'headlines' => 'http://some.server.com/headlines'
154 'technews' => 'http://some.server.com/technews'
155 'humor' => 'http://some.server.com/humor'
156 EOF
157  
158 push @prompt,<<EOF;
159 What is the default value for the attribute?
160 EOF
161  
162 my ($attr,$values,$default) = Prompt(@prompt);
163  
164 $values =~ s/\n/,\n /gs;
165  
166 $info{urlcode} = " my \%urlMap = (\n $values,\n );\n\n";
167 $info{urlcode} .= " my \$url = \$urlMap{\$attributes->{'$attr'}};";
168  
169 $info{defaults} .= " \$attributes->{'$attr'} = '$default'\n " .
170 "unless defined \$attributes->{'$attr'};\n";
171  
172 $values =~ s/'\s*=>\s*'[^']+'/|/gs;
173 $values =~ s/['\n ,]//g;
174 $values =~ s/\|$//gs;
175  
176 push @{$info{attributes}}, "$attr:($values)";
177 }
178 else
179 {
180 push @prompt,<<EOF;
181 What is the URL from which News Clipper should grab the data?
182 EOF
183  
184 ($info{urlcode}) = Prompt(@prompt);
185 $info{urlcode} = " my \$url = '$info{urlcode}';";
186 }
187  
188 #-------------------------------------------------------------------------------
189  
190 RETRY1:
191  
192 print <<EOF;
193 Choose an acquisition function:
194 (1) GetUrl -- Gets raw data from a URL, without making links absolute. Use
195 this for text and such. Grabs all the data from the URL.
196 (2) GetText -- Extracts text from HTML, fixing < to &lt;, etc.
197 (3) GetHtml -- Extracts HTML, fixing relative links.
198 (4) GetImages -- Extracts images, fixing relative links.
199 (5) GetLinks -- Extracts hyperlinks, fixing relative links and removing
200 formatting.
201 EOF
202  
203 my $input = <STDIN>;
204 chomp $input;
205 goto RETRY1 if $input !~ /^[12345]$/;
206  
207 $info{getcode} = ' my $data = ';
208  
209 my $acqFunction;
210 $info{getfunction} = 'GetUrl' if $input eq '1';
211 $info{getfunction} = 'GetText' if $input eq '2';
212 $info{getfunction} = 'GetHtml' if $input eq '3';
213 $info{getfunction} = 'GetImages' if $input eq '4';
214 $info{getfunction} = 'GetLinks' if $input eq '5';
215  
216 $info{getcode} .= 'GetUrl' if $input eq '1';
217 $info{getcode} .= 'GetText' if $input eq '2';
218 $info{getcode} .= 'GetHtml' if $input eq '3';
219 $info{getcode} .= 'GetImages' if $input eq '4';
220 $info{getcode} .= 'GetLinks' if $input eq '5';
221  
222 $info{getcode} .= "(\$url,\$startPattern,\$endPattern);\n"
223 if $info{getfunction} ne 'GetUrl';
224 $info{getcode} .= "(\$url);\n"
225 if $info{getfunction} eq 'GetUrl';
226  
227 $info{getcode} .= " return undef unless defined \$data;\n";
228  
229 #-------------------------------------------------------------------------------
230  
231 if ($info{getfunction} ne 'GetUrl')
232 {
233 print <<EOF;
234 Sometimes there are several sections on a web page, and you want to allow the
235 user to choose only one. For example, a site might put headlines and tech
236 articles on the same web page. Answer "no" to this question if there are
237 several pieces of data, and it's likely that the user will want them all.
238  
239 Do you want the grabbed data to depend on a parameter?
240 EOF
241  
242 $yesno;
243 $yesno = <STDIN>;
244 if ($yesno =~ /^y/i)
245 {
246 push @prompt,<<EOF;
247 First, a word about patterns. You should prefix the pattern with (?i) if you
248 want it to be case insensitive. ^ matches the beginning of the web page, and \$
249 matches the end. You can use \\n to match the end of a line.
250  
251 Try to choose something that is unlikely to change when the web site gets
252 redesigned. For example, if you're grabbing a comic, and you know that the
253 comic is the only image that has a filename like "blah29385829.gif", don't try
254 to precisely grab the <img src="blah29385829.gif"> tag using GetHtml. Instead,
255 grab all the images using GetImages, then weed out everything but the one you
256 want. (When using GetLinks and GetImages, you can afford to have a "loose"
257 match if it allows you to pick a better pattern.)
258  
259 You should return "clean" HTML, without any unclosed <em>s and such. In fact,
260 you should strip out <font> tags, since that restricts the web designer. Later
261 you can manually edit the handler and clean up the HTML using the TrimTags and
262 StripTags functions.
263  
264 What is the name of the attribute that you would like the grabbed data to
265 depend on? ("source", for example.)
266 EOF
267  
268 push @prompt,<<EOF;
269 Enter the values (in lower case) and corresponding starting and ending
270 patterns that News Clipper can use to grab the information, like so:
271 'headlines' => ['<!-- Start Headlines -->','<!-- End Headlines -->'],
272 'technews' => ['<!-- Start Technews -->','<!-- End Technews -->'],
273 'humor' => ['<!-- Start Humor -->','<!-- End Humor -->'],
274 EOF
275  
276 push @prompt,<<EOF;
277 What is the default value for the attribute?
278 EOF
279  
280 my ($attr,$values,$default) = Prompt(@prompt);
281  
282 $values =~ s/\n/\n /gs;
283  
284 $info{patterncode} = " my \%patternMap = (\n $values,\n );\n\n";
285 $info{patterncode} .= " my \$startPattern = \$patternMap{\$attributes->{'$attr'}}[0];\n";
286 $info{patterncode} .= " my \$endPattern = \$patternMap{\$attributes->{'$attr'}}[1];\n";
287  
288 $info{defaults} .= " \$attributes->{'$attr'} = '$default'\n " .
289 "unless defined \$attributes->{'$attr'};\n";
290  
291 $values =~ s/'\s*=>\s*\[[^\]]+\]/|/gs;
292 $values =~ s/['\n ,]//g;
293 $values =~ s/\|$//gs;
294  
295 push @{$info{attributes}}, "$attr:($values)";
296 }
297 else
298 {
299 push @prompt,<<EOF;
300 First, a word about patterns. You should prefix the pattern with (?i) if you
301 want it to be case insensitive. ^ matches the beginning of the web page, and \$
302 matches the end. You can use \\n to match the end of a line.
303  
304 Try to choose something that is unlikely to change when the web site gets
305 redesigned. For example, if you're grabbing a comic, and you know that the
306 comic is the only image that has a filename like "blah29385829.gif", don't try
307 to precisely grab the <img src="blah29385829.gif"> tag using GetHtml. Instead,
308 grab all the images using GetImages, then weed out everything but the one you
309 want. (When using GetLinks and GetImages, you can afford to have a "loose"
310 match if it allows you to pick a better pattern.)
311  
312 You should return "clean" HTML, without any unclosed <em>s and such. In fact,
313 you should strip out <font> tags, since that restricts the web designer. Later
314 you can manually edit the handler and clean up the HTML using the TrimTags and
315 StripTags functions.
316  
317 What is the starting pattern News Clipper can use to grab the data?
318 EOF
319  
320 push @prompt,<<EOF;
321 What is the ending pattern News Clipper can use to grab the data?
322 EOF
323  
324 my ($start,$end) = Prompt(@prompt);
325  
326 $info{patterncode} = " my \$startPattern = '$start';\n";
327 $info{patterncode} .= " my \$endPattern = '$end';\n";
328 }
329 }
330  
331 #-------------------------------------------------------------------------------
332  
333 if ($info{getfunction} =~ /(Url|Text|Html)/)
334 {
335 $info{defaulthandlers} =<<EOF;
336 sub GetDefaultHandlers
337 {
338 my \$self = shift;
339 my \$inputAttributes = shift;
340  
341 my \$returnVal =<<' EOF';
342 <output name='string'>
343 EOF
344  
345 return \$returnVal;
346 }
347 EOF
348 }
349 else
350 {
351 $info{defaulthandlers} =<<EOF;
352 sub GetDefaultHandlers
353 {
354 my \$self = shift;
355 my \$inputAttributes = shift;
356  
357 my \$returnVal =<<' EOF';
358 <filter name='limit' number=10>
359 <output name='array'>
360 EOF
361  
362 return \$returnVal;
363 }
364 EOF
365 }
366  
367 #-------------------------------------------------------------------------------
368  
369 push @prompt,<<EOF;
370 Now specify the times at which you know the remote server updates its data,
371 and that News Clipper should refresh its cached data. Please be a little
372 conservative here -- If you specify every hour of the day, lots of people will
373 be hitting their server when they probably aren't even looking at their News
374 Clipper webpage.
375  
376 Date specifications are of the form "[day] hour,hour,hour [time zone]". If you
377 leave out the day, every day is assumed. If you leave out the time zone,
378 Pacific Standard Time is assumed. If you leave out everything, the default of
379 "2,5,8,11,14,17,20,23 PST" is used. For example, if you are making a handler
380 for a daily comic, you might want to just use '7', since the comic changes at
381 6 am PST every day.
382  
383 The days are: sun,mon,tue,wed,thu,fri,sat. You can specify multiple times, for
384 example:
385  
386 mon 6,8 EST
387 tues 16 CST
388 20
389  
390 would update Mondays at 6am and 8am EST, Tuesdays at 4pm CST, and every day
391 at 8pm PST.
392  
393 Enter your date specification:
394 EOF
395  
396 my ($datespec) = Prompt(@prompt);
397  
398 if ($datespec eq '')
399 {
400 $info{date} = '';
401 }
402 else
403 {
404 $datespec =~ s/\n/',\n '/gs;
405  
406 $info{date} =<<EOF;
407 sub GetUpdateTimes
408 {
409 return ['$datespec'];
410 }
411 EOF
412 }
413  
414 #-------------------------------------------------------------------------------
415  
416 my $attributes;
417 $attributes = join ' ',@{$info{attributes}} if defined $info{attributes};
418 $attributes = '' unless defined $info{attributes};
419  
420 $attributes =~ s/:([^)]+)\)/=X/g;
421 $attributes = " $attributes" if $attributes ne '';
422  
423 my $att2;
424 $att2 = join "\n ",@{$info{attributes}} if defined $info{attributes};
425 $att2 = '' unless defined $info{attributes};
426 $att2 = "\n $att2" if $att2 ne '';
427  
428 my $code =<<" EOF";
429 --------> THESE LINES WILL BE REMOVED BY MAKEHANDLER <--------
430 --------> EDIT THIS VERSION OF THE HANDLER IF YOU NEED TO. <--------
431 --------> FOR EXAMPLE, IF YOU WERE DOING THE "ASTROPIC" HANDLER, <--------
432 --------> YOU WOULD WANT TO RETURN A HASH CONTAINING SEVERAL DATA <--------
433 --------> ITEMS, SO YOU'D HAVE TO EDIT THE "GET" FUNCTION, AS WELL <--------
434 --------> AS THE "GETDEFAULTHANDLERS" FUNCTION. <--------
435 # -*- mode: Perl; -*-
436  
437 package NewsClipper::Handler::Acquisition::$info{name};
438  
439 use vars qw( \@ISA \$VERSION \%handlerInfo );
440  
441 --------> FIX THESE COMMENTS. ADD ANY ADDITIONAL ATTRIBUTES YOU <--------
442 --------> NEED, AND EXPLAIN WHAT THE ATTRIBUTES MEAN. THIS IS THE <--------
443 --------> DOCUMENTATION THAT THE USER WILL REFER TO IN ORDER TO <--------
444 --------> USE YOUR HANDLER. <--------
445 \$handlerInfo{'Author_Name'} = '$info{author_name}';
446 \$handlerInfo{'Author_Email'} = '$info{author_email}';
447 \$handlerInfo{'Maintainer_Name'} = '$info{maintainer_name}';
448 \$handlerInfo{'Maintainer_Email'} = '$info{maintainer_email}';
449 \$handlerInfo{'Description'} = <<'EOF';
450 $info{description}
451 EOF
452 \$handlerInfo{'Category'} = '$info{category}';
453 \$handlerInfo{'URL'} = <<'EOF';
454 $info{url}
455 EOF
456 \$handlerInfo{'License'} = '$info{license}';
457 \$handlerInfo{'For_News_Clipper_Version'} = '$info{for_news_clipper_version}';
458 \$handlerInfo{'Language'} = '$info{language}';
459 \$handlerInfo{'Notes'} = <<'EOF';
460 EOF
461 \$handlerInfo{'Syntax'} = <<'EOF';
462 <input name=$info{name}$attributes>$att2
463 EOF
464  
465 use strict;
466 use NewsClipper::Handler;
467 \@ISA = qw(NewsClipper::Handler);
468  
469 # - The first number should be incremented when a change is made to the
470 # handler that will break people's input files.
471 # - The second number should be incremented when a change is made that won't
472 # break people's input files, but changes the functionality.
473 # - The third number should be incremented when only a bugfix is applied.
474  
475 \$VERSION = do {my \@r=('0.1.0'=~/\\d+/g);sprintf "\%d."."\%02d"x\$#r,\@r};
476  
477 # ------------------------------------------------------------------------------
478  
479 sub ComputeURL
480 {
481 my \$self = shift;
482 my \$attributes = shift;
483  
484 $info{urlcode}
485  
486 return \$url;
487 }
488  
489 # ------------------------------------------------------------------------------
490  
491 # This subroutine checks the handler's attributes to make sure they are valid,
492 # and sets any default attributes if necessary.
493  
494 sub ProcessAttributes
495 {
496 my \$self = shift;
497 my \$attributes = shift;
498 my \$handlerRole = shift;
499  
500 # Set defaults here. You can safely delete this function if your handler has
501 # no attributes with default values.
502  
503 # \$attributes->{'some_attribute'} = 'default_value'
504 # unless defined \$attributes->{'some_attribute'};
505  
506 # Verify any attributes you need to here. Output an error and return undef
507 # if something is wrong.
508  
509 # unless (\$attributes->{somevalue} > 0)
510 # {
511 # error "The \\"somevalue\\" attribute for handler \\"HANDLERNAME\\" " .
512 # "should be greater than 0.\\n";
513 # return undef;
514 # }
515  
516 $info{defaults}
517  
518 return \$attributes;
519 }
520  
521 # ------------------------------------------------------------------------------
522  
523  
524 # This function is used to get the raw data from the URL.
525 sub Get
526 {
527 my \$self = shift;
528 my \$attributes = shift;
529  
530 $info{patterncode}
531  
532 my \$url = \$self->ComputeURL(\$attributes);
533  
534 $info{getcode}
535 --------> IF YOU NEED TO DO ADDITIONAL PROCESSING, LIKE A <--------
536 --------> \@\$data = grep {/\d{5}.gif/} \@\$data; <--------
537 --------> TO FILTER OUT IMAGES THAT DON'T HAVE 5 DIGITS, OR IF <--------
538 --------> YOU NEED TO SPLIT THE DATA UP FURTHER, DO IT HERE. <--------
539 return \$data;
540 }
541  
542 # ------------------------------------------------------------------------------
543  
544 --------> MAKEHANDLER TRIED TO MAKE A GOOD GUESS HERE. YOU MIGHT <--------
545 --------> NEED TO CHANGE THIS. <--------
546 $info{defaulthandlers}
547  
548 # ------------------------------------------------------------------------------
549  
550 $info{date}
551  
552 1;
553  
554 EOF
555  
556 open FILE,">MakeHandler.inp";
557 print FILE $code;
558 close FILE;
559  
560 system ("$editor MakeHandler.inp");
561  
562 open FILE,"<MakeHandler.inp";
563 my $finishedcode = join '',<FILE>;
564 close FILE;
565  
566 $finishedcode =~ s/-------->[^<]+<--------//gs;
567 $finishedcode =~ s/\n\n\n+/\n\n/gs;
568  
569 $finishedcode =~ s/^\n*//s;
570 $finishedcode =~ s/\n*$/\n/s;
571  
572 open FILE, ">$info{name}.pm";
573 print FILE $finishedcode;
574 close FILE;
575  
576 print <<EOF;
577 A basic handler called $info{name}.pm has been created for you.
578  
579 To try it out, put it in your handlers directory (typically
580 NewsClipper/Handler/) and put
581 <!--newsclipper
582 <input name=$info{name}>
583 -->
584 in your input file.
585  
586 Have fun!
587 EOF
588  
589 #-------------------------------------------------------------------------------
590  
591 sub Prompt
592 {
593 open FILE,">MakeHandler.inp";
594 foreach my $data (@prompt)
595 {
596 print FILE $data,"vvvvv\n\n","^^^^^\n","-"x78,"\n";
597 }
598 close FILE;
599  
600 RETRY2:
601  
602 system ("$editor MakeHandler.inp");
603  
604 open FILE,"<MakeHandler.inp";
605 my $returnString = join '',<FILE>;
606 close FILE;
607  
608 my @returnvals = ();
609  
610 foreach my $data (@prompt)
611 {
612 my $response = undef;
613 my $pattern = $data;
614  
615 ($response) = $returnString =~ /vvvvv\s*(.*?)\s*\^\^\^\^\^/s;
616  
617 unless (defined $response)
618 {
619 print "Sorry, I couldn't figure out what you answered for the ",
620 "question:\n\n$data\nTry again. <press enter to confirm>\n";
621 goto RETRY2;
622 }
623  
624 $returnString =~ s/vvvvv/vvvv/;
625  
626 push @returnvals, $response;
627 }
628  
629 @prompt = ();
630  
631 return @returnvals;
632 }
633  
634 #-------------------------------------------------------------------------------
635  
636 # Needed by compiler
637  
638 #perl2exe_include File/Spec/Win32
639  
640 #-------------------------------------------------------------------------------
641  
642 =head1 NAME
643  
644 MakeHandler.pl - A generator for handlers suitable for use by News Clipper.
645  
646 =head1 DESCRIPTION
647  
648 I<MakeHandler.pl> is a handler generator. It asks the user a few questions,
649 and then creates a handler.pm file, which can then be edited further. It
650 jump-starts the handler writing process.
651  
652 Handlers are the extensible mechanism by which I<News Clipper> can be
653 customized to acquire and display information from new data sources. News
654 Clipper provides an API of useful functions that can be used by the handler
655 writer.
656  
657 For more information and hints about writing handlers, see
658 http://www.newsclipper.com/makehan.htm. Also
659 see the API description in the documentation for NewsClipper.pl.
660  
661 =head1 OPTIONS AND ARGUMENTS
662  
663 None.
664  
665 =head1 PREREQUISITES
666  
667 No additional Perl modules are needed.
668  
669 =head1 AUTHOR
670  
671 Spinnaker Software, Inc.
672 David Coppit, <david@coppit.org>, http://coppit.org/
673  
674 =begin CPAN
675  
676 =pod COREQUISITES
677  
678 none
679  
680 =pod OSNAMES
681  
682 any
683  
684 =pod SCRIPT CATEGORIES
685  
686 HTML/Preprocessors
687  
688 =end CPAN
689  
690 =cut
691