Example #1
0
function fetch_yachtworld_images()
{
    global $wpdb;
    $query = "SELECT * FROM " . $wpdb->prefix . "yachts ORDER BY images ASC";
    $yachts = $wpdb->get_results($query);
    foreach ($yachts as $yacht) {
        write_log('Fetching images for ' . $yacht->id . ' from yachtworld');
        write_log('///////////////////////////////////////////////////////');
        if ($yacht->is_selenenw) {
            $html = selenenw_get_page('http://www.yachtworld.com/core/listing/photo_gallery.jsp?access=Public&slim=' . $yacht->slim . '&ywo=seleneyachtsnorthwest&ywo=seleneyachtsnorthwest&hosturl=seleneyachtsnorthwest&hosturl=seleneyachtsnorthwest&units=Feet&boat_id=' . $yacht->id . '&back=pl_boat_detail.jsp&boat_id=' . $yacht->id . '');
        } else {
            $html = selenenw_get_page('http://www.yachtworld.com/privatelabel/listing/photo_gallery.jsp?slim=' . $yacht->slim . '&lang=en&currency=USD&units=Feet&id=' . $yacht->id . '&back=/privatelabel/listing/pl_boat_detail.jsp&boat_id=' . $yacht->id);
        }
        if ($html) {
            write_log('Success: Fetched photo gallery from yachtworld');
            $doc = new DOMDocument();
            $doc->preserveWhiteSpace = true;
            @$doc->loadHTML($html);
            $xpath = new DOMXpath($doc);
            $columns = $xpath->query('/html/body/table/tr[4]/td/div/table/tr/td');
            $js = $xpath->query('/html/head/script')->item(0)->childNodes->item(0)->nodeValue;
            $new_images = array();
            if (!is_null($yacht->images)) {
                $images = json_decode($yacht->images, true);
            } else {
                $images = array();
            }
            $i = 1;
            foreach ($columns as $column) {
                $image = $xpath->query('img/@src', $column)->item(0)->nodeValue;
                $image = substr($image, 0, strpos($image, '?f='));
                $image_exists = false;
                $new_image = explode('/', $image);
                $new_image = explode('.', end($new_image));
                $new_image = $new_image[0];
                if (!empty($images)) {
                    $image_exists = array_filter($images, function ($item) use($new_image) {
                        if (stripos($item['original'], $new_image) !== false) {
                            return true;
                        }
                        return false;
                    });
                }
                if (!$image_exists || !selenenw_images_exist(current($image_exists))) {
                    $result = get_yacht_image($image);
                    $pos = strpos($js, 'PicDescription[' . $i . ']');
                    $eol_pos = strpos($js, ';', $pos) - $pos;
                    $caption = str_replace(array('PicDescription[' . $i . '] = \'', '\''), '', substr($js, $pos, $eol_pos));
                    sleep(rand(1, 3));
                    //Just a contingency to not get blacklisted
                    if (!is_wp_error($result)) {
                        write_log('Success: Downloaded ' . $image);
                        $new_images[] = array('original' => str_replace('640x465_', '', $result), 'resized' => $result, 'caption' => $caption);
                    } else {
                        write_log(array('Failure: Unable to download' . $image, $result));
                    }
                } else {
                    $new_images[] = current($image_exists);
                    write_log('NOTICE: Image exists ' . $image);
                }
                $i++;
            }
            $delete_images = array_filter($images, function ($item) use($new_images) {
                foreach ($new_images as $image) {
                    if ($item === $image) {
                        return false;
                    }
                }
                return true;
            });
            if (!empty($delete_images)) {
                require_once ABSPATH . 'wp-admin/includes/file.php';
                foreach ($delete_images as $delete_image) {
                    $image_paths = array(get_home_path() . parse_url($delete_image['original'], PHP_URL_PATH), get_home_path() . parse_url($delete_image['resized'], PHP_URL_PATH));
                    write_log('NOTICE: Deleting following images');
                    write_log($image_paths);
                    selenenw_delete_files($image_paths);
                }
            }
            if (!empty($new_images)) {
                $yacht_data = array('images' => json_encode($new_images));
                if ($wpdb->update($wpdb->prefix . 'yachts', $yacht_data, array('id' => $yacht->id))) {
                    write_log('Success: Updated details in DB');
                } else {
                    write_log('Failure: Unable to update DB');
                }
            } else {
                write_log('Failure: Images array is empty');
            }
        }
        write_log('///////////////////////////////////////////////////////');
    }
}
Example #2
0
<?php

/**
Template Name: Temp Get Image
*/
if (isset($_POST['urls'])) {
    $urls = json_decode($_POST['urls']);
    $images = array();
    foreach ($urls as $url) {
        $result = get_yacht_image($url);
        $images[] = array('original' => str_replace('640x465_', '', $result), 'resized' => $result);
    }
    var_dump(json_encode($images));
}
echo '<form action="' . get_permalink() . '" method="post"><textarea name="urls"></textarea><input type="submit" value="Get Images" name="get-images"></form>';