Advertisement
Guest User

gw2 v2 api tradepost ranking

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