function curl_download($Url, $post_id)
{
    if (!function_exists('curl_init')) {
        die('cURL is not installed. Install and try again.');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    //curl_setopt($ch, CURLOPT_POST, TRUE);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, array("searchId" => $sID));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $title = '';
    $date = '';
    $date_start = '';
    $time_start = '';
    $date_end = '';
    $time_end = '';
    $content = '';
    $notice = '';
    $phone = '';
    $email = '';
    $needle1 = '';
    $needle2 = '';
    $haystack = '';
    // perform post
    $result = curl_exec($ch);
    $start = strpos($result, '<h4 id="news_title">');
    if ($start) {
        //if there is content
        $end = strpos($result, '<!-- /#main -->');
        $section = $end - $start;
        $main_content = substr($result, $start, $section);
        //$main_content = htmlentities($main_content);
        // perform title
        preg_match("'<h4 id=\"news_title\">(.*?)</h4>'", $main_content, $matches);
        $title = $matches[1];
        preg_match('\'<p class="date">(.*?)</p>\'', $main_content, $matches);
        $date = $matches[1];
        $date = date('Y-m-d H:i:s', strtotime($date . ' ' . $time_start));
        preg_match("/\\<div class=\"content\"\\>(.*?)\\<\\/div\\>/is", $main_content, $matches);
        $content = $matches[1];
        // Notice File
        if (strpos($main_content, '<p class="notice">')) {
            preg_match('\'<p class="notice"><a href=\\"(.*?)\\">Read\'', $main_content, $matches);
            $notice = $matches[1];
        }
        $output = 'Post ID: ' . $post_id . '<br />Title: ' . $title . '<br />Date: ' . $date . '<br />Notice: ' . $notice . '<br />Content: ' . $content;
        echo $output . "\r\n";
        echo '<hr />';
        $new_post = array('title' => $title, 'date' => $date, 'content' => $content, 'notice' => $notice);
        add_event_posts($new_post);
        update_option('latest_notice_id', $post_id);
    }
    curl_close($ch);
}
function curl_event_scrape($Url, $post_id)
{
    if (!function_exists('curl_init')) {
        die('cURL is not installed. Install and try again.');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $type = '';
    $display = '';
    $title = '';
    $dates = '';
    $date_start = '';
    $time_start = '';
    $date_end = '';
    $time_end = '';
    $location = '';
    $location_name = '';
    $address = '';
    $city = '';
    $state = '';
    $zip = '';
    $content = '';
    $meta = '';
    $name = '';
    $phone = '';
    $email = '';
    $web = '';
    // perform post
    $result = curl_exec($ch);
    $start = strpos($result, "<div class='event display");
    if ($start) {
        $end = strpos($result, '<!-- /#main -->');
        $section = $end - $start;
        $main_content = substr($result, $start, $section);
        // get type
        $start = strpos($main_content, "<p class='type_");
        $end = strpos($main_content, " '></p>", $start);
        $section = $end - $start;
        $type = substr($main_content, $start, $section);
        $type = str_replace("'>", "", $type);
        $type = str_replace("<p class='type_", "", $type);
        //if ( $type != 'city_meetings' ) {
        //if ( $type != 'library' ) {
        $start = strpos($main_content, "<div class='event display_");
        $end = strpos($main_content, "'>", $start);
        $section = $end - $start;
        $display = substr($main_content, $start, $section);
        $display = str_replace("<div class='event display_", "", $display);
        $display = str_replace("'>", "", $display);
        // Open the file to get existing content
        //$test_output = $post_id . ' - ' . $display . '<br />';
        if ($display == 'y') {
            // perform title
            $start = strpos($main_content, "<h3>");
            $end = strpos($main_content, '</h3>', $start);
            $section = $end - $start;
            $title = substr($main_content, $start, $section);
            $title = str_replace('<h3>', '', $title);
            // perform dates
            $start = strpos($main_content, '<p class="date_time">');
            $end = strpos($main_content, '</p>', $start);
            $section = $end - $start;
            $dates = substr($main_content, $start, $section);
            $dates = str_replace('<p class="date_time">', '', $dates);
            if (strpos($dates, '-')) {
                $first = preg_split("/ - /", $dates);
                $date_start = $first[0];
                if (preg_match('/( from | at )/', $first[1])) {
                    if (preg_match('/( from )/', $first[1])) {
                        $first = preg_split('/ from /', $first[1]);
                        $date_end = $first[0];
                        if (preg_match('/( to )/', $first[1])) {
                            $first = preg_split('/ to /', $first[1]);
                            $time_start = $first[0];
                            $time_end = $first[1];
                        } else {
                            $time_start = $first[1];
                            $time_end = $first[1];
                        }
                    } else {
                        $first = preg_split('/ at /', $first[1]);
                        $date_end = $first[0];
                        $time_start = $first[1];
                        $time_end = $first[1];
                    }
                } else {
                    $date_end = $first[1];
                }
            } else {
                // did not have -
                if (preg_match('/( from | at )/', $dates)) {
                    if (preg_match('/( from )/', $dates)) {
                        $first = preg_split('/ from /', $dates);
                        $date_start = $first[0];
                        $date_end = $first[0];
                        if (preg_match('/( to )/', $first[1])) {
                            $first = preg_split('/ to /', $first[1]);
                            $time_start = $first[0];
                            $time_end = $first[1];
                        } else {
                            $time_start = $first[1];
                            $time_end = $first[1];
                        }
                    } else {
                        $first = preg_split('/ at /', $dates);
                        $date_start = $first[0];
                        $date_end = $first[0];
                        $time_start = $first[1];
                        $time_end = $first[1];
                    }
                }
            }
            $when_start = date('Y-m-d H:i:s', strtotime($date_start . ' ' . $time_start));
            $when_end = date('Y-m-d H:i:s', strtotime($date_end . ' ' . $time_end));
            // location
            $start = strpos($main_content, '<p class=\'location\'>');
            $end = strpos($main_content, '</p>', $start);
            $section = $end - $start;
            $location = substr($main_content, $start, $section);
            $location = str_replace('<p class=\'location\'>', '', $location);
            // name
            $start = strpos($location, '<span class=\'location_name\'>');
            $end = strpos($location, '</span>', $start);
            $section = $end - $start;
            $location_name = substr($location, $start, $section);
            $location_name = str_replace('<span class=\'location_name\'>', '', $location_name);
            // address
            $start = strpos($location, '<span class=\'address\'>');
            $end = strpos($location, '</span>', $start);
            $section = $end - $start;
            $address = substr($location, $start, $section);
            $address = str_replace('<span class=\'address\'>', '', $address);
            // city
            $start = strpos($location, '<span class=\'city\'>');
            $end = strpos($location, '</span>', $start);
            $section = $end - $start;
            $city = substr($location, $start, $section);
            $city = str_replace('<span class=\'city\'>', '', $city);
            // state
            $start = strpos($location, '<span class=\'state\'>');
            $end = strpos($location, '</span>', $start);
            $section = $end - $start;
            $state = substr($location, $start, $section);
            $state = str_replace('<span class=\'state\'>', '', $state);
            // zip
            $start = strpos($location, '<span class=\'zip\'>');
            $end = strpos($location, '</span>', $start);
            $section = $end - $start;
            $zip = substr($location, $start, $section);
            $zip = str_replace('<span class=\'zip\'>', '', $zip);
            // content
            $start = strpos($main_content, '<div class=\'desc\'>');
            $end = strpos($main_content, '</div>', $start);
            $section = $end - $start;
            $content = substr($main_content, $start, $section);
            $content = str_replace('<div class=\'desc\'>', '', $content);
            // Meta
            //echo 'Main content: ' . $main_content . '<br />';
            $start = strpos($main_content, '<div class=\'meta\'>');
            $end = strpos($main_content, '</div>', $start);
            $section = $end - $start;
            $meta = substr($main_content, $start, $section);
            $meta = str_replace('<div class=\'meta\'>', '', $meta);
            // Name
            $start = strpos($meta, '<span class=\'contact_name\'>');
            $end = strpos($meta, '</span>', $start);
            $section = $end - $start;
            $name = substr($meta, $start, $section);
            $name = str_replace('<span class=\'contact_name\'>', '', $name);
            // Phone
            $start = strpos($meta, '<span class=\'contact_phone\'>');
            $end = strpos($meta, '</span>', $start);
            $section = $end - $start;
            $phone = substr($meta, $start, $section);
            $phone = str_replace('<span class=\'contact_phone\'>', '', $phone);
            // Email
            $start = strpos($meta, '<span class=\'contact_email\'>');
            $end = strpos($meta, '</span>', $start);
            $section = $end - $start;
            $email = substr($meta, $start, $section);
            $email = str_replace('<span class=\'contact_email\'>', '', $email);
            // Web
            $start = strpos($meta, '<span class=\'web\'>');
            $end = strpos($meta, '</span>', $start);
            $section = $end - $start;
            $web = substr($meta, $start, $section);
            $web = str_replace('<span class=\'web\'>', '', $web);
            $output = 'ID: ' . $post_id . '<br />Type_: ' . $type . '<br />Title: ' . $title . '<br />Dates: ' . $dates . '<br />Start: ' . $when_start . '<br />End: ' . $when_end . '<br />content: ' . $content . '<br />Name_: ' . $name . '<br />Phone_: ' . $phone . '<br />Email_: ' . $email . '<br />Web_: ' . $web . '<br />Location: ' . $location . '<br />Location Name: ' . $location_name . '<br />Address: ' . $address . '<br />City: ' . $city . '<br />State: ' . $state . '<br />Zip: ' . $zip;
            //$output = 'Dates: ' .  $dates . '<br />Date Start: ' . $date_start . '<br />Time Start: ' . $time_start . '<br />Date end: ' . $date_end . '<br />Time End: ' . $time_end . '<br />Start: ' . $when_start . '<br />End: ' . $when_end;
            //$output = 'Location: ' .  $location . '<br />Name: ' . $location_name . '<br />Address: ' . $address . '<br />City: ' . $city . '<br />State: ' . $state . '<br />Zip: ' . $zip;
            echo $output . "\r\n";
            echo '<hr />';
            $new_post = array('title' => $title, 'dates' => $dates, 'when_start' => $when_start, 'when_end' => $when_end, 'location' => $location, 'location_name' => $location_name, 'content' => $content, 'address' => $address, 'city' => $city, 'state' => $state, 'zip' => $zip, 'name' => $name, 'phone' => $phone, 'email' => $email, 'web' => $web, 'type' => $type);
            add_event_posts($new_post, $post_id);
            update_option('latest_event_id', $post_id);
            $posted = true;
            return $status[] = array($post_id, $posted);
        } else {
            // for posts taht are not displayed
            $posted = false;
            return $status[] = array($post_id, $posted);
        }
        // if display
        //} // end if not library
        //}  // end if not city meetings
    }
    // end if start
    curl_close($ch);
}
function curl_download($Url)
{
    if (!function_exists('curl_init')) {
        die('cURL is not installed. Install and try again.');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    //curl_setopt($ch, CURLOPT_POST, TRUE);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, array("searchId" => $sID));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $title = '';
    $when = '';
    $date = '';
    $time = '';
    $time_new = '';
    $time_start = '';
    $time_end = '';
    $multi_day = '';
    $where = '';
    $venue = '';
    $venue[0] = '';
    $venue[1] = '';
    $venue[2] = '';
    $venue_city_state = '';
    $venue_city_state[0] = '';
    $venue_city_state[1] = '';
    $venue_city_state[2] = '';
    $venue_title = '';
    $venue_address = '';
    $venue_city = '';
    $venue_state = '';
    $venue_zip = '';
    $cost = '';
    $name = '';
    $email = '';
    $phone = '';
    $web = '';
    $cats = '';
    $image = '';
    $content = '';
    // perform post
    $result = curl_exec($ch);
    $start = strpos($result, '<h1 class="entry-title">');
    $end = strpos($result, '<!-- .entry-content -->');
    $section = $end - $start;
    $main_content = substr($result, $start, $section);
    // TITLE
    $start = strpos($main_content, '<h1 class="entry-title">');
    $end = strpos($main_content, '</h1>', $start);
    $section = $end - $start;
    $title = substr($main_content, $start, $section);
    $title = substr($title, $start, $section);
    $title = str_replace('<h1 class="entry-title">', '', $title);
    $title = ucwords($title);
    // WHEN
    $start = strpos($main_content, '<div class="ai1ec-time">');
    $end = strpos($main_content, '<div class="ai1ec-map');
    $section = $end - $start;
    $when = substr($main_content, $start, $section);
    $start = strpos($when, '<div class="ai1ec-field-value">');
    $end = strpos($when, '</div>', $start);
    $section = $end - $start;
    $when = substr($when, $start, $section);
    $when = str_replace('<div class="ai1ec-field-value">', '', $when);
    // Remove em dash
    $em_dash = html_entity_decode('&#x2013;', ENT_COMPAT, 'UTF-8');
    $when = str_replace($em_dash, '-', $when);
    if (!strpos($when, '-')) {
        $first = preg_split("/ @ /", $when);
        $date_start = $first[0];
        $time_start = $first[1];
        $date_end = $date_start;
        $time_end = $time_start;
    } else {
        $when = explode(' - ', $when);
        $first = $when[0];
        $second = $when[1];
        if (!strpos($second, '@')) {
            if (!strpos($first, '@')) {
                $date_start = $first;
                $date_end = $second;
            } else {
                $first = preg_split('/ @ /i', $first);
                $date_start = $first[0];
                $time_start = $first[1];
                $date_end = $date_start;
                $time_end = $second;
            }
        } else {
            $first = preg_split('/ @ /i', $first);
            $second = preg_split('/ @ /i', $second);
            $date_start = $first[0];
            $time_start = $first[1];
            $date_end = $second[0];
            $time_end = $second[1];
        }
    }
    $when_start = date('Y-m-d H:i:s', strtotime($date_start . ' ' . $time_start));
    $when_end = date('Y-m-d H:i:s', strtotime($date_end . ' ' . $time_end));
    // WHERE
    $start = strpos($main_content, '<div class="ai1ec-location">');
    $end = strpos($main_content, '<div class="ai1ec-contact">');
    $section = $end - $start;
    $where = substr($main_content, $start, $section);
    $start = strpos($where, '<div class="ai1ec-field-value">');
    $end = strpos($where, '</div>', $start);
    $section = $end - $start;
    $where = substr($where, $start, $section);
    $where = str_replace('<div class="ai1ec-field-value">', '', $where);
    if (strpos($where, '<br')) {
        $venue = preg_split('/<br[^>]*>/i', $where);
        if (!empty($venue[0])) {
            $venue_title = $venue[0];
        }
        if (!empty($venue[1])) {
            $venue_address = $venue[1];
        }
        if (!empty($venue[2])) {
            $venue_city = $venue[2];
        }
        //if ( strpos( $venue_city, '/[\s,]+/' ) ) {
        if ($venue_city) {
            $venue_city_state = preg_split('/[\\s,]+/i', $venue_city);
            array_shift($venue_city_state);
            //print_r($venue_city_state);
            //echo $venue_city_state[1] ;
            if (!empty($venue_city_state[0])) {
                $venue_city = $venue_city_state[0];
            }
            if (!empty($venue_city_state[1])) {
                $venue_state = $venue_city_state[1];
            }
            if (!empty($venue_city_state[2])) {
                $venue_zip = $venue_city_state[2];
            }
        }
    }
    // COST
    if (strpos($main_content, '<div class="ai1ec-cost">')) {
        $start = strpos($main_content, '<div class="ai1ec-cost">');
        $end = strpos($main_content, '<div class="ai1ec-contact">');
        $section = $end - $start;
        $cost = substr($main_content, $start, $section);
        $start = strpos($cost, '<div class="ai1ec-field-value">');
        $end = strpos($cost, '</div>', $start);
        $section = $end - $start;
        $cost = substr($cost, $start, $section);
        $cost = str_replace('<div class="ai1ec-field-value">', '', $cost);
    }
    // CONTACT
    if (strpos($main_content, '<div class="ai1ec-contact">')) {
        $contact = explode('<div class="ai1ec-contact">', $main_content);
        $contact = $contact[1];
        $start = strpos($contact, '<div class="ai1ec-field-value">');
        $end = strpos($contact, '</div>', $start);
        $section = $end - $start;
        $contact = substr($contact, $start, $section);
        $contact = str_replace('<div class="ai1ec-field-value">', '', $contact);
        //// name
        if (strpos($contact, '<i class="icon-user"></i>')) {
            $start = strpos($contact, '<i class="icon-user"></i>');
            $end = strpos($contact, '</span>', $start);
            $section = $end - $start;
            $name = substr($contact, $start, $section);
            $name = str_replace('<i class="icon-user"></i>', '', $name);
        }
        //// phone
        if (strpos($contact, '<i class="icon-phone"></i>')) {
            $start = strpos($contact, '<i class="icon-phone"></i>');
            $end = strpos($contact, '</span>', $start);
            $section = $end - $start;
            $phone = substr($contact, $start, $section);
            $phone = str_replace('<i class="icon-phone"></i>', '', $phone);
        }
        //// email
        if (strpos($contact, 'mailto:')) {
            $start = strpos($contact, 'mailto:');
            $end = strpos($contact, '">', $start);
            $section = $end - $start;
            $email = substr($contact, $start, $section);
            $email = str_replace('mailto:', '', $email);
        }
        //// website
        if (strpos($contact, 'ai1ec-contact-url')) {
            $start = strpos($contact, 'href="', $start);
            $end = strpos($contact, '">', $start);
            $section = $end - $start;
            $web = substr($contact, $start, $section);
            $web = str_replace('href="', '', $web);
        }
    }
    // CATEGORIES
    if (strpos($main_content, '<div class="ai1ec-categories">')) {
        $cat_section = explode('<div class="ai1ec-categories">', $main_content);
        $cat_section = $cat_section[1];
        $start = strpos($cat_section, '<div class="ai1ec-field-value">');
        $end = strpos($cat_section, '</div>', $start);
        $section = $end - $start;
        $cat_section = substr($cat_section, $start, $section);
        $cat_section .= "</div>";
        $cats_array = explode('<a', $cat_section);
        array_shift($cats_array);
        $cats = array();
        foreach ($cats_array as $cat) {
            $cat = '<a class' . $cat;
            $cats[] = strip_tags($cat);
        }
    }
    // CONTENT
    $start = strpos($main_content, '<p>');
    $end = strpos($main_content, '<footer class="ai1ec-event-footer">');
    $section = $end - $start;
    $content = substr($main_content, $start, $section);
    $content_array = explode('<p>', $content);
    $image = $content_array[1];
    unset($content_array[0]);
    unset($content_array[1]);
    $content = '<p>' . implode('<p>', $content_array);
    // IMAGE
    $start = strpos($image, 'wp-content/uploads/');
    $end = strpos($image, '">');
    $section = $end - $start;
    $image = substr($image, $start, $section);
    $cats_string = '';
    if ($cats) {
        foreach ($cats as $cat) {
            $cats_string .= $cat . ', ';
        }
        $cats_string = rtrim($cats_string, ',');
    }
    $slug = $Url;
    $slug_replace = home_url() . '/event/';
    $slug = str_replace('http://visit-prescott.com/wordpress/ai1ec_event/', '', $slug);
    $output = '<strong>' . $title . '</strong><br />' . 'Url: ' . $slug . '<br />' . 'Start Date: ' . $when_start . "<br />" . 'End Date: ' . $when_end . "<br />" . 'Venue: ' . $venue_title . "|" . $venue_address . "|" . $venue_city . "|" . $venue_state . "|" . $venue_zip . "<br />" . 'Cost: ' . $cost . "<br />" . 'Organizer: ' . $name . "|" . $phone . "|" . $email . "|" . $web . "<br />" . 'Categories: ' . $cats_string . "<br />" . 'Image: ' . $image . "<br />";
    //$content
    $output = str_replace(array("\r", "\n"), "", $output);
    echo $output . "\r\n";
    //echo '<hr />';
    $new_post = array('title' => $title, 'slug' => $slug, 'date' => $date, 'time' => $time, 'when_start' => $when_start, 'when_end' => $when_end, 'multi_day' => $multi_day, 'where' => $where, 'venue_title' => $venue_title, 'venue_address' => $venue_address, 'venue_city' => $venue_city, 'venue_state' => $venue_state, 'venue_zip' => $venue_zip, 'cost' => $cost, 'name' => $name, 'phone' => $phone, 'email' => $email, 'web' => $web, 'cats' => $cats, 'image' => $image, 'content' => $content);
    add_event_posts($new_post);
    curl_close($ch);
}