public function consume($filter) { // Save the filter $this->filter = $filter; $definition = new \DataSift_Definition(new \DataSift_User(DATASIFT_USERNAME, DATASIFT_API_KEY), $filter->csdl); // Create the consumer $consumer = $definition->getConsumer(\DataSift_StreamConsumer::TYPE_HTTP, $this); // And start consuming (this will block) $consumer->consume(); }
/** * Subscribe this endpoint to a Definition. * * @param DataSift_Definition $definition The definition to which to subscribe. * @param string $name A name for this subscription. * * @return DataSift_PushSubscription The new subscription. * @throws DataSift_Exception_InvalidData * @throws DataSift_Exception_AccessDenied * @throws DataSift_Exception_APIError */ public function subscribeDefinition($definition, $name) { return $this->subscribeStreamHash($definition->getHash(), $name); }
public function onDisconnect($consumer) { echo 'Disconnected' . PHP_EOL; } /** * Called when the consumer stops for some reason. * * @param DataSift_StreamConsumer $consumer The consumer sending the event. * @param string $reason The reason the consumer stopped. * * @return void */ public function onStopped($consumer, $reason) { echo PHP_EOL . 'Stopped: ' . $reason . PHP_EOL . PHP_EOL; } } // Authenticate echo "Creating user...\n"; $user = new DataSift_User(USERNAME, API_KEY); // Create the definition $csdl = 'interaction.content contains "football"'; echo "Creating definition...\n {$csdl}\n"; $definition = new DataSift_Definition($user, $csdl); // Create the consumer echo "Getting the consumer...\n"; $consumer = $definition->getConsumer(DataSift_StreamConsumer::TYPE_HTTP, new EventHandler()); // And start consuming echo "Consuming...\n--\n"; $consumer->consume(); echo "Finished consuming\n\n";
public function testGetDPUBreakdownOnInvalidDefinition() { $def = new DataSift_Definition($this->user, testdata('invalid_definition')); $this->assertEquals($def->get(), testdata('invalid_definition')); $response = array('response_code' => 400, 'data' => array('error' => 'The target interactin.content does not exist'), 'rate_limit' => 200, 'rate_limit_remaining' => 150); DataSift_MockApiClient::setResponse($response); try { $def->getDPUBreakdown(); $this->fail('CompileFailed exception expected, but not thrown'); } catch (DataSift_Exception_InvalidData $e) { // Check the error message $this->assertEquals($e->getMessage(), $response['data']['error']); } catch (DataSift_Exception_APIError $e) { $this->fail('APIError: ' . $e->getMessage() . ' (' . $e->getCode() . ')'); } catch (Exception $e) { $this->fail('Unhandled exception: ' . $e->getMessage() . ' (' . $e->getCode() . ')'); } }
// Read it from stdin $csdl = ''; while (!feof(STDIN)) { $csdl .= fread(STDIN, 4096); } } $csdl = trim($csdl); if (strlen($csdl) == 0) { die("CSDL is empty\n\n"); } // Authenticate echo "Creating user...\n"; $user = new DataSift_User(USERNAME, API_KEY); // Create the definition echo "Creating definition...\n"; $definition = new DataSift_Definition($user, $csdl); // Get the DPU. This will compile the definition, so we catch potential // errors from that. echo "Getting DPU...\n"; try { $dpu = $definition->getDPUBreakdown(); } catch (DataSift_Exception_CompileFailed $e) { die("CSDL compilation failed: " . $e->getMessage() . "\n\n"); } // Format the DPU details for output in a table $dputable = array(); $maxlength = array('target' => strlen('Target'), 'times used' => strlen('Times used'), 'complexity' => strlen('Complexity')); foreach ($dpu['detail'] as $tgt => $c) { $maxlength['target'] = max($maxlength['target'], strlen($tgt)); $maxlength['times used'] = max($maxlength['times used'], strlen(number_format($c['count']))); $maxlength['complexity'] = max($maxlength['complexity'], strlen(number_format($c['dpu'], 2)));
public function testGetBuffered() { $def = new DataSift_Definition($this->user, testdata('definition')); $response = array('response_code' => 200, 'data' => array('hash' => testdata('definition_hash'), 'created_at' => date('Y-m-d H:i:s', time()), 'dpu' => 10), 'rate_limit' => 200, 'rate_limit_remaining' => 150); DataSift_MockApiClient::setResponse($response); $this->assertEquals($def->get(), testdata('definition'), 'Definition string not set correctly'); $response = array('response_code' => 200, 'data' => array('stream' => array('Test interaction 1', 'Test interaction 2'), 'hash' => testdata('definition_hash'), 'created_at' => date('Y-m-d H:i:s', time()), 'dpu' => 10), 'rate_limit' => 200, 'rate_limit_remaining' => 150); DataSift_MockApiClient::setResponse($response); $interactions = $def->getBuffered(); $this->assertTrue(is_array($interactions), 'Failed to get buffered interactions'); }
* for full details. */ if (function_exists('date_default_timezone_set')) { date_default_timezone_set('UTC'); } // Include the DataSift library require dirname(__FILE__) . '/../lib/datasift.php'; // Include the configuration - put your username and API key in this file require dirname(__FILE__) . '/../config.php'; // Authenticate echo "Creating user...\n"; $user = new DataSift_User(USERNAME, API_KEY); // Create the definition $csdl = 'interaction.content contains "football"'; echo "Creating definition...\n {$csdl}\n"; $definition = new DataSift_Definition($user, $csdl); // Get buffered interactions until we've had 10 echo "Getting buffered interactions...\n--\n"; $num = 10; $from_id = false; do { $interactions = $definition->getBuffered($num, $from_id); foreach ($interactions as $interaction) { echo 'Type: ' . $interaction['interaction']['type'] . "\n"; echo 'Content: ' . $interaction['interaction']['content'] . "\n--\n"; $from_id = $interaction['interaction']['id']; $num--; } if ($num > 0) { // Sleep for 10 seconds before trying to get more echo "Sleeping...\n";