<?php

function filter_name($name) {
    return 
$name;
}

$time 60 60 24 - (time() - filemtime("gpu.json"));
if (
$time 0) {
    die(
"Data was already updated in the last day. Please wait another " . ($time) . " seconds.");
}
touch('gpu.json');
copy('gpu.json''gpu-'.date('d').'.json');
print 
"You are the lucky one! Starting update. <b>Please leave this window open until it says 'All Done' at the bottom.</b><br/>"flush();
ignore_user_abort(TRUE);
set_time_limit(30 60); // 30 minutes


function yql($q) { 
    
$url "http://query.yahooapis.com/v1/public/yql?format=json&diagnostics=true&q=" urlencode($q);
    
$ch curl_init($url);
    
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    
$data curl_exec($ch);
    
$info curl_getinfo($ch);
    if (
$info['http_code'] == 999) {
      print 
'HTTP 999 from YQL. Sleeping for 5 seconds<br/>';
      
flush();
      
sleep(5);
      return 
yql($q);
    }
    return 
json_decode($datatrue);
}

$result yql("SELECT * FROM html WHERE url = \"http://www.videocardbenchmark.net/gpu_list.php\" AND xpath = \"//table[@id='gputable']\"");

$good false;
$data = array();
$trs $result['query']['results']['table']['tbody']['tr'];
if (!
is_array($trs)) {
  die(
'<br/><b style="color:red">Can\'t find any tables in gpubenchmark.net. Please email <a href="mailto:comparegpu@paulisageek.com">Paul</a>.</b>');
}
foreach (
$trs as $count => $tr) {
    if (! isset(
$tr['td'][0]['a']['content'])) continue;

    
$row = array(
        
"name" => preg_replace("/\s\s+/"" "$tr['td'][0]['a']['content']),
        
"score" => $tr['td'][1]['p'],
        
"rank" => $tr['td'][2]['p']
    );

    
$row["search_name"] = filter_name($row["name"]);
    if (! 
$row["search_name"]) continue;

    
$tries 0;
    while (
$tries++ < 4) {
      
$newegg yql("use 'http://paulisageek.com/yql-tables/newegg/newegg.search.xml'; SELECT * FROM newegg.search WHERE query = '{$row['search_name']}' AND order='BESTMATCH' AND N='40000048'");
  
      
// if there was no warning then it was probably a good fetch
      
if (! $newegg['query']['diagnostics']['warning']) {
        break;
      }
      print 
" Bad fetch, trying again...";
      
flush();
    }

    if (
count($newegg['query']['results']) > 0) {
        
$good TRUE;

        if ( ! isset(
$newegg['query']['results']['item'][0])) {
            
// stupid YQL
            
$row['newegg'] = $newegg['query']['results']['item'];
        } else {
            foreach (
$newegg['query']['results']['item'] as $item) {
                if (
strpos($item['name'], 'OEM') === FALSE) {
                    
$row['newegg'] = $item;
                    break;
                }
            }

            
// Worse case
            
if (!isset($row['newegg']))
              
$row['newegg'] = $newegg['query']['results']['item'][0];
        }
    }

    if (!isset(
$row['newegg'])) {
      print 
$row['name'] . ' not found in newegg<br/>';
    } else {
      
$data[] = $row;
      print 
json_encode($row) . "<br>";
    }

    
flush();
}

if (
$good) {
    
file_put_contents("gpu.json"json_encode($data));
    print 
'<a href=".">All done. Thanks so much for updaing it!</a>';
} else 
    print 
"<b>No data came from newegg. Please report this as a bug</b><br/>";
?>