Advertisement
Guest User

gw2 multicurl tradepost ranking v2 api

a guest
Oct 9th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.02 KB | None | 0 0
  1. <?php
  2. # Get the all the items numbers
  3. $url1 = "http://api.guildwars2.com/v2/commerce/listings";
  4. $response1 = file_get_contents($url1);
  5. $data1 = json_decode($response1, true);
  6. $amount_added_to_db = 0;
  7.  
  8. # Process in chunks to avoid out of memory error
  9. $chunksize = round(sizeof($data1)/10); # divided by 10 equals around 2215
  10. $offset = 0;
  11. $length = $chunksize;
  12. $smallchunk = array_slice($data1,$offset, $length);  
  13.  
  14. #Retrieve item names and link with numbers
  15. function request_callback($response,$info) {
  16.     global $associative_array;
  17.     if($info['http_code'] === 200){
  18.         $temporary_array = json_decode($response, true);
  19.         if (array_key_exists('name', $temporary_array)){
  20.         $associative_array[] = array('name' => $temporary_array['name'],'id' => $temporary_array['id']);
  21.         }
  22.     }
  23. }
  24.  
  25. # Queue up multiple curl request for handling
  26. require("rollingcurl.php"); // from https://github.com/LionsAd/rolling-curl
  27.  
  28. for ($x=0;$x<$length;$x++){
  29.         $itemurl1 = "https://api.guildwars2.com/v2/items/{$smallchunk[$x]}";
  30.         $urls[$x]= $itemurl1;  
  31.     }  
  32. $rc = new RollingCurl("request_callback");
  33. $rc->window_size = 20;
  34. foreach ($urls as $url) {
  35.      $request = new  RollingCurlRequest ( $url ) ;
  36.      $rc -> add ( $request ) ;
  37. }
  38. $rc->execute();
  39.  
  40. try {
  41.         # Make a connection to the database
  42.         $db = new PDO('sqlite:/var/www/gw2db/gw2.sqlite');
  43.         $db ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  44.      
  45.         # Create the database (if already made, IF NOT EXISTS will prevent error)
  46.         $db->exec("CREATE TABLE IF NOT EXISTS alltheitems (Id INTEGER PRIMARY KEY, itemname TEXT, itemnumber INTEGER)");       
  47.    
  48.         # Prepare the query ONCE
  49.         $stmt = $db->prepare('INSERT INTO alltheitems (itemname, itemnumber) VALUES(:name, :number)');
  50.         $stmt->bindParam(':name', $itemname, SQLITE3_TEXT);
  51.         $stmt->bindParam(':number', $itemnumber, SQLITE3_INTEGER);
  52.        
  53.         # Insert values and execute statements
  54.         for ($x=0;$x<sizeof($associative_array);$x++){
  55.             $itemname = $associative_array[$x]['name'];
  56.             $itemnumber = $associative_array[$x]['id'];
  57.            
  58.                 # Check if the item is already in the database or invalid URL return
  59.                 $duplicatecheck = $db->query("SELECT * FROM alltheitems WHERE itemnumber LIKE '$itemnumber'");
  60.                 $alreadyinDB=0;
  61.                 foreach($duplicatecheck as $row)
  62.                     {
  63.                       $alreadyinDB=1;
  64.                     }
  65.  
  66.             if ($alreadyinDB==0) {
  67.                 $stmt->execute();
  68.                 $amount_added_to_db = $amount_added_to_db + 1;
  69.             }
  70.         }
  71.  
  72.    
  73.         $fetchdb = $db->query("SELECT itemnumber, itemname FROM alltheitems");
  74.        
  75.         # Make array from fetchdb object
  76.         $data_array = $fetchdb->fetchAll();
  77.        
  78.         if ($amount_added_to_db > 0){
  79.        
  80.             $data_array_latest_added = array_slice($data_array,-$amount_added_to_db);
  81.            
  82.        
  83.             function request_callback2($response) {
  84.                 global $joint_array;
  85.                 $db2 = new PDO('sqlite:/var/www/gw2db/gw2.sqlite');
  86.                
  87.                 $temporary_array = json_decode($response, true);
  88.                 $idnumber = $temporary_array['id'];
  89.                 $searchname = $db2 ->query("SELECT itemname FROM alltheitems WHERE itemnumber LIKE '$idnumber'");
  90.                 $searcharray = $searchname->fetchAll();
  91.                 $itemname = $searcharray[0]['itemname'];
  92.                
  93.                 $joint_array[]= array('name'=>$itemname, 'id'=>$temporary_array['id'],'buys'=>$temporary_array['buys'],'sells'=>$temporary_array['sells']);
  94.                 $db2 = NULL;
  95.             }
  96.            
  97.             for ($x=0; $x<sizeof($data_array_latest_added);$x++) {
  98.                 $itemurl = "https://api.guildwars2.com/v2/commerce/listings/{$data_array_latest_added[$x]['itemnumber']}";
  99.                 $urls2[$x]= $itemurl;
  100.             }  
  101.            
  102.             $rc = new RollingCurl("request_callback2");
  103.             $rc->window_size = 20;
  104.             foreach ($urls2 as $url) {
  105.                  $request = new  RollingCurlRequest ( $url ) ;
  106.                  $rc -> add ( $request ) ;
  107.             }
  108.             $rc->execute();    
  109.            
  110.             #echo '<pre>'; print_r($joint_array); echo '</pre>';
  111.            
  112.             for ($y=0; $y<sizeof($joint_array);$y++) {
  113.            
  114.                 $totalbuys = 0;
  115.                 $totalsells = 0;
  116.                 $totalbuyprice = 0;
  117.                 $totalsellprice = 0;
  118.                
  119.                 for ($x=0;$x<sizeof($joint_array[$y]['buys']);$x++) {
  120.                     $totalbuys = $joint_array[$y]['buys'][$x]['listings'] * $joint_array[$y]['buys'][$x]['quantity'] + $totalbuys;
  121.                     $totalbuyprice = $joint_array[$y]['buys'][$x]['listings'] * $joint_array[$y]['buys'][$x]['quantity'] * $joint_array[$y]['buys'][$x]['unit_price'];
  122.                     $countB = $x;
  123.                     if ($totalbuys >= 1000){
  124.                         break;
  125.                     }
  126.                 }
  127.                
  128.                 for ($x=0;$x<sizeof($joint_array[$y]['sells']);$x++) {
  129.                     $totalsells = $joint_array[$y]['sells'][$x]['listings'] * $joint_array[$y]['sells'][$x]['quantity'] + $totalsells;
  130.                     $totalsellprice = $joint_array[$y]['sells'][$x]['listings'] * $joint_array[$y]['sells'][$x]['quantity'] * $joint_array[$y]['sells'][$x]['unit_price'];
  131.                     $countS = $x;
  132.                     if ($totalsells >= 1000){
  133.                         break;
  134.                     }
  135.                 }
  136.                
  137.                 if (($totalsells >= 1000) && ($totalbuys >= 1000)){
  138.                     $totalbuyprice = $totalbuyprice - ($totalbuys - 1000)* $joint_array[$y]['buys'][$countB]['unit_price'];
  139.                     $totalsellprice = $totalsellprice - ($totalsells - 1000)* $joint_array[$y]['sells'][$countS]['unit_price'];
  140.                                
  141.                     $ratio = $totalbuyprice / ($totalsellprice + $totalbuyprice);
  142.                     $ratio_small = number_format((float)$ratio, 2, '.', '');
  143.                 } else {
  144.                     $ratio_small = 0;
  145.                 }
  146.  
  147.                 $db->exec("CREATE TABLE IF NOT EXISTS rating (Id INTEGER PRIMARY KEY, itemnumber INTEGER, itemname TEXT, itemratio REAL)");  
  148.                    
  149.                     # Prepare the query ONCE
  150.                     $stmt = $db->prepare('INSERT INTO rating (itemnumber, itemname, itemratio) VALUES(:number, :name, :ratio)');
  151.                     $stmt->bindParam(':number', $itemnumber, SQLITE3_INTEGER);
  152.                     $stmt->bindParam(':name', $itemname, SQLITE3_TEXT);
  153.                     $stmt->bindParam(':ratio', $itemratio, SQLITE3_FLOAT);
  154.                
  155.                     # Insert values and execute statements
  156.                     $idnumber = $joint_array[$y]['id'];
  157.                     #echo $joint_array[$y][id];'<br>';
  158.                     $alreadyinDBcheck = $db->query("SELECT * FROM rating WHERE itemnumber LIKE '$idnumber'");
  159.                         $found=0;
  160.                         foreach($alreadyinDBcheck as $row)
  161.                             {
  162.                                 $found=1;
  163.                             }  
  164.                         if ($found==0) {
  165.                             $itemratio = $ratio_small;
  166.                             $itemnumber = $joint_array[$y]['id'];
  167.                             $itemname = $joint_array[$y]['name'];
  168.                             $stmt->execute();
  169.                         } else {
  170.                             $db->exec("UPDATE rating SET itemratio='$ratio_small' WHERE itemnumber LIKE '$joint_array[$y][id]'");
  171.                             #echo ' already in DB <br>';
  172.                             }
  173.                                                    
  174.                
  175.            
  176.             }
  177.         }
  178.  
  179.         # add creation timestamp
  180.         date_default_timezone_set("Europe/Amsterdam");
  181.         $time = "This page has been updated at " . date("d-m-Y h:i:sa"). "<br><br>";
  182.         $db->exec("CREATE TABLE IF NOT EXISTS creationtime (Id INTEGER PRIMARY KEY, timestring TEXT)");
  183.             $stmt = $db->prepare('INSERT INTO creationtime (timestring) VALUES(:time)');
  184.             $stmt->bindParam(':time', $timestring, SQLITE3_TEXT);
  185.             $timestring = $time;
  186.             $stmt->execute();
  187.            
  188.         # Clear the table
  189.         #$db-> exec ('DELETE FROM rating');
  190.         #$db-> exec ('DELETE FROM alltheitems');
  191.        
  192.         # close the database connection
  193.         $db = NULL;
  194.  
  195.     }
  196.    
  197.     catch(PDOException $e) {
  198.     echo 'ERROR: ' . $e->getMessage();
  199. }
  200.  
  201.    
  202.  
  203.  
  204. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement