WordPress.org

Plugin Directory

Changeset 526178


Ignore:
Timestamp:
04/02/12 12:42:44 (2 years ago)
Author:
dllh
Message:
  • Tidy up handling of API sleeping to prevent retry-after headers from resulting in 403 errors.
  • Fix a nasty little bug with + and = signs in S3 media URLs (so all Posterous media) that causes the files to return errors rather than the desired media. This manifests, as far as I can tell, only when safe_mode or open_basedir are on and we bypass curl's CURLOPT_FOLLOWLOCATION setting, so there may be an underlying core bug here, or it may just be that S3 is lame and using entities it shouldn't in signatures so that a hack is necessary.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • posterous-importer/branches/v2api/posterous.php

    r525736 r526178  
    1111 
    1212// TODO: Actually honor the retry-after headers instead of using an arbitrary wait time. This probably requires doing some fun cron stuff. 
    13 // TODO: Figure out Posterous's weird shortcode/hashing thing for inserting images into pages and body_html (e.g. [[posterous-content:jhsmtHnEjaoooaxfmmbm]]) 
    14 // Do something with private posts. 
     13// TODO: Figure out Posterous's weird shortcode/hashing thing for inserting images into pages and body_html (e.g. [[posterous-content:jhsmtHnEjaoooaxfmmbm]]). Looks like it may just be a placeholder and that we'd need to create some kind of gallery using the media metadata. See https://github.com/mojombo/jekyll/pull/509 
     14// TODO: Do something with private posts. (May not be much to do here, since Posterous doesn't seem to have a private post password option -- private posts are just limited so that only you can read them unless you hand out the url.) 
     15// TODO: Make the submit button work when you press return. 
     16// TODO: Make the status area update while doing the import (not sure whether this is a regression or not). 
    1517 
    1618if ( !defined('WP_LOAD_IMPORTERS') ) 
     
    6365    function __construct() { 
    6466        parent::__construct(); 
     67        $this->last_api_call_timestamp = time(); 
    6568        add_action( 'process_attachment', array( &$this, 'process_attachment' ), 10, 3 ); 
    6669        add_action( 'posterous_handle_bad_response', array( &$this, 'handle_bad_response' ), 10, 2 ); 
     
    177180        $url = 'http://posterous.com/api/2/sites/'; 
    178181        $url = add_query_arg( 'api_token', $this->api_token, $url ); 
    179         $this->last_api_call_timestamp = time(); 
     182        do_action( 'posterous_api_sleep' ); 
    180183        $data = $this->get_page( $url, $this->username, $this->password ); 
    181184        if ( is_wp_error( $data ) ) { 
     
    252255     */ 
    253256    function process_posts( $url, $type ) { 
    254         $this->last_api_call_timestamp = time(); 
     257        do_action( 'posterous_api_sleep' ); 
    255258        $data = $this->get_page( $url, $this->username, $this->password ); 
    256259        if ( is_wp_error( $data ) ) { 
     
    669672        } 
    670673 
     674        /** 
     675         * Hosts in safe mode or with open_basedir set use custom stuff for  
     676         * handling redirects when curl is the http transport. Something in that process 
     677         * that I haven't figured out yet mucks up URLs with entities like "=" in them. 
     678         * This is bad for some Amazon S3 URLs that end with an = sign that gets  
     679         * translated to %3D in S3 file signatures and causes the redirection to 
     680         * result in an error response from S3. Just intercept redirects here and 
     681         * go ahead and set the URL to the destination S3 url to bypass the underlying  
     682         * issue, which probably needs further investigation. 
     683         */ 
     684        $head = wp_remote_request( $url, array('method' => 'HEAD') ); 
     685                if ( wp_remote_retrieve_header( $head, 'location' ) ) { 
     686            $original_url = $url; 
     687                        $url = wp_remote_retrieve_header( $head, 'location' ); 
     688            // Fix plus signs too so that they don't get turned into spaces. 
     689            $url = str_replace( '+', '%2B', $url ); 
     690        } 
     691 
    671692        // fetch the remote url and write it to the placeholder file 
    672693        $headers = wp_get_http( $url, $upload['file'] ); 
     
    9871008        $username = strtolower( $username ); 
    9881009 
     1010        do_action( 'posterous_api_sleep' ); 
    9891011        $this->username = $username; 
    9901012        $this->password = $password; 
    9911013        $this->auth = true; 
    9921014        $url = 'http://posterous.com/api/2/sites?api_token=' . $this->api_token; 
    993         $this->last_api_call_timestamp = time(); 
    9941015        $data = $this->get_page( $url, $this->username, $this->password ); 
    9951016        if ( is_wp_error( $data ) ) { 
     
    11431164        $url = add_query_arg( 'api_token', $this->api_token, $url );         
    11441165        do_action( 'posterous_api_sleep' ); 
    1145         $this->last_api_call_timestamp = time(); 
    11461166        $data = $this->get_page( $url, $this->username, $this->password );       
    11471167        if ( is_wp_error( $data ) ) { 
     
    11631183                $links_url = 'http://posterous.com/api/2/sites/' . $this->site_id . '/link_categories/' . $cat->id . '/links'; 
    11641184                $links_url = add_query_arg( 'api_token', $this->api_token, $links_url ); 
    1165                 $this->last_api_call_timestamp = time(); 
    11661185                $data = $this->get_page( $links_url, $this->username, $this->password ); 
    11671186                 
     
    12221241     */ 
    12231242    function api_sleep() { 
    1224         if ( time() - $this->last_api_call_timestamp < $this->api_sleep_duration )  
     1243        if ( time() - $this->last_api_call_timestamp < $this->api_sleep_duration ) { 
    12251244            usleep( $this->api_sleep_duration ); 
     1245        } 
     1246        $this->last_api_call_timestamp = time(); 
    12261247    } 
    12271248 
Note: See TracChangeset for help on using the changeset viewer.