Okay mais c'est un peu bourrin.
<?php
$user_id = 'TheMLDEG';
$url = "https://gdata.youtube.com/feeds/api/users/".$user_id."/playlists?v=2&alt=json";
$data = json_decode(file_get_contents($url), true);
$info = $data"feed"];
$playlist = $info"entry"];
$nplaylist = count($playlist);
// Create Category if there is a new playlist
for ($i=0; $i < count($playlist); $i++) {
$cat_name = $playlist$i]'title']'$t'];
$term = term_exists($cat_name, 'category');
if ($term == 0 && $term == null) {
wp_insert_term($cat_name, 'category');
}
}
$url = "https://gdata.youtube.com/feeds/api/users/". $user_id ."/uploads?v=2&alt=json";
$data = json_decode(file_get_contents($url), true);
$info = $data"feed"];
$video = $info"entry"];
$nVideo = 1; //count($video);
$title = $video[0]'title']'$t'];
$description = $video[0]'media$group']'media$description']'$t'];
$args = array(
'type' => 'post',
'hide_empty' => 0,
'orderby' => 'name',
'taxonomy' => 'category'
);
$categories = get_categories( $args );
$series = trim ( preg_replace('# \#.([0-9]{1,100})#', '', $title) );
$cat_id = 1;
for ($i=0; $i < count($categories); $i++) {
if(trim($series) == trim($categories$i]->name)) {
$cat_id = $categories$i]->term_id;
} elseif(strstr($series, "Week's Video")) { // Check si c'est un Week's Video
$cat = get_term_by('slug', 'weeks-video', 'category');
$cat_id = $cat->term_id;
}
}
// Insert Post
$post = array(
'post_content' => $description, // The full text of the post.
'post_name' => urlencode($title), // The name (slug) for your post
'post_title' => $title, // The title of your post.
'post_status' => 'publish', // Post status
'post_author' => null, // Post Author
'post_category' => array($cat_id)
);
if( wp_exist_post_by_title( $video[0]'title']'$t'] ) == false ) {
//var_dump(!wp_exist_post_by_title( $video[0]'title']'$t'] ));
$post_id = wp_insert_post( $post );
//add_post_meta($post_id, 'video_id', $videoID);
add_post_meta($post_id, 'thumb', $video[0]'media$group']'media$thumbnail'][2]'url']);
$src = $video[0]'media$group']'media$thumbnail'][2]'url'];
$filename = $video[0]'gd$etag'];
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
$photo = new WP_Http();
$photo = $photo->request( $src );
$attachment = wp_upload_bits( $filename.'.jpg', null, $photo'body'], date("Y-m") );
$filetype = wp_check_filetype( basename( $attachment'file'] ), null );
$postinfo = array(
'post_mime_type' => $filetype'type'],
'post_title' => $filename,
'post_content' => '',
'post_status' => 'inherit',
);
$filename = $attachment'file'];
$attach_id = wp_insert_attachment( $postinfo, $filename );
// ATTACH
set_post_thumbnail($post_id, $attach_id);
if( !function_exists( 'wp_generate_attachment_data' ) )
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
?>