Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 282 lines (247 sloc) 6.3 KB
#!/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