public
Description: Totally illegal, immoral infringe-o-matic.
Homepage: http://its.a.secret
Clone URL: git://github.com/kastner/audio-sinner.git
Search Repo:
audio-sinner / mp3.rb
100644 93 lines (74 sloc) 2.649 kb
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
$:.unshift File.dirname(__FILE__) + '/sinatra/lib'
require 'rubygems'
require 'sinatra'
require 'aws/s3'
require 'memcache'
require 'sha1'
require 'open-uri'
 
require 'ruby-debug'
 
include AWS::S3
 
before do
  # always send utf-8
  header "Content-Type" => "text/html; charset=utf-8"
  
  # set css body id
  @body_id = "home"
  
  # set up a cache object
  CACHE = MemCache.new 'localhost:11211', :namespace => 'my_namespace' unless Object.const_defined?("CACHE")
  
  # the bucket to upload to
  BUCKET = "mp3s.metaatem.net" unless Object.const_defined?("BUCKET")
  
  # set up s3
  AWS::S3::Base.establish_connection!(
    :access_key_id => ENV["AMAZON_ACCESS_KEY_ID"],
    :secret_access_key => ENV["AMAZON_SECRET_ACCESS_KEY"]
  )
end
 
get "/" do
  @files = CACHE["file-list"] ||= begin
    puts "Cache miss on file-list"
    Bucket.find(BUCKET).objects.collect do |object|
      "<a class='mp3' id='#{object.key.to_s}' href='http://#{BUCKET}/#{object.key.to_s}' >#{URI.unescape(object.metadata[:name].to_s)}</a>"
    end.join("<br/>")
  end
  
  erb :index
end
 
get "/check" do
  key = params[:key]
  CACHE["download-#{key}"] ||= "0"
end
 
get "/fetch" do
  name = params[:key]
  url = CACHE[params[:key]]
 
  if S3Object.exists? name, BUCKET
    redirect "http://#{BUCKET}/#{name}"
    halt
  end
  
  remote_header = %x{curl -s -u #{ENV["EZNEWS_USER"]}:#{ENV["EZNEWS_PASS"]} --head "#{url}"}
  
  CACHE["download-#{name}"] = "0"
  Thread.new do
    puts "In thread"
    bits = open(CACHE[params[:key]], :http_basic_authentication => [ENV["EZNEWS_USER"], ENV["EZNEWS_PASS"]])
    
    item = S3Object.store(name, bits, BUCKET, :access => :public_read, :content_type => remote_header[/Content-Type: ([^\n]*)/, 1], "x-amz-meta-name" => File.basename(url))
    puts "uploaded!"
    CACHE["download-#{name}"] = "1"
    CACHE.delete("file-list")
  end
  
  redirect "/?send=true&key=#{name}"
end
 
get "/search" do
  require 'hpricot'
  contents = eznews_contents(params[:q])
  p = Hpricot.XML(contents)
  @files = (p / "item").map do |i|
    link = i.at("link").innerHTML
    size = i.at("description").innerHTML[/\d+[\.\d]*\s(MB|K)/]
    string = URI.unescape(File.basename(link))
    hash_key = SHA1.hexdigest(link)
    CACHE[hash_key] = link
    "<a href='/fetch?key=#{hash_key}'>#{string}</a> [#{size}]"
  end.reverse.join "<br/>"
  
  erb :results
end
 
def eznews_contents(terms)
  url = "http://members.easynews.com/global4/search.html?gps=#{URI.escape(terms)}&fex=mp3&fty%5B%5D=AUDIO&s1=dtime&s1d=-&s2=dsize&s2d=-&s3=dsize&s3d=%2B&pby=20&pno=1&sS=5"
  open(url, :http_basic_authentication => [ENV["EZNEWS_USER"], ENV["EZNEWS_PASS"]]).read
end