2015-02-04 07:32:07 +00:00
|
|
|
class Utils
|
|
|
|
|
|
2015-02-15 23:10:29 +00:00
|
|
|
RECORDING_SRC_PREFIX = 'rec_'
|
|
|
|
|
RECORDING_SOURCES = ["#{RECORDING_SRC_PREFIX}youtube", "#{RECORDING_SRC_PREFIX}soundcloud"]
|
2015-02-06 07:42:46 +00:00
|
|
|
USERNAME_SITES = %W{youtube facebook soundcloud bandcamp fandalism twitter reverbnation}
|
2015-02-12 08:16:54 +00:00
|
|
|
SITE_TYPES = ['url'].concat(USERNAME_SITES).concat(RECORDING_SOURCES)
|
|
|
|
|
|
|
|
|
|
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def self.recording_source?(site)
|
|
|
|
|
RECORDING_SOURCES.include?(site)
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-04 06:31:12 +00:00
|
|
|
def self.extract_recording_data(site, recording_url)
|
2015-02-12 08:16:54 +00:00
|
|
|
recording_url.strip!
|
2015-04-04 06:31:12 +00:00
|
|
|
rec_data = {}
|
2015-02-12 08:16:54 +00:00
|
|
|
case site
|
|
|
|
|
when 'rec_youtube'
|
|
|
|
|
# regex derived from: https://gist.github.com/afeld/1254889
|
|
|
|
|
if recording_url =~ /(youtu.be\/|youtube.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&\"\'>]+)/
|
2015-04-04 06:31:12 +00:00
|
|
|
rec_data["id"] = $5
|
2015-02-12 08:16:54 +00:00
|
|
|
end
|
2015-04-04 06:31:12 +00:00
|
|
|
|
|
|
|
|
uri = URI.parse("https://gdata.youtube.com/feeds/api/videos/#{$5}?v=2&alt=json")
|
|
|
|
|
|
|
|
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
|
http.use_ssl = true if uri.scheme == 'https'
|
|
|
|
|
req = Net::HTTP::Get.new(uri)
|
|
|
|
|
response = http.request(req)
|
|
|
|
|
json = JSON.parse(response.body) if response
|
|
|
|
|
rec_data["title"] = json["entry"]["title"]["$t"]
|
|
|
|
|
return rec_data unless rec_data.empty?
|
|
|
|
|
|
2015-02-12 08:16:54 +00:00
|
|
|
when 'rec_soundcloud'
|
|
|
|
|
if recording_url =~ /^https?:\/\/.*soundcloud.com\/.+/
|
|
|
|
|
tmpfile = Tempfile.new(site)
|
|
|
|
|
tmpfile.close
|
|
|
|
|
curl_args = "-A '#{USER_AGENT}' -L --output #{tmpfile.path} --fail --show-error "
|
|
|
|
|
`curl #{curl_args} '#{recording_url}' 2>&1`
|
|
|
|
|
result = File.read(tmpfile.path)
|
|
|
|
|
File.delete(tmpfile.path)
|
|
|
|
|
if result =~ /"soundcloud:\/\/sounds:(\d+)"/
|
2015-04-04 06:31:12 +00:00
|
|
|
rec_data["id"] = $1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if result =~ /property=\"og:title\" content=\"([\w\W]+)\"><meta property=\"og:image\"/
|
|
|
|
|
rec_data["title"] = $1
|
2015-02-12 08:16:54 +00:00
|
|
|
end
|
2015-04-04 06:31:12 +00:00
|
|
|
return rec_data unless rec_data.empty?
|
2015-02-12 08:16:54 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
nil
|
|
|
|
|
end
|
2015-02-06 07:42:46 +00:00
|
|
|
|
2015-02-04 07:32:07 +00:00
|
|
|
def self.username_url(username, site)
|
|
|
|
|
case site
|
|
|
|
|
when 'youtube'
|
|
|
|
|
"https://www.youtube.com/c/#{username}"
|
2015-02-08 06:10:31 +00:00
|
|
|
when 'facebook'
|
2015-02-04 07:32:07 +00:00
|
|
|
"https://www.facebook.com/#{username}"
|
2015-02-08 06:10:31 +00:00
|
|
|
when 'soundcloud'
|
2015-02-04 07:32:07 +00:00
|
|
|
"https://soundcloud.com/#{username}"
|
2015-02-08 06:10:31 +00:00
|
|
|
when 'bandcamp'
|
2015-02-04 07:32:07 +00:00
|
|
|
"http://#{username}.bandcamp.com"
|
2015-02-08 06:10:31 +00:00
|
|
|
when 'bandcamp-fan'
|
|
|
|
|
"http://bandcamp.com/#{username}"
|
2015-02-04 07:32:07 +00:00
|
|
|
when 'fandalism'
|
|
|
|
|
"http://fandalism.com/#{username}"
|
|
|
|
|
when 'twitter'
|
|
|
|
|
"https://twitter.com/#{username}"
|
|
|
|
|
when 'reverbnation'
|
|
|
|
|
"http://www.reverbnation.com/#{username}"
|
|
|
|
|
else
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-02-08 06:10:31 +00:00
|
|
|
def self.site_validator(url, site=nil)
|
2015-02-12 08:16:54 +00:00
|
|
|
curl_args = "-A '#{USER_AGENT}' --silent --head --fail --show-error "
|
2015-02-08 06:10:31 +00:00
|
|
|
case site
|
|
|
|
|
when 'bandcamp'
|
|
|
|
|
cmd = "curl #{curl_args} '#{url}' 2>&1"
|
|
|
|
|
result = `#{cmd}`.chomp
|
|
|
|
|
if $?.success?
|
|
|
|
|
if /^HTTP\/\d+\.\d+ 2\d\d OK/ =~ result.lines[0]
|
|
|
|
|
return nil
|
|
|
|
|
else
|
|
|
|
|
if /http:\/\/(.*)\.bandcamp.com/ =~ url
|
|
|
|
|
return self.site_validator(self.username_url($1, 'bandcamp-fan'), 'bandcamp-fan')
|
|
|
|
|
end
|
|
|
|
|
return "Unrecognized url (#{url})"
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
result =~ /curl: \(\d+\) (.*)/
|
|
|
|
|
return "#{$1} (#{url})"
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
curl_args << "-L --output /dev/null "
|
|
|
|
|
cmd = "curl #{curl_args} '#{url}' 2>&1"
|
|
|
|
|
result = `#{cmd}`.chomp
|
|
|
|
|
end
|
2015-02-04 07:32:07 +00:00
|
|
|
if $?.success?
|
|
|
|
|
return nil
|
|
|
|
|
else
|
|
|
|
|
result =~ /curl: \(\d+\) (.*)/
|
|
|
|
|
return "#{$1} (#{url})"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|