Permalink
Browse files

My first set of changes:\n - added a "timeframe" option [today|yester…

…day|thismonth|lastmonth],\n - made clicking anywhere on the widget download new stats\n - changed the format of interactions with the php script\n - - it now recieves all params on a single line (for easier commandline testing)\n - - it responds with JSON data
  • Loading branch information...
1 parent 2527666 commit db9cf6099364b78d758720456341abf790c1a1ea @nfriedly committed Mar 8, 2011
Showing with 114 additions and 37 deletions.
  1. +10 −3 MacAdSense.wdgt/MacAdSense.html
  2. +70 −27 MacAdSense.wdgt/MacAdSense.js
  3. +34 −7 MacAdSense.wdgt/MacAdSense.php
@@ -13,15 +13,16 @@
<body onload="setup();">
<div id="front">
+ <div id="big_timeframe"></div>
<div id="front_content">
<div id="top">
<span id="earnings"></span>
</div>
<div id="bottom">
+ <b>Timeframe</b>: <span id="timeframe"></span><br>
<b>Updated</b>: <span id="updated"></span><br>
<b>Clicks</b>: <span id="clicks"></span><br>
- <b>eCPM</b>: <span id="ecpm"></span><br>
- <b>Extrapolated</b>: <span id="extrapolated"></span>
+ <b>eCPM</b>: <span id="ecpm"></span>
</div>
</div>
<div id="dialog"><div id="dialog_content"></div></div>
@@ -31,7 +32,13 @@
<div id="back_content">
<form id="ff" onSubmit="hideBack(); return false;">
<b>Username</b>:<br><input type="text" name="username"><br>
- <b>Password</b>:<br><input type="password" name="password">
+ <b>Password</b>:<br><input type="password" name="password"><br />
+ <select name="timeframe">
+ <option value="today">today</option>
+ <option value="yesterday">yesterday</option>
+ <option value="thismonth">this month</option>
+ <option value="lastmonth">last month</option>
+ </select>
</div>
<div id="version"></div>
<div id="done"></div>
@@ -38,7 +38,8 @@ function showBack()
back.style.display="block";
if (window.widget)
- setTimeout ('widget.performTransition();', 0);
+ setTimeout ('widget.performTransition();', 0);
+
}
@@ -49,33 +50,54 @@ function hideBack()
if (window.widget)
{
- var form=document.getElementById("ff");
-
- if(form.username.value!="" && form.password.value!="")
- {
- widget.setPreferenceForKey(form.username.value, "username");
-
- now=0;
- showDialog("Saving to keychain...");
- var command=widget.system("./MacAdSense.php setcredentials", onshow);
- command.write(form.username.value+"\n");
- command.write(form.password.value+"\n");
- command.close();
-
- form.password.value="";
- }
+ var form=document.getElementById("ff");
+
+
+ if(form.username.value!="" && form.password.value!="" )
+ {
+ widget.setPreferenceForKey(form.username.value, "username");
+
+
+ widget.setPreferenceForKey(form.timeframe.value, "timeframe");
+ document.getElementById('big_timeframe').innerHTML = widget.preferenceForKey("timeframe");
+
+
+ now=0;
+ showDialog("Saving to keychain...");
+ var command=widget.system("./MacAdSense.php setcredentials", onshow);
+ command.write(form.username.value+"\n");
+ command.write(form.password.value+"\n");
+ command.close();
+
+ form.password.value="";
+
+ }
+
+ if(widget.preferenceForKey("timeframe") != form.timeframe.value){
+
+ showDialog("Loading...");
+
+ widget.setPreferenceForKey(form.timeframe.value, "timeframe");
+ document.getElementById('big_timeframe').innerHTML = widget.preferenceForKey("timeframe");
+
+ }
+
+ fetchData();
+
}
if (window.widget)
widget.prepareForTransition("ToFront");
+
back.style.display="none";
front.style.display="block";
if (window.widget)
{
setTimeout ('widget.performTransition();', 0);
+
}
}
@@ -98,23 +120,29 @@ function endHandler()
function fetchData()
{
- var command=widget.system("./MacAdSense.php getdata", displayData);
- command.write(widget.preferenceForKey("username")+"\n");
+ var command=widget.system("./MacAdSense.php getdata " +
+ widget.preferenceForKey("username") + " " +
+ widget.preferenceForKey("timeframe"), displayData);
command.close();
}
function displayData(data)
{
- output=data.outputString.split("#");
+ //output=data.outputString.split("#");
+
+ output = JSON.parse(data.outputString);
// do we get real data
- if(output[1]!=0)
+ //if(output[1]!=0)
+ if(output.time)
{
- document.getElementById("updated").innerHTML=output[0];
- document.getElementById("extrapolated").innerHTML="$"+output[1];
- document.getElementById("clicks").innerHTML=output[3];
- document.getElementById("earnings").innerHTML="$"+output[2];
- document.getElementById("ecpm").innerHTML=output[5];
+
+ document.getElementById("updated").innerHTML=output.time; // 0
+ document.getElementById("timeframe").innerHTML=output.input.timeframe; //1
+ //document.getElementById("extrapolated").innerHTML="$"+output[1];
+ document.getElementById("clicks").innerHTML=output.clicks; //3
+ document.getElementById("earnings").innerHTML="$"+output.usd; // 2
+ document.getElementById("ecpm").innerHTML="$"+output.ecpm; //5
now = Math.round(new Date().getTime()/1000);
@@ -142,9 +170,11 @@ function onshow()
}
else
{
- showDialog("Loading...");
+ showDialog("Loading...");
setTimeout('fetchData()',100);
+
+ document.getElementById('big_timeframe').innerHTML = widget.preferenceForKey("timeframe");
}
}
else
@@ -166,13 +196,26 @@ function setup()
var form=document.getElementById("ff");
form.username.value=widget.preferenceForKey("username");
+
+ // selecte the appropriate timeframe
+ for(var ops = form.timeframe.options, i=0; i< ops.length; i++){
+ if(ops[i].value == widget.preferenceForKey("timeframe")){
+ ops.selectedIndex = i;
+ }
+ }
+
}
var done_button = new AppleGlassButton(document.getElementById("done"), "Done", hideBack);
i_button = new AppleInfoButton(document.getElementById("i"), document.getElementById("front"), "white", "white", showBack);
document.getElementById("version").innerHTML=version;
-
+
var dialog = document.getElementById("dialog");
var dialog_content = document.getElementById("dialog_content");
var front_content = document.getElementById("front_content");
+
+ document.getElementById("front_content").onclick = function(){
+ showDialog("Loading...");
+ setTimeout('fetchData()',100);
+ };
}
@@ -44,9 +44,16 @@
}
-$fp = fopen("php://stdin", "r") or die("can't read stdin");
-$username = trim(fgets($fp));
-fclose($fp);
+if(isset($argv[2])) $username = $argv[2];
+else exit("Usage: getdata {username} [today|yesterday|thismonth|lastmonth]\n\n");
+
+if(isset($argv[3])) $mode = $argv[3];
+else $mode = "today";
+
+//$fp = fopen("php://stdin", "r") or die("can't read stdin");
+//$username =trim(fgets($fp));
+//$mode = trim(fgets($fp));
+//fclose($fp);
$password=kc_getPassword($keychain,$username);
@@ -57,7 +64,7 @@
$cookie = tempnam(dirname(__FILE__), "cookie");
-$mode = 'thismonth';
+//$mode = 'today'; //thismonth
$chain=array(
"https://www.google.com/accounts/ServiceLoginBoxAuth",
@@ -115,6 +122,9 @@
}
curl_close($ch);
+// keep a copy of the original data handy.
+$oresult = $result;
+
// poor mans UTF-8 to Latin-1 recode (because Apple's PHP is missing iconv and recode extensions)
$result = str_replace("\x00","",$result);
@@ -137,7 +147,7 @@
{
$max=$a[1];
$clicks=$a[2];
- $ctr=$a[3];
+ $ctr=$a[3]; // doesn't seem to work
$usd=$a[5];
}
if($a[4]!="")
@@ -150,8 +160,25 @@
$usd=preg_replace('/\./','',$usd,1);
}
-// Output format: TIME # ESTIMATED EARNINGS # EARNINGS # CLICKS # CTR # ECMP
-echo @strftime("%d.%m. %H:%M")."#".round($usd*31/@date("d"))."#".$usd."#".$clicks."#".$ctr."#".$ecpm."\n";
+// make php happy
+date_default_timezone_set('America/New_York');
+
+$json = array(
+ "input" => array("username" => $username, "timeframe" => $mode),
+ "time" => date("h:i a"), // j, M for day, month
+ "usd" => $usd,
+ "clicks" => $clicks,
+ "ctr" => $ctr, // doesn't seem to work
+ "ecpm" => $ecpm //,
+ //"raw" => $oresult
+);
+
+// Output format: TIME # timeframe # EARNINGS # CLICKS # CTR # ECMP
+//echo @strftime("%d.%m %H:%M")."#". $mode ."#".$usd."#".$clicks."#".$ctr."#".$ecpm."\n";
+
+echo json_encode($json) . "\n";
+
+//round($usd*31/@date("d")) - ESTIMATED EARNINGS
unlink($cookie);

0 comments on commit db9cf60

Please sign in to comment.