xmltv/grab Get_nice.pm,1.24,1.25

Geoff <[email protected]>
Newsgroups gmane.comp.tv.xmltv.cvs
Message-ID <[email protected]>
Update of /cvsroot/xmltv/xmltv/grab
In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv30453

Modified Files:
	Get_nice.pm 
Log Message:
add new methods get_nice_json(), get_nice_xml()
add utf8 decode option to get_nice_tree()

Index: Get_nice.pm
===================================================================
RCS file: /cvsroot/xmltv/xmltv/grab/Get_nice.pm,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** Get_nice.pm	19 Oct 2013 09:14:13 -0000	1.24
--- Get_nice.pm	16 Jan 2014 11:38:54 -0000	1.25
***************
*** 22,25 ****
--- 22,27 ----
  # XMLTV::Memoize or whatever.  If you want an HTML::Tree object use
  # get_nice_tree().
+ # Alternatively, get_nice_json() will get you a JSON object,
+ # or get_nice_xml() will get a XML::Parser 'Tree' object
  #
  
***************
*** 27,32 ****
  
  package XMLTV::Get_nice;
  use base 'Exporter';
! our @EXPORT = qw(get_nice get_nice_tree error_msg);
  use LWP::UserAgent;
  use XMLTV;
--- 29,40 ----
  
  package XMLTV::Get_nice;
+ 
+ # use version number for feature detection:
+ # 0.005065 : new methods get_nice_json(), get_nice_xml()
+ # 0.005065 : add utf8 decode option to get_nice_tree()
+ our $VERSION = 0.005065;
+ 
  use base 'Exporter';
! our @EXPORT = qw(get_nice get_nice_tree get_nice_xml get_nice_json error_msg);
  use LWP::UserAgent;
  use XMLTV;
***************
*** 54,72 ****
  }
  
! # Fetch page and return as HTML::Tree object.  Optional argument is a
! # function to put the page data through (eg, to clean up bad
  # characters) before parsing.
  #
! sub get_nice_tree( $;$ ) {
!     my ($uri, $filter) = @_;
      require HTML::TreeBuilder;
      my $content = get_nice $uri;
      $content = $filter->($content) if $filter;
!     my $t = new HTML::TreeBuilder;
!     $t->parse($content) or die "cannot parse content of $uri\n";
      $t->eof;
      return $t;
  }
  
  my $last_get_time;
  sub get_nice_aux( $ ) {
--- 62,121 ----
  }
  
! # Fetch page and return as HTML::Tree object.   
! # Optional arguments:
! # i) a function to put the page data through (eg, to clean up bad
  # characters) before parsing.
+ # ii) convert incoming UTF-8 to UNICODE when set to 1
  #
! sub get_nice_tree( $;$$ ) {
!     my ($uri, $filter, $utf8) = @_;
      require HTML::TreeBuilder;
      my $content = get_nice $uri;
      $content = $filter->($content) if $filter;
!     if ($utf8) {
!       # (note: HTML::Parser->utf8_mode is only available Perl 5.8) 
!       require Encode;
!       $content = decode('UTF-8', $content);   # strict utf-8
!     }
!     my $t = HTML::TreeBuilder->new()->parse($content) or die "cannot parse content of $uri\n";
      $t->eof;
      return $t;
  }
  
+ # Fetch page and return as XML::Parser 'Tree' object.  
+ # Optional arguments:
+ # i) a function to put the page data through (eg, to clean up bad
+ # characters) before parsing.
+ # ii) convert incoming UTF-8 to UNICODE when set to 1
+ #
+ sub get_nice_xml( $;$$ ) {
+     my ($uri, $filter, $utf8) = @_;
+     require XML::Parser;
+     my $content = get_nice $uri;
+     $content = $filter->($content) if $filter;
+     if ($utf8) {
+       require Encode;
+       $content = decode('UTF-8', $content);   # strict utf-8
+     }
+     my $t = XML::Parser->new(Style => 'Tree')->parse($content) or die "cannot parse content of $uri\n";
+     return $t;
+ }
+ 
+ # Fetch page and return as JSON::PP object.  
+ # Optional arguments:
+ # i) a function to put the page data through (eg, to clean up bad
+ # characters) before parsing.
+ # ii) convert incoming UTF-8 to UNICODE when set to 1
+ #
+ sub get_nice_json( $;$$ ) {
+     my ($uri, $filter, $utf8) = @_;
+     require JSON::PP;
+     my $content = get_nice $uri;
+     $content = $filter->($content) if $filter;
+     $utf8 = 0 if !defined $utf8; #(not necessary but safer)
+     my $t = JSON::PP->new()->utf8($utf8)->decode($content) or die "cannot parse content of $uri\n";
+     return $t;
+ }
+ 
  my $last_get_time;
  sub get_nice_aux( $ ) {


------------------------------------------------------------------------------
CenturyLink Cloud: The Leader in Enterprise Cloud Services.
Learn Why More Businesses Are Choosing CenturyLink Cloud For
Critical Workloads, Development Environments & Everything In Between.
Get a Quote or Start a Free Trial Today. 
http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.