forked from willfarrell/alfred-pkgman-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metacran.php
54 lines (44 loc) · 1.57 KB
/
Metacran.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace WillFarrell\AlfredPkgMan;
require_once('Cache.php');
require_once('Repo.php');
class Metacran extends Repo
{
protected $id = 'metacran';
protected $kind = 'package';
protected $url = 'https://www.r-pkg.org';
protected $search_url = 'http://seer.r-pkg.org:9200/_search?q=';
public function search($query)
{
if (!$this->hasMinQueryLength($query)) {
return $this->xml();
}
$this->pkgs = $this->cache->get_query_json(
$this->id,
$query,
"{$this->search_url}{$query}&size={$this->max_return}"
)->hits->hits;
foreach ($this->pkgs as $pkg) {
// make params
$title = "{$pkg->_source->Package} (v{$pkg->_source->Version}) - Score: {$pkg->_score}";
$url = "{$this->url}/pkg/{$pkg->_source->Package}";
$details = "{$pkg->_source->Description}";
$this->cache->w->result(
$pkg->_source->Package,
$this->makeArg($pkg->_source->Package, $url),
$title,
$details,
"icon-cache/{$this->id}.png"
);
// only search till max return reached
if (count($this->cache->w->results()) == $this->max_return) {
break;
}
}
$this->noResults($query, "{$this->url}/search.html?q={$query}");
return $this->xml();
}
}
// Test code, uncomment to debug this script from the command-line
// $repo = new Metacran();
// echo $repo->search('ggplot2');