[*] MQ тестовая задача - парсинг с баша
This commit is contained in:
1 parent
1ef2b47080
commit
bd6d058db8
2 files changed
+52
-1
No files matched your search
+2
-1
@@ -16,6 +16,7 @@
|
||||
],
|
||||
"require": {
|
||||
"twig/twig": "^3.0",
|
||||
"sentry/sdk": "^3.2"
|
||||
"sentry/sdk": "^3.2",
|
||||
"ext-iconv": "*"
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
use Core\Helpers\SystemFunctions;
|
||||
use Core\Helpers\Thumbs;
|
||||
use Core\ExternalServices\TelegramSender;
|
||||
use Core\Models\DB;
|
||||
|
||||
class MQTasks
|
||||
{
|
||||
@@ -24,4 +26,52 @@
|
||||
|
||||
return $saveImage;
|
||||
}
|
||||
|
||||
public static function sendBash(...$args): ?string
|
||||
{
|
||||
/** @var DB $DB */
|
||||
$DB = DB::getInstance();
|
||||
|
||||
$file = file_get_contents('http://bashorg.org/random');
|
||||
$file = preg_match_all('|<div class="quote">(.+)</div>|U', $file, $frazes);
|
||||
|
||||
$frazes = $frazes[1];
|
||||
$frazes = array_map(function ($e) {
|
||||
return \iconv('windows-1251//IGNORE', 'UTF-8//IGNORE', $e);
|
||||
}, $frazes);
|
||||
|
||||
$i = 0;
|
||||
foreach ($frazes as $post) {
|
||||
$text = str_replace('<br />', PHP_EOL, $post);
|
||||
$text = htmlspecialchars_decode($text);
|
||||
$text = str_replace('\'', '\\\'', $text);
|
||||
$hash = md5($text);
|
||||
$res = $DB->query('SELECT * FROM bashorg WHERE hash="' . $hash . '"');
|
||||
if (!$res) {
|
||||
$i++;
|
||||
$DB->addItem('bashorg', ['hash' => $hash, 'text' => $text]);
|
||||
}
|
||||
}
|
||||
sleep(2);
|
||||
return 'Добавлено ' . $i . ' записей из ' . count($frazes);
|
||||
}
|
||||
/*
|
||||
public static function sendBash2(...$args): void
|
||||
{
|
||||
|
||||
|
||||
$DB = DB::getInstance();
|
||||
|
||||
$html = file_get_contents('http://bashorg.org/random');
|
||||
|
||||
//$file = preg_match_all('|<div class="vote">(.+)</div>|U',$html,$frazes);
|
||||
$file = preg_match_all('<div class="q">(.+?)</div>',$html,$top);
|
||||
|
||||
print_r($top);
|
||||
$frazes = $frazes[1];
|
||||
$frazes = array_map(function($e){
|
||||
return \iconv('windows-1251//IGNORE', 'UTF-8//IGNORE', $e);
|
||||
}, $frazes);
|
||||
|
||||
}*/
|
||||
}
|
||||
Reference in new issue
Block a user