Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('xdebug.var_display_max_depth', -1);
- ini_set('xdebug.var_display_max_children', -1);
- ini_set('xdebug.var_display_max_data', -1);
- function bbc_run(){
- global $inserted;
- global $tried;
- global $bbc_cron;
- $isbn = (isset($_REQUEST['bbc_isbn']) ? $_REQUEST['bbc_isbn'] : false);
- if($isbn)
- $isbn = explode(',', $isbn);
- $amazon_api_key = get_field('bbc_api_amazon_access_key', 'options');
- $amazon_api_secret = get_field('bbc_api_amazon_secret_key', 'options');
- $amazon_api_tag = get_field('bbc_api_amazon_tag', 'options');
- $amazon_api_sort = get_field('bbc_sort', 'options');
- $amazon_api_nodes = get_field('bbc_ids_de_categorias', 'options');
- $amazon_tops = get_field('bbc_bestseller_id', 'options');
- $url_servidor_proxy = get_field('bbc_proxy_url', 'options');
- do {
- if( empty($url_servidor_proxy) ) break;
- $urls_servidor_proxy = explode(",", $url_servidor_proxy);
- if( !is_array($urls_servidor_proxy) ) break;
- $k = array_rand($urls_servidor_proxy);
- $url_servidor_proxy = $urls_servidor_proxy[$k];
- } while(0);
- $news_max = get_field('bbc_import_max', 'options');
- $tops_max = 20;
- if(!$news_max)
- $news_max = 99999;
- if(!$tops_max)
- $tops_max = 99999;
- $max_page = 10;
- $nodes = explode(',', $amazon_api_nodes);
- $c_node = next_node($nodes);
- if (isset($url_servidor_proxy) && empty($url_servidor_proxy)) $url_servidor_proxy = "";
- if($isbn && $isbn[0] != ''){
- foreach($isbn as $i){
- $xml_answer = bbc_amazon($amazon_api_key, $amazon_api_secret, $amazon_api_tag, $amazon_api_sort, 1, $i, $nodes[0], $url_servidor_proxy);
- $xml = $xml_answer->Items;
- foreach($xml->Item as $item){
- bbc_parse_item($item);
- }
- }
- }else{
- delete_post_meta_by_key( 'bbc_tops' );
- // delete_post_meta_by_key( 'bbc_news' );
- global $meta;
- $meta = array('tops' => 0, 'news' => 0);
- $p = 1;
- if(!$bbc_cron)
- echo '<p>TOPS: </p>';
- while($p <= $max_page){
- $xml_answer = bbc_amazon($amazon_api_key, $amazon_api_secret, $amazon_api_tag, $amazon_api_sort, $p, '', $amazon_tops, $url_servidor_proxy);
- $xml = $xml_answer->Items;
- if($p == 1)
- $totalPages = (int)$xml->TotalPages;
- foreach($xml->Item as $item){
- $r = bbc_parse_item($item, 'tops');
- $tops_max--;
- if($tops_max == 0)
- break;
- }
- $p++;
- if($p >= $totalPages)
- break;
- if($tops_max == 0)
- break;
- }
- //// foreach($nodes as $node){
- $node = $nodes[$c_node];
- $p = 1;
- $xml_answer = bbc_amazon($amazon_api_key, $amazon_api_secret, $amazon_api_tag, $amazon_api_sort, $p, '', $amazon_tops, $url_servidor_proxy);
- $xml = $xml_answer->Items;
- $new_books_max = $news_max;
- while($p <= $max_page){
- $xml_answer = bbc_amazon($amazon_api_key, $amazon_api_secret, $amazon_api_tag, $amazon_api_sort, $p, '', $node, $url_servidor_proxy);
- $xml = $xml_answer->Items;
- if($p == 1)
- $totalPages = (int)$xml->TotalPages;
- foreach($xml->Item as $item){
- $r = bbc_parse_item($item);
- if($r == 'new')
- $new_books_max--;
- if($new_books_max == 0)
- break;
- }
- $p++;
- if($p >= $totalPages)
- break;
- if($new_books_max == 0)
- break;
- }
- //// }
- }
- if(!$bbc_cron){
- echo '<br>'.$tried.' Libros fueron encontrados';
- echo ($inserted ? '<br>'.$inserted.' Libros nuevos fueron insertados' : 'Ningun libro nuevo fue insertado');
- }
- }
- function next_node($node){
- $c_node = get_option('current_node');
- if($c_node === false){
- update_option('current_node', 0);
- $c_node = get_option('current_node');
- }else{
- $c_node++;
- if(!isset($node[$c_node]))
- $c_node = 0;
- update_option('current_node', $c_node);
- }
- return $c_node;
- }
- function bbc_parse_item($item, $m = ''){
- global $inserted;
- global $tried;
- global $meta;
- global $bbc_cron;
- if($m){
- $meta[$m]++;
- }
- $tried++;
- $att = $item->ItemAttributes;
- $titulo = trim(preg_replace('/[\(\)\?!].*/', '', (string)$att->Title));
- $isbn = (string)$att->ISBN;
- if(!$isbn)
- $isbn = (string)$att->EISBN;
- if(!$isbn)
- $isbn = (string)$item->ASIN;
- if($post = get_page_by_title($titulo, OBJECT, 'libro')){
- if($m && !metadata_exists('post', $post->ID, 'bbc_'.$m)){
- update_post_meta($post->ID, 'bbc_'.$m, $meta[$m]);
- }
- if(!$bbc_cron)
- echo '<br>' . $titulo . __(' Ya existe el titulo con ISBN: ', 'books_theme') . $isbn;
- return 'old';
- }
- $args = array(
- 'post_type' => 'libro',
- 'meta_key' => 'isbn',
- 'meta_value' => $isbn,
- );
- $post = get_posts($args);
- if($post && !is_wp_error($post)){
- if($m && !metadata_exists('post', $post[0]->ID, 'bbc_'.$m)){
- update_post_meta($post[0]->ID, 'bbc_'.$m, $meta[$m]);
- }
- if(!$bbc_cron)
- echo '<br>' . $titulo . __(' Ya existe, ISBN: ', 'books_theme') . $isbn;
- return 'old';
- }
- $isbn13 = (string)$att->EAN;
- $autor = false;
- $autor_slug = '';
- if($att->Author) {
- if(count($att->Author)==1){
- $autor = (string)$att->Author;
- }else if(count($att->Author)>1){
- $autor = (string)$att->Author[0];
- }
- }
- if($autor){
- $autor_slug = bbc_slug($autor);
- if(!get_term_by('slug', $autor_slug, 'autor')){
- $autor_term = wp_insert_term($autor, 'autor', array('slug' => $autor_slug))['term_id'];
- }else{
- $autor_term = get_term_by('slug', $autor_slug, 'autor')->term_id;
- }
- }else{
- if($att->Creator) {
- if(count($att->Creator)==1){
- $autor = (string)$att->Creator;
- }else if(count($att->Creator)>1){
- $autor = (string)$att->Creator[0];
- }
- }
- if($autor){
- $autor_slug = bbc_slug($autor);
- if(!get_term_by('slug', $autor_slug, 'autor')){
- $autor_term = wp_insert_term($autor, 'autor', array('slug' => $autor_slug))['term_id'];
- }else{
- $autor_term = get_term_by('slug', $autor_slug, 'autor')->term_id;
- }
- }
- }
- $editorial = (string)$att->Publisher;
- if($editorial){
- $editorial_slug = bbc_slug($editorial);
- if(!get_term_by('slug', $editorial_slug, 'editorial')){
- $editorial_term = wp_insert_term($editorial, 'editorial', array('slug' => $editorial_slug))['term_id'];
- }else{
- $editorial_term = get_term_by('slug', $editorial_slug, 'editorial')->term_id;
- }
- }else{
- $editorial_term = false;
- }
- $genre = $subgenre = false;
- $limit = 9;
- while($limit>1){
- $limit--;
- if(!$genre){
- $node = $item->BrowseNodes;
- }else{
- $node = $browseNode->Ancestors;
- }
- if(count($node)==1){
- $browseNode = $node->BrowseNode;
- }else if(count($node)>1){
- $browseNode = $node->BrowseNode[0];
- }
- if(!$browseNode)
- break;
- if($browseNode->IsCategoryRoot)
- break;
- // if($genre){
- // $subgenre = $genre;
- // }
- $genre = (string)$browseNode->Name;
- }
- if($genre){
- $genre_slug = bbc_slug($genre);
- if(!get_term_by('slug', $genre_slug, 'genre')){
- $genre_term = wp_insert_term($genre, 'genre', array('slug' => $genre_slug))['term_id'];
- }else{
- $genre_term = get_term_by('slug', $genre_slug, 'genre')->term_id;
- }
- }
- $imagen = (string)$item->LargeImage->URL;
- $fecha = (string)$att->PublicationDate;
- if($fecha){
- $f = explode('-', $fecha);
- if(count($f) == 3)
- $fecha = $f[2] . '/' . $f[1] . '/' . $f[0];
- if(count($f) == 2)
- $fecha = '01/' . $f[1] . '/' . $f[0];
- }
- $paginas = (int)$att->NumberOfPages;
- if(!$paginas || $paginas <= 1)
- $paginas = rand(100,200);
- $rating = (4 + (rand(0, 9) / 10)) + (rand(0, 1) / 10);
- $views = rand(19000, 21000);
- $opiniones = rand(1000, 2000);
- $t = (string)$item->EditorialReviews->EditorialReview->Content;
- $content = bbc_Yandexize($t);
- $excerpt = bbc_get_words($content, 100);
- if($excerpt != $content)
- $excerpt .= ' ...';
- $post_name = sanitize_title($titulo);
- // $post_name .= '-'.$autor_slug.'-epub-pdf-mobi';
- $id = wp_insert_post(
- array(
- 'post_title'=>$titulo,
- 'post_name' => $post_name,
- 'post_excerpt' => $excerpt,
- 'post_content' => $content,
- 'post_type'=>'libro',
- 'post_status' => 'publish',
- )
- );
- if($m && !metadata_exists('post', $id, 'bbc_'.$m))
- update_post_meta($id, 'bbc_'.$m, $meta[$m]);
- if($autor_term){
- wp_set_post_terms( $id, $autor, 'autor');
- update_field('autor', (int)$autor_term, $id);
- }else{
- $autor = get_term(get_field('autor_default', 'options'));
- if(!is_wp_error($autor)){
- wp_set_post_terms( $id, $autor->name, 'autor');
- update_field('autor', get_field('autor_default', 'options'), $id);
- }
- }
- if($editorial_term){
- wp_set_post_terms( $id, $editorial, 'editorial');
- update_field('editorial', (int)$editorial_term, $id);
- }else{
- $editorial = get_term(get_field('editorial_default', 'options'));
- if(!is_wp_error($editorial)){
- wp_set_post_terms( $id, $editorial->name, 'editorial');
- update_field('editorial', get_field('editorial_default', 'options'), $id);
- }
- }
- if($genre_term){
- wp_set_post_terms( $id, $genre_term, 'genre');
- }else{
- $genre = get_term(get_field('genre_default', 'options'));
- if(!is_wp_error($genre)){
- wp_set_post_terms( $id, $genre->name, 'genre');
- }
- }
- if($t)
- update_field('original_content', $t, $id);
- if($imagen)
- update_field('imagen', $imagen, $id);
- if($fecha)
- update_field('fecha_de_publicacion', $fecha, $id);
- if($isbn)
- update_field('isbn', $isbn, $id);
- if($isbn13)
- update_field('isbn13', $isbn13, $id);
- if($paginas)
- update_field('paginas', $paginas, $id);
- if($rating)
- update_field('valoracion', (int)$rating, $id);
- if($views)
- update_post_meta($id, 'views', $views);
- if($opiniones)
- update_field('opiniones', (int)$opiniones, $id);
- if(!$bbc_cron)
- echo __('<br>Nuevo libro: ', 'books_theme') . $titulo;
- $inserted++;
- return 'new';
- }
- function bbc_GetAPIYandex($string) {
- $apis = explode(",", $string);
- $cantidad = count($apis);
- if (get_option('bbc_yandex_api_actual') == false) {
- update_option('bbc_yandex_api_actual', $apis[mt_rand(0, $cantidad - 1)]);
- } else {
- $yandex_actual = get_option('bbc_yandex_api_actual');
- $continuar = false;
- while ($continuar == false) {
- $randtemporal = $apis[mt_rand(0, $cantidad - 1)];
- if ($yandex_actual != $randtemporal) {
- update_option('bbc_yandex_api_actual', $randtemporal);
- $continuar = true;
- }
- }
- }
- return get_option('bbc_yandex_api_actual');
- }
- function bbc_YandexTraslate($contenido, $lenguaje) {
- if(!$contenido)
- return '';
- $apis = get_field('bbc_api_yandex_key', 'options');
- $YandexAPI = bbc_GetAPIYandex($apis);
- $resultado = json_decode(@file_get_contents("https://translate.yandex.net/api/v1.5/tr.json/translate?key=" . $YandexAPI . "&text=" . urlencode($contenido) . "&lang=" . $lenguaje . "&format=html"));
- if ($resultado->code == 200) {
- $contenido = $resultado->text['0'];
- }
- return $contenido;
- }
- function bbc_Yandexize($c){
- $c = bbc_YandexTraslate($c, "es-fr");
- $c = bbc_YandexTraslate($c, "fr-es");
- return $c;
- }
- function bbc_amazon($aws_access_key_id, $amazon_api_secret, $amazon_api_tag, $amazon_api_sort, $itemPage, $books_isbn, $browseNode, $url_servidor_proxy) {
- sleep(1);
- try {
- $endpoint = "webservices.amazon.es";
- $uri = "/onca/xml";
- if ($books_isbn == "") {
- $params = array(
- "Service" => "AWSECommerceService",
- "Operation" => "ItemSearch",
- "AWSAccessKeyId" => $aws_access_key_id,
- "AssociateTag" => $amazon_api_tag,
- "SearchIndex" => "Books",
- "Keywords" => "Book",
- "BrowseNode" => $browseNode,
- "ResponseGroup" => "BrowseNodes,EditorialReview,Images,ItemAttributes,ItemIds,SalesRank,AlternateVersions",
- "ItemPage" => $itemPage,
- "Version" => "2015-10-01",
- "Sort" => $amazon_api_sort
- );
- } else {
- $params = array(
- "Service" => "AWSECommerceService",
- "Operation" => "ItemLookup",
- "ResponseGroup" => "BrowseNodes,EditorialReview,Images,ItemAttributes,ItemIds,SalesRank,AlternateVersions",
- "IdType" => "ASIN",
- "ItemId" => $books_isbn,
- "AWSAccessKeyId" => $aws_access_key_id,
- "AssociateTag" => $amazon_api_tag,
- "Version" => "2015-10-01",
- );
- }
- $params["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z');
- ksort($params);
- $pairs = array();
- foreach ($params as $key => $value) {
- array_push($pairs, rawurlencode($key) . "=" . rawurlencode($value));
- }
- $canonical_query_string = join("&", $pairs);
- $string_to_sign = "GET\n" . $endpoint . "\n" . $uri . "\n" . $canonical_query_string;
- $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $amazon_api_secret, true));
- $request_url = 'https://' . $endpoint . $uri . '?' . $canonical_query_string . '&Signature=' . rawurlencode($signature);
- if (isset($url_servidor_proxy) && !empty($url_servidor_proxy)) $response = getDataProxy($request_url,$url_servidor_proxy);
- else $response = file_get_contents($request_url);
- $parsed_xml = simplexml_load_string($response);
- return $parsed_xml;
- } catch (Exception $e) {
- var_dump($e->getMessage());
- return false;
- }
- }
- function getDataProxy($url, $proxy){
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_PROXY, $proxy);
- $user_pass = get_field('bbc_proxy_user_pass', 'options');
- if( !empty($user_pass) ) {
- curl_setopt($ch, CURLOPT_PROXYUSERPWD, $user_pass);
- }
- $resultado = curl_exec($ch);
- curl_close($ch);
- return $resultado;
- }
- function bbc_slug($string){
- return sanitize_title(preg_replace("/[^A-Za-z0-9 ]/", '-', trim(strtolower(bbc_corrector($string)))));
- }
- function bbc_corrector($txt) {
- $transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', 'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh', 'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E', 'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E', 'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G', 'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H', 'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', 'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L', 'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N', 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O', 'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O', 'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE', 'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R', 'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S', 'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't', 'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u', 'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u', 'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u', 'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w', 'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z', 'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'a', 'б' => 'b', 'Б' => 'b', 'в' => 'v', 'В' => 'v', 'г' => 'g', 'Г' => 'g', 'д' => 'd', 'Д' => 'd', 'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'zh', 'з' => 'z', 'З' => 'z', 'и' => 'i', 'И' => 'i', 'й' => 'j', 'Й' => 'j', 'к' => 'k', 'К' => 'k', 'л' => 'l', 'Л' => 'l', 'м' => 'm', 'М' => 'm', 'н' => 'n', 'Н' => 'n', 'о' => 'o', 'О' => 'o', 'п' => 'p', 'П' => 'p', 'р' => 'r', 'Р' => 'r', 'с' => 's', 'С' => 's', 'т' => 't', 'Т' => 't', 'у' => 'u', 'У' => 'u', 'ф' => 'f', 'Ф' => 'f', 'х' => 'h', 'Х' => 'h', 'ц' => 'c', 'Ц' => 'c', 'ч' => 'ch', 'Ч' => 'ch', 'ш' => 'sh', 'Ш' => 'sh', 'щ' => 'sch', 'Щ' => 'sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'y', 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja');
- return str_replace(array_keys($transliterationTable), array_values($transliterationTable), $txt);
- }
- function bbc_get_words($sentence, $count = 10) {
- preg_match("/(?:\S+(?:\W+|$)){0,$count}/", $sentence, $matches);
- return $matches[0];
- }
Add Comment
Please, Sign In to add comment