예제 #1
0
 public function testParsePostArray()
 {
     // Get sample response
     $sampleResponse = $this->sampleResponses['post.json'];
     $response = new Response($sampleResponse, 'json');
     // Get parser
     $parser = new InstagramParser($response);
     // Parse data
     $post = $parser->parsePostArray($response->getArray());
     // Test post
     $this->assertInstanceOf('\\Evolution7\\SocialApi\\Entity\\Post', $post);
     $this->assertEquals('3', $post->getId());
     $this->assertEquals('Hello world', $post->getBody());
     $this->assertEquals('image', $post->getMediaType());
     $this->assertEquals('//distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_7.jpg', $post->getMediaUrl());
     $dateTime = new \DateTime();
     $dateTime->setTimestamp(1279340983);
     $this->assertEquals($dateTime, $post->getCreated());
     // Test user
     $user = $post->getUser();
     $this->assertInstanceOf('\\Evolution7\\SocialApi\\Entity\\User', $user);
     $this->assertEquals('3', $user->getId());
     $this->assertEquals('kevin', $user->getHandle());
     $this->assertEquals('http://instagram.com/kevin', $user->getUrl());
     $this->assertEquals('Kevin S', $user->getName());
     $this->assertEquals('...', $user->getImageUrl());
 }
예제 #2
0
 /**
  * {@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;
 }