WordPress.org

Plugin Directory

Changeset 626309


Ignore:
Timestamp:
11/16/12 22:36:15 (17 months ago)
Author:
dllh
Message:

Tumblr Importer: Get the basic importing working. There were some wpcom-specific bits scraped into the plugin that were causing fatals because functions didn't exist. Once that was fixed, there were fatals because fetch_posts() hadn't been updated to accept data structures from the new API. I've pasted in but hardly tested stuff that's working on wpcom, and we can now do a basic import.

Lots more testing and probably other conversion needs still to be done, though.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tumblr-importer/branches/newapi/tumblr-importer.php

    r626304 r626309  
    741741     * @returns false on error, array of posts on success 
    742742     */ 
    743     function fetch_posts($url, $start=0, $count = 50, $email = null, $password = null, $state = null) { 
     743    function fetch_posts($url, $start=0, $count = 50, $email = null, $password = null, $state = null ) { 
    744744        $url = parse_url( $url, PHP_URL_HOST ); 
    745         $url = trailingslashit( "http://api.tumblr.com/v2/blog/$url/posts" ); 
     745        $post_type = apply_filters( 'tumblr_post_type', '' ); 
     746        $url = trailingslashit( "http://api.tumblr.com/v2/blog/$url/posts/$post_type" ); 
    746747 
    747748        $params = array( 
    748749            'offset'=>$start, 
    749750            'limit'=>$count, 
    750             'api_key' => $this->tumblr_keyring->get_consumer_key()->key, 
     751            'api_key' => $this->consumerkey, 
    751752        ); 
    752753 
    753754        if ( !empty($state) ) $params['state'] = $state; 
    754755        $url = add_query_arg( $params, $url ); 
    755         print $url . "\n"; 
    756756 
    757757        $response = $this->oauth_get_request($url); 
    758         //print_r( $response ); 
    759758 
    760759        switch ( $response->meta->status ) { 
    761             case 403: // Bad Username / Password 
    762                 do_action( 'tumblr_importer_handle_error', 'get_blogs_403' ); 
    763                 return new WP_Error('tumblr_error', __('Tumblr says that the the app is not authorized. Please check the settings and try to connect again.', 'tumblr-importer' ) ); 
    764760            case 200: // OK 
    765761                break; 
     
    772768        $posts = array(); 
    773769        $tposts = $response->response->posts; 
    774         foreach($tposts->post as $tpost) { 
     770        foreach( $tposts as $tpost ) { 
    775771            $post = array(); 
    776             $post['tumblr_id'] = (string) $tpost['id']; 
    777             $post['tumblr_url'] = (string) $tpost['url-with-slug']; 
    778             $post['post_date'] = date( 'Y-m-d H:i:s', strtotime ( (string) $tpost['date'] ) ); 
    779             $post['post_date_gmt'] = date( 'Y-m-d H:i:s', strtotime ( (string) $tpost['date-gmt'] ) ); 
    780             $post['post_name'] = (string) $tpost['slug']; 
    781             if ( isset($tpost['private']) ) $post['private'] = (string) $tpost['private']; 
    782             if ( isset($tpost->{'tag'}) ) { 
     772            $post['tumblr_id'] = (string) $tpost->id; 
     773            $post['tumblr_url'] = (string) $tpost->post_url; 
     774            $post['post_date'] = date( 'Y-m-d H:i:s', strtotime ( (string) $tpost->date ) ); 
     775            $post['post_date_gmt'] = date( 'Y-m-d H:i:s', strtotime ( (string) $tpost->date ) ); 
     776            $post['post_name'] = (string) $tpost->slug; 
     777            if ( 'private' == $tpost->state )  
     778                $post['private'] = (string) $tpost->state; 
     779            if ( isset( $tpost->tags ) ) { 
    783780                $post['tags_input'] = array(); 
    784                 foreach ( $tpost->{'tag'} as $tag ) 
     781                foreach ( $tpost->tags as $tag ) 
    785782                    $post['tags_input'][] = rtrim( (string) $tag, ','); // Strip trailing Commas off it too. 
    786783            } 
    787784 
    788             // set the various post info for each special format tumblr offers 
    789             // TODO reorg this as needed 
    790             switch ((string) $tpost['type']) { 
     785            switch ( (string) $tpost->type ) { 
    791786                case 'photo': 
    792787                    $post['format'] = 'image'; 
    793                     $post['media']['src'] = (string) $tpost->{'photo-url'}[0]; 
    794                     $post['media']['link'] =(string) $tpost->{'photo-link-url'}; 
    795                     $post['media']['width'] = (string) $tpost['width']; 
    796                     $post['media']['height'] = (string) $tpost['height']; 
    797                     $post['post_content'] = (string) $tpost->{'photo-caption'}; 
    798                     if ( !empty( $tpost->{'photoset'} ) ) { 
     788                    $post['media']['src'] = (string) $tpost->photos[0]->original_size->url; 
     789                    $post['media']['link'] = '';//TODO: Find out what to use here.(string) $tpost->{'photo-link-url'}; 
     790                    $post['media']['width'] = (string) $tpost->photos[0]->original_size->width; 
     791                    $post['media']['height'] = (string) $tpost->photos[0]->original_size->height; 
     792                    $post['post_content'] = (string) $tpost->photos[0]->caption; 
     793                    if ( ! empty( $tpost->photos ) ) { 
    799794                        $post['format'] = 'gallery'; 
    800                         foreach ( $tpost->{'photoset'}->{'photo'} as $photo ) { 
     795                        foreach ( $tpost->photos as $photo ) { 
    801796                            $post['gallery'][] = array ( 
    802                                 'src'=>$photo->{'photo-url'}[0], 
    803                                 'width'=>$photo['width'], 
    804                                 'height'=>$photo['height'], 
    805                                 'caption'=>$photo['caption'], 
     797                                'src'     => $photo->original_size->url, 
     798                                'width'   => $photo->original_size->width, 
     799                                'height'  => $photo->original_size->height, 
     800                                'caption' => $photo->caption, 
    806801                            ); 
    807802                        } 
     
    810805                case 'quote': 
    811806                    $post['format'] = 'quote'; 
    812                     $post['post_content'] = (string) $tpost->{'quote-text'}; 
    813                     $post['post_content'] .= "\n\n" . (string) $tpost->{'quote-source'}; 
     807                    $post['post_content'] = (string) $tpost->text; 
     808                    $post['post_content'] .= "\n\n" . (string) $tpost->source; 
    814809                    break; 
    815810                case 'link': 
    816811                    $post['format'] = 'link'; 
    817                     $linkurl = (string) $tpost->{'link-url'}; 
    818                     $linktext = (string) $tpost->{'link-text'}; 
    819                     $post['post_content'] = "<a href='{$linkurl}'>{$linktext}</a>"; 
    820                     $post['post_title'] = (string) $tpost->{'link-description'}; 
     812                    $linkurl = (string) $tpost->url; 
     813                    $linktext = (string) $tpost->title; 
     814                    $post['post_content'] = ''; 
     815                    if ( ! empty( $tpost->description ) ) 
     816                        $post['content'] .= $tpost->description . "<br />"; 
     817                    $post['post_content'] .= "<a href='$linkurl'>$linktext</a>"; 
     818                    $post['post_title'] = (string) $tpost->title; 
    821819                    break; 
    822820                case 'conversation': 
    823821                    $post['format'] = 'chat'; 
    824                     $post['post_title'] = (string) $tpost->{'conversation-title'}; 
    825                     $post['post_content'] = (string) $tpost->{'conversation-text'}; 
     822                    $post['post_title'] = (string) $tpost->title; 
     823                    $post['post_content'] = (string) $tpost->body; 
    826824                    break; 
    827825                case 'audio': 
    828826                    $post['format'] = 'audio'; 
    829                     $post['media']['filename'] = basename( (string) $tpost->{'authorized-download-url'} ) . '.mp3'; 
    830                     $post['media']['audio'] = (string) $tpost->{'authorized-download-url'} .'?plead=please-dont-download-this-or-our-lawyers-wont-let-us-host-audio'; 
    831                     $post['post_content'] = (string) $tpost->{'audio-player'} . "\n" . (string) $tpost->{'audio-caption'}; 
    832                     if ( !empty($tpost->{'id3-artist'}) ) 
    833                         $post['post_title'] = $tpost->{'id3-artist'} . ' - ' . $tpost->{'id3-title'}; 
     827                    $post['media']['filename'] = basename( (string) $tpost->audio_url ); 
     828                    $post['media']['audio'] = (string) $tpost->audio_url .'?plead=please-dont-download-this-or-our-lawyers-wont-let-us-host-audio'; 
     829                    $post['post_content'] = (string) $tpost->player . "\n" . (string) $tpost->caption; 
    834830                    break; 
    835831                case 'video': 
    836832                    $post['format'] = 'video'; 
    837833                    $post['post_content'] = ''; 
    838                     if ( is_serialized( (string) $tpost->{'video-source'} ) ) { 
    839                         if ( preg_match('|\'(http://.*video_file.*)\'|U', $tpost->{'video-player'}[0], $matches) ) { 
    840                             $post['media']['video'] = $matches[1]; 
    841                             $val = unserialize( (string) $tpost->{'video-source'} ); 
    842                             $vidmeta = $val['o1']; 
    843                             $post['media']['filename'] = basename($post['media']['video']) . '.' . $vidmeta['extension']; 
    844                             $post['media']['width'] = $vidmeta['width']; 
    845                             $post['media']['height'] = $vidmeta['height']; 
    846                         } 
    847                     } else if ( false !== strpos( (string) $tpost->{'video-source'}, 'embed' ) ) { 
    848                         if ( preg_match_all('/<embed (.+?)>/', (string) $tpost->{'video-source'}, $matches) ) { 
    849                             foreach ($matches[1] as $match) { 
    850                                 foreach ( wp_kses_hair($match, array('http')) as $attr) 
    851                                     $embed[$attr['name']] = $attr['value']; 
    852                             } 
    853                              
    854                             // special case for weird youtube vids 
    855                             $embed['src'] = preg_replace('|http://www.youtube.com/v/([a-zA-Z0-9_]+).*|i', 'http://www.youtube.com/watch?v=$1', $embed['src']); 
    856                              
    857                             // TODO find other special cases, since tumblr is full of them 
    858                              
    859                             $post['post_content'] = $embed['src']; 
    860                         } 
    861                         // Sometimes, video-source contains iframe markup. 
    862                         if ( preg_match( '/<iframe/', $tpost->{'video-source'} ) ) { 
    863                             $embed['src'] = preg_replace( '|<iframe.*src="http://www.youtube.com/embed/([a-zA-Z0-9_\-]+)\??.*".*</iframe>|', 'http://www.youtube.com/watch/?v=$1', $tpost->{'video-source'} ); 
    864                             $post['post_content'] = $embed['src']; 
    865                         } 
    866                      
    867                     } else { 
    868                         // @todo: See if the video-source is going to be oEmbed'able before adding the flash player 
    869                         // 1 Seems to be "original" size, with 0 being set otherwise. 
    870                         $post['post_content'] .= isset($tpost->{'video-player'}[1]) ? $tpost->{'video-player'}[1] : (string) $tpost->{'video-player'}[0]; 
     834                    if ( isset( $tpost->video_url ) ) { 
     835                        $post['media']['video'] = $tpost->video_url; 
     836                    } else if ( isset( $tpost->player ) ) { 
     837                        $player = $tpost->player[ count( $tpost->player ) - 1 ]->embed_code; 
     838                        $player = preg_replace( '|http://www.youtube.com/v/([a-zA-Z0-9_]+).*|i', 'http://www.youtube.com/watch?v=$1', $player ); 
     839                        $post['post_content']  .= $player; 
    871840                    } 
    872                     $post['post_content'] .= "\n" . (string) $tpost->{'video-caption'}; 
     841                    $post['post_content'] .= "\n" . (string) $tpost->caption; 
    873842                    break; 
    874843                case 'answer': 
    875                     $post['post_title'] = (string) $tpost->{'question'}; 
    876                     $post['post_content'] = (string) $tpost->{'answer'}; 
     844                    // TODO: Include asking_name and asking_url values? 
     845                    $post['post_title'] = (string) $tpost->question; 
     846                    $post['post_content'] = (string) $tpost->answer; 
    877847                    break; 
    878848                case 'regular': 
     849                case 'text': 
    879850                default: 
    880                     $post['post_title'] = (string) $tpost->{'regular-title'}; 
    881                     $post['post_content'] = (string) $tpost->{'regular-body'}; 
     851                    $post['post_title'] = (string) $tpost->title; 
     852                    $post['post_content'] = (string) $tpost->body; 
    882853                    break; 
    883854            } 
Note: See TracChangeset for help on using the changeset viewer.