Advertisement
RyanJ93

Treading parser

Mar 9th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1. <?php
  2. umask(0);
  3. define('FROM', 'from');
  4. define('KEYSPACE', 'keyspace');
  5. define('SLEEP', 60);
  6.  
  7. $mail_user = 'address';
  8. $mail_pass = 'password';
  9.  
  10. $cluster = Cassandra::cluster()->build();
  11. $session = $cluster->connect(KEYSPACE);
  12. $statement = new Cassandra\SimpleStatement("CREATE TABLE IF NOT EXISTS data (id UUID, start TIMESTAMP, end TIMESTAMP, status VARCHAR, buy VARCHAR, price FLOAT, profit FLOAT, loss FLOAT, PRIMARY KEY ((id), start)) WITH CLUSTERING ORDER BY (start DESC);");
  13. $session->execute($statement);
  14.  
  15. $pid = pcntl_fork();
  16. if ( $pid > 0 ){
  17.     echo "Daemon started";
  18.     exit 0;
  19. }else{
  20.     $sid = posix_setsid();
  21.     chdir('/');
  22.     file_put_contents('daemon.pid', getmypid());
  23.     $inbox = imap_open('{imap.mail.ru:993/imap/ssl}INBOX', $mail_user, $mail_pass);
  24.     if ( $inbox == false ){
  25.         file_put_contents('errors.log', imap_last_error());
  26.         exit -1;
  27.     }
  28.     while (true){
  29.         $emails = imap_search($inbox, 'ALL');
  30.         foreach($emails as $email_number) {
  31.             $message = imap_fetch_overview($inbox, $email_number, 0);
  32.             if ( isset($message[0]->from) && mb_strtolower($message[0]->from, 'UTF-8') == FROM && isset($message[0]->seen) && $message[0]->seen != 1 ){
  33.                 try{
  34.                     $data = imap_fetchbody($inbox, $email_number, 2);
  35.                     preg_match_all('/(?<=\bOrdine\s)(?:[\w-]+)/is', $data, $matches);
  36.                     $order = isset($matches[0]) ? $matches[0] : '';
  37.                     preg_match_all('/(?<=\bPrezzo\s)(?:[\w-]+)/is', $data, $matches);
  38.                     $price = isset($matches[0]) ? floatval($matches[0]) : '';
  39.                     preg_match_all('/(?<=\bTake profit\s)(?:[\w-]+)/is', $data, $matches);
  40.                     $profit = isset($matches[0]) ? floatval($matches[0]) : '';
  41.                     preg_match_all('/(?<=\bStop loss\s)(?:[\w-]+)/is', $data, $matches);
  42.                     $loss = isset($matches[0]) ? floatval($matches[0]) : '';
  43.                     preg_match_all('/(?<=\bCompra\s)(?:[\w-]+)/is', $data, $matches);
  44.                     $buy = isset($matches[0]) ? $matches[0] : '';
  45.                    
  46.                     $start = NULL; //PARSE DATE
  47.                     $end = NULL; //PARSE DATE
  48.                    
  49.                     $start = new Cassandra\Timestamp($start);
  50.                     $end = new Cassandra\Timestamp($end);
  51.                     $price = Cassandra\Float($price);
  52.                     $profit = Cassandra\Float($profit);
  53.                     $loss = Cassandra\Float($loss);
  54.                     $statement = $session->prepare("INSERT INTO data (id, start, end, status, buy, price, profit, loss) VALUES (uuid(), ?, ?, ?, ?, ?, ?, ?);");
  55.                     $session->execute($statement, new Cassandra\ExecutionOptions(array('arguments' => array($start, $end, $order, $buy, $price, $profit, $loss))));
  56.                     /*
  57.                         Ordine in attesa
  58.                         Compra USD/CHF
  59.                         Prezzo 0,9923
  60.                         Take profit   0,9968
  61.                         Stop loss 0,9868
  62.                         dal 12:35 08-03-2016 GMT al 16:35 08-03-2016 GMT
  63.                     */
  64.                     imap_setflag_full($inbox, $email_number, "\\Seen");
  65.                 }catch( Exception $ex ){
  66.                     file_put_contents('errors.log', $ex->getMessage());
  67.                 }
  68.             }
  69.         }
  70.         sleep(SLEEP);
  71.     }
  72. }
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement