PageRenderTime 34ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/TOOLS/PHP/quotes_to_db.php

http://stratlab.googlecode.com/
PHP | 196 lines | 166 code | 14 blank | 16 comment | 21 complexity | b0b5e18504ddade294ff8093d90a1087 MD5 | raw file
  1. <?php
  2. require_once 'mysql.class.php';
  3. $QUOTES_DIR ="/home/voellenk/QUOTES/";
  4. $dbhost = 'localhost:3036';
  5. $dbuser = 'va';
  6. $dbpass = 'dummy';
  7. $dbname = 'va_stratlab';
  8. // parse command line parameters
  9. $args= parseArgs($argv);
  10. if (array_key_exists("exchange", $args)) {
  11. $exchange=$args['exchange'];
  12. } else {
  13. echo "Usage: ".$_SERVER['PHP_SELF']." --exchange=<EXCHANGENAME>\n";
  14. die;
  15. }
  16. // verify if directory with zip files exists
  17. if (!is_dir($QUOTES_DIR))
  18. die ("Directory $QUOTES_DIR does not exist.\n");
  19. // establish connection to db
  20. $db = new mysql($dbhost, $dbuser, $dbpass);
  21. if (!$db->connect($dbname))
  22. die;
  23. $sql = "SELECT id FROM exchange WHERE name='$exchange'";
  24. $rows= $db->select($sql);
  25. if (!$rows) {
  26. $sql = "INSERT INTO exchange (name) VALUES ('$exchange')";
  27. $result = $db->insert($sql);
  28. if ($result) {
  29. $sql = "SELECT id FROM exchange WHERE name='$exchange'";
  30. $rows= $db->select($sql);
  31. $exchange_id = $rows[0];
  32. } else {
  33. die($db->error);
  34. }
  35. } else {
  36. // use exchange_id for coming INSERTS
  37. $exchange_id = $rows[0];
  38. }
  39. $zipfilearr=array();
  40. $dh = opendir($QUOTES_DIR);
  41. if ($dh==TRUE) {
  42. while (($file = readdir($dh)) !== false) {
  43. //echo "filename: $file : filetype: " . filetype($QUOTES_DIR . $file) . "\n";
  44. if ($file == $exchange & filetype($QUOTES_DIR . $file)=="dir" ) {
  45. // found exchange directory
  46. $dh1 = opendir($QUOTES_DIR.$file);
  47. while (($zfile = readdir($dh1)) !== false) {
  48. if (substr(strtoupper($zfile),-4) == ".ZIP") {
  49. $zipfilearr[]=$zfile;
  50. }
  51. }
  52. closedir($dh1);
  53. }
  54. }
  55. closedir($dh);
  56. }
  57. sort($zipfilearr); //alphabetically sort filenames
  58. foreach ($zipfilearr as $zfile) {
  59. echo "processing $zfile...";
  60. readZipFile($QUOTES_DIR.$exchange."/".$zfile);
  61. }
  62. $db->disconnect();
  63. // ---- end of script ----
  64. function readZipFile($zfile) {
  65. $namecounter=0;
  66. $pricecounter=0;
  67. $txtfilearr=array();
  68. $za = new ZipArchive();
  69. $res = $za->open($zfile);
  70. if ($res===true) {
  71. //loop through all text file in zip archive
  72. $j=0;
  73. for ($i=0; $i<$za->numFiles; $i++) {
  74. $arr = $za->statIndex($i);
  75. if (substr(strtoupper($arr['name']),-4) == ".TXT") {
  76. $txtfilearr[$j]['name']=$arr['name'];
  77. $txtfilearr[$j++]['index']=$i;
  78. }
  79. }
  80. sort($txtfilearr);
  81. echo "found ".sizeof($txtfilearr)." files. ";
  82. // now uncompress each file and read the contents
  83. foreach($txtfilearr as $txtfile) {
  84. $za->extractTo(".", $txtfile['name']);
  85. $fcontents= file($txtfile['name']); //read file contents into array
  86. insertIntoDb($fcontents, &$namecounter, &$pricecounter); //insert into va_tradedb
  87. unlink($txtfile['name']); //remove txt file after processing
  88. }
  89. $za->close();
  90. } else {
  91. echo "opening of zip file $zfile failed.\n";
  92. }
  93. echo "inserted $namecounter names and $pricecounter prices.\n";
  94. return true;
  95. }
  96. function insertIntoDb($fcontents, $namecounter, $pricecounter) {
  97. global $exchange_id;
  98. global $db;
  99. // loop through every line of text file
  100. foreach ($fcontents as $line) {
  101. $cells= explode(",",$line);
  102. // $cells[0] : name
  103. // $cells[1] : code
  104. // $cells[2] : date
  105. // $cells[3:6] : OHLC
  106. // $cells[7] : volume
  107. // $cells[8] : oi
  108. if (sizeof ($cells) <8 | sizeof ($cells) >9)
  109. die ("line $line invalid.\n");
  110. $name= str_replace("'","\'",$cells[0]);
  111. if (is_numeric($cells[2])) { // omit first line
  112. $sql="SELECT stockname.id,exchange.id FROM exchange JOIN stockname ON (exchange.id=stockname.exchange_id)
  113. WHERE stockname.code='".$cells[1]."' AND exchange.id='".$exchange_id."'";
  114. $rows=$db->select($sql);
  115. // $rows[0] = stockname.id
  116. // $rows[1] = exchange.id
  117. if (!$rows) {
  118. // if no result found insert row into db
  119. $sql="INSERT INTO stockname (name, code, exchange_id) VALUES ('".$name."','".$cells[1]."','".$exchange_id."')";
  120. if ($db->insert($sql)) {
  121. $namecounter++;
  122. } else {
  123. die("Insert Error: ".mysql_error()."\n");
  124. }
  125. }
  126. $date = substr($cells[2],0,4)."-".substr($cells[2],4,2)."-".substr($cells[2],6,2);
  127. $sql="SELECT id FROM stockname WHERE code='".$cells[1]."' AND exchange_id=".$exchange_id;
  128. $id=$db->select($sql);
  129. // $id[0] includes id
  130. //$sql="SELECT volume FROM stockprice WHERE stock_id='".$id[0]."' AND date='$date'";
  131. //$rows=$db->select($sql);
  132. if (sizeof($cells)==8) { //without open interest
  133. $sql="INSERT INTO stockprice (stock_id, date, open, high, low, close, volume)
  134. VALUES('".$id[0]."','".$date."','".$cells[3]."','".$cells[4]."','".$cells[5]."','".$cells[6]."','".$cells[7]."')";
  135. } elseif (sizeof($cells)==9) { //include oi
  136. $sql="INSERT INTO stockprice (stock_id, date, open, high, low, close, volume, oi)
  137. VALUES('".$id[0]."','".$date."','".$cells[3]."','".$cells[4]."','".$cells[5]."','".$cells[6]."','".$cells[7]."','".$cells[8]."')";
  138. }
  139. //echo $sql."\n";
  140. if (@$db->insert($sql)) {
  141. $pricecounter++;
  142. } else {
  143. //die("Insert Error: ".mysql_error()."\n");
  144. }
  145. }
  146. }
  147. }
  148. function parseArgs($argv){
  149. array_shift($argv);
  150. $out = array();
  151. foreach ($argv as $arg){
  152. if (substr($arg,0,2) == '--'){
  153. $eqPos = strpos($arg,'=');
  154. if ($eqPos === false){
  155. $key = substr($arg,2);
  156. $out[$key] = isset($out[$key]) ? $out[$key] : true;
  157. } else {
  158. $key = substr($arg,2,$eqPos-2);
  159. $out[$key] = substr($arg,$eqPos+1);
  160. }
  161. } else if (substr($arg,0,1) == '-'){
  162. if (substr($arg,2,1) == '='){
  163. $key = substr($arg,1,1);
  164. $out[$key] = substr($arg,3);
  165. } else {
  166. $chars = str_split(substr($arg,1));
  167. foreach ($chars as $char){
  168. $key = $char;
  169. $out[$key] = isset($out[$key]) ? $out[$key] : true;
  170. }
  171. }
  172. } else {
  173. $out[] = $arg;
  174. }
  175. }
  176. return $out;
  177. }
  178. ?>