Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- umask(0);
- define('FROM', 'from');
- define('KEYSPACE', 'keyspace');
- define('SLEEP', 60);
- $mail_user = 'address';
- $mail_pass = 'password';
- $cluster = Cassandra::cluster()->build();
- $session = $cluster->connect(KEYSPACE);
- $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);");
- $session->execute($statement);
- $pid = pcntl_fork();
- if ( $pid > 0 ){
- echo "Daemon started";
- exit 0;
- }else{
- $sid = posix_setsid();
- chdir('/');
- file_put_contents('daemon.pid', getmypid());
- $inbox = imap_open('{imap.mail.ru:993/imap/ssl}INBOX', $mail_user, $mail_pass);
- if ( $inbox == false ){
- file_put_contents('errors.log', imap_last_error());
- exit -1;
- }
- while (true){
- $emails = imap_search($inbox, 'ALL');
- foreach($emails as $email_number) {
- $message = imap_fetch_overview($inbox, $email_number, 0);
- if ( isset($message[0]->from) && mb_strtolower($message[0]->from, 'UTF-8') == FROM && isset($message[0]->seen) && $message[0]->seen != 1 ){
- try{
- $data = imap_fetchbody($inbox, $email_number, 2);
- preg_match_all('/(?<=\bOrdine\s)(?:[\w-]+)/is', $data, $matches);
- $order = isset($matches[0]) ? $matches[0] : '';
- preg_match_all('/(?<=\bPrezzo\s)(?:[\w-]+)/is', $data, $matches);
- $price = isset($matches[0]) ? floatval($matches[0]) : '';
- preg_match_all('/(?<=\bTake profit\s)(?:[\w-]+)/is', $data, $matches);
- $profit = isset($matches[0]) ? floatval($matches[0]) : '';
- preg_match_all('/(?<=\bStop loss\s)(?:[\w-]+)/is', $data, $matches);
- $loss = isset($matches[0]) ? floatval($matches[0]) : '';
- preg_match_all('/(?<=\bCompra\s)(?:[\w-]+)/is', $data, $matches);
- $buy = isset($matches[0]) ? $matches[0] : '';
- $start = NULL; //PARSE DATE
- $end = NULL; //PARSE DATE
- $start = new Cassandra\Timestamp($start);
- $end = new Cassandra\Timestamp($end);
- $price = Cassandra\Float($price);
- $profit = Cassandra\Float($profit);
- $loss = Cassandra\Float($loss);
- $statement = $session->prepare("INSERT INTO data (id, start, end, status, buy, price, profit, loss) VALUES (uuid(), ?, ?, ?, ?, ?, ?, ?);");
- $session->execute($statement, new Cassandra\ExecutionOptions(array('arguments' => array($start, $end, $order, $buy, $price, $profit, $loss))));
- /*
- Ordine in attesa
- Compra USD/CHF
- Prezzo 0,9923
- Take profit 0,9968
- Stop loss 0,9868
- dal 12:35 08-03-2016 GMT al 16:35 08-03-2016 GMT
- */
- imap_setflag_full($inbox, $email_number, "\\Seen");
- }catch( Exception $ex ){
- file_put_contents('errors.log', $ex->getMessage());
- }
- }
- }
- sleep(SLEEP);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement