WordPress.org

Plugin Directory

Changeset 525499


Ignore:
Timestamp:
03/31/12 00:01:44 (2 years ago)
Author:
dllh
Message:

Adding work in progress for a version of the importer that uses the v2 Posterous API. Everything seems to be working pretty well except for imports of audio and video files, which is sort of hobbled because the post body data we have access to doesn't contain references to the URLs. There are other members in the data structures that we can use to fetch the media; just need to actually implement.

Location:
posterous-importer/branches/v2api
Files:
1 edited
1 copied

Legend:

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

    r524279 r525499  
    4343    var $url_remap = array(); 
    4444    var $have_posts = true; 
     45    var $api_sleep_duration = 1100000; 
     46    var $last_api_call_timestamp = 0; 
     47    var $api_token = 'CglsrqheeigInlfBkmyHypByCspDCGzB'; 
    4548 
    4649 
     
    5457        add_action( 'process_attachment', array( &$this, 'process_attachment' ), 10, 3 ); 
    5558        add_action( 'posterous_handle_bad_response', array( &$this, 'handle_bad_response' ), 10, 2 ); 
     59        add_action( 'posterous_api_sleep', array( &$this, 'api_sleep' ) ); 
    5660 
    5761        if ( isset( $_GET['import'] ) && 'posterous' == $_GET['import'] ) { 
     
    96100        $this->sites = $this->get_sites(); 
    97101        // Sleep after fetching sites because Posterous doesn't like if you do more than one request in a second. 
    98         usleep( 1100000 ); 
     102        do_action( 'posterous_api_sleep' ); 
    99103        $this->permalinks = $this->get_imported_posts( 'posterous', $this->bid ); 
    100104        $this->comments = $this->get_imported_comments(); 
     
    106110 
    107111    function get_sites() { 
    108         $url = 'http://posterous.com/api/getsites'; 
     112        $url = 'http://posterous.com/api/2/sites/'; 
     113        $url = add_query_arg( 'api_token', $this->api_token, $url ); 
     114        $this->last_api_call_timestamp = time(); 
    109115        $data = $this->get_page( $url, $this->username, $this->password ); 
    110116        if ( is_wp_error( $data ) ) { 
     
    119125        } 
    120126 
    121         $xml = simplexml_load_string( $data['body'] ); 
    122         if ( !$xml ) { 
    123             printf( __METHOD__ . ": <em>%s</em><br />\n", __( 'Error loading XML' ) ); 
     127        $sites = json_decode( $data['body'] ); 
     128        if ( !is_array( $sites ) ) { 
     129            printf( __METHOD__ . ": <em>%s</em><br />\n", __( 'Error loading JSON data' ) ); 
    124130            exit(); 
    125131        } 
    126132 
    127133        $subdomain = substr( $this->hostname, 0, strpos( $this->hostname, '.posterous.com' ) ); 
    128         foreach ( $xml->site as $site ) { 
     134        foreach ( $sites as $site ) { 
    129135            // Compare the subdomain to the hostname from Posterous. 
    130136            if ( $subdomain == (string) $site->hostname ) { 
     
    143149        $page = 1; 
    144150        do { 
    145             $url = 'http://posterous.com/api/readposts'; 
    146             $url = add_query_arg( 'site_id', $this->site_id, $url ); 
     151            $url = 'http://posterous.com/api/2/sites/' . $this->site_id . '/posts/'; 
     152            $url = add_query_arg( 'api_token', $this->api_token, $url ); 
    147153            $url = add_query_arg( 'num_posts', 10, $url ); 
    148154            $url = add_query_arg( 'page', $page, $url ); 
     
    150156            $page++; 
    151157            // Sleep after fetching sites because Posterous doesn't like if you do more than one request in a second. 
    152             usleep( 1100000 ); 
     158            do_action( 'posterous_api_sleep' ); 
    153159        } while ( true === $this->have_posts ); 
    154160    } 
     
    171177     */ 
    172178    function process_posts( $url ) { 
     179        $this->last_api_call_timestamp = time(); 
    173180        $data = $this->get_page( $url, $this->username, $this->password ); 
    174181        if ( is_wp_error( $data ) ) { 
     
    183190 
    184191        $body = $data['body']; 
    185         $xml = @simplexml_load_string( $body ); 
    186         if ( !$xml ) { 
    187             printf( __METHOD__ . ": <em>%s</em><br />\n", __( 'Error loading XML' ) ); 
     192        $posts = json_decode( $body ); 
     193        if ( !is_array( $posts ) ) { 
     194            printf( __METHOD__ . ": <em>%s</em><br />\n", __( 'Error loading JSON data.' ) ); 
    188195            $this->have_posts = false;  
    189196            return; 
     
    191198 
    192199        // If posts is empty, we're done here, move along 
    193         if ( empty( $xml->post ) ) { 
     200        if ( 0 == count( $posts ) ) { 
    194201            $this->have_posts = false; 
    195202            return; 
    196203        } 
    197204 
    198         foreach ( $xml->post as $entry ) { 
     205        foreach ( $posts as $entry ) { 
    199206            $entry->title = (string) $entry->title; 
    200             $permalink = (string) $entry->link; 
    201             $commentscount = (int) $entry->commentsCount;            
    202  
     207            $permalink = (string) $entry->full_url; 
     208            $commentscount = (int) $entry->number_of_comments; 
    203209            if ( isset( $this->permalinks[$permalink] ) ) { 
    204210                printf( "<em>%s</em><br />\n", __( 'Skipping' ) . ' ' . $entry->title ); 
    205211                // process_comments here to check for any new comments or ones that we missed 
    206212                printf( "\t<em>%s</em><br />\n", __( 'Found Comments:' ) . ' ' . $commentscount ); 
    207                 if ( $commentscount > 0 ) 
    208                     $this->process_comments( $this->permalinks[$permalink], $entry->comment ); 
     213                if ( $commentscount >= 0 ) // TODO: Change to just > 
     214                    $this->process_comments( $this->permalinks[$permalink], $entry ); 
    209215                continue; 
    210216            }                    
     
    212218            $post = array(); 
    213219            $post['post_title'] = (string) $entry->title; 
    214             $post['post_date'] = gmdate( "Y-m-d H:i:s", strtotime( (string) $entry->date ) ); 
    215             $post['post_content'] = (string) $entry->body; 
     220            $post['post_date'] = gmdate( "Y-m-d H:i:s", strtotime( (string) $entry->display_date ) ); 
     221            $post['post_content'] = (string) $entry->body_full; 
    216222            $post['post_status'] = 'publish'; 
    217             if ( 'true' == (string) $entry->private ) 
     223            if ( 'true' == (string) $entry->is_private ) 
    218224                $post['post_status'] = 'private'; 
    219225 
     
    233239                     
    234240                foreach ( $entry->media as $m ) { 
    235                     if ( in_array( (string) $m->type, array( 'audio', 'video' ) ) ) {                        
     241                    if ( in_array( (string) $m->type, array( 'audio_files', 'video' ) ) ) {                      
    236242                        $file = new stdClass(); 
    237243                        $file->type = (string) $m->type; 
    238244                        $file->url = (string) $m->url; 
    239                         $file->filesize = (string) $m->filesize; 
     245                        $file->filesize = (string) $m->size; 
    240246                        $file->thumb = (string) $m->thumb; 
    241247                        $media[] = $file; 
     
    255261 
    256262            printf( "\t<em>%s</em><br />\n", __( 'Found Comments:' ) . ' ' . $commentscount ); 
    257             if ( $commentscount > 0 ) 
    258                 $this->process_comments( $post_id, $entry->comment ); 
    259         } 
    260     } 
    261  
    262     /** 
    263      * Extract XML from URL, find and import comments 
     263            if ( $commentscount >= 0 ) // TODO: Change to just > 
     264                $this->process_comments( $post_id, $entry ); 
     265        } 
     266    } 
     267 
     268    /** 
     269     * Extract JSON from URL, find and import comments 
    264270     * 
    265271     * @param string $url 
    266272     * @return array 
    267273     */ 
    268     function process_comments( $post_id, $comments ) { 
    269         foreach ( $comments as $comment ) { 
    270             $author = (string) $comment->author; 
    271             $date = gmdate( "Y-m-d H:i:s", strtotime( (string) $comment->date ) ); 
     274    function process_comments( $post_id, $entry ) { 
     275/* 
     276        do_action( 'posterous_api_sleep' ); 
     277        $url = 'http://posterous.com/api/2/sites/' . $this->site_id . '/posts/' . $posterous_post_id . '/comments/'; 
     278        $url = add_query_arg( 'api_token', $this->api_token, $url ); 
     279 
     280        $this->last_api_call_timestamp = time(); 
     281        $data = $this->get_page( $url, $this->username, $this->password ); 
     282        if ( is_wp_error( $data ) ) { 
     283            echo "Error:\n" . $data->get_error_message() . "\n"; 
     284            return; 
     285        } 
     286 
     287        $code = (int) $data['response']['code']; 
     288        if ( 200 !== $code ) { 
     289            printf( "<em>%s</em><br />\n", __( 'Got HTTP code' ) . ' ' . $code . ' ' . __( 'from' ) . ' ' . $url ); 
     290            exit(); 
     291        } 
     292 
     293        $comments = json_decode( $data['body'] ); 
     294*/ 
     295        foreach ( $entry->comments as $comment ) { 
     296            $author = (string) $comment->user->display_name; 
     297            $date = gmdate( "Y-m-d H:i:s", strtotime( (string) $comment->created_at ) ); 
    272298            $import_comment_hash = md5( $author . $date ); 
    273299            // Skip existing comments 
     
    332358    function get_tags( $entry ) { 
    333359        $tags = array(); 
    334         if ( isset( $entry->tag ) ) { 
    335             foreach ( $entry->tag as $tag ) { 
     360        if ( isset( $entry->tags ) ) { 
     361            foreach ( $entry->tags as $tag ) { 
    336362                $tags[] = (string) $tag; 
    337363            } 
     
    384410     */ 
    385411    function process_attachment( $post, $fullsizes, $thumbs ) {          
    386  
    387412        // Add media files from postmeta to the $fullsizes array for fetching. 
    388413        $media_types = array(); 
     
    591616         
    592617        unset( $post_content, $matches ); 
    593  
     618print '<pre>' . print_r( $attachments, 1 ) . '</pre>'; 
    594619        return $attachments; 
    595620    } 
     
    855880        $this->password = $password; 
    856881        $this->auth = true; 
    857         $url = 'http://posterous.com/api/getsites'; 
     882        $url = 'http://posterous.com/api/2/sites?api_token=' . $this->api_token; 
     883        $this->last_api_call_timestamp = time(); 
    858884        $data = $this->get_page( $url, $this->username, $this->password ); 
    859885        if ( is_wp_error( $data ) ) { 
     
    861887            return; 
    862888        } 
    863  
    864889        $code = (int) $data['response']['code']; 
    865890        unset( $data ); 
     
    9951020        $this->print_footer(); 
    9961021    } 
     1022 
     1023    /** 
     1024     * Hook to run when we want to sleep to keep from getting 403 responses from the Posterous API. 
     1025     */ 
     1026    function api_sleep() { 
     1027        if ( time() - $this->last_api_call_timestamp < $this->api_sleep_duration )  
     1028            usleep( $this->api_sleep_duration ); 
     1029    } 
     1030 
    9971031} 
    9981032} 
Note: See TracChangeset for help on using the changeset viewer.