xmltv/grab Get_nice.pm,1.32,1.33

Geoff <[email protected]> Mon, 10 Aug 2015 10:39:09 +0000
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-serv8080

Modified Files:
	Get_nice.pm 
Log Message:
Added post_nice_json() for emulating AJAX post requests

Index: Get_nice.pm
===================================================================
RCS file: /cvsroot/xmltv/xmltv/grab/Get_nice.pm,v
retrieving revision 1.32
retrieving revision 1.33
diff -C2 -d -r1.32 -r1.33
*** Get_nice.pm	12 Jul 2015 00:46:37 -0000	1.32
--- Get_nice.pm	10 Aug 2015 10:39:07 -0000	1.33
***************
*** 35,42 ****
  # 0.005065 : expose the LWP response object ($Response)
  # 0.005066 : support unknown tags in HTML::TreeBuilder ($IncludeUnknownTags)
! our $VERSION = 0.005066;
  
  use base 'Exporter';
! our @EXPORT = qw(get_nice get_nice_tree get_nice_xml get_nice_json error_msg);
  use Encode qw(decode);
  use LWP::UserAgent;
--- 35,43 ----
  # 0.005065 : expose the LWP response object ($Response)
  # 0.005066 : support unknown tags in HTML::TreeBuilder ($IncludeUnknownTags)
! # 0.005067 : new method post_nice_json()
! our $VERSION = 0.005067;
  
  use base 'Exporter';
! our @EXPORT = qw(get_nice get_nice_tree get_nice_xml get_nice_json post_nice_json error_msg);
  use Encode qw(decode);
  use LWP::UserAgent;
***************
*** 154,159 ****
      $last_get_time = time();
  
! 		# expose the response object for those grabbers which need to process the headers, status code, etc.
! 		$Response = $r;
  
      if ($r->is_error) {
--- 155,160 ----
      $last_get_time = time();
  
!     # expose the response object for those grabbers which need to process the headers, status code, etc.
!     $Response = $r;
  
      if ($r->is_error) {
***************
*** 167,174 ****
          return undef;
      } else {
!         # De-compress, if necessary, but skip character set conversions
!         return $r->decoded_content(charset => 'none');
      }
  
  }
  
--- 168,210 ----
          return undef;
      } else {
!         return $r->content;
!     }
! 
! }
! 
! # Fetch page via a JSON object in the Content and return as a JSON object.  
! # Arguments:
! #    URI to post to
! #    JSON object with the AJAX data to be posted e.g. "{ 'programId':'123456', 'channel':'BBC'}"
! #
! sub post_nice_json( $$ ) {
!     my $url = shift;
!     my $json = shift;
! 
!     require JSON::PP;
! 
!     if (defined $last_get_time) {
!         # A page has already been retrieved recently.  See if we need
!         # to sleep for a while before getting the next page 
!         #
!         my $next_get_time = $last_get_time + (rand $Delay) + $MinDelay;
!         my $sleep_time = $next_get_time - time();
!         sleep $sleep_time if $sleep_time > 0;
      }
  
+     my $r = $ua->post($url, 'Content_Type' => 'application/json; charset=utf-8', 'Content' => $json);
+ 
+     $last_get_time = time();
+ 
+     # expose the response object for those grabbers which need to process the headers, status code, etc.
+     $Response = $r;
+ 
+     if ($r->is_error) {
+         die "could not fetch $url, error: " . $r->status_line . ", aborting\n" if $FailOnError;
+         $errors{$url} = $r->status_line;
+         return undef;
+     } else {
+         return JSON::PP->new()->utf8(1)->decode($r->content) or die "cannot parse content of $url\n";
+     }
  }
  


------------------------------------------------------------------------------