-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgshell
executable file
·151 lines (134 loc) · 5.24 KB
/
gshell
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/php
<?PHP
// Google Search shell 1.0.0
// Original Author: Tyler Hall <[email protected]>
// Latest Source and Bug Tracker: http://github.com/tylerhall/google-shell
//
// Programatic interface to Google's search resutls.
//
// This code is licensed under the MIT Open Source License.
// Copyright (c) 2010 [email protected]
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
$shortops = 'c::n::utajf';
$longopts = array('count::', 'number::', 'url', 'title', 'abstract', 'help', 'json', 'no-format');
$options = getopt($shortops, $longopts);
if($GLOBALS['argc'] == 1 || array_key_exists('help', $options))
{
echo "Usage: " . $GLOBALS['argv'][0] . " [OPTION]... QUERY\n";
echo "Programatic interface to Google's search resutls.\n";
echo "\n";
echo "-c, --count number of results to return\n";
echo "-n, --number returns only the nth result\n";
echo "-u, --url returns only the results' urls\n";
echo "-t, --title returns only the results' title\n";
echo "-a, --abstract returns only the results' abstract\n";
echo "-j, --json format results in json\n";
echo "-f, --no-format returns results without any additional formatting\n";
echo " --help display this message and exit\n";
echo "\n";
echo "Report bugs to <[email protected]>\n";
exit;
}
$query = $GLOBALS['argv'][$GLOBALS['argc'] - 1];
$query = urlencode($query);
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&q=' . $query;
$json_raw = geturl($url);
$json = json_decode($json_raw);
$results = array();
foreach($json->responseData->results as $gresult)
{
$result = new Result();
$result->url = $gresult->unescapedUrl;
$result->title = $gresult->titleNoFormatting;
$result->abstract = trim(strip_tags($gresult->content));
$results[] = $result;
}
if(array_key_exists('c', $options) || array_key_exists('count', $options))
{
if(array_key_exists('c', $options)) $count = $options['c'];
if(array_key_exists('count', $options)) $count = $options['count'];
$results = array_slice($results, 0, $count);
}
if(array_key_exists('n', $options) || array_key_exists('number', $options))
{
if(array_key_exists('n', $options)) $number = $options['n'];
if(array_key_exists('number', $options)) $number = $options['number'];
$results = array_slice($results, $number, 1);
}
if(array_key_exists('u', $options) || array_key_exists('t', $options) || array_key_exists('a', $options) ||
array_key_exists('url', $options) || array_key_exists('title', $options) || array_key_exists('abstract', $options))
{
$array = array('u' => true, 't' => true, 'a' => true);
if(array_key_exists('u', $options) || array_key_exists('url', $options))
unset($array['u']);
if(array_key_exists('t', $options) || array_key_exists('title', $options))
unset($array['t']);
if(array_key_exists('a', $options) || array_key_exists('abstract', $options))
unset($array['a']);
if(isset($array['u']))
foreach($results as &$result)
unset($result->url);
if(isset($array['t']))
foreach($results as &$result)
unset($result->title);
if(isset($array['a']))
foreach($results as &$result)
unset($result->abstract);
}
if(array_key_exists('j', $options) || array_key_exists('json', $options))
{
echo json_encode($results);
exit;
}
$no_format = array_key_exists('f', $options) || array_key_exists('no-format', $options) ? true : false;
for($i = 0; $i < count($results); $i++)
{
$result = $results[$i];
if($no_format)
{
foreach(get_object_vars($result) as $key => $val)
echo $val . "\n";
}
else
{
foreach(get_object_vars($result) as $key => $val)
{
echo ($i + 1) . ") " . ucwords($key) . ": $val\n";
}
echo "\n";
}
}
function geturl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0");
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
class Result
{
public $url;
public $title;
public $abstract;
}