-
Notifications
You must be signed in to change notification settings - Fork 3
/
skreemr
executable file
·281 lines (247 loc) · 6.3 KB
/
skreemr
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env ruby
# Author: Joseph Pecoraro
# Contributor: Michael Fellinger (manveru)
# Date: Friday February 20, 2009
# Description: Download MP3s from Skreemr
require 'rubygems'
require 'hpricot'
require 'cgi'
require 'open-uri'
require 'readline'
require File.dirname(__FILE__) + '/escape'
class Skreemr
attr_reader :results, :hrefs
MAGIC = { 'Accept-Language' => 'en-us' }
URI_PREFIX = 'http://skreemr.com/results.jsp?q='
PAGE_PREFIX = '&l=10&s='
PER_PAGE = 10
def url(str, start=0)
return URI_PREFIX if str.nil?
uri = URI_PREFIX + CGI::escape(str)
uri += PAGE_PREFIX + (PER_PAGE*start).to_s if start > 0
uri
end
def query(str, start=0)
uri = url(str, start)
doc = Hpricot( open(uri, MAGIC).read )
@results = []; @hrefs = []
details = nil; file = nil
(doc/'span.title').each do |span|
if file.nil? && span.inner_text =~ /File:/
@hrefs.push(span.next_sibling)
file = span.next_sibling.inner_text.strip
elsif details.nil? && span.inner_text =~ /Details:/
details = span.parent.inner_text.strip
details.gsub!(/Details:.*?\-/,'').gsub!(/\s+/,' ').strip!
details.gsub!(/\d{2}:(\d\d:\d\d)/,'\1')
end
if !file.nil? && !details.nil?
@results.push( file + ' (' + details + ')' )
details = nil; file = nil
end
end
@results
end
end
class SkreemrShell
HISTORY_SIZE = 10
HISTORY_FILE = File.expand_path('~/.skreemr')
HOME = File.expand_path('~')
def initialize
@skreem = Skreemr.new
@query_string = ''
@history = []
@list = []
@page = 0
puts
puts '---------------------'
puts ' Skreemr Mp3 Shell'
puts '---------------------'
load_history
await
end
def prompt
puts
Readline::readline('>> ', true)
end
def await
while input = prompt
begin
puts
args = input.split
case args.shift
when 'c', 'clear' then system 'clear'
when 'q', 'quit' then break
when 'd', 'download' then download(args)
when 'o', 'open' then open(args)
when 'n', 'next' then next_page(args)
when 'p', 'prev' then prev_page(args)
when 'b', 'past', 'history' then past(args)
when 'h', 'help' then help
when 'u', 'url' then copy(args)
when 's', 'search' then search(args)
when 'k', 'skreemr' then skreemr
when 'i', 'itunes' then itunes(args)
else puts 'Bad Command. Try `h` for help.'
end
rescue Exception => e
puts 'Error with the last command. Try `h` for help.'
puts e.to_s
end
end
write_history
puts
end
def skreemr
url = @skreem.url(@query_sting, @page)
system Escape.shell_command( ['open', url] ).to_s
end
def search(args)
@page = 0
@query_sting = args.join(' ')
@list = @skreem.query(@query_sting)
print_list(@list)
end
def next_page(args)
@page += 1
@list = @skreem.query(@query_sting, @page)
print_list(@list)
end
def prev_page(args)
@page -= 1
@page = 0 if @page < 0
@list = @skreem.query(@query_sting, @page)
print_list(@list)
end
def download(args)
empty_list_msg if @list.empty?
args.each do |num|
elem = @skreem.hrefs[ num.to_i ]
name = elem.inner_text.strip
link = elem['href']
name += File.extname(link)
system Escape.shell_command( ['curl', link, '-o', "#{HOME}/Desktop/#{name}"] ).to_s
end
end
def open(args)
if @list.empty?
empty_list_msg
else
link = @skreem.hrefs[ args[0].to_i ]
href = link['href']
system Escape.shell_command( ['open', href] ).to_s
end
end
def copy(args)
if @list.empty?
empty_list_msg
else
link = @skreem.hrefs[ args[0].to_i ]
href = link['href']
IO.popen('pbcopy', 'w') { |io| io.write(href) }
puts 'Copied to Clipboard'
end
end
def itunes(args)
url = 'itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?term='
url += CGI::escape(@query_sting)
system Escape.shell_command( ['open', url] ).to_s
end
def help
puts '------------'
puts ' Commands'
puts '------------'
puts
puts ' s STR | search --> search'
puts ' k | skreemr --> open the skreemr website'
puts ' d #s | download --> download to your desktop'
puts ' o # | open --> open in browser'
puts ' u # | url --> copy url to clipboard'
puts ' n | next --> next page'
puts ' p | prev --> previous page'
puts ' c | clear --> clear screen'
puts ' h | help --> help menu'
puts ' i | itunes --> search iTunes'
puts ' b | past | history --> list past searches'
puts ' b # | past | history --> repeat a past search'
puts ' q | quit --> quit'
end
def past(args)
if args.empty?
puts '-----------------'
puts ' Past Searches'
puts '-----------------'
puts
load_past
@history.reverse!.each_with_index do |entry, i|
puts( "[%d] %s" % [i, entry] )
end
else
search( [ @history[ args[0].to_i ] ] )
end
end
private
def empty_list_msg
puts 'Make a Search First!'
end
def load_past
@history = []
Readline::HISTORY.to_a.reverse.each do |entry|
if m = entry.match( /^s\s+(.*)$/ )
@history.push(m[1])
break if @history.size >= HISTORY_SIZE
end
end
end
def print_list(arr)
if arr.empty?
puts 'No Results'
else
arr.each_with_index do |result, i|
puts( "[%d] %s" % [i, result] )
end
end
end
def load_history
if File.exists?(HISTORY_FILE)
File.readlines(HISTORY_FILE).each do |entry|
Readline::HISTORY.push(entry)
end
end
load_past
end
def write_history
hist = Readline::HISTORY.to_a
hist = hist[-HISTORY_SIZE, HISTORY_SIZE] if hist.size > HISTORY_SIZE
File.open( File.expand_path(HISTORY_FILE), 'w') do |f|
hist.each do |entry|
f.puts(entry)
end
end
end
end
# ---------------------------
# Completion Enhancements
# ---------------------------
# Ignore the ^C Interrupt
trap('INT', 'SIG_IGN')
SUBCOMMANDS = [
'search', 'skreemr',
'download',
'open',
'url',
'next',
'prev', 'past',
'clear',
'help', 'history',
'itunes',
'quit'
].sort.collect { |i| i+=' ' }
def sub_list(lst, str)
return SUBCOMMANDS if lst.nil?
lst.grep(/^#{Regexp.escape(str)}/)
end
Readline.completion_append_character = " "
Readline.completion_proc = proc { |s| sub_list(SUBCOMMANDS, s) }
# Run the Shell!
SkreemrShell.new