<?php error_reporting(0); require_once dirname(__FILE__) . '/../inc/init.php'; require_once dirname(__FILE__) . '/../inc/twitter/twitteroauth.php'; // only tweet when we reach these milestones $mileStones = array(10, 12, 15, 18, 20, 22, 24, 25); $current = getCurrentData(); if (shouldTweet($current)) { tweetPercentage($current); } function getCurrentData() { // get latest data // inner select grabs highest % tweeted today // only check today's readings $hour = date('H'); if ($hour < 12) { $start = time(); $end = time(); $dateSMYSQL = es(date("Y-m-d 00:00", $start)); // today 00 $dateEMYSQL = es(date("Y-m-d 12:00", $end)); // today 12 } else { $start = time(); $end = time() + 3600 * 24; $dateSMYSQL = es(date("Y-m-d 12:00", $start)); // today 12 $dateEMYSQL = es(date("Y-m-d 00:00", $end)); // tomorrow 00
<?php require_once dirname(__FILE__) . '/../inc/init.php'; require_once dirname(__FILE__) . '/../inc/twitter/twitteroauth.php'; $maxPercentage = getMaxPercentageForTheNight(); if (shouldTweet($maxPercentage)) { tweetPercentage($maxPercentage); } function getMaxPercentageForTheNight() { $today = time(); $dateSMYSQL = es(date("Y-m-d 00:00", $today)); // midnight $dateEMYSQL = es(date("Y-m-d 09:00", $today)); // 9am $query = "SELECT\n timestamp,\n FLOOR(100*wind/demand) AS percent\n FROM\n wind_vs_demand\n WHERE\n timestamp BETWEEN '{$dateSMYSQL}' AND '{$dateEMYSQL}'\n ORDER BY\n percent DESC\n LIMIT 1"; return fetchAssoc(query($query)); } function shouldTweet($data) { // if the max production went over 10% return $data['percent'] > 10; } function tweetPercentage($current) { global $socialUrl; // tweet the max percentage for last night $percent = $current['percent']; $time = $current['timestamp']; $message = "While you were sleeping, #windenergy reached {$percent}% of the National Grid's electricity demand. {$socialUrl}"; $connection = new TwitterOAuth(API_KEY, API_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);