[GithubSearchBridge] repair bridge / handle new search ui (#3647)

This commit is contained in:
User123698745 2023-09-04 03:00:08 +02:00 committed by GitHub
parent b9fdd20f8f
commit 99b86c0e1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,9 +2,10 @@
class GithubSearchBridge extends BridgeAbstract class GithubSearchBridge extends BridgeAbstract
{ {
const MAINTAINER = 'corenting'; const MAINTAINER = 'corenting, User123698745';
const NAME = 'Github Repositories Search'; const NAME = 'Github Repositories Search';
const URI = 'https://github.com/'; const BASE_URI = 'https://github.com';
const URI = self::BASE_URI . '/search';
const CACHE_TIMEOUT = 600; // 10min const CACHE_TIMEOUT = 600; // 10min
const DESCRIPTION = 'Returns a specified repositories search (sorted by recently updated)'; const DESCRIPTION = 'Returns a specified repositories search (sorted by recently updated)';
const PARAMETERS = [ [ const PARAMETERS = [ [
@ -18,60 +19,73 @@ class GithubSearchBridge extends BridgeAbstract
public function collectData() public function collectData()
{ {
$params = ['utf8' => '✓', $html = getSimpleHTMLDOM(self::getURI());
'q' => urlencode($this->getInput('s')),
's' => 'updated',
'o' => 'desc',
'type' => 'Repositories'];
$url = self::URI . 'search?' . http_build_query($params);
$html = getSimpleHTMLDOM($url); $resultElement = $html->find('[data-testid="results-list"]', 0);
foreach ($resultElement->children as $element) {
$titleElement = $element->find('.search-title', 0);
$descriptionElement = $element->find('div > .search-match', 0);
$topicElements = $element->find('a[href^="/topic"]');
$languageElement = $element->find('li [aria-label$="language"]', 0);
$dateElement = $element->find('li [title*=" "]', 0);
foreach ($html->find('li.repo-list-item') as $element) {
$item = []; $item = [];
$item['uri'] = self::BASE_URI . $titleElement->find('a', 0)->href;
$item['title'] = trim($titleElement->plaintext);
$item['timestamp'] = strtotime($dateElement->attr['title']);
$uri = $element->find('.f4 a', 0)->href; $categories = [];
$uri = substr(self::URI, 0, -1) . $uri;
$item['uri'] = $uri;
$title = $element->find('.f4', 0)->plaintext;
$item['title'] = $title;
// Description // Description
if (count($element->find('p.mb-1')) != 0) { $content = '<p>';
$content = $element->find('p.mb-1', 0)->innertext; if (isset($descriptionElement)) {
$content .= trim($descriptionElement->plaintext);
} else { } else {
$content = 'No description'; $content .= 'No description';
} }
$content .= '</p>';
// Tags // Topics
$content = $content . '<br />'; if (count($topicElements) > 0) {
$tags = $element->find('a.topic-tag'); $content .= '<p>';
$tags_array = []; $content .= 'Topics: ';
if (count($tags) != 0) { foreach ($topicElements as $topicElement) {
$content = $content . 'Tags: '; $topicLink = self::BASE_URI . $topicElement->href;
foreach ($tags as $tag_element) { $topicTitle = trim($topicElement->plaintext);
$tag_link = 'https://github.com' . $tag_element->href; $content .= '<a href="' . $topicLink . '">' . $topicTitle . '</a> ';
$tag_name = trim($tag_element->innertext); $categories[] = $topicTitle;
$content = $content . '<a href="' . $tag_link . '">' . $tag_name . '</a> ';
array_push($tags_array, $tag_element->innertext);
} }
$content .= '</p>';
} }
// Programming language // Programming language
if (count($element->find('span[itemprop=programmingLanguage]')) != 0) { if (isset($languageElement)) {
$language = $element->find('span[itemprop=programmingLanguage]', 0)->innertext; $content .= '<p>';
$content .= 'Language: ';
$content = $content . '<br />'; $content .= trim($languageElement->plaintext);
$content = $content . 'Language: ' . $language; $content .= '</p>';
} }
$item['categories'] = $tags_array;
$item['content'] = $content; $item['content'] = $content;
$date = $element->find('relative-time', 0)->datetime; $item['categories'] = $categories;
$item['timestamp'] = strtotime($date);
$this->items[] = $item; $this->items[] = $item;
} }
} }
public function getURI()
{
$searchValue = $this->getInput('s');
if (isset($searchValue)) {
$params = [
'q' => $searchValue,
'type' => 'repositories',
's' => 'updated',
'o' => 'desc',
];
return self::URI . '?' . http_build_query($params);
}
return self::URI;
}
} }