[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
{
const MAINTAINER = 'corenting';
const MAINTAINER = 'corenting, User123698745';
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 DESCRIPTION = 'Returns a specified repositories search (sorted by recently updated)';
const PARAMETERS = [ [
@ -18,60 +19,73 @@ class GithubSearchBridge extends BridgeAbstract
public function collectData()
{
$params = ['utf8' => '✓',
'q' => urlencode($this->getInput('s')),
's' => 'updated',
'o' => 'desc',
'type' => 'Repositories'];
$url = self::URI . 'search?' . http_build_query($params);
$html = getSimpleHTMLDOM(self::getURI());
$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['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;
$uri = substr(self::URI, 0, -1) . $uri;
$item['uri'] = $uri;
$title = $element->find('.f4', 0)->plaintext;
$item['title'] = $title;
$categories = [];
// Description
if (count($element->find('p.mb-1')) != 0) {
$content = $element->find('p.mb-1', 0)->innertext;
$content = '<p>';
if (isset($descriptionElement)) {
$content .= trim($descriptionElement->plaintext);
} else {
$content = 'No description';
$content .= 'No description';
}
$content .= '</p>';
// Tags
$content = $content . '<br />';
$tags = $element->find('a.topic-tag');
$tags_array = [];
if (count($tags) != 0) {
$content = $content . 'Tags: ';
foreach ($tags as $tag_element) {
$tag_link = 'https://github.com' . $tag_element->href;
$tag_name = trim($tag_element->innertext);
$content = $content . '<a href="' . $tag_link . '">' . $tag_name . '</a> ';
array_push($tags_array, $tag_element->innertext);
// Topics
if (count($topicElements) > 0) {
$content .= '<p>';
$content .= 'Topics: ';
foreach ($topicElements as $topicElement) {
$topicLink = self::BASE_URI . $topicElement->href;
$topicTitle = trim($topicElement->plaintext);
$content .= '<a href="' . $topicLink . '">' . $topicTitle . '</a> ';
$categories[] = $topicTitle;
}
$content .= '</p>';
}
// Programming language
if (count($element->find('span[itemprop=programmingLanguage]')) != 0) {
$language = $element->find('span[itemprop=programmingLanguage]', 0)->innertext;
$content = $content . '<br />';
$content = $content . 'Language: ' . $language;
if (isset($languageElement)) {
$content .= '<p>';
$content .= 'Language: ';
$content .= trim($languageElement->plaintext);
$content .= '</p>';
}
$item['categories'] = $tags_array;
$item['content'] = $content;
$date = $element->find('relative-time', 0)->datetime;
$item['timestamp'] = strtotime($date);
$item['categories'] = $categories;
$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;
}
}