Skip to content

Instantly share code, notes, and snippets.

@alanboy
Created June 27, 2014 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanboy/795bbc1430a31ba27b6e to your computer and use it in GitHub Desktop.
Save alanboy/795bbc1430a31ba27b6e to your computer and use it in GitHub Desktop.
trunk/spiders/foursquare.php
<?php
>----include("../server/bootstrap.php");
>----define("OAUTH" , "");
>----define("SPIDER_LAST_UPDATE" , "20110905");
>----$options = array('http'=>array(
>---->----'method'=>"GET",·
>---->----'header'=>"Accept-language: en\r\n"
>---->----."Accept: application/json\r\n"
>---->----."Content-Type: application/json\r\n" ));
>---->----
>----
>----
>----$contexto = stream_context_create($options);
function getVenues($ciudad_id, $lat, $long){
>----global $contexto;
>----$query = "https://api.foursquare.com/v2/venues/search"
>---->---->---- . "?ll=" .$lat. "," . $long
>---->---->---- . "&limit=150"
>---->---->---- . "&oauth_token=" . OAUTH
>---->---->---- . "&v=" . SPIDER_LAST_UPDATE;
>----$response = json_decode(file_get_contents( $query , false, $contexto ));
>----foreach( $response->response->venues as $venue ){
>---->----
>---->----// $id_foursquare = $venue->id;
>---->----$v = new Venues();
>---->----$v->setIdCiudad>----($ciudad_id);
>---->----$v->setLng >---->----($venue->location->lng);
>---->----$v->setLat >---->----($venue->location->lat);
>---->----$nq = VenuesDAO::search( $v );
>---->----if(sizeof($nq) > 0){
>---->---->----echo "Updating ". $venue->name . " ...";
>---->---->----$v->setIdVenue( $nq[0]->getIdVenue() );
>---->----}else{
>---->---->----echo "Creating ". $venue->name . " ...";
>---->----}
>---->----if(isset($venue->categories[0]->icon) == 0){
>---->---->----$v->setImageUrl>----('');
>---->----}else{
>---->---->----$v->setImageUrl>----($venue->categories[0]->icon);
>---->----}
>---->----$v->setName >---->----(addslashes($venue->name));
>---->----$v->setTrendingLevel(-1);
>---->----$v->setUsersCount($venue->stats->checkinsCount);
>---->----if(sizeof($venue->categories) > 0)
>---->---->----$v->setCategory>----(addslashes($venue->categories[0]->name));
>---->----$v->setDescription>----(addslashes($venue->name));
>---->----$v->setPublic>----(1);
>---->----try{
>---->---->----VenuesDAO::save($v);
>---->----}catch(Exception $e){
>---->---->----echo "[error]\n";
>---->---->----echo "------- Exception infomation ------\n";
>---->---->----echo $e . "\n";
>---->---->----echo "------- Exception infomation ------\n";
>---->---->----continue;
>---->----}
>---->----
>---->----echo "[done]\n";
>----}
}
$sql = "select id_ciudad, nombre, lat, lng from ciudad order by popularidad desc;";
$query = mysql_query($sql) or die(mysql_error());
while($line = mysql_fetch_assoc($query)){
>----if($line["lat"] == 0 || $line["lng"] == 0){
>---->----echo $line["nombre"] . " no tiene coordenadas validas !\n";>----
>----}else{
>---->----echo "\n\n------- ------\n";
>---->----echo "Buscando " .·$line["nombre"] . "\n";
>---->----echo "------- ------\n";
>---->----
>---->----getVenues( $line["id_ciudad"], $line["lat"], $line["lng"] );
>----}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment