-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractor.rb
56 lines (45 loc) · 1.19 KB
/
extractor.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
require 'httparty'
require 'awesome_print'
require 'soundcloud'
class Extractor
class SoundCloudConnector
include HTTParty
base_uri 'www.soundcloud.com'
end
def initialize(set_url)
@set_url = set_url
end
def extract
response = SoundCloudConnector.get(@set_url)
content = response.scan(/meta content=\"http:\/\/soundcloud.com\/.*itemprop=/)
base = "http:\/\/soundcloud.com"
urls = []
content.each do |c|
match = /#{base}(?<rest>(\/[A-Za-z\-0-9]*)*)/.match(c)
urls << match[:rest]
end
api_call = 'http://api.soundcloud.com/tracks/'
consumer_key = '/stream?consumer_key=hijuflqxoOqzLdtr6W4NA'
client = SoundCloud.new(:client_id => "hijuflqxoOqzLdtr6W4NA")
formatted = urls[1..-1].map{|a|
url = base + a
begin
track = client.get('/resolve', :url => url)
OpenStruct.new(
url: base + a,
track_id: track.id,
api_call: api_call + track.id.to_s + consumer_key,
title: track.title
)
rescue
nil
end
}
formatted.compact
end
def write_to_file(media_content, file_name)
file = File.new(file_name, "w")
File.open(file, 'w') { |file| file.write(media_content.map{|a| a[:api_call]}.join("\n").to_s) }
return file
end
end