Добрый день.
Хочу поделиться с вами своим информационным ботом для сайта freelansim.ru twitter.com/freelansimru
Хочу поделиться с вами своим информационным ботом для сайта freelansim.ru twitter.com/freelansimru
#!/usr/bin/php -q <? // подключаем библиотеки include 'lib/simple_html_dom.php'; include 'lib/twitteroauth.php'; include 'lib/Array_Capable_PDO.php'; // конектимся к бд try { $DBH = new Array_Capable_PDO("mysql:host=database_ip;dbname=parser", 'username', 'password'); } catch(PDOException $e) { echo $e->getMessage(); die; } // получаем список id новых тасков с freelansim.ru $html = file_get_html('http://freelansim.ru/tasks'); $tasks = $html->find('.task'); $ids=array(); foreach($tasks as $ts) { $ids[] = str_replace('/tasks/','',$ts->children(2)->children(0)->href); } Проверяем наличие тасков в бд $STH = $DBH->prepare_with_arrays( 'SELECT group_concat(id) as ids from freelansim where id in :ids', array(':ids'), array($ids) ); $STH->setFetchMode(PDO::FETCH_ASSOC); $STH->execute(); $res = $STH->fetch(); if($res['ids']!=null) $db_ids = explode(',',$res['ids']); else $db_ids = array(); //Подключаем api tweetter $oauth = new TwitterOAuth('TWITTER_KEY', 'TWITTER_SECRET', 'accessToken', 'accessSecret'); $credentials = $oauth->get("account/verify_credentials"); foreach($ids as $i){ // Если id есть в базе то пропускам if(in_array($i,$db_ids)) continue; // получаем данные нового таска $details = file_get_html('http://freelansim.ru/tasks/'.$i); $d = $details->find('.more_information'); $t = $details->find('.short_info'); $title = $t[0]->children(1)->children(0)->innertext."\n"; $price= $t[0]->children(2)->children(0)->innertext."\n"; $suffix= $t[0]->children(2)->children(1)->innertext."\n"; $descr= $d[0]->children(0)->children(2)->innertext."\n"; // пишем данные нового таска в бд $STH = $DBH->prepare("INSERT INTO freelansim (id, title, descr, price, suffix) values (?, ?, ?, ?, ?)"); $STH->bindParam(1, $i); $STH->bindParam(2, $title); $STH->bindParam(3, $descr); $STH->bindParam(4, $price); $STH->bindParam(5, $suffix); $STH->execute(); // и постим в твиттер $msg = "$title $price $suffix http://mixgift.ru/r.php?i=$i #freelansim"; $oauth->post('statuses/update', array('status' => $msg)); } ?>
Использованные материалы:
Пишем бота tweetter
Парсер на php
Расширение класса PDO
ссылка на оригинал статьи http://habrahabr.ru/post/160815/
Добавить комментарий