/** * {@inheritdoc} */ public function search(QueryInterface $query) { // Get library service $libService = $this->getLibService(); // Build search value $filters = array(); if (!is_null($query->getHashtag())) { $filters[] = '#' . $query->getHashtag(); } if (!is_null($query->getMedia())) { if (in_array('images', $query->getMedia())) { $filters[] = 'filter:images'; } if (in_array('videos', $query->getMedia())) { $filters[] = 'filter:videos'; } } // Build request params $requestParams = array('include_entities' => 'true'); if (count($filters) > 0) { $requestParams['q'] = implode(' ', $filters); } if (!is_null($query->getFrom())) { $requestParams['since_id'] = $query->getFrom()->getId(); } if (!is_null($query->getTo())) { $requestParams['max_id'] = $query->getTo()->getId(); } if (!is_null($query->getNumResults())) { $requestParams['count'] = $query->getNumResults(); } else { $requestParams['count'] = 100; } // Build request url $requestUrl = 'search/tweets.json?' . http_build_query($requestParams, null, '&', PHP_QUERY_RFC3986); // Search api try { $response = new Response($libService->request($requestUrl), 'json'); } catch (\Exception $e) { $this->propogateException($e); } // Parse response $parser = new TwitterParser($response); $parser->parseSearchTweets(); return $parser->getPosts(); }
/** * {@inheritdoc} */ public function search(QueryInterface $query) { // Get library service $libService = $this->getLibService(); // Build request params $requestParams = array(); $qFrom = $query->getFrom(); $qTo = $query->getTo(); if (is_null($query->getHashtag())) { if (!is_null($qFrom)) { $requestParams['min_timestamp'] = $qFrom->getCreated(); } if (!is_null($qTo)) { $requestParams['max_timestamp'] = $qTo->getCreated(); } } else { if (!is_null($qFrom)) { // Return media before/newer than this min ID $paginationId = $qFrom->getPaginationId(); $paginationId = is_null($paginationId) ? $qFrom->getId() : $paginationId; $requestParams['min_tag_id'] = $paginationId; } if (!is_null($qTo)) { // Return media after/older than this max ID $paginationId = $qTo->getPaginationId(); $paginationId = is_null($paginationId) ? $qTo->getId() : $paginationId; $requestParams['max_tag_id'] = $qTo->getPaginationId(); } if (!is_null($query->getNumResults())) { $requestParams['count'] = $query->getNumResults(); } } // Build request url if (is_null($query->getHashtag())) { $requestUrl = 'media/search?'; $parseMethod = 'parseMediaSearch'; } else { $requestUrl = 'tags/' . urlencode($query->getHashtag()) . '/media/recent?'; $parseMethod = 'parseTagsMediaRecent'; } $requestUrl .= http_build_query($requestParams, null, '&', PHP_QUERY_RFC3986); // Search api try { $response = new Response($libService->request($requestUrl), 'json'); } catch (\Exception $e) { $this->propogateException($e); } // Parse response $parser = new InstagramParser($response); $parser->{$parseMethod}(); $posts = $parser->getPosts(); // Filter posts $filteredPosts = array(); if (!is_null($query->getMedia())) { $mediaTypes = array(); foreach ($query->getMedia() as $media) { switch ($media) { case 'images': $mediaTypes[] = 'image'; break; case 'videos': $mediaTypes[] = 'video'; break; } } foreach ($posts as $i => $post) { if (in_array($post->getMediaType(), $mediaTypes)) { $filteredPosts[$i] = $post; } } } else { $filteredPosts = $posts; } return $filteredPosts; }