What is this article about?

This document gives an example on how to extract facebook events using a Drupal 7 module

What does this document cover?

Why do I need to extract facebook events?

I have built a website devote to Turkish Jazz. I am listing Jazz events in town, doing that manually is a lot of work. Since nearly all the venues are sharing the events on FB, I have decided to grab the data from their facebook pages. Using a single API (facebook graph API) also reduces the amount of work for different venues.

Requirements

The module


function YOUR_MODULE_menu(){
  $items = array();
  
  $items['events_importer'] = array( 
      'title' => 'event importer',
      'description' => 'super event importer',
      'page callback' => 'YOUR_MODULE_event_importer', 
      'access callback' => TRUE,
      'type' => MENU_NORMAL_ITEM,
  );
  
  return $items;
}

YOUR_MODULE_menu will create a url like /events_importer


/**
 * Implements hook_fboauth_actions().
 */
function YOUR_MODULE_fboauth_actions() {
  $actions['YOUR_MODULE_event_import'] = array(
    'title' => t('Event import'),
    'callback' => 'YOUR_MODULE_fboauth_action_event_import',
  );
  return $actions;
}

YOUR_MODULE_fboauth_actions hooks an action, you may leave it as it is.


function YOUR_MODULE_fboauth_action_event_import($app_id, $access_token) {

  $venues = YOUR_MODULE_venue_fb_ids();
  
  $tsv = "venue	title	musicians	image	startdate	enddate	description	cost	id\n";
  
  foreach ($venues as $venue_name=>$venue_fb_id) {
    $page_events = fboauth_graph_query('/'.$venue_fb_id.'/events?time_filter=upcoming&fields=id,cover,description,end_time,start_time,name', $access_token);
    foreach ($page_events->data as $event_obj) {
      $event_data = YOUR_MODULE_individual_event($venue_name,$event_obj,$access_token);

      if($event_data["musicians"]!=="") {
        $tsv .= $event_data["venue"] . "\t" . $event_data["name"] . "\t" . $event_data["musicians"] . "\t" . $event_data["picture"] . "\t" . $event_data["start_time"] . "\t" . $event_data["end_time"]  . "\t" . /*$event_data["description"]*/ "  "  . "\t" . $event_data["cost"] . "\t" . $event_data["id"] . "\n";
      }
    }
  }
  file_save_data($tsv,'public://event_importer.tsv',$replace = FILE_EXISTS_RENAME);
  drupal_goto("/events_importer");
  
}

YOUR_MODULE_fboauth_action_event_import hook action, trig this function where all the magic happens.


function YOUR_MODULE_individual_event($venue,$event_obj,$access_token) {
  $description = trim(preg_replace('/\s+/', ' ', $event_obj->description));
  $musicians = YOUR_MODULE_populate_musicians($description);
  $cost = YOUR_MODULE_populate_cost($description);
  $event_data = array(
  	  "description" => $description,
      "end_time" => $event_obj->end_time,
      "start_time" => $event_obj->start_time,
      "name" => $event_obj->name,
      "id" => $event_obj->id,
      "picture" => $event_obj->cover->source,
      "venue" => $venue,
      "musicians" => $musicians,
      "cost" => $cost
  );
  return $event_data;
}

YOUR_MODULE_individual_event format the event data.


function YOUR_MODULE_populate_musicians($description) {
  $raw = drupal_http_request("https://www.turkishjazz.org/artists-json");
  $raw_arr = json_decode($raw->data);
  
  $musicians = "";
  foreach ($raw_arr as $artist) {
  	if(strpos($description,$artist->title) !== false) {
  	  $musicians .= $artist->title . ",";
  	}
  }
  $musicians = rtrim($musicians,",");
  
  return $musicians;
}

YOUR_MODULE_populate_musicians format the event data.


function YOUR_MODULE_populate_cost($description) {
  $event_cost_data = preg_match_all("/(:|\s|:\s|;|;\s)(\d{2,3})(\s|.|.-|\s.-|\s.-\s)TL/i",$description,$matches);
  $event_cost = "-";
  if($matches[2][0]>0) {
    $event_cost = $matches[2][0] . "USD";
  }
  return $event_cost;
}

YOUR_MODULE_populate_cost format the event data.


function YOUR_MODULE_event_importer() {
  return fboauth_action_display('YOUR_MODULE_event_import');
}

YOUR_MODULE_event_importer the event importer page.



function YOUR_MODULE_venue_fb_ids() {
  
  $venues = array(
  	  "The Badau İstanbul" => 167317496965160,
      "Nardis Jazz" => 8482520218,
      "Kaset Mitanni" => 215803695103308,
      "Tamirane" => 210613578979409,
      "DasDas" => 136827509664438,
      "Zorlu PSM Studio" => 132312760706407,
      "Jazz Company/ Elite World İstanbul Hotel" => 272245359480852,
      "Moda Kayıkhane" => 141314409760781,
      "Living Room" => 169035839811731,
      "BOVA" => 179019409325003
  );
  return $venues;
}

YOUR_MODULE_venue_fb_ids manually enter the venue FB IDs in this array.

Now visit /events_importer, click on the button, then it will create a file called “event_importer.tsv” in your public files directory. You can use this tsv file to create a feed to automate the import process by using Drupal Feeds module.