private function getRawMessage(ChatRoom $room, string $link) { $messageID = $this->messageResolver->resolveMessageIDFromPermalink($link); $messageInfo = (yield $this->chatClient->getMessageHTML($room, $messageID)); $messageBody = html_entity_decode($messageInfo, ENT_QUOTES); return domdocument_load_html($messageBody, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); }
public function search(Command $command) : \Generator { if (!$command->hasParameters()) { return new Success(); } $uri = "https://www.google.com/search?q=site:xkcd.com+intitle%3a%22xkcd%3a+%22+" . urlencode(implode(' ', $command->getParameters())); /** @var HttpResponse $response */ $response = (yield $this->httpClient->request($uri)); if ($response->getStatus() !== 200) { return $this->chatClient->postMessage($command->getRoom(), "Useless error message here so debugging this is harder than needed."); } $dom = domdocument_load_html($response->getBody()); $nodes = (new \DOMXPath($dom))->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' g ')]/h3/a"); /** @var \DOMElement $node */ foreach ($nodes as $node) { if (preg_match('~^/url\\?q=(https://xkcd\\.com/\\d+/)~', $node->getAttribute('href'), $matches)) { return $this->chatClient->postMessage($command->getRoom(), $matches[1]); } } if (preg_match('/^(\\d+)$/', trim(implode(' ', $command->getParameters())), $matches) !== 1) { return $this->chatClient->postMessage($command->getRoom(), self::NOT_FOUND_COMIC); } /** @var HttpResponse $response */ $response = (yield $this->httpClient->request('https://xkcd.com/' . $matches[1])); if ($response->getStatus() !== 200) { return $this->chatClient->postMessage($command->getRoom(), self::NOT_FOUND_COMIC); } return $this->chatClient->postMessage($command->getRoom(), 'https://xkcd.com/' . $matches[1]); }
private function getMessage(HttpResponse $response) : string { $dom = domdocument_load_html($response->getBody()); if ($dom->getElementsByTagName('description')->length === 0) { return 'I dun goofed'; } preg_match('/([^:]+)/', $dom->getElementsByTagName('description')->item(2)->textContent, $before); preg_match('/\\:(.*)/', $dom->getElementsByTagName('description')->item(2)->textContent, $after); return '**[' . $before[0] . '](http://www.dictionary.com/browse/' . str_replace(" ", "-", $before[0]) . ')**' . $after[0]; }
public function __construct(array $data, ChatRoom $room) { parent::__construct($data, $room->getIdentifier()->getHost()); $this->room = $room; $this->userId = (int) $data['user_id']; $this->userName = (string) $data['user_name']; $this->messageId = (int) $data['message_id']; $this->messageContent = domdocument_load_html((string) ($data['content'] ?? ''), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $this->parentId = (int) ($data['parent_id'] ?? -1); $this->showParent = (bool) ($data['show_parent'] ?? false); }
private function getMessage(Command $command, string $url) { preg_match('~^http://chat\\.stackoverflow\\.com/transcript/message/(\\d+)(?:#\\d+)?$~', $url, $matches); $messageInfo = (yield $this->chatClient->getMessageHTML($command->getRoom(), (int) $matches[1])); $messageBody = html_entity_decode($messageInfo, ENT_QUOTES); $dom = domdocument_load_html($messageBody, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $this->replaceEmphasizeTags($dom); $this->replaceStrikeTags($dom); $this->replaceImages($dom); $this->replaceHrefs($dom); return $this->removePings($dom->textContent); }
private function getMessage(HttpResponse $response) : string { $dom = domdocument_load_html($response->getBody()); if ($dom->getElementsByTagName('resultset')->length === 0) { return 'I cannot find that title.'; } /** @var \DOMElement $result */ $result = $dom->getElementsByTagName('imdbentity')->item(0); /** @var \DOMText $titleNode */ $titleNode = $result->firstChild; return sprintf('[ [%s](%s) ] %s', $titleNode->wholeText, 'http://www.imdb.com/title/' . $result->getAttribute('id'), $result->getElementsByTagName('description')->item(0)->textContent); }
public function search(Command $command) : \Generator { if (!$command->hasParameters()) { return new Success(); } /** @var HttpResponse $response */ $response = (yield $this->httpClient->request("https://man.freebsd.org/" . rawurlencode(implode("%20", $command->getParameters())))); $dom = domdocument_load_html($response->getBody()); $xpath = new \DOMXPath($dom); if (!$this->isFound($xpath)) { return $this->postNoResult($command); } return $this->postResult($command, $xpath, $response->getRequest()->getUri()); }
private function getOpenGrokSearchResults(string $branch, array $params) : \Generator { $branch = $branch === self::DEFAULT_BRANCH ? $branch : ($branch = 'PHP-' . $branch); $url = self::BASE_URL . '?project=' . $branch . '&n=10000&' . http_build_query($params); try { $request = (new HttpRequest())->setMethod('GET')->setUri($url); $this->cookieJar->store(new Cookie('OpenGrokProject', $branch, null, null, self::BASE_URL)); /** @var HttpResponse $response */ $response = (yield $this->httpClient->request($request)); /** @var \DOMDocument $doc */ $doc = domdocument_load_html($response->getBody()); } catch (\Throwable $e) { throw new OpenGrokSearchFailureException("Totally failed to get a valid [results page]({$url})", 1); } if (!($resultsDiv = $doc->getElementById('results'))) { throw new OpenGrokSearchFailureException("The [results page]({$url}) is not in the format I expected it to be", 1); } try { $resultsTable = xpath_get_element($resultsDiv, './table'); } catch (ElementNotFoundException $e) { throw new OpenGrokSearchFailureException("There were no [results]({$url}) for that search", 0); } $dir = null; $tests = false; $trim = strlen('/xref/' . $branch); $results = ['url' => $url, 'count' => 0, 'code' => [], 'tests' => []]; $baseUrl = new Uri($url); foreach ($resultsTable->getElementsByTagName('tr') as $row) { /** @var \DOMElement $row */ if (preg_match('#\\bdir\\b#', $row->getAttribute('class'))) { $tests = (bool) preg_match('#/tests/#i', xpath_get_element($row, './td/a')->textContent); continue; } foreach (xpath_get_elements($row, "./td/tt/a[@class='s']") as $resultAnchor) { $hrefAttr = $resultAnchor->getAttribute('href'); $path = substr($hrefAttr, $trim); $href = (string) $baseUrl->resolve($hrefAttr); $el = xpath_get_element($resultAnchor, "./span[@class='l']"); $line = $el->textContent; $code = ''; while ($el = $el->nextSibling) { $code .= $el->textContent; } $results[$tests ? 'tests' : 'code'][] = ['href' => $href, 'path' => $path, 'line' => $line, 'code' => trim(preg_replace('#\\s+#', ' ', $code))]; $results['count']++; } } return $results; }
private function getResultFromSearchFallback(string $vendor, string $package) : \Generator { $url = 'https://packagist.org/search/?q=' . urlencode($vendor) . '%2F' . urldecode($package); /** @var HttpResponse $response */ $response = (yield $this->httpClient->request($url)); $dom = domdocument_load_html($response->getBody()); $nodes = (new \DOMXPath($dom))->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' packages ')]/li"); if ($nodes->length === 0) { throw new \RuntimeException('Search page contains no results'); } /** @var \DOMElement $node */ $node = $nodes->item(0); if (!$node->hasAttribute('data-url')) { throw new \RuntimeException('First result has no URL'); } return (yield $this->httpClient->request('https://packagist.org' . $node->getAttribute('data-url') . '.json')); }
public function search(Command $command) : \Generator { if (!$command->hasParameters()) { return new Success(); } $text = implode(' ', $command->getParameters()); $searchTerm = (yield $this->messageResolver->resolveMessageText($command->getRoom(), $text)); $uri = $this->getSearchURL($searchTerm); $request = (new HttpRequest())->setMethod('GET')->setUri($uri)->setHeader('User-Agent', self::USER_AGENT); /** @var HttpResponse $response */ $response = (yield $this->httpClient->request($request)); if ($response->getStatus() !== 200) { return $this->chatClient->postMessage($command->getRoom(), "Google responded with {$response->getStatus()}"); } if (preg_match('#charset\\s*=\\s*([^;]+)#i', trim(implode(', ', $response->getHeader('Content-Type'))), $match) && !preg_match('/' . preg_quote(self::ENCODING, '/') . '/i', $match[1])) { $body = iconv($match[1], self::ENCODING, $response->getBody()); } if (empty($body)) { $body = $response->getBody(); } $dom = domdocument_load_html($body); $xpath = new \DOMXPath($dom); $nodes = $this->getResultNodes($xpath); if ($nodes->length === 0) { return $this->postNoResultsMessage($command); } $searchResults = $this->getSearchResults($nodes, $xpath); $postMessage = $this->getPostMessage($searchResults, $uri, $searchTerm); return $this->chatClient->postMessage($command->getRoom(), $postMessage); }
private function getMessageFromSearch(HttpResponse $response) : \Generator { try { $dom = domdocument_load_html($response->getBody()); /** @var \DOMElement $firstResult */ $firstResult = $dom->getElementById("quickref_functions")->getElementsByTagName("li")->item(0); /** @var \DOMElement $anchor */ $anchor = $firstResult->getElementsByTagName("a")->item(0); $response = (yield $this->httpClient->request(self::URL_BASE . $anchor->getAttribute("href"))); return $this->getMessageFromMatch($response); } catch (\Throwable $e) { return 'Something went badly wrong with that lookup... ' . $e->getMessage(); } }
/** * @param ChatRoom|ChatRoomIdentifier $room * @return Promise */ public function getPinnedMessages($room) : Promise { $url = $this->urlResolver->getEndpointURL($room, ChatRoomEndpoint::CHATROOM_STARS_LIST); return resolve(function () use($url) { /** @var HttpResponse $response */ $this->logger->log(Level::DEBUG, 'Getting pinned messages'); $response = (yield $this->httpClient->request($url)); $doc = domdocument_load_html($response->getBody()); try { $pinnedEls = xpath_get_elements($doc, ".//li[./span[contains(concat(' ', normalize-space(@class), ' '), ' owner-star ')]]"); } catch (ElementNotFoundException $e) { return []; } $result = []; foreach ($pinnedEls as $el) { $result[] = (int) explode('_', $el->getAttribute('id'))[1]; } $this->logger->log(Level::DEBUG, 'Got pinned messages: ' . implode(',', $result)); return $result; }); }
private static function parseVotes(string $html) { $dom = domdocument_load_html($html); $votes = []; /** @var \DOMElement $form */ foreach ($dom->getElementsByTagName('form') as $form) { if ($form->getAttribute('name') != 'doodle__form') { continue; } $id = $form->getAttribute('id'); $info = ['name' => $id, 'votes' => []]; $options = []; /** @var \DOMElement $table */ $table = $form->getElementsByTagName('table')->item(0); /** @var \DOMElement $row */ foreach ($table->getElementsByTagName('tr') as $row) { $class = $row->getAttribute('class'); if ($class === 'row0') { // Title $title = trim($row->getElementsByTagName('th')->item(0)->textContent); if (!empty($title)) { $info['name'] = $title; } continue; } if ($class == 'row1') { // Options /** @var \DOMElement $opt */ foreach ($row->getElementsByTagName('td') as $i => $opt) { $options[$i] = strval($opt->textContent); $info['votes'][$options[$i]] = 0; } continue; } /** @var \DOMElement $vote */ foreach ($row->getElementsByTagName('td') as $i => $vote) { // Adjust by one to ignore voter name if ($vote->getElementsByTagName('img')->length > 0) { ++$info['votes'][$options[$i - 1]]; } } } $votes[$id] = $info; } return $votes; }
private function logInMainSite(\DOMDocument $doc, Credentials $credentials) { $url = $this->getLogInURL(new \DOMXPath($doc)); /** @var HttpResponse $response */ $response = (yield from $this->authenticator->logIn($url, $credentials)); $doc = domdocument_load_html($response->getBody()); if (!$this->isLoggedInMainSite($doc)) { throw new \RuntimeException('Still not logged in'); //todo } return new \DOMXPath($doc); }