This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

RSS Feed to DB

By: a guest on Oct 21st, 2012  |  syntax: PHP  |  size: 4.36 KB  |  views: 1,916  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php // Get RSS Feed
  2. include_once"wp-config.php";
  3. include_once"wp-load.php";
  4. include_once"wp-includes/wp-db.php";
  5. global $wpdb;
  6.  
  7. $feeds = ('http://www.trumba.com/calendars/vd.rss?mixin=236385%2c236392%2c236393%2c236288');
  8. $xml = simplexml_load_file($feeds);
  9.  
  10. $post_fields = array('event_slug','event_owner','event_name','event_attributes','post_id','post_content');
  11. $event_metas = array(); //restart metas
  12.  
  13. $i=0;
  14.  
  15. foreach($xml->channel->item as $item) {
  16.        
  17.         //Data for each event entry
  18.         $title = $item->title;
  19.         $slug = strtolower(str_replace(" ", "-", $title));
  20.         $description = $item->description;
  21.         $weblink = $item->children('x-trumba', true)->weblink;
  22.         $location = $item->children('xCal', true)->location;
  23.         $location = substr($location,0,-1); //Only for VailDaily Feed
  24.         $sdate = substr(date("Y-n-j H:i:s",strtotime($item->children('x-trumba', true)->localstart)),0, -9);
  25.         $edate = substr(date("Y-n-j H:i:s",strtotime($item->children('x-trumba', true)->localend)),0, -9);
  26.         $stime = substr(date("Y-n-j H:i:s",strtotime($item->children('x-trumba', true)->localstart)),-8);
  27.         $etime = substr(date("Y-n-j H:i:s",strtotime($item->children('x-trumba', true)->localend)),-8);
  28.         $category = $item->children('x-trumba', true)->customfield;
  29.        
  30.         //Insert New WP Post
  31.         $post_array = array();
  32.         $post_array['post_type'] = 'event';
  33.         $post_array['post_title'] = wp_strip_all_tags($title);
  34.         $post_array['post_content'] = $description;
  35.         $post_array['post_status'] = 'pending';
  36.         $post_array['post_author'] = 1;
  37.         $event['start_ts'] = $sdate;
  38.         $event['end_ts'] = $edate;
  39.         //Save post, register post id in index
  40.         $post_id = wp_insert_post($post_array);
  41.         if( is_wp_error($post_id) || $post_id == 0 ){ $post_id = 999999999999999999; }
  42.         if( $post_id != 999999999999999999 ){
  43.                 $wpdb->query('UPDATE '.EM_EVENTS_TABLE." SET post_id='$post_id' WHERE event_id='{$event['event_id']}'");
  44.                 //meta
  45.                 foreach($event as $meta_key => $meta_val){
  46.                         if( !in_array($meta_key, $post_fields) && $meta_key != 'event_attributes' ){
  47.                                 $event_metas[] = $wpdb->prepare("(%d, '%s', '%s')", array($post_id, '_'.$meta_key, $meta_val));
  48.                         }elseif($meta_key == 'event_attributes'){
  49.                                 $event_attributes = unserialize($meta_val); //from em table it's serialized
  50.                                 if( is_array($event_attributes) ){
  51.                                         foreach($event_attributes as $att_key => $att_val){
  52.                                                 $event_metas[] = $wpdb->prepare("(%d, '%s', '%s')", array($post_id, $att_key, $att_val));
  53.                                         }
  54.                                 }
  55.                         }
  56.                 }
  57.         }
  58.        
  59.         //Find possible existing location via text query
  60.         $query = 'SELECT location_id FROM wp_em_locations WHERE location_name ="'.$location.'"';
  61.         $locresult = mysql_query($query);
  62.                   echo mysql_error();
  63.                 //Set location_id to query returned    
  64.                 if (mysql_num_rows($locresult) > 0) {
  65.                     while ($row = mysql_fetch_assoc($locresult)) {
  66.                                 $location_id = $row['location_id'];
  67.                         }
  68.                 } else { $location_id = NULL; }
  69.        
  70.        
  71.         //Insert INTO EM Table
  72.         $sql = "INSERT INTO wp_em_events (event_owner, event_status, event_name, event_start_time, event_end_time, event_start_date, event_end_date, post_content, event_rsvp, post_id, location_id)
  73.         values (
  74.         1,
  75.         0,
  76.         '".mysql_real_escape_string($title)."' ,
  77.         '".$stime."' ,
  78.         '".$etime."' ,
  79.         '".$sdate."' ,
  80.         '".$edate."' ,
  81.         '".mysql_real_escape_string($description)."',
  82.         0,
  83.         '".$post_id."',
  84.         '".$location_id."'
  85.         )";
  86.                
  87.                 mysql_query($sql);
  88.                 echo mysql_error();
  89.  
  90.         //Display Results
  91.     echo '<div>TITLE: '.$title.'</div>';
  92.         echo '<div>LOCATION: '.$location.'</div>';
  93.         echo '<div>START: '.$sdate.'</div>';
  94.         echo '<div>END: ' .$edate.'</div>';
  95.         echo '<div>START: '.$stime.'</div>';
  96.         echo '<div>END: ' .$etime.'</div>';
  97.         // echo '<div>LINK: ' .$weblink.'</div>';
  98.         // echo '<div>CATEGORY: '.$category.'</div>';
  99.                                 if (mysql_num_rows($locresult) > 0) {
  100.                                     while ($row = mysql_fetch_assoc($locresult)) {
  101.                                                 echo $row['location_id'];
  102.                                         }
  103.                                 } else {
  104.                                     echo "No existing location matched this in the Database";
  105.                                 }
  106.         echo '<hr>';
  107.  
  108. //TODO Delete this for production or tweak to a manageable count
  109. $i++;
  110. if($i==3) break;
  111. }
  112.         //insert the metas in one go, faster than one by one
  113.         if( count($event_metas) > 0 ){
  114.                 $result = $wpdb->query("INSERT INTO ".$wpdb->postmeta." (post_id,meta_key,meta_value) VALUES ".implode(',',$event_metas));
  115.         }
  116.  
  117. //Confirmation
  118. echo "<br><strong>Events have been inserted into the database</strong>";                
  119. ?>
clone this paste RAW Paste Data