public static function kanjiToRomaji($input, &$returnCode = '') { if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { return $input; } $japanese = "/([\\x{3000}-\\x{303f}\\x{3040}-\\x{309f}\\x{30a0}-\\x{30ff}\\x{ff00}-\\x{ff9f}\\x{4e00}-\\x{9faf}\\x{3400}-\\x{4dbf}])+/iu"; preg_match_all($japanese, $input, $matches); if (empty($matches[0]) || !is_array($matches[0])) { return $input; } $placeholders = ['in' => $matches[0], 'out' => []]; $uniq = uniqid(); $kakasi = 'kakasi -i euc -w | kakasi -i euc -Ha -Ka -Ja -Ea -ka'; $process = new Process($kakasi); $words = implode($uniq, $placeholders['in']); $convertedWords = mb_convert_encoding($words, 'eucjp', 'utf-8'); $process->setInput($convertedWords); $process->run(); if (!$process->isSuccessful()) { return IRCHelper::colorText('ERROR', IRCHelper::COLOR_RED) . ': can\'t run kakasi'; } $romaji = $process->getOutput(); $placeholders['out'] = explode($uniq, $romaji); $input = str_replace($placeholders['in'], $placeholders['out'], $input); return $input; }
protected function unit($arguments) { $request = trim($arguments[0]); $request = rtrim($request, '!@#$%^&*()_-+={}[]:;\'"\\|<>,./?'); foreach ($this->lastRequest as $id => $timeToClear) { if (time() - $timeToClear >= self::CLEAR_TIME) { unset($this->lastRequest[$id]); } } parent::RedBeanConnect(self::DB_NAME); if (preg_match('/^[\\x{30A0}-\\x{30FF}\\x{31F0}-\\x{31FF}]+$/iu', $request)) { $units = R::findAll(self::TB_NAME, '`orginal` like ?', ["%{$request}%"]); } else { $units = R::findAll(self::TB_NAME, '`name` like ?', [$request]); } if ($units) { $linkHolder = []; foreach ($units as $unit) { if (!$unit->linkgc || isset($linkHolder[$unit->linkgc]) || isset($this->lastRequest[$unit->id])) { continue; } $linkHolder[$unit->linkgc] = $unit->orginal; $this->lastRequest[$unit->id] = time(); $text = IRCHelper::colorText('Romanji', IRCHelper::COLOR_ORANGE) . ': ' . $unit->orginal; $text .= ' ' . IRCHelper::colorText('Link', IRCHelper::COLOR_ORANGE) . ': ' . $unit->linkgc; $this->reply($text); } } else { $this->reply('Not found ' . $request); } R::close(); }
protected function random(array $arguments = []) { $arguments = $this->setApi($arguments); $arguments = $this->setRate($arguments); $tags = array_map('strtolower', array_merge($arguments, [$this->rate])); $url = $this->api . 'tags=' . trim(implode('+', $tags)); $html = $this->loadStreamUrl($url); if (!$html) { return $this->reply('I can\'t open api url.'); } $doc = $this->getDOM(); if (!$doc->loadHTML($html)) { return $this->reply('I can\'t open api url.'); } if ($doc->getElementsByTagName('posts')->length == 0 || empty($doc->getElementsByTagName('posts')->item(0)->getAttribute('count'))) { return $this->reply('Sorry, I didn\'t find ' . trim(implode(' ', $arguments)) . '.'); } $count = $doc->getElementsByTagName('posts')->item(0)->getAttribute('count'); $random = mt_rand(0, $count - 1); $pid = floor($random / 100); switch ($this->api) { case self::API_GEL: $url .= '&json=1&pid=' . $pid; break; case self::API_YAN: $url = str_ireplace('.xml', '.json', $url) . '&limit=100&page=' . $pid; break; } $newHtml = $this->loadStreamUrl($url); if (!$newHtml) { return $this->reply('Can\'t load html to dom.'); } $image = json_decode($newHtml)[$random % 100]; $url = $this->shortIt($image->file_url, $this->api == self::API_GEL ? $image->hash : $image->md5); $warn = ''; switch ($image->rating) { case self::RATING_NSFW: $warn = IRCHelper::colorText('NSFW', IRCHelper::COLOR_PINK); break; case self::RATING_SAFE: $warn = IRCHelper::colorText('Safe', IRCHelper::COLOR_GREEN); break; case static::RATING_QUESTIONABLE: $warn = IRCHelper::colorText('Questionable', IRCHelper::COLOR_ORANGE); break; } $tag = !empty($arguments) ? ' ' . trim(implode(' ', $arguments)) : ''; $this->reply("Random{$tag}: {$warn} {$url} (from {$count} pic)"); }
protected function action($arguments) { if (strtoupper($arguments[0]) == 'LIST') { $this->reply($this->getHelp()); return false; } if (!preg_match(self::SPELL_REGEX, $arguments[0], $matches)) { $this->reply($this->getHelp()); return false; } if (empty($matches[2])) { $this->reply($this->getHelp()); return false; } if (!($json = json_decode($this->loadStreamUrl("{$this->api}/{$matches[1]}/" . (int) $matches[2])))) { $this->reply($this->getHelp()); return false; } $string = IRCHelper::colorText('Name', IRCHelper::COLOR_ORANGE) . ': ' . $json->titleEN . ' '; $string .= IRCHelper::colorText('Character', IRCHelper::COLOR_ORANGE) . ': ' . $json->titleEN . ' '; $string .= IRCHelper::colorText('Stage', IRCHelper::COLOR_ORANGE) . ': ' . $json->titleEN; if (!empty($json->diff)) { $string .= IRCHelper::colorText(' Difficult level', IRCHelper::COLOR_ORANGE) . ': ' . $json->diff; } if (!empty($json->youtube)) { $string .= IRCHelper::colorText('YT', IRCHelper::COLOR_RED) . ': https://youtu.be/' . $json->youtube; } $this->reply($string); }
private function text($title, $type = self::TYPE_PAGE) { $text = trim($title); if (empty($text)) { return false; } switch ($type) { case self::TYPE_PAGE: return IRCHelper::colorText('Title', IRCHelper::COLOR_ORANGE) . ': ' . $text; case self::TYPE_IMG: return IRCHelper::colorText('Image', IRCHelper::COLOR_ORANGE) . ': ' . $text; case self::TYPE_ERROR: return IRCHelper::colorText('Error', IRCHelper::COLOR_RED) . ': ' . $text; case self::TYPE_VIDEO: return IRCHelper::colorText('Notice', IRCHelper::COLOR_ORANGE) . ': ' . $text; } }