Skip to content

Instantly share code, notes, and snippets.

@jmoz
Last active September 27, 2015 01:38
Show Gist options
  • Select an option

  • Save jmoz/1191678 to your computer and use it in GitHub Desktop.

Select an option

Save jmoz/1191678 to your computer and use it in GitHub Desktop.
Symfony2 Foursquare venuehistory command
<?php
namespace JMOZ\FoursquareBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use JMOZ\FoursquareBundle\Entity\Checkin;
/**
* Pull 4sq venuehistory into checkins
* @author James Morris <james@jmoz.co.uk>
*/
class CheckinsCommand extends ContainerAwareCommand {
protected function configure() {
$this->setName('4sq:checkins')
->setDescription('Pull venue history from Foursquare into checkins table')
->addArgument('access_token', InputArgument::OPTIONAL, 'oauth access_token (optional, defaults to 4sq.access_token parameter)')
->addOption('no-truncate', $shortcut = null, InputOption::VALUE_NONE, 'Skip TRUNCATE table')
->setHelp(<<<EOT
The <info>4sq:checkins</info> command hits the 4sq venuehistory endpoint and populates
the checkins table.
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output) {
$access_token = $input->getArgument('access_token') ?: $this->getContainer()->getParameter('4sq.access_token');
$response = @file_get_contents(sprintf('https://api.foursquare.com/v2/users/%s/venuehistory?oauth_token=%s', $this->getContainer()->getParameter('4sq.my_user_id'), $access_token));
if (!$response) {
throw new \Exception("Error requesting venuehistory");
}
$responseDecoded = json_decode($response, true);
$em = $this->getContainer()->get('doctrine')->getEntityManager();
// (optionally) truncate table first
if (!$input->getOption('no-truncate')) {
$em->getConnection()->exec($em->getConnection()->getDatabasePlatform()->getTruncateTableSql('checkins'));
}
foreach ($responseDecoded['response']['venues']['items'] as $key => $item) {
$checkin = new Checkin();
$checkin->setCount($item['beenHere']);
$checkin->setVenue($item['venue']['name']);
$checkin->setLat($item['venue']['location']['lat']);
$checkin->setLng($item['venue']['location']['lng']);
$em->persist($checkin);
}
$em->flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment