Skip to content
This repository has been archived by the owner on Dec 20, 2022. It is now read-only.

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaflik committed May 29, 2012
0 parents commit bc24d65
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--color
--format documentation
5 changes: 5 additions & 0 deletions Gemfile
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in yt_mp3.gemspec
gemspec
gem 'rspec'
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2012 Jakub Kaflik

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 changes: 34 additions & 0 deletions README.md
@@ -0,0 +1,34 @@
# YtMp3

Easily download YouTube videos as Mp3 with commandline tool!

## Installation
$ gem install yt_mp3

## Usage

$ yt_mp3 http://www.youtube.com/watch?v=DYyWVlh8NLM

## YouTube MP3 API:
This gem was build on top of [YouTube-Mp3.org](http://youtube-mp3.org). Every MP3 file convertion are made by them. There I've described an API:

### Request a video to mp3 convert:
GET: http://www.youtube-mp3.org/api/pushItem/?item=#{video_url}&xy=yx&bf=false&r=#{Time.now.to_i}
RESPONSE: video_id

HTTParty.get("http://www.youtube-mp3.org/api/pushItem/?item=http%3A//www.youtube.com/watch%3Fv%3D41L3a0QUzwY%26feature%3Dg-all-u&xy=yx&bf=false&r=#{Time.now.to_i}").body
=> "41L3a0QUzwY"

### Get video info (we need it to get a hash). Repeat until status is "serving".
GET: http://www.youtube-mp3.org/api/itemInfo/?video_id=#{video_id}&ac=www&r=#{Time.now.to_i}
RESPONSE: info JSON fetched by response.body.match(/\Ainfo = (.*?);\z/)[1]

HTTParty.get()

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
8 changes: 8 additions & 0 deletions Rakefile
@@ -0,0 +1,8 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new('spec')

# If you want to make this the default task
task :default => :spec
28 changes: 28 additions & 0 deletions bin/yt_mp3
@@ -0,0 +1,28 @@
#!/usr/bin/env ruby

require 'trollop'

$:.unshift(File.join(File.dirname(__FILE__), "/../lib"))
require "yt_mp3"

opts = Trollop::options do
version "yt_mp3 #{YTMp3::VERSION} (c) 2012 #{YTMp3::AUTHORS.join(', ')}"
banner <<-EOS
USAGE: #{$0} [url]
EOS
end

if ARGV.empty?
STDERR.puts "You need to provide a URL"
STDERR.puts "USAGE: #{$0} [url]"
else
mp3 = YTMp3::YouTubeMP3.new ARGV.first
puts "Converting #{mp3.url}..."
begin
mp3.convert
rescue Exception => e
STDERR.puts e
end

system "curl --progress-bar -L \"#{mp3.downloadable}\" > \"#{mp3.title}\".mp3"
end
17 changes: 17 additions & 0 deletions lib/yt_mp3.rb
@@ -0,0 +1,17 @@
require "yt_mp3/version"
require "yt_mp3/youtube_mp3"

module YTMp3
def self.uri?(string)
uri = URI.parse(string)
%w( http https ).include?(uri.scheme)
rescue URI::BadURIError
false
rescue URI::InvalidURIError
false
end

class RequestFailed < StandardError; end

YOUTUBE_MP3_TIMEOUT = 90
end
4 changes: 4 additions & 0 deletions lib/yt_mp3/version.rb
@@ -0,0 +1,4 @@
module YTMp3
VERSION = "0.0.1"
AUTHORS = ["Jakub Kaflik"]
end
39 changes: 39 additions & 0 deletions lib/yt_mp3/youtube_mp3.rb
@@ -0,0 +1,39 @@
require 'timeout'
require 'httparty'
require 'uri'
require 'json'

module YTMp3
class YouTubeMP3

attr_reader :url, :downloadable, :title

def initialize( url_or_id )
@url = YTMp3::uri?(url_or_id) ? url_or_id : "http://youtube.com/watch?v=#{url_or_id}"
end

def convert
r = HTTParty.get "http://www.youtube-mp3.org/api/pushItem/?item=#{URI.escape(@url)}&xy=yx&bf=false&r#{Time.now.to_i}"

raise RequestFailed, "YouTubeMP3 service responsed: #{r.body}" unless r.response.class == Net::HTTPOK

video_id = r.body

Timeout::timeout(YOUTUBE_MP3_TIMEOUT) do
while true
r = HTTParty.get "http://www.youtube-mp3.org/api/itemInfo/?video_id=#{video_id}&ac=www&r=#{Time.now.to_i}"

raise RequestFailed, "YouTubeMP3 service responsed: #{r.body}" unless r.response.class == Net::HTTPOK

info = JSON.parse(r.body.match(/\Ainfo = (.*?);\z/)[1])

if info["status"] == "serving"
@downloadable = "http://www.youtube-mp3.org/get?video_id=#{video_id}&h=#{info['h']}&r=#{Time.now.to_i}"
@title = info["title"]
break
end
end
end
end
end
end
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,8 @@
require 'rubygems'
require 'bundler/setup'

require 'yt_mp3' # and any other gems you need

RSpec.configure do |config|

end
23 changes: 23 additions & 0 deletions spec/youtube_mp3_spec.rb
@@ -0,0 +1,23 @@
require 'spec_helper'

describe YTMp3::YouTubeMP3 do
it "initialize by URL" do
YTMp3::YouTubeMP3.new("http://www.youtube.com/watch?v=KlJy_Cb21Lw").url.should eq "http://www.youtube.com/watch?v=KlJy_Cb21Lw"
end

it "initialize by video_id" do
YTMp3::YouTubeMP3.new("KlJy_Cb21Lw").url.should eq "http://youtube.com/watch?v=KlJy_Cb21Lw"
end

it "convert's video" do
mp3 = YTMp3::YouTubeMP3.new("KlJy_Cb21Lw")
mp3.convert
mp3.downloadable.should include "http://www.youtube-mp3.org/get?video_id=KlJy_Cb21Lw"
end

it "has title" do
mp3 = YTMp3::YouTubeMP3.new("KlJy_Cb21Lw")
mp3.convert
mp3.title.should eq "Lady Antebellum - Need You Now (HQ) [Lyrics]"
end
end
21 changes: 21 additions & 0 deletions yt_mp3.gemspec
@@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/yt_mp3/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = YTMp3::AUTHORS
gem.email = ["kofels@gmail.com"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = "http://kofel.github.com/yt_mp3/"

gem.rubyforge_project = "yt_mp3"
gem.add_dependency "httparty"
gem.add_dependency "trollop"

gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "yt_mp3"
gem.require_paths = ["lib"]
gem.version = YTMp3::VERSION
end

0 comments on commit bc24d65

Please sign in to comment.