2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
2022-06-22 18:32:54 +02:00
|
|
|
|
2016-12-06 22:45:52 +01:00
|
|
|
class GoogleSearchBridge extends BridgeAbstract
|
|
|
|
{
|
2017-02-11 16:16:56 +01:00
|
|
|
const MAINTAINER = 'sebsauvage';
|
|
|
|
const NAME = 'Google search';
|
|
|
|
const URI = 'https://www.google.com/';
|
2023-07-23 23:05:35 +02:00
|
|
|
const CACHE_TIMEOUT = 60 * 30; // 30m
|
2022-06-22 18:32:54 +02:00
|
|
|
const DESCRIPTION = 'Returns max 100 results from the past year.';
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2022-06-22 18:32:54 +02:00
|
|
|
const PARAMETERS = [[
|
2022-07-01 15:10:30 +02:00
|
|
|
'q' => [
|
2018-06-29 23:55:33 +02:00
|
|
|
'name' => 'keyword',
|
2022-06-22 18:32:54 +02:00
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'rss-bridge',
|
2022-07-01 15:10:30 +02:00
|
|
|
],
|
2022-06-24 11:18:27 +02:00
|
|
|
'verbatim' => [
|
|
|
|
'name' => 'Verbatim',
|
2022-06-22 18:32:54 +02:00
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Use literal keyword(s) without making improvements',
|
2022-07-01 15:10:30 +02:00
|
|
|
],
|
2022-06-22 18:32:54 +02:00
|
|
|
]];
|
2015-11-05 15:50:18 +00:00
|
|
|
|
2016-12-06 22:45:52 +01:00
|
|
|
public function collectData()
|
2022-07-01 15:10:30 +02:00
|
|
|
{
|
2022-10-26 00:47:45 +02:00
|
|
|
// todo: wrap this in try..catch because 429 too many requests happens a lot
|
2022-06-24 11:18:27 +02:00
|
|
|
$dom = getSimpleHTMLDOM($this->getURI(), ['Accept-language: en-US']);
|
|
|
|
if (!$dom) {
|
|
|
|
returnServerError('No results for this query.');
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2022-06-24 11:18:27 +02:00
|
|
|
$result = $dom->find('div[id=res]', 0);
|
2016-12-06 22:45:52 +01:00
|
|
|
|
2022-06-22 18:32:54 +02:00
|
|
|
if (!$result) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-06 22:45:52 +01:00
|
|
|
|
2022-06-22 18:32:54 +02:00
|
|
|
foreach ($result->find('div[class~=g]') as $element) {
|
|
|
|
$item = [];
|
2016-12-06 22:45:52 +01:00
|
|
|
|
2022-06-22 18:32:54 +02:00
|
|
|
$url = $element->find('a[href]', 0)->href;
|
|
|
|
$item['uri'] = htmlspecialchars_decode($url);
|
|
|
|
$item['title'] = $element->find('h3', 0)->plaintext;
|
2016-12-06 22:45:52 +01:00
|
|
|
|
2022-06-22 18:32:54 +02:00
|
|
|
$resultDom = $element->find('div[data-content-feature=1]', 0);
|
|
|
|
if ($resultDom) {
|
|
|
|
// Split by — or ·
|
|
|
|
$resultParts = preg_split('/( — | · )/', $resultDom->plaintext);
|
|
|
|
$resultDate = trim($resultParts[0]);
|
|
|
|
$resultContent = trim($resultParts[1] ?? '');
|
|
|
|
} else {
|
|
|
|
// Some search results don't have this particular dom identifier
|
|
|
|
$resultDate = null;
|
|
|
|
$resultContent = null;
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2022-04-16 21:16:38 +00:00
|
|
|
|
2022-06-22 18:32:54 +02:00
|
|
|
if ($resultDate) {
|
2022-07-01 15:10:30 +02:00
|
|
|
try {
|
2022-06-22 18:32:54 +02:00
|
|
|
$createdAt = new \DateTime($resultDate);
|
|
|
|
// Set to midnight for consistent datetime
|
|
|
|
$createdAt->setTime(0, 0);
|
|
|
|
$item['timestamp'] = $createdAt->format('U');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$item['timestamp'] = 0;
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2022-06-22 18:32:54 +02:00
|
|
|
} else {
|
|
|
|
$item['timestamp'] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($resultContent) {
|
|
|
|
$item['content'] = $resultContent;
|
|
|
|
}
|
2016-12-06 22:45:52 +01:00
|
|
|
|
2022-06-22 18:32:54 +02:00
|
|
|
$this->items[] = $item;
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2022-06-22 18:32:54 +02:00
|
|
|
// Sort by descending date
|
|
|
|
usort($this->items, function ($a, $b) {
|
|
|
|
return $b['timestamp'] <=> $a['timestamp'];
|
2022-07-01 15:10:30 +02:00
|
|
|
});
|
2016-12-06 22:45:52 +01:00
|
|
|
}
|
2022-06-22 18:32:54 +02:00
|
|
|
|
2019-10-16 19:44:28 +00:00
|
|
|
public function getURI()
|
2022-07-01 15:10:30 +02:00
|
|
|
{
|
2022-06-22 18:32:54 +02:00
|
|
|
if ($this->getInput('q')) {
|
|
|
|
$queryParameters = [
|
|
|
|
'q' => $this->getInput('q'),
|
|
|
|
'hl' => 'en',
|
|
|
|
'num' => '100', // get 100 results
|
|
|
|
'complete' => '0',
|
|
|
|
// in past year, sort by date, optionally verbatim
|
2022-04-16 21:16:38 +00:00
|
|
|
'tbs' => 'qdr:y,sbd:1' . ($this->getInput('verbatim') ? ',li:1' : ''),
|
2022-06-22 18:32:54 +02:00
|
|
|
];
|
|
|
|
return sprintf('https://www.google.com/search?%s', http_build_query($queryParameters));
|
2016-12-06 22:45:52 +01:00
|
|
|
}
|
|
|
|
|
2022-06-22 18:32:54 +02:00
|
|
|
return parent::getURI();
|
2019-10-16 19:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
2022-07-01 15:10:30 +02:00
|
|
|
{
|
2017-07-29 19:28:00 +02:00
|
|
|
if (!is_null($this->getInput('q'))) {
|
2019-10-16 19:44:28 +00:00
|
|
|
return $this->getInput('q') . ' - Google search';
|
|
|
|
}
|
|
|
|
|
2017-02-14 22:20:55 +01:00
|
|
|
return parent::getName();
|
2016-12-06 22:45:52 +01:00
|
|
|
}
|
2014-05-21 19:15:52 +02:00
|
|
|
}
|