Last active
August 29, 2015 14:03
-
-
Save Hounddog/40fea18f6815648fcb3a to your computer and use it in GitHub Desktop.
Search Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace API\Service | |
use HD\Api\Client\Client; | |
abstract class AbstractSearch | |
{ | |
protected $client; | |
public function __construct(Client $client) | |
{ | |
$this->client = $client; | |
} | |
private function fetchAPIResults($token){} | |
private function prepareQuery($query){} | |
private function processResults($results, $tokens){} | |
public function search($query) | |
{ | |
$query = $this->prepareQuery($query); | |
$tokens = $this->prepareTokens($query); | |
$results = $this->fetchTokenResults($tokens); | |
$results = $this->processResults($results, $tokens); | |
return $results; | |
} | |
private function prepareTokens($query) | |
{ | |
$tokens = explode('AND', $query); | |
foreach($tokens as $key => $value) { | |
$orTokens = explode('OR', $value); | |
if(count($orTokens) >1) { | |
$tokens[$key] = $orTokens; | |
} | |
} | |
return $tokens; | |
} | |
private function fetchTokenResults($tokens) | |
{ | |
if(is_array($tokens[0])) { | |
foreach($tokens[0] as $token) { | |
array_merge($this->fetchResults(trim($token)), $results); | |
} | |
} else { | |
$results = $this->fetchResults(trim($tokens[0])); | |
} | |
return $results; | |
} | |
private function fetchResults($token) | |
{ | |
$fetchResults = $this->fetchAPIResults($token) | |
$counter =1; | |
$results = array(); | |
foreach($fetchResults as $result) | |
{ | |
if($counter++ > 19) { | |
break; | |
} | |
$results[] = $result; | |
} | |
return $results; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Zend Framework (http://framework.zend.com/) | |
* | |
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository | |
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | |
* @license http://framework.zend.com/license/new-bsd New BSD License | |
*/ | |
namespace Instagram\Controller; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use Zend\View\Model\ViewModel; | |
use TwitterOAuth\TwitterOAuth; | |
use Instagram\Service; | |
class IndexController extends AbstractActionController | |
{ | |
protected $service; | |
protected $itemMapper; | |
public function __construct($service) | |
{ | |
$this->service = $service; | |
} | |
public function indexAction() | |
{ | |
/*$tag = $this->params('tag'); | |
$max = $this->params('max'); | |
$this->service->fetch($tag, $max); | |
return new ViewModel();*/ | |
} | |
public function subscribeAction() | |
{ | |
$tag= $this->params('tag'); | |
$result = $this->service->subscribe($tag); | |
var_dump($result); | |
exit; | |
} | |
// List realtime subscriptions | |
public function listAction() | |
{ | |
$listings = $this->service->listSubscription(); | |
print_r($listings); | |
exit; | |
} | |
public function searchAction() | |
{ | |
$sm = $this->getServiceLocator(); | |
$instagram = $sm->get('API\Service\InstagramSearch'); | |
$results = $instagram->search('welovehh wirsindhamburg'); | |
foreach($results as $result) | |
{ | |
print_r($result->tags); | |
} | |
} | |
private function filterInstagramDuplicates() | |
{ | |
$sm = $this->getServiceLocator(); | |
$em = $sm->get('doctrine.entitymanager.orm_default'); | |
$itemService = $sm->get('page_service_item'); | |
$this->itemMapper = $itemService->getMapper(); | |
if($this->itemMapper->findByItemId($result->id, 'instagram', $site->getId())){ | |
echo 'found duplicate' . "\r\n"; | |
continue; | |
} | |
} | |
private function save($results) | |
{ | |
$sm = $this->getServiceLocator(); | |
$em = $sm->get('doctrine.entitymanager.orm_default'); | |
$entities = array(); | |
foreach($results as $result) | |
{ | |
if($counter++ > 19) { | |
break; | |
} | |
$site = $tag->getSite(); | |
if($this->itemMapper->findByItemId($result->id, 'instagram', $site->getId())){ | |
echo 'found duplicate' . "\r\n"; | |
continue; | |
} | |
$objDateTime = new \DateTime(Date('c', ($result->created_time))); | |
$entity = new \API\V1\Entity\Item; | |
$entity->setType('instagram'); | |
$entity->setItemId($result->id); | |
$entity->setObject($result); | |
$entity->setCreatedDate($objDateTime); | |
$entity->setSite($site); | |
$em->persist($entity); | |
$entities[] = $entity; | |
} | |
$em->flush(); | |
foreach ($entities as $entity) { | |
$em->detach($entity); | |
} | |
} | |
public function refreshAction() | |
{ | |
$sm = $this->getServiceLocator(); | |
$em = $sm->get('doctrine.entitymanager.orm_default'); | |
$itemService = $sm->get('page_service_item'); | |
$this->itemMapper = $itemService->getMapper(); | |
$siteRepository = $em->getRepository('API\\V1\\Entity\\Site'); | |
$sites = $siteRepository->findAll(); | |
foreach($sites as $site) { | |
$hashtags = $site->getHashtags(); | |
$tags = array(); | |
foreach ($hashtags as $tag) { | |
$tags[$tag->getType()][] = $tag; | |
} | |
foreach($tags as $key => $value) | |
{ | |
$func = 'get' . ucfirst($key); | |
$this->$func($value); | |
} | |
} | |
} | |
public function getVine($hashtags) | |
{ | |
echo 'getting vine'. "\r\n"; | |
$sm = $this->getServiceLocator(); | |
$em = $sm->get('doctrine.entitymanager.orm_default'); | |
$client = $sm->get('HD\Vine\Client'); | |
foreach($hashtags as $tag) { | |
$em->merge($tag); | |
$results = $client->api('tags')->recent($tag->getName()); | |
$counter = 1; | |
$entities = array(); | |
foreach($results as $result) | |
{ | |
if($counter++ > 19) { | |
break; | |
} | |
$site = $tag->getSite(); | |
if($this->itemMapper->findByItemId($result->postId, 'vine', $site->getId())){ | |
echo 'found duplicate' . "\r\n"; | |
continue; | |
} | |
$objDateTime = new \DateTime($result->created); | |
$entity = new \API\V1\Entity\Item; | |
$entity->setType('vine'); | |
$entity->setItemId($result->id); | |
$entity->setObject($result); | |
$entity->setCreatedDate($objDateTime); | |
$entity->setSite($site); | |
$em->persist($entity); | |
$entities[] = $entity; | |
} | |
$em->flush(); | |
foreach ($entities as $entity) { | |
$em->detach($entity); | |
} | |
} | |
} | |
public function getInstagram($hashtags) | |
{ | |
echo 'getting instagram'. "\r\n"; | |
$sm = $this->getServiceLocator(); | |
$em = $sm->get('doctrine.entitymanager.orm_default'); | |
$client = $sm->get('HD\Instagram\Client'); | |
foreach($hashtags as $tag) { | |
$results = $client->api('tags')->recent($tag->getName()); | |
$counter = 1; | |
$entities = array(); | |
foreach($results as $result) | |
{ | |
if($counter++ > 19) { | |
break; | |
} | |
$site = $tag->getSite(); | |
if($this->itemMapper->findByItemId($result->id, 'instagram', $site->getId())){ | |
echo 'found duplicate' . "\r\n"; | |
continue; | |
} | |
$objDateTime = new \DateTime(Date('c', ($result->created_time))); | |
$entity = new \API\V1\Entity\Item; | |
$entity->setType('instagram'); | |
$entity->setItemId($result->id); | |
$entity->setObject($result); | |
$entity->setCreatedDate($objDateTime); | |
$entity->setSite($site); | |
$em->persist($entity); | |
$entities[] = $entity; | |
} | |
$em->flush(); | |
foreach ($entities as $entity) { | |
$em->detach($entity); | |
} | |
} | |
} | |
public function getTwitter($hashtags) | |
{ | |
echo 'getting twitter'. "\r\n"; | |
$sm = $this->getServiceLocator(); | |
$em = $sm->get('doctrine.entitymanager.orm_default'); | |
$client = $sm->get('HD\Twitter\Client'); | |
foreach($hashtags as $tag) { | |
$results = $client->api('search')->show($tag->getName()); | |
$counter = 1; | |
$entities = array(); | |
foreach($results as $result) | |
{ | |
if($counter++ > 99) { | |
break; | |
} | |
$site = $tag->getSite(); | |
if($this->itemMapper->findByItemId($result->id, 'twitter', $site->getId())){ | |
echo 'found duplicate' . "\r\n"; | |
continue; | |
} | |
$objDateTime = \DateTime::createFromFormat('D M d H:i:s T Y', $result->created_at); | |
$entity = new \API\V1\Entity\Item; | |
$entity->setType('twitter'); | |
$entity->setItemId($result->id); | |
$entity->setObject($result); | |
$entity->setCreatedDate($objDateTime); | |
$entity->setSite($site); | |
$em->persist($entity); | |
$entities[] = $entity; | |
} | |
$em->flush(); | |
foreach ($entities as $entity) { | |
$em->detach($entity); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace API\Service; | |
class InstagramSearch extends AbstractSearch | |
{ | |
private function processResults($results, $tokens) | |
{ | |
foreach($tokens as $token) { | |
foreach($results as $key => $result) { | |
if(is_array($token)) { | |
$found = false; | |
foreach($token as $or) { | |
if(in_array(trim($or), $result->tags)) { | |
$found = true; | |
} | |
} | |
if(!$found) { | |
unset($results[$key]); | |
} | |
} else { | |
if(!in_array(trim($token), $result->tags)) { | |
unset($results[$key]); | |
}; | |
} | |
} | |
} | |
return $results; | |
} | |
private function fetchAPIResults($token) | |
{ | |
return $this->client->api('tags')->recent($token); | |
} | |
private function prepareQuery($query) | |
{ | |
$query = str_replace(' ', ' * ', $query); | |
$query = str_replace(' * OR * ', ' OR ', $query); | |
$query = str_replace(' * ', ' AND ', $query); | |
return $query; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment