How to make a local cache of mshots images

I think this should work.

function upload_image_from_url($url) {

    /** Require dependencies */
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );     

    // Save as a temporary file
    $tmp = download_url( $url );

    // Check for download errors
    if ( is_wp_error( $tmp ) ) 
    {
        @unlink( $file_array[ 'tmp_name' ] );
        return $tmp;
    }

    // Image name (just random-number)
    $name = rand(0,100000).".jpg";

    // Take care of image files without extension:
    $path = pathinfo( $tmp );
    if( ! isset( $path['extension'] ) ):
        $tmpnew = $tmp . '.tmp';
        if( ! rename( $tmp, $tmpnew ) ):
            return '';
        else:
            $name = rand(0,100000).".jpg";
            $tmp = $tmpnew;
        endif;
    endif;

    // Upload the image into the WordPress Media Library:
    $file_array = array(
        'name'     => $name,
        'tmp_name' => $tmp
    );
    $id = media_handle_sideload( $file_array, 0 );

    // Check for handle sideload errors:
    if ( is_wp_error( $id ) )
    {
        @unlink( $file_array['tmp_name'] );
        return $id;
    }

    return $id;
}

I know this script is working, but I would do a few changes. Like how the images are named. You can also take a look at this.