-
Notifications
You must be signed in to change notification settings - Fork 3
/
jsbot
executable file
·151 lines (125 loc) · 2.85 KB
/
jsbot
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/env ruby
# Author: Joseph Pecoraro
# Date: Friday March 6, 2009
# Description: Simple Interface for the
# really neat jsbot!
require 'rubygems'
require 'open-uri'
require 'json'
require 'cgi'
require File.dirname(__FILE__) + '/escape'
class JSBot
JSON_PREFIX = 'http://fn-js.info/jsbot.xhr?'
SITE_PREFIX = 'http://js.isite.net.au/jsbot?'
def search(str)
uri = url(str, JSON_PREFIX, "search=")
JSON.parse( open( uri ).read )
end
def show(str)
uri = url(str, JSON_PREFIX, "show=")
JSON.parse( open( uri ).read )
end
def url(str, u=SITE_PREFIX, q='q=')
u + q + CGI::escape(str)
end
end
class JSBotDriver
URL_REGEX = /(?:http|https):\/\/[a-z0-9]+(?:[\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(?:\:[0-9]{1,5})?(?:\/.*?)?(?=\s|$|[\)\],])/ix
MAX_LENGTH = 80 - '99: '.length
MAX_RESULTS = 15
def initialize(args)
@bot = JSBot.new
query(args)
puts
end
def query(args)
mode = :show
args.each_index do |i|
arg = ARGV[i]
if arg =~ /^-l$|^--(list|search)$/
mode = :list
args[i] = nil
elsif arg =~ /^-s$|^--show$/
mode = :show
args[i] = nil
elsif arg =~ /^-o$|^--open$/
mode = :open
args[i] = nil
else
break
end
end
args.delete_if { |elem| elem.nil? }
usage if args.size < 1
str = args.join(' ')
case mode
when :show then show(str)
when :open then page(str)
when :list then list(str)
end
end
def list(str)
num_regex = /\s+(\d+)$/
is_num = false
if match = str.match(num_regex)
is_num = true
num = match[1].to_i
str.sub!(num_regex, '')
end
puts
results = @bot.search(str)
if is_num
print_result( results[num] )
elsif results.size == 0
puts 'No Results Found.'
else
results.each_with_index do |elem, i|
term = elem['term']
value = elem['value'].gsub(URL_REGEX,'--URL--')[0, MAX_LENGTH-term.length]
puts( '%2d: %s - %s' % [ i, term, value ] )
break if i >= MAX_RESULTS
end
end
end
def show(str)
elem = @bot.show(str)
print_result( elem )
end
def page(str)
system Escape.shell_command( ['open', @bot.url(str)] ).to_s
end
def print_result(elem)
puts
if elem.nil? || elem=={}
puts 'No Results Found'
else
hrefs = elem['value'].scan(URL_REGEX)
puts 'Response: '
puts "#{elem['term']} - #{elem['value']}"
if hrefs.size > 0
puts
puts 'Opening...'
hrefs.each do |href|
puts href
system Escape.shell_command( ['open', href] ).to_s
end
end
end
end
def usage
puts 'usage: jsbot [options] term [#]'
puts
puts 'options:'
puts ' --show | -s open the top result [default]'
puts ' --open | -o go to the jsbot results page'
puts ' --list | -l | --search show top 15 results'
puts ' --list ... # open the #\'d result'
puts
puts 'term examples:'
puts ' dom ref'
puts ' js ref ceil'
puts
exit
end
end
JSBotDriver.new( ARGV )