๐Ÿ“ฆ Haroenv / Alfred-Workflows

๐Ÿ“„ chef.php ยท 122 lines
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122<?php

/*
Chef

*/

// ****************

require_once('cache.php');
require_once('workflows.php');

class Repo {
	
	private $id = 'chef';
	private $kind = 'plugins'; // for none found msg
	private $min_query_length = 1; // increase for slow DBs
	private $max_return = 25;
	
	private $cache;
	private $w;
	private $pkgs;
	
	function __construct() {
		
		$this->cache = new Cache();
		$this->w = new Workflows();
		
		// get DB here if not dynamic search
		//$data = (array) $this->cache->get_db($this->id);
		//$this->pkgs = $data;
	}
	
	// return id | url | pkgstr
	function makeArg($id, $url, $version) {
		return $id . "|" . $url . "|" . $id;//"\"$id\":\"$version\",";
	}
	
	function check($pkg, $query) {
		if (!$query) { return true; }

		if (   strpos($pkg->cookbook_name, $query) !== false
			|| strpos($pkg->cookbook_description, $query) !== false
		) {
			return true;
		}
		return false;
	}
	
	function search($query) {
		if ( strlen($query) < $this->min_query_length) {
			$this->w->result(
				"{$this->id}-min",
				$query,
				"Minimum query length of {$this->min_query_length} not met.",
				"",
				"icon-cache/{$this->id}.png"
			);
			return;
		}
		
		$this->pkgs = $this->cache->get_query_json($this->id, $query, "https://supermarket.getchef.com/api/v1/search?q={$query}");
		
		foreach ($this->pkgs->items as $pkg) {
			if ($this->check($pkg, $query)) {
				$title = $pkg->cookbook_name;
		
				// add author to title
				if (isset($pkg->cookbook_maintainer)) {
					$title .= " by {$pkg->cookbook_maintainer}";
				}
		
				$this->w->result(
					$pkg->cookbook_name,
					$this->makeArg($pkg->cookbook_name, "https://supermarket.getchef.com/cookbooks/{$pkg->cookbook_name}", "*"),
					$title,
					$pkg->cookbook_description,
					"icon-cache/{$this->id}.png"
				);
			}
			// only search till max return reached
			if ( count ( $this->w->results() ) == $this->max_return ) {
				break;
			}
		}
		
		if ( count( $this->w->results() ) == 0) {
			$this->w->result(
				"{$this->id}-search",
				"http://supermarket.getchef.com/cookbooks/{$query}",
				"No {$this->kind} were found that matched \"{$query}\"",
				"Click to see the results for yourself",
				"icon-cache/{$this->id}.png"
			);
		}
	}
	
	function xml() {
		
		$this->w->result(
			"{$this->id}-www",
			"http://supermarket.getchef.com/",
			"Go to the website",
			"http://supermarket.getchef.com",
			"icon-cache/{$this->id}.png"
		);
		
		return $this->w->toxml();
	}

}

// ****************

/*
$query = "or";
$repo = new Repo();
$repo->search($query);
echo $repo->xml();
*/

?>