Esempio n. 1
0
CatchKill();
$startTime = time();
$connectionTracking = ['created' => 0, 'requests' => 0];
$file = [];
$file['note'] = 'Brought to you by https://does.theapi.work/';
$file['started'] = JSNow();
foreach (['us', 'eu'] as $region) {
    $file['regions'][$region] = FetchRegionData($region);
    if ($caughtKill) {
        break;
    }
}
$file['finished'] = JSNow();
if (!$caughtKill) {
    $fn = isset($argv[1]) ? $argv[1] : __DIR__ . '/../theapi.work/times.json';
    AtomicFilePutContents($fn, json_encode($file, JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE));
}
DebugMessage("Opened {$connectionTracking['created']} connections to service {$connectionTracking['requests']} requests.");
DebugMessage('Done! Started ' . TimeDiff($startTime, ['precision' => 'second']));
function JSNow()
{
    return floor(microtime(true) * 1000);
}
function FetchRegionData($region)
{
    global $caughtKill;
    $region = trim(strtolower($region));
    $results = [];
    DebugMessage("Fetching realms for {$region}");
    $url = GetBattleNetURL($region, 'wow/realm/status');
    $jsonString = HTTP::Get($url);
Esempio n. 2
0
function BuildIncludes($regions)
{
    global $db;
    global $resultCodes, $timeZones, $timeLeftCodes, $timeLeftNumbers;
    $blankImage = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
    $htmlFormat = <<<EOF
                <table class="results">
                    <tbody>
                        <tr>
                            <td>Buy Price</td>
                            <td class="buy-price" id="##region##-buy">##buy##</td>
                        </tr>
                        <tr>
                            <td style="vertical-align: bottom">24-Hour Range</td>
                            <td>
                                <span id="##region##-24min">##24min##</span>
                                <div class="range-bar"><div class="range-point" style="left: ##24pct##%" id="##region##-24pct-left"></div></div>
                                <span id="##region##-24max">##24max##</span>
                            </td>
                        </tr>
                        <tr>
                            <td>API Result</td>
                            <td id="##region##-result">##result##</td>
                        </tr>
                        <tr>
                            <td>Updated</td>
                            <td id="##region##-updatedhtml">##updatedhtml##</td>
                        </tr>
                    </tbody>
                </table>
EOF;
    $json = [];
    $historyJson = [];
    $historyJsonFull = [];
    $csv = "Region,UTC Date,Buy Price\r\n";
    //,Time Left
    foreach ($regions as $region) {
        $fileRegion = strtoupper($region);
        if ($fileRegion == 'US') {
            $fileRegion = 'NA';
        }
        $filenm = __DIR__ . '/../wowtoken/' . $fileRegion . '.incl.html';
        $sql = 'select * from tblWowToken w where region = ? and `when` = (select max(w2.`when`) from tblWowToken w2 where w2.region = ?)';
        $stmt = $db->prepare($sql);
        $stmt->bind_param('ss', $region, $region);
        $stmt->execute();
        $result = $stmt->get_result();
        $tokenData = DBMapArray($result, null);
        $tokenData = array_pop($tokenData);
        $stmt->close();
        $tokenData['24min'] = $tokenData['24max'] = null;
        $sql = 'select min(`marketgold`) `min`, max(`marketgold`) `max` from tblWowToken w where region = ? and `when` between timestampadd(hour, -24, ?) and ?';
        $stmt = $db->prepare($sql);
        $stmt->bind_param('sss', $region, $tokenData['when'], $tokenData['when']);
        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()) {
            $tokenData['24min'] = $row['min'];
            $tokenData['24max'] = $row['max'];
        }
        $result->close();
        $stmt->close();
        $d = new DateTime('now', timezone_open($timeZones[$region]));
        $d->setTimestamp(strtotime($tokenData['when']));
        $sparkUrl = GetChartURL($region, $fileRegion);
        if (!$sparkUrl) {
            $sparkUrl = $blankImage;
        }
        $historyJsonFull[$fileRegion] = BuildHistoryData($region);
        $historyJson[$fileRegion] = [];
        $cutOff = time() - 259200;
        // 72 hours
        $prevPrice = -1;
        foreach ($historyJsonFull[$fileRegion] as $row) {
            if ($row[0] > $cutOff) {
                $historyJson[$fileRegion][] = $row;
            }
            if ($row[1] != $prevPrice) {
                $prevPrice = $row[1];
                $csv .= "{$fileRegion}," . date('Y-m-d H:i:s', $row[0]) . ",{$row[1]}\r\n";
                //,{$row[2]}
            }
        }
        $json[$fileRegion] = ['timestamp' => strtotime($tokenData['when']), 'raw' => ['buy' => $tokenData['marketgold'], '24min' => $tokenData['24min'], '24max' => $tokenData['24max'], 'timeToSell' => isset($timeLeftNumbers[$tokenData['timeleft']]) ? $timeLeftNumbers[$tokenData['timeleft']] : $tokenData['timeleft'], 'timeToSellSeconds' => $tokenData['timeleftraw'], 'result' => $tokenData['result'], 'updated' => strtotime($tokenData['when']), 'updatedISO8601' => date(DATE_ISO8601, strtotime($tokenData['when']))], 'formatted' => ['buy' => number_format($tokenData['marketgold']) . 'g', '24min' => isset($tokenData['24min']) ? number_format($tokenData['24min']) . 'g' : '', '24max' => isset($tokenData['24max']) ? number_format($tokenData['24max']) . 'g' : '', '24pct' => $tokenData['24max'] != $tokenData['24min'] ? round(($tokenData['marketgold'] - $tokenData['24min']) / ($tokenData['24max'] - $tokenData['24min']) * 100, 1) : 50, 'timeToSell' => isset($timeLeftCodes[$tokenData['timeleft']]) ? $timeLeftCodes[$tokenData['timeleft']] : $tokenData['timeleft'], 'result' => isset($resultCodes[$tokenData['result']]) ? $resultCodes[$tokenData['result']] : 'Unknown: ' . $tokenData['result'], 'updated' => $d->format('M jS, Y g:ia T'), 'updatedhtml' => $d->format('M jS, Y g:ia\\&\\n\\b\\s\\p\\;T'), 'sparkurl' => $sparkUrl, 'region' => $fileRegion]];
        $replacements = $json[$fileRegion]['formatted'];
        $html = preg_replace_callback('/##([a-zA-Z0-9]+)##/', function ($m) use($replacements) {
            if (isset($replacements[$m[1]])) {
                return $replacements[$m[1]];
            }
            return $m[0];
        }, $htmlFormat);
        AtomicFilePutContents($filenm, $html);
    }
    AtomicFilePutContents(__DIR__ . '/../wowtoken/snapshot.json', json_encode($json, JSON_NUMERIC_CHECK), true);
    AtomicFilePutContents(__DIR__ . '/../wowtoken/snapshot-history.csv', $csv, true);
    AtomicFilePutContents(__DIR__ . '/../wowtoken/snapshot-history.json', json_encode(['attention' => 'Please see usage guidelines on https://wowtoken.info/', 'note' => 'Data is truncated since it was fetched without gzip encoding.', 'update' => $json, 'history' => $historyJson], JSON_NUMERIC_CHECK), json_encode(['attention' => 'Please see usage guidelines on https://wowtoken.info/', 'update' => $json, 'history' => $historyJsonFull], JSON_NUMERIC_CHECK));
    $shtmlPath = __DIR__ . '/../wowtoken/index-template.shtml';
    if (file_exists($shtmlPath)) {
        $shtml = file_get_contents($shtmlPath);
        $shtmlPath = str_replace('-template', '', $shtmlPath);
        $html = preg_replace_callback('/<!--#include virtual="([^"]+)"-->/', function ($m) {
            $path = __DIR__ . '/../wowtoken/' . $m[1];
            if (file_exists($path)) {
                return file_get_contents($path);
            }
            return '';
        }, $shtml);
        AtomicFilePutContents($shtmlPath, $html);
        $htmlPath = preg_replace('/\\.shtml$/', '.html', $shtmlPath);
        if (strpos($html, '<!--#') === false) {
            AtomicFilePutContents($htmlPath, $html);
        } else {
            unlink($htmlPath);
        }
    }
}