-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuxtape-snatcher.rb
executable file
·129 lines (111 loc) · 2.78 KB
/
muxtape-snatcher.rb
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
#!/usr/bin/env ruby
# -*- mode: ruby; coding: utf-8-unix; indent-tabs-mode: nil -*-
#
# Author:: Takeru Naito (mailto:[email protected])
# Copyright:: Copyright (c) 2008 Takeru Naito
# License:: Distributes under the same terms as Ruby
#
#
#= muxtape から mp3 を抜いてきます。
#
# 内容が内容だけに表に出せなげ。
#
#
#==依存ライブラリ
#
#*mechanize
#
#
#==使用方法
#% ./muxtape-snatcher --target=<id>
#
require 'rubygems'
require 'mechanize'
require 'logger'
class MuxTapeSnatcher
def initialize(opts = {})
@target = opts[:target]
@agent = WWW::Mechanize.new do |a|
a.max_history = 1
a.user_agent_alias = 'Mac FireFox'
a.log = Logger.new(opts[:log_output])
a.log.level = opts[:log_level]
end
end
def run
if @target
get_song
else
STDERR.puts('Oops. Please. ID.')
end
end
private
def fetch(uri)
begin
sleep 2 #wait
@agent.get(uri)
rescue Timeout::Error
@agent.log.warn " caught Timeout::Error !"
retry
rescue WWW::Mechanize::ResponseCodeError => e
case e.response_code
when "502"
@agent.log.warn " caught Net::HTTPBadGateway !"
retry
when "404"
@agent.log.warn " caught Net::HTTPNotFound !"
else
@agent.log.warn " caught Excepcion !" + e.response_code
end
end
end
def analyzing_tape_page
page = fetch("http://#{@target}.muxtape.com/")
songs, signatures =
page.search("script[text()*='Kettle']").inner_text.
match(/(\[.+?\]).+(\[.+?\])/)[1, 2]
songs = eval(songs)
signatures = eval(signatures)
(1..songs.length).zip(songs, signatures)
end
def get_song
table = analyzing_tape_page
table.each do |record|
index = record[0]
song_id = record[1]
signature = record[2]
puts "fetching #{index} of #{table.length}..."
Object::File::open("%02d_%s.mp3" % [index, @target], 'w') do |f|
f.binmode
f.write fetch(song_url(song_id, signature)).body
end
end
end
def song_url(song_id, signature)
"http://muxtape.s3.amazonaws.com/songs/#{song_id}" +
"?PLEASE=DO_NOT_STEAL_MUSIC&#{signature}"
end
end
if $0 == __FILE__
require 'optparse'
opts = {
:target => nil,
:log_output => STDERR,
:log_level => Logger::WARN
}
OptionParser.new do |parser|
parser.instance_eval do
on('-t target', '--target=target', 'target user') do |arg|
opts[:target] = arg
end
on('-v', '--verbose', 'verbose mode') do |arg|
opts[:log_level] = Logger::INFO
end
on('-d', '--debug', 'debug mode') do |arg|
opts[:log_level] = Logger::DEBUG
end
parse!
end
end
MuxTapeSnatcher.new(opts).run
end