Файл: anek.php
Строк: 75
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "anek";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
mysql_query('SET NAMES utf8');
function curl_get_file_contents($URL){
$header[] = 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
$header[] = 'Connection: Close';
$header[] = 'Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1';
$header[] = 'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8';
$header[] = 'User-Agent: Opera';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $URL);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_AUTOREFERER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, false);
$contents = curl_exec($curl);
curl_close($curl);
return $contents;
}
if (!empty($_POST)) {
switch ($_POST['category']) {
case 1:
$url = 'http://fishki.net/anekdot/best/';
switch ($_POST['data']) {
case 0:
$url .= 'day/';
break;
case 1:
$url .= 'month/';
break;
case 2:
$url .= 'all/';
break;
}
break;
case 2:
$url = 'http://fishki.net/anekdot/recent/';
switch ($_POST['data']) {
case 0:
$url .= 'day/';
break;
case 1:
$url .= 'month/';
break;
case 2:
$url .= 'all/';
break;
}
break;
default:
$url = 'http://fishki.net/anekdot/';
switch ($_POST['data']) {
case 0:
$url .= 'day/';
break;
case 1:
$url .= 'month/';
break;
case 2:
$url .= 'all/';
break;
}
break;
}
$start = (empty($_POST['start']) ? 1 : (int)$_POST['start']);
$end = (empty($_POST['end']) || $_POST['end'] == 1 ? 2 : (int)$_POST['end']);
$all = 0;
for ($i = $start; $i <= $end; ++$i) {
if ($i == 1) {
$data = @curl_get_file_contents($url);
} else {
$data = @curl_get_file_contents($url . $i . '/');
}
if (empty($data)) {
break;
}
preg_match_all("|<p class="text">(.*)</p>|Uis", $data, $out);
foreach ($out[1] AS $value) {
mysql_query("insert into `anek` set `text`='".($value)."', `time` = ".time().";");
$all++;
}
}
echo 'Скопировано <b>' . $all . '</b> анекдотов<br />';
}
if(isset($_GET['del'])){
mysql_query("TRUNCATE `anek`;");
echo 'Успешно очищено!';
}
?>
<form action="" method="POST">
<p>Начать с [min. 1]: <input type="text" name="start" value="1" size="3"/> - и до [min. 2]: <input type="text" name="end" value="10" size="3"/> страницы</p>
<p>Парсить только:</p>
<select name="category">
<option value="0">Горячее</option>
<option value="1">Лучшее</option>
<option value="2">Новое</option>
</select>
<p>Только за период:</p>
<select name="data">
<option value="2">За все время</option>
<option value="0">За 24 часа</option>
<option value="1">За месяц</option>
</select>
<p><input type="submit" value="Начать скачивание"/></p>
</form>
<a href="?del">Очистить базу</a><br />
<?php
$sql = 'SELECT * FROM `anek`';
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo '<b>ID:</b> ' . $row['id'] . '<br />';
echo '<b>Текст:</b> ' . $row['text'] . '<br />';
echo '<b>Время добавления:</b> ' . $row['time'] . '<hr />';
}
?>