Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gonjay committed May 30, 2015
0 parents commit 5f748d8
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.bundle
*.so
*.o
*.a
mkmf.log
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in youkuVideo.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2015 GonJay

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# YoukuVideo

解析出 YouKu 视频的 M3U8 播放地址

## 安装

把这行代码加到你的 Gemfile 中

```ruby
gem 'youkuVideo'
```

然后运行:

$ bundle

或者你可以直接安装:

$ gem install youkuVideo

## 使用方法

```
2.2.0 :007 > YoukuVideo::M3U8.getVideoURL("XNjgzMTU0MzU2", "hd3")
=> "http://pl.youku.com/playlist/m3u8?ctype=12&ep=dCaXGECKUs4G4iDagT8bZCu0cX4GXJZ0rEjP/LYHAMZuLerQmzbQwQ==%0A&ev=1&keyframe=1&oip=1899608454&sid=64329740673591237f599&token=2816&type=hd3&vid=XNjgzMTU0MzU2"
```

## Contributing

1. Fork it ( https://github.com/gonjay/youkuVideo/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "bundler/gem_tasks"

task :console do
exec "irb -r youkuVideo -I ./lib"
end
62 changes: 62 additions & 0 deletions lib/youkuVideo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require "youkuVideo/version"
require "base64"
require "uri"
require "rest-client"

module YoukuVideo
class M3U8
def self.youkuEncoder(a, c, isToBase64)
result = ""
bytesR = []
f,h,q = 0,0,0
b = []
256.times{|i| b[i]=i}
while h < 256 do
f = (f + b[h] + a[h % a.length].ord) % 256
tmp = b[h]
b[h] = b[f]
b[f] = tmp
h += 1
end
f,h,q = 0,0,0
while q < c.length
h = (h + 1) % 256
f = (f + b[h]) % 256
tmp = b[h]
b[h] = b[f]
b[f] = tmp
bytes = [c[q].ord ^ b[(b[h] + b[f]) % 256]].pack "l"
bytesR.push(bytes[0])
result += bytes[0]
q += 1
end
if isToBase64
result = Base64.encode64(bytesR.join(""))
end
return result
end

def self.getEp(vid, ep)
template1 = "becaf9be"
template2 = "bf7e5f01"
bytes = Base64.decode64(ep).split(//)
tmp = youkuEncoder(template1, bytes, false)
sid = tmp.split("_")[0]
token = tmp.split("_")[1]
whole = "#{sid}_#{vid}_#{token}"
newbytes = whole.split("").map { |e| e.ord }
epNew = youkuEncoder(template2, newbytes, true)
return URI.encode(epNew), sid, token
end

def self.getVideoURL(url, type)
vid = url.split("id_").last.split(".").first
content = RestClient.get("http://v.youku.com/player/getPlayList/VideoIDS/#{vid}/Pf/4/ctype/12/ev/1")
json = JSON.parse(content)
ip = json["data"][0]["ip"]
ep = json["data"][0]["ep"]
ep,sid,token = getEp(vid, ep)
"http://pl.youku.com/playlist/m3u8?ctype=12&ep=#{ep}&ev=1&keyframe=1&oip=#{ip}&sid=#{sid}&token=#{token}&type=#{type}&vid=#{vid}"
end
end
end
3 changes: 3 additions & 0 deletions lib/youkuVideo/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module YoukuVideo
VERSION = "0.0.1"
end
23 changes: 23 additions & 0 deletions youkuVideo.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'youkuVideo/version'

Gem::Specification.new do |spec|
spec.name = "youkuVideo"
spec.version = YoukuVideo::VERSION
spec.authors = ["GonJay"]
spec.email = ["[email protected]"]
spec.summary = %q{解析出 YouKu 视频的 M3U8 播放地址}
spec.description = %q{Write a longer description. Optional.}
spec.homepage = ""
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
end

1 comment on commit 5f748d8

@monroemann
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello!

Does this gem work like the YT gem? I want users to be able to copy and paste a Youku Video Link into my form, and then the YouKu video shows up on the user page. Can I do this with this gem?

Thanks!
-Monroe

Please sign in to comment.