Skip to content

Instantly share code, notes, and snippets.

Created April 1, 2012 20:11
Show Gist options
  • Select an option

  • Save anonymous/2278294 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/2278294 to your computer and use it in GitHub Desktop.
suggested by meeech
<?php
$test = true;
$dbhost = "localhost:8889";
$dbname = "db";
$dbuser = "dba";
$dbpass = "pw";
$db = dbConnect($dbname);
$plan_id = 2; //1=5k, 2=10k
$query = "SELECT * from plan where plan_id=$plan_id";
$result = mysql_query($query);
while($row = mysql_fetch_object($result)){
$plan = $row;
}
$base = 'https://api.nirvanahq.com/?api=json&requestid=093C76EE-4CC4-4000-9EC1-FEA517763EF3&clienttime=1328732951&authtoken=828ea80a8f8271c783b95931d8448b21&appid=n2desktop&appversion=391%0A';
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
//log in to system
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init("$base");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1) ;
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, '/Users/tneier/Desktop/cookies.txt');
report("about to connect");
$contents = curl_exec($ch);
report("connected");
sleep(2);
flush();
$query = 'SELECT * FROM details WHERE plan_id=1 ORDER BY day';
$race_date = '7/22/2012';
$unixrace = strtotime($race_date);
$startday = $unixrace - ($plan->day_of_race * 60 * 60 * 24);
$result = mysql_query($query);
while($row = mysql_fetch_object($result)){
$thisdate = date($startday + ($row->day * 24 * 60 * 60));
$uuid = genUUID();
$x = '[{"method":"task.save","id":"'.$uuid.'","type":0,"_type":1326222297,';
$x .= '"parentid":"AE983112-2140-4B8A-84D4-844E36BED633","_parentid":1326222297,"waitingfor":"","_waitingfor":1326222297,"state":1,"_state":1326222297,"completed":0,"_completed":1326222297,"cancelled":0,"_cancelled":1326222297,"seq":1326222297,"_seq":1326222297,"seqt":0,"_seqt":1326222297,"seqp":1326222297,"_seqp":1326222297,';
$x .= '"name":"'.$row->text.'","_name":1326222481,"tags":",wharf2wharf,","_tags":1326222481,"note":"note","_note":1326222481,"ps":0,"_ps":1326222297,"etime":"45",';
$x .= '"_etime":1326222481,"energy":"3","_energy":1326222481,"startdate":"","_startdate":1326222297,"duedate":"'. date('Ymd', $thisdate) .'","_duedate":1326222481,"recurring":"","_recurring":1326222297}]';
report($x);
if (!$test){
postToNirvana($x, $ch);
} else {
echo date('Ymd', $thisdate) . $row->text;
echo "<br>";
}
}
function dbConnect($db) {
global $dbhost, $dbuser, $dbpass;
$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass)
or die("The site database appears to be down." . mysql_error());
if ($db!="" and !@mysql_select_db($db))
die("The site database is unavailable.");
return $dbcnx;
}
function postToNirvana($item, &$ch){
curl_setopt($ch, CURLOPT_POSTFIELDS, $item);
$contents = curl_exec($ch);
report($contents);
}
function genUUID(){
$base = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
$ball = '';
//echo strlen($base);
for ($i = 0; $i < strlen($base); $i++){
// echo $i;
$c = substr($base,$i,1);
// echo $c;
if ($c =='x' || $c =='y'){
$r = rand() * 16 | 0;
// echo "r: $r <br>\n";
$r = substr($r, 4, 2);
$v = ($c == 'x') ? $r: ($r & 0x3 | 0x8);
// echo "$v<br>";
$v = substr(dechex($v), 0,1);
$ball .= $v;
} else {
$ball .= $c;
}
}
return strtoupper($ball);
}
function report($message){
error_log($message);
echo "$message \n";
flush();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment