Skip to content

Instantly share code, notes, and snippets.

@thomasv314
Forked from acidtib/save.php
Created December 6, 2012 21:30
Show Gist options
  • Save thomasv314/4228657 to your computer and use it in GitHub Desktop.
Save thomasv314/4228657 to your computer and use it in GitHub Desktop.
<?php
include('lib/crane.php');
$token = "XXXXXXXXXXXXXXXXXXXXXXXX";
$api = "https://api.foursquare.com/v2";
$api_v = "20120321";
$userinfo = file_get_contents("https://api.foursquare.com/v2/users/self?oauth_token=".$token);
$decoded_userinfo = json_decode($userinfo, true);
$name = $decoded_userinfo;
function parse_empty_string($hash, $variable, $default = "") {
if (!empty($hash[$variable]) {
return $hash[$variable];
} else {
return $default;
}
}
?>
<html>
<head>
<title>fugu - foursquare</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
$managed = file_get_contents("".$api."/venues/managed?v=".$api_v."&oauth_token=".$token);
$decoded_managed = json_decode($managed, true);
$user_managed = $decoded_managed['response']['venues'];
foreach ($user_managed as $manage) {
//Save the venues alocated to the account
$manage_id = parse_empty_string($manage, 'id');
$manage_name = parse_empty_string($manage, 'name');
$manage_canonicalUrl = parse_empty_string($manage, 'canonicalUrl');
$manage_categories_name = parse_empty_string($manage['categories'][0], 'name');
$manage_categories_icon = parse_empty_string($manage['categories'][0]['icon'], 'prefix');
mysql_query("INSERT INTO foursquare_venues (venue_id, name, url, cat_name, icon, timestamp)
VALUES (
'".mysql_real_escape_string($manage_id)."',
'".mysql_real_escape_string($manage_name)."',
'".mysql_real_escape_string($manage_canonicalUrl)."',
'".mysql_real_escape_string($manage_categories_name)."',
'".mysql_real_escape_string($manage_categories_icon)."',
NOW() ) ON DUPLICATE KEY UPDATE
venue_id='".mysql_real_escape_string($manage_id)."',
name='".mysql_real_escape_string($manage_name)."',
url='".mysql_real_escape_string($manage_canonicalUrl)."',
cat_name='".mysql_real_escape_string($manage_categories_name)."',
icon='".mysql_real_escape_string($manage_categories_icon)."',
timestamp=NOW() ")
or die(mysql_error());
//Get venues stats ALL TIME
$stats = file_get_contents("".$api."/venues/".$manage['id']."/stats?v=".$api_v."&oauth_token=".$token);
$decoded_stats = json_decode($stats, true);
$stat = $decoded_stats['response']['stats'];
$stat_totalCheckins = parse_empty_string($stat, 'totalCheckins');
$topvisitor_firstname = parse_empty_string($stat['topVisitors'][0]['user'], 'firstName');
$topvisitor_lastname = parse_empty_string($stat['topVisitors'][0]['user'], 'lastName');
$topvisitor_photo = parse_empty_string($stat['topVisitors'][0]['user'], 'photo');
$stat_uniqueVisitors = parse_empty_string($stat, 'uniqueVisitors', "0");
$stat_newCheckins = parse_empty_string($stat, 'newCheckins', "0");
$stat_genderBreakdown_male = parse_empty_string($stat['genderBreakdown'], 'male', "0");
$stat_genderBreakdown_female = parse_empty_string($stat['genderBreakdown'], 'female', "0");
$stat_ageBreakdown_0 = parse_empty_string($stat['ageBreakdown'][0], 'checkins', "0");
$stat_ageBreakdown_1 = parse_empty_string($stat['ageBreakdown'][1], 'checkins', "0");
$stat_ageBreakdown_2 = parse_empty_string($stat['ageBreakdown'][2], 'checkins', "0");
$stat_ageBreakdown_3 = parse_empty_string($stat['ageBreakdown'][3], 'checkins', "0");
$stat_ageBreakdown_4 = parse_empty_string($stat['ageBreakdown'][4], 'checkins', "0");
$stat_ageBreakdown_5 = parse_empty_string($stat['ageBreakdown'][5], 'checkins', "0");
$averagecheckin_all = substr($stat['totalCheckins'] / 30, 0, 1);
mysql_query("INSERT INTO foursquare_all_time (venue_id, totalcheckins, mayor, mayor_avatar, uniquevisitors, averagecheckins, newvisitors, male, female, 13_17, 18_24, 25_34, 35_44, 45_54, 55mas, timestamp)
VALUES (
'".mysql_real_escape_string($manage_id)."',
'".mysql_real_escape_string($stat_totalCheckins)."',
'".mysql_real_escape_string($topvisitor_firstname ." ". $topvisitor_lastname)."',
'".mysql_real_escape_string($topvisitor_photo)."',
'".mysql_real_escape_string($stat_uniqueVisitors)."',
'".$averagecheckin_all."',
'".mysql_real_escape_string($stat_newCheckins)."',
'".mysql_real_escape_string($stat_genderBreakdown_male)."',
'".mysql_real_escape_string($stat_genderBreakdown_female)."',
'".mysql_real_escape_string($stat_ageBreakdown_0)."',
'".mysql_real_escape_string($stat_ageBreakdown_1)."',
'".mysql_real_escape_string($stat_ageBreakdown_2)."',
'".mysql_real_escape_string($stat_ageBreakdown_3)."',
'".mysql_real_escape_string($stat_ageBreakdown_4)."',
'".mysql_real_escape_string($stat_ageBreakdown_5)."',
NOW() ) ON DUPLICATE KEY UPDATE
venue_id='".mysql_real_escape_string($manage_id)."',
totalcheckins='".mysql_real_escape_string($stat_totalCheckins)."',
mayor='".mysql_real_escape_string($topvisitor_firstname ." ". $topvisitor_lastname)."',
mayor_avatar='".mysql_real_escape_string($topvisitor_photo)."',
uniquevisitors='".mysql_real_escape_string($stat_uniqueVisitors)."',
averagecheckins='".$averagecheckin_all."',
newvisitors='".mysql_real_escape_string($stat_newCheckins)."',
male='".mysql_real_escape_string($stat_genderBreakdown_male)."',
female='".mysql_real_escape_string($stat_genderBreakdown_female)."',
13_17='".mysql_real_escape_string($stat_ageBreakdown_0)."',
18_24='".mysql_real_escape_string($stat_ageBreakdown_1)."',
25_34='".mysql_real_escape_string($stat_ageBreakdown_2)."',
35_44='".mysql_real_escape_string($stat_ageBreakdown_3)."',
45_54='".mysql_real_escape_string($stat_ageBreakdown_4)."',
55mas='".mysql_real_escape_string($stat_ageBreakdown_5)."',
timestamp=NOW() ")
or die(mysql_error());
//Get todays data
$one = file_get_contents("".$api."/venues/".$manage['id']."/stats?v=".$api_v."&startAt=".date_create(date('m/d/Y', strtotime('-1 day')))->format('U')."&endAt=".date_create(date('m/d/Y'))->format('U')."&oauth_token=".$token);
$decoded_one = json_decode($one, true);
$one_day = $decoded_one['response']['stats'];
$one_day_totalCheckins = parse_empty_string($one_day, 'totalCheckins', "0");
$one_day_topvisitor_firstname = parse_empty_string($one_day['topVisitors'][0]['user'], 'firstName');
$one_day_topvisitor_lastName = parse_empty_string($one_day['topVisitors'][0]['user'], 'lastName');
$one_day_topvisitor_photo = parse_empty_string($one_day['topVisitors'][0]['user'], 'photo');
$one_day_uniqueVisitors = parse_empty_string($one_day, 'uniqueVisitors');
$one_day_newCheckins = parse_empty_string($one_day, 'newCheckins');
$one_day_genderBreakdown_male = $one_day['genderBreakdown']['male'];
$one_day_genderBreakdown_female = $one_day['genderBreakdown']['female'];
$one_day_ageBreakdown_0 = $one_day['ageBreakdown'][0]['checkins'];
$one_day_ageBreakdown_1 = $one_day['ageBreakdown'][1]['checkins'];
$one_day_ageBreakdown_2 = $one_day['ageBreakdown'][2]['checkins'];
$one_day_ageBreakdown_3 = $one_day['ageBreakdown'][3]['checkins'];
$one_day_ageBreakdown_4 = $one_day['ageBreakdown'][4]['checkins'];
$one_day_ageBreakdown_5 = $one_day['ageBreakdown'][5]['checkins'];
mysql_query("INSERT INTO foursquare_stats_1_day (venue_id, totalcheckins, mayor, mayor_avatar, uniquevisitors, newvisitors, male, female, 13_17, 18_24, 25_34, 35_44, 45_54, 55mas, timestamp)
VALUES (
'".mysql_real_escape_string($manage_id)."',
'".mysql_real_escape_string($one_day_totalCheckins)."',
'".mysql_real_escape_string($one_day_topvisitor_firstname ." ". $one_day_topvisitor_lastName)."',
'".mysql_real_escape_string($one_day_topvisitor_photo)."',
'".mysql_real_escape_string($one_day_uniqueVisitors)."',
'".mysql_real_escape_string($one_day_newCheckins)."',
'".mysql_real_escape_string($one_day_genderBreakdown_male)."',
'".mysql_real_escape_string($one_day_genderBreakdown_female)."',
'".mysql_real_escape_string($one_day_ageBreakdown_0)."',
'".mysql_real_escape_string($one_day_ageBreakdown_1)."',
'".mysql_real_escape_string($one_day_ageBreakdown_2)."',
'".mysql_real_escape_string($one_day_ageBreakdown_3)."',
'".mysql_real_escape_string($one_day_ageBreakdown_4)."',
'".mysql_real_escape_string($one_day_ageBreakdown_5)."',
NOW() ) ")
or die(mysql_error());
//Get venues stats LAST 30 DAYS//
$stats30 = file_get_contents("".$api."/venues/".$manage['id']."/stats?v=".$api_v."&startAt=".date_create(date('m/d/Y', strtotime('-30 day')))->format('U')."&endAt=".date_create(date('m/d/Y'))->format('U')."&oauth_token=".$token);
$decoded_stats30 = json_decode($stats30, true);
$stat30 = $decoded_stats30['response']['stats'];
$averagecheckin = substr($stat30['totalCheckins'] / 30, 0, 1);
$stat30_totalCheckins = $stat30['totalCheckins'];
$stat30_topVisitors_firstname = $stat30['topVisitors'][0]['user']['firstName'];
$stat30_topVisitors_lastName = $stat30['topVisitors'][0]['user']['lastName'];
$stat30_topVisitors_photo = $stat30['topVisitors'][0]['user']['photo'];
$stat30_topVisitors_uniqueVisitors = $stat30['uniqueVisitors'];
$stat30_topVisitors_newCheckins = $stat30['newCheckins'];
$stat30_genderBreakdown_male = $stat30['genderBreakdown']['male'];
$stat30_genderBreakdown_female = $stat30['genderBreakdown']['female'];
$stat30_ageBreakdown_0 = $stat30['ageBreakdown'][0]['checkins'];
$stat30_ageBreakdown_1 = $stat30['ageBreakdown'][1]['checkins'];
$stat30_ageBreakdown_2 = $stat30['ageBreakdown'][2]['checkins'];
$stat30_ageBreakdown_3 = $stat30['ageBreakdown'][3]['checkins'];
$stat30_ageBreakdown_4 = $stat30['ageBreakdown'][4]['checkins'];
$stat30_ageBreakdown_5 = $stat30['ageBreakdown'][5]['checkins'];
mysql_query("INSERT INTO foursquare_stats_30_days (venue_id, totalcheckins, mayor, mayor_avatar, uniquevisitors, averagecheckins, newvisitors, male, female, 13_17, 18_24, 25_34, 35_44, 45_54, 55mas, timestamp)
VALUES (
'".mysql_real_escape_string($manage_id)."',
'".mysql_real_escape_string($stat30_totalCheckins)."',
'".mysql_real_escape_string($stat30_topVisitors_firstname ." ". $stat30_topVisitors_lastName)."',
'".mysql_real_escape_string($stat30_topVisitors_photo)."',
'".mysql_real_escape_string($stat30_topVisitors_uniqueVisitors)."',
'".$averagecheckin."',
'".mysql_real_escape_string($stat30_topVisitors_newCheckins)."',
'".mysql_real_escape_string($stat30_genderBreakdown_male)."',
'".mysql_real_escape_string($stat30_genderBreakdown_female)."',
'".mysql_real_escape_string($stat30_ageBreakdown_0)."',
'".mysql_real_escape_string($stat30_ageBreakdown_1)."',
'".mysql_real_escape_string($stat30_ageBreakdown_2)."',
'".mysql_real_escape_string($stat30_ageBreakdown_3)."',
'".mysql_real_escape_string($stat30_ageBreakdown_4)."',
'".mysql_real_escape_string($stat30_ageBreakdown_5)."',
NOW() ) ON DUPLICATE KEY UPDATE
venue_id='".mysql_real_escape_string($manage_id)."',
totalcheckins='".mysql_real_escape_string($stat30_totalCheckins)."',
mayor='".mysql_real_escape_string($stat30_topVisitors_firstname ." ". $stat30_topVisitors_lastName)."',
mayor_avatar='".mysql_real_escape_string($stat30_topVisitors_photo)."',
uniquevisitors='".mysql_real_escape_string($stat30_topVisitors_uniqueVisitors)."',
averagecheckins='".$averagecheckin."',
newvisitors='".mysql_real_escape_string($stat30_topVisitors_newCheckins)."',
male='".mysql_real_escape_string($stat30_genderBreakdown_male)."',
female='".mysql_real_escape_string($stat30_genderBreakdown_female)."',
13_17='".mysql_real_escape_string($stat30_ageBreakdown_0)."',
18_24='".mysql_real_escape_string($stat30_ageBreakdown_1)."',
25_34='".mysql_real_escape_string($stat30_ageBreakdown_2)."',
35_44='".mysql_real_escape_string($stat30_ageBreakdown_3)."',
45_54='".mysql_real_escape_string($stat30_ageBreakdown_4)."',
55mas='".mysql_real_escape_string($stat30_ageBreakdown_5)."',
timestamp=NOW() ")
or die(mysql_error());
// Get Tips
$tips = file_get_contents("".$api."/venues/".$manage['id']."/tips?v=".$api_v."&oauth_token=".$token);
$decoded_tips = json_decode($tips, true);
$biz_tips = $decoded_tips['response']['tips'];
$tip = $biz_tips;
foreach ($tip['items'] as $tips) {
if (!empty($tips['user']['lastName'])) {
$apellido = $tips['user']['lastName'];
} else {
$apellido = "";
}
mysql_query("INSERT INTO foursquare_tips (venue_id, tip_id, createdat, user_id, user_name, user_photo, tip, timestamp)
VALUES (
'".mysql_real_escape_string($manage_id)."',
'".mysql_real_escape_string($tips['id'])."',
'".mysql_real_escape_string($tips['createdAt'])."',
'".mysql_real_escape_string($tips['user']['id'])."',
'".mysql_real_escape_string($tips['user']['firstName'] ." ". $apellido)."',
'".mysql_real_escape_string($tips['user']['photo'])."',
'".mysql_real_escape_string($tips['text'])."',
NOW() ) ON DUPLICATE KEY UPDATE
venue_id='".mysql_real_escape_string($manage['id'])."',
tip_id='".mysql_real_escape_string($tips['id'])."',
createdat='".mysql_real_escape_string($tips['createdAt'])."',
user_id='".mysql_real_escape_string($tips['user']['id'])."',
user_name='".mysql_real_escape_string($tips['user']['firstName'] ." ". $apellido)."',
user_photo='".mysql_real_escape_string($tips['user']['photo'])."',
tip='".mysql_real_escape_string($tips['text'])."',
timestamp=NOW() ")
or die(mysql_error());
}
}
//echo date_create(date('m/d/Y', strtotime('-1 day')))->format('U');
//echo "<br>";
//echo date_create(date('m/d/Y'))->format('U');
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment