getvideo 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50e448a1b0d1171b5a43900fbcba5660059840bd
4
- data.tar.gz: dd220588081b5a4d1fd99622d8d0640ec3bb5cce
3
+ metadata.gz: a05caec5a828055c6ed17d483e8323240803f4f5
4
+ data.tar.gz: 883ed8d880add6c2247534fd8a9e6db91a368c59
5
5
  SHA512:
6
- metadata.gz: d2a288e9d6b9c38371c117d481c970bfaecbede33c96669ca4df1c2e4e292f12fe9c26f3dc157aee8c03ac9817e399ec4fd9c5c43f9181b4fae35ff6c47e0b96
7
- data.tar.gz: e163fc082316b4ba6a4dcf75bd14425ad53d4b17afcd47568b46b2a84a3b23ce8a06d146ce308cdef65c4f6bd8d0f15b6fc3de79192e7ba92cfd84011ab8e3df
6
+ metadata.gz: 23eba5aff34a41371bb27c58bd619ba5c1709f4f1936ca36d868b02b82d3074d31d7d47bea6c0fccb31c095908bfcc1f6f5d8753d3662ae15000b83239792de1
7
+ data.tar.gz: d139dbbc2ebfe31e629fdbf10a4d30547734b2f1591d8a115941876326cd8f23e5a01625cdc5951eefd23b3cdf639e0874c825e0a608166e580c85e6de34c710
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source 'http://ruby.taobao.org'
2
2
 
3
3
  # Specify your gem's dependencies in getvideo.gemspec
4
4
  gemspec
@@ -17,5 +17,6 @@ Gem::Specification.new do |gem|
17
17
  gem.version = Getvideo::VERSION
18
18
  gem.add_dependency 'faraday'
19
19
  gem.add_dependency 'multi_json'
20
+ gem.add_dependency 'multi_xml'
20
21
  gem.add_dependency 'nokogiri'
21
22
  end
@@ -4,6 +4,7 @@ require 'cgi'
4
4
  require 'json'
5
5
  require 'nokogiri'
6
6
  require 'multi_json'
7
+ require 'multi_xml'
7
8
  require 'getvideo/video'
8
9
  require "getvideo/version"
9
10
  require "getvideo/youku"
@@ -13,7 +14,7 @@ require "getvideo/tudou"
13
14
  require "getvideo/sohu"
14
15
  require "getvideo/iqiyi"
15
16
  require "getvideo/youtube"
16
- require 'getvideo/iask'
17
+ require 'getvideo/sina'
17
18
 
18
19
  module Getvideo
19
20
  def self.parse(url)
@@ -32,7 +33,7 @@ module Getvideo
32
33
  elsif url =~ /youtube/
33
34
  Youtube.new(url)
34
35
  else url =~ /(iask|sina)/
35
- Iask.new(url)
36
+ Sina.new(url)
36
37
  end
37
38
  end
38
39
  end
@@ -3,33 +3,31 @@
3
3
  module Getvideo
4
4
  class Iqiyi < Video
5
5
  set_api_uri { "http://cache.video.qiyi.com/v/#{id}" }
6
- attr_reader :ipad_response
7
6
 
8
- def initialize(url)
9
- super
10
- ipad_connection
7
+ def ipad_response
8
+ @ipad_response ||= ipad_connection
11
9
  end
12
10
 
13
11
  def html_url
14
- response.css("videoUrl").text
12
+ response_info["videoUrl"]
15
13
  end
16
14
 
17
15
  def id
18
- if url =~ /\.html/
19
- parse_vid[1]
20
- elsif url =~ /swf/
21
- url.scan(/com\/([^\/]+)/)[0][0]
22
- else
16
+ if url =~ /\.html/
17
+ parse_vid[1]
18
+ elsif url =~ /swf/
19
+ url.scan(/com\/([^\/]+)/)[0][0]
20
+ else
23
21
  url
24
- end
22
+ end
25
23
  end
26
24
 
27
25
  def title
28
- response.css("title").text
26
+ response_info["title"]
29
27
  end
30
28
 
31
29
  def cover
32
- response.css("albumImage").text
30
+ response_info["albumImage"]
33
31
  end
34
32
 
35
33
  def m3u8
@@ -37,21 +35,24 @@ module Getvideo
37
35
  end
38
36
 
39
37
  def flash
40
- position = response.css("logoPosition").text
41
- duration = response.css("totalDuration").text
42
- video_url = response.css("videoUrl").text.gsub("http://www.iqiyi.com/","")
43
- "http://player.video.qiyi.com/#{id}/#{position}/#{duration}/#{video_url}"[0..-5]+"swf"
38
+ position = response_info["logoPosition"]
39
+ duration = response_info["totalDuration"]
40
+ video_url = response_info["videoUrl"].gsub("http://www.iqiyi.com/","")
41
+ "http://player.video.qiyi.com/#{id}/#{position}/#{duration}/#{video_url}"[0..-5]+"swf"
44
42
  end
45
43
 
46
44
  def media
47
45
  video_list = {}
48
46
  video_list["mp4"] = []
49
- video_list["mp4"] << parse_mp4
50
- video_list["ts"] = []
51
- file = response.css("file")
52
- size = response.css("size")
53
- file.zip(size).each do |f|
54
- video_list["ts"] << f[0].content[0..-4] + "ts?start=0&end=99999999&hsize=" + f[1].text
47
+ video_list["f4v"] = []
48
+ mp4_file = ipad_response["data"]["mp4Url"]
49
+ res = Faraday.get "http://data.video.qiyi.com/#{mp4_file.split("/")[-1].split(".")[0]}.ts"
50
+ video_list["mp4"] << "#{mp4_file}?#{URI.parse(res["location"]).query}"
51
+ file = response_info["fileUrl"]["file"]
52
+ size = response_info["fileBytes"]["size"]
53
+ file.each_with_index do |f, i|
54
+ res = Faraday.get "http://data.video.qiyi.com/#{file[i].split("/")[-1].split(".")[0]}.ts"
55
+ video_list["f4v"] << "#{f}?#{URI.parse(res["location"]).query}"
55
56
  end
56
57
  return video_list
57
58
  end
@@ -59,16 +60,10 @@ module Getvideo
59
60
  private
60
61
 
61
62
  def ipad_connection
62
- conn = Faraday.new
63
- response= conn.get "http://cache.video.qiyi.com/m/#{id}/"
64
- @ipad_response = MultiJson.load response.body.scan(/ipadUrl=([^*]+)/)[0][0]
65
- end
66
-
67
- def parse_mp4(url=nil)
68
- site = ipad_response["data"]["mp4Url"]
69
- conn = Faraday.new
70
- res = conn.get(site)
71
- res.body.scan(/"l":"([^,]+)"/)[0][0]
63
+ conn = Faraday.new "http://cache.video.qiyi.com"
64
+ conn.headers["User-Agent"] = ""
65
+ response= conn.get "/m/#{id}/"
66
+ MultiJson.load response.body.scan(/ipadUrl=([^*]+)/)[0][0]
72
67
  end
73
68
 
74
69
  def parse_vid
@@ -76,5 +71,9 @@ module Getvideo
76
71
  page_res = conn.get(url)
77
72
  return page_res.body.match(/data-player-videoid\s*[:=]\s*["']([^"']+)["']/)
78
73
  end
74
+
75
+ def response_info
76
+ response["root"]["video"]
77
+ end
79
78
  end
80
79
  end
@@ -49,8 +49,8 @@ module Getvideo
49
49
 
50
50
  def connection
51
51
  conn = Faraday.new
52
- response= conn.post "http://v.ku6.com/fetch.htm", {"t" => "getVideo4Player", "vid" => id}
53
- @response = Response.new(response).parsed
52
+ response= conn.post "http://v.ku6.com/fetch.htm", {"t" => "getVideo4Player", "vid" => id}
53
+ Response.new(response).parsed
54
54
  end
55
55
  end
56
56
  end
@@ -1,18 +1,20 @@
1
1
  #coding:utf-8
2
2
 
3
3
  module Getvideo
4
- class Iask < Video
4
+ class Sina < Video
5
5
  set_api_uri { "http://v.iask.com/v_play.php?vid=#{id}" }
6
- attr_reader :ipad_response, :info_response
7
6
 
8
7
  def initialize(url)
9
8
  @url = url
10
- set_id()
11
- connection
12
- if has_html?
13
- @info_response = Nokogiri::HTML(parse_html).css("head script").text.gsub(" ", "")
14
- ipad_connect if ipad_id
15
- end
9
+ set_id
10
+ end
11
+
12
+ def ipad_response
13
+ @ipad_response ||= ipad_connect
14
+ end
15
+
16
+ def info_response
17
+ @info_response ||= Nokogiri::HTML(parse_html).css("head script").text.gsub(" ", "")
16
18
  end
17
19
 
18
20
  def html_url
@@ -31,7 +33,7 @@ module Getvideo
31
33
  if has_html?
32
34
  info_response.scan(/title:'([^']+)'/)[0][0]
33
35
  else
34
- response.css("vname").text
36
+ response["vname"]
35
37
  end
36
38
  end
37
39
 
@@ -59,7 +61,9 @@ module Getvideo
59
61
 
60
62
  def m3u8
61
63
  if has_html?
62
- ipad_response.nil? ? "" : media["mp4"][0]
64
+ if !ipad_response.nil?
65
+ media["mp4"][0]
66
+ end
63
67
  else
64
68
  ""
65
69
  end
@@ -72,15 +76,16 @@ module Getvideo
72
76
  def media
73
77
  vedio_list = {}
74
78
  vedio_list["hlv"] = []
75
-
76
- response.css("url").each do |d|
77
- vedio_list["hlv"] << d.text
79
+ vedio_list["mp4"] = []
80
+ if res = response
81
+ if res["video"]["result"] != "error"
82
+ vedio_list["hlv"] << res["video"]["durl"]["url"]
83
+ end
78
84
  end
79
85
 
80
- if ipad_response
81
- vedio_list["mp4"] = []
82
- ipad_response.css("url").each do |d|
83
- vedio_list["mp4"] << d.text
86
+ if m_res = ipad_response
87
+ if m_res["video"]["result"] != "error"
88
+ vedio_list["mp4"] << m_res["vdieo"]["durl"]["url"]
84
89
  end
85
90
  end
86
91
 
@@ -97,16 +102,16 @@ module Getvideo
97
102
  if url =~ /\/v\/b\/([^\D]+)-([^\D]+)\.html/
98
103
  ids = url.scan(/\/v\/b\/([^\D]+)-([^\D]+)\.html/)[0]
99
104
  @id = ids[0]
100
- @uid = ids[1]
105
+ @uid = ids[1]
101
106
  elsif url =~ /\/playlist\/([^\D]+).*.#([^\D]+)/
102
107
  ids = url.scan(/\/playlist\/[^\D]+-([^\D]+).*#([^\D]+)/)[0]
103
108
  @id = ids[1]
104
- @uid = ids[0]
109
+ @uid = ids[0]
105
110
  elsif url =~ /\.swf/
106
111
  ids = url.scan(/vid=([^\D]+)_([^\D]+)_.+\.swf/)[0]
107
112
  @id = ids[0]
108
- @uid = ids[1]
109
- elsif url =~ /(\/[\S]?\/)/
113
+ @uid = ids[1]
114
+ elsif url =~ /(\/[\S]?\/)/
110
115
  if url.index("#").nil?
111
116
  html = parse_html
112
117
  ids = html.scan(/vid[\s]?:[\s]?['|"]([^\D]+)['|"]/)[0]
@@ -132,7 +137,7 @@ module Getvideo
132
137
  def ipad_connect
133
138
  conn = Faraday.new
134
139
  response = conn.get "http://v.iask.com/v_play.php?vid=#{ipad_id}"
135
- @ipad_response = Response.new(response).parsed
140
+ Response.new(response).parsed
136
141
  end
137
142
 
138
143
  def html_info_path
@@ -144,7 +149,7 @@ module Getvideo
144
149
  url
145
150
  end
146
151
  else
147
- url
152
+ url
148
153
  end
149
154
  elsif url =~ /\.swf/
150
155
  html_url
@@ -153,9 +158,11 @@ module Getvideo
153
158
  end
154
159
  end
155
160
 
156
- def parse_html()
157
- conn = Faraday.new
158
- conn.get(html_info_path).body
161
+ def parse_html
162
+ if !html_info_path.empty?
163
+ conn = Faraday.new
164
+ conn.get(html_info_path).body
165
+ end
159
166
  end
160
167
 
161
168
  def get_media(type=nil)
@@ -2,162 +2,300 @@
2
2
 
3
3
  module Getvideo
4
4
  class Tudou < Video
5
- attr_reader :info
5
+
6
+ set_api_uri do
7
+ if url_type == "albumplay" || url_type == "oplay"
8
+ "http://v.youku.com/player/getPlayList/VideoIDS/#{page_get_vcode}"
9
+ else
10
+ "http://www.tudou.com/outplay/goto/getItemSegs.action?iid=#{page_get_iid}"
11
+ end
12
+ end
6
13
 
7
14
  def initialize(url)
8
15
  @url = url
9
- parse_html
10
- tudou_connect
11
- @body = Nokogiri::XML(response)
16
+ parse_page
17
+ get_type
18
+ if !swf_iid && url_type != "views"
19
+ get_item_info(page_get_iid)
20
+ end
12
21
  end
13
22
 
14
23
  def id
15
- iid
24
+ page_get_iid
16
25
  end
17
26
 
18
27
  def html_url
19
- if url =~ /\/(a|o|l)\/.*.\.swf/
20
- type = url.scan(/\/(a|o|l)\/.*.\.swf/)[0][0]
21
- case type
22
- when "a"
23
- "http://www.tudou.com/albumplay/#{acode}#{"/" + lcode if lcode }.html"
24
- when "o"
25
- "http://www.tudou.com/oplay/#{acode}#{"/" + lcode if lcode}.html"
26
- when "l"
27
- "http://www.tudou.com/listplay/#{acode}#{"/" + lcode if lcode}.html"
28
- end
29
- elsif url =~ /dp\.tudou.com\/(a|o|l|v|albumplay|oplay|listplay)\//
30
- type = url.scan(/dp\.tudou.com\/(a|o|l|v|albumplay|oplay|listplay)\/.*.\.html/)[0][0]
31
- case type
32
- when "a", "albumplay"
33
- "http://www.tudou.com/albumplay/#{acode}#{"/" + lcode if lcode}.html"
34
- when "o", "oplay"
35
- "http://www.tudou.com/oplay/#{acode}#{"/" + lcode if lcode}.html"
36
- when "l", "listplay"
37
- "http://www.tudou.com/listplay/#{acode}#{"/" + lcode if lcode}.html"
38
- when "v"
39
- "http://www.tudou.com/programs/view/#{lcode}/"
40
- end
41
- elsif url =~ /www\.tudou.com\/(programs|albumplay|listplay|oplay)/
42
- url
43
- else
44
- "http://www.tudou.com/programs/view/#{lcode}/"
28
+ case url_type
29
+ when "views"
30
+ "http://www.tudou.com/programs/view/#{page_get_icode}/"
31
+ when "albumplay", "oplay"
32
+ "http://www.tudou.com/albumplay/#{page_get_lcode}/#{page_get_icode}.html"
33
+ when "listplay"
34
+ "http://www.tudou.com/listplay/#{page_get_lcode}/#{page_get_icode}.html"
45
35
  end
46
36
  end
47
37
 
48
38
  def title
49
- @body.children()[0].attr("title")
39
+ if page_get_title.encoding != 'UTF-8'
40
+ CGI.unescapeHTML(page_get_title)
41
+ else
42
+ page_get_title
43
+ end
50
44
  end
51
45
 
52
46
  def cover
53
- @info.match(/pic\s*:\s*(\S+)/)[1].delete("\"").gsub(/["|']/,"")
47
+ if url_type == "albumplay" || url_type == "oplay"
48
+ response["data"][0]["logo"]
49
+ else
50
+ page_get_pic
51
+ end
54
52
  end
55
53
 
56
54
  def flash
57
- if url =~ /\/(v|a|o|l)\/.*.\.swf/
58
- url
59
- elsif url =~ /\/(albumplay|listplay|oplay)/
60
- url_type = url.scan(/\/((a)lbumplay|(l)istplay|(o)play)/)[0].compact()[1]
61
- "http://www.tudou.com/#{url_type}/#{acode}/&rpid=116105338&resourceId=116105338_04_05_99&iid=#{iid}/v.swf"
62
- elsif url =~ /dp\.tudou\.com\/(l|a|o)/
63
- url_type = url.scan(/dp\.tudou.com\/(a|l|o)/)[0][0]
64
- "http://www.tudou.com/#{url_type}/#{acode}/&rpid=116105338&resourceId=116105338_04_05_99&iid=#{iid}/v.swf"
65
- elsif url =~ /dp\.tudou\.com\/v/
66
- "http://www.tudou.com/v/#{lcode}/&rpid=116105338&resourceId=116105338_04_05_99/v.swf"
67
- else
68
- "http://www.tudou.com/v/#{lcode}/&rpid=116105338&resourceId=116105338_04_05_99/v.swf"
55
+ case url_type
56
+ when "views"
57
+ "http://www.tudou.com/v/#{page_get_icode}/v.swf"
58
+ when "albumplay", "oplay"
59
+ "http://www.tudou.com/a/#{page_get_lcode}/&iid=#{page_get_iid}/v.swf"
60
+ when "listplay"
61
+ "http://www.tudou.com/l/#{page_get_lcode}/&iid=#{page_get_iid}/v.swf"
69
62
  end
70
63
  end
71
64
 
72
65
  def m3u8
73
- "http://vr.tudou.com/v2proxy/v2.m3u8?it=#{iid}&st=2&pw="
66
+ "http://vr.tudou.com/v2proxy/v2.m3u8?it=#{page_get_iid}&st=2&pw="
74
67
  end
75
68
 
76
- def media
77
- video_list = {}
78
- video_list["f4v"] = []
79
- video_list["flv"] = []
80
- old_brt = ""
81
- @body.css("f").each do | file |
82
- brt = file.attr("brt")
83
- if brt != old_brt
84
- if file.content =~ /f4v/
85
- video_list["f4v"] << file.content
86
- elsif file.content =~ /flv/
87
- video_list["flv"] << file.content
88
- end
89
- old_brt = brt
90
- end
69
+ def media(type = nil)
70
+ if url_type == "albumplay" || url_type == "oplay"
71
+ albumplay_media(type)
72
+ else
73
+ view_media(type)
91
74
  end
92
- return video_list
93
75
  end
94
76
 
95
77
  private
96
78
 
97
- def tudou_connect
98
- uri = URI.parse "http://v2.tudou.com/v?it=#{iid}"
99
- http = Net::HTTP.new uri.host, uri.port
100
- req = Net::HTTP::Get.new(uri.request_uri,{"User-Agent"=> ""})
101
- res = http.request req
102
- @response = res.body
79
+ def get_items
80
+ if url_type == "albumplay" || url_type == "oplay"
81
+ Getvideo::Response.new(Faraday.get("http://www.tudou.com/outplay/goto/getAlbumItems.html?aid=#{page_get_lid}")).parsed
82
+ elsif url_type == "listplay"
83
+ Getvideo::Response.new(Faraday.get("http://www.tudou.com/outplay/goto/getPlaylistItems.html?lid=#{page_get_lid}")).parsed
84
+ else
85
+ nil
86
+ end
87
+ end
88
+
89
+ def get_item_info(id)
90
+ get_items["message"].each do |item|
91
+ if item["itemId"].to_s == id
92
+ @item_info = item
93
+ break
94
+ end
95
+ end
103
96
  end
104
97
 
105
- def lcode
106
- if url =~ /\/v\/.*.\.swf/
107
- url.scan(/\/v\/([^\/]+)\//)[0][0]
108
- elsif url =~ /dp\.tudou\.com\/(l|a|o|listplay|albumplay|oplay)\/([^.]+)\/([^.]+)\.html/
109
- url.scan(/dp\.tudou\.com\/(?:l|a|o|listplay|albumplay|oplay)\/([^.]+)\/([^.]+)\.html/)[0][1]
110
- elsif url =~ /dp\.tudou\.com\/(l|a|o|listplay|albumplay|oplay)\/([^.]+)\.html/
111
- nil
112
- elsif url =~ /dp\.tudou\.com\/v\/([^.]+)\.html/
113
- url.scan(/dp\.tudou\.com\/v\/([^.]+)\.html/)[0][0]
98
+ def parse_page
99
+ video_url = get_url
100
+ if swf_iid
101
+ res = Faraday.get(video_url)
102
+ @swf_info = {}
103
+ res.headers['location'].split("?")[1].split("&").each do |data|
104
+ data_info = data.split("=")
105
+ if data_info.length > 1
106
+ @swf_info[data_info[0]] = CGI::unescape(data_info[1])
107
+ end
108
+ end
114
109
  else
115
- tudou_connect
116
- Nokogiri::XML(response).children()[0].attr("code")
110
+ @page = Getvideo::Response.new(Faraday.get(video_url)).parsed.css("script").text
117
111
  end
118
112
  end
119
113
 
120
- def acode
114
+ def get_type
115
+ type = url.match(/\/(v|a|o|l|listplay|albumplay|oplay|programs)\//)[1]
116
+ @url_type = case type
117
+ when "v", "programs"
118
+ "views"
119
+ when "a", "albumplay"
120
+ "albumplay"
121
+ when "o", "oplay"
122
+ "oplay"
123
+ when "l", "listplay"
124
+ "listplay"
125
+ end
126
+ end
127
+
128
+ def get_url
121
129
  if url =~ /\/(a|o|l)\/.*.\.swf/
122
- url.scan(/\/[a|o|l]\/([^\/]+)\//)[0][0]
123
- elsif url =~ /\/(listplay|albumplay|oplay)\/([^.]+)\/([^.]+)\.html/
124
- url.scan(/\/(?:listplay|albumplay|oplay)\/([^.]+)\/([^.]+)\.html/)[0][0]
125
- elsif url =~ /dp\.tudou\.com\/(l|a|o)\/([^.]+)\/([^.]+)\.html/
126
- url.scan(/dp\.tudou\.com\/(?:l|a|o)\/([^.]+)\/([^.]+)\.html/)[0][0]
127
- elsif url =~ /dp\.tudou\.com\/(l|a|o)\/([^.]+)\.html/
128
- url.scan(/dp\.tudou\.com\/(?:l|a|o)\/([^.]+)\.html/)[0][0]
130
+ code = url.match(/\/[a|o|l]\/([^\/]+)\//)[1]
131
+ @swf_iid = url.match(/iid=([^&]+)/)[1]
132
+ type = url.match(/\/(a|o|l)\/.*.\.swf/)[1]
133
+ url
134
+ elsif url =~ /\/v\/.*.\.swf/
135
+ code = url.match(/\/v\/([^\/]+)\//)[1]
136
+ "http://www.tudou.com/programs/view/#{code}/"
137
+ elsif url =~ /www\.tudou.com\/oplay/
138
+ url_id = url.match(/www\.tudou.com\/oplay\/(.*.).html/)[1]
139
+ "http://www.tudou.com/albumplay/#{url_id}.html"
140
+ else
141
+ url
142
+ end
143
+ end
129
144
 
130
- elsif url =~ /\/(listplay|albumplay|oplay)\/([^.]+)\.html/
131
- url.scan(/\/(?:listplay|albumplay|oplay)\/([^.]+)\.html/)[0][0]
145
+ def page_get_lid
146
+ if swf_iid
147
+ if url_type == "albumplay"
148
+ swf_info["aid"]
149
+ elsif url_type == "listplay"
150
+ swf_info["lid"]
151
+ end
152
+ else
153
+ page_match(/lid[\s|'|:]*(\S+)/)[1]
132
154
  end
133
155
  end
134
156
 
135
- def iid
136
- if url =~ /\/(programs|albumplay|listplay|oplay)/
137
- get_iid[0][0]
138
- elsif url =~ /\/v\/.*.\.swf/
139
- get_iid[0][0]
140
- elsif url =~ /\/(a|o|l)\/.*.\.swf/
141
- url.scan(/iid=([^\/]+)/)[0][0]
142
- elsif url =~ /dp\.tudou\.com\/(a|o|l|v)\//
143
- get_iid[0][0]
144
- elsif url =~ /dp\.tudou\.com\/player\.php/
145
- url.scan(/player\.php\?id=([^&]+)/)[0][0]
146
- elsif url =~ /dp.tudou.com\/nplayer\.swf/
147
- url.scan(/nplayer\.swf\?([^&]+)/)[0][0]
157
+ def page_get_lcode
158
+ if swf_iid
159
+ if url_type == "albumplay" || url_type == "oplay"
160
+ swf_info["aCode"]
161
+ elsif url_type == "listplay"
162
+ swf_info["lCode"]
163
+ else
164
+ end
148
165
  else
149
- url
166
+ page_match(/lcode[\s|'|:]*(\S+)'/)[1]
150
167
  end
151
168
  end
152
169
 
153
- def get_iid
154
- @info.scan(/iid\s*:\s*(\S+)/)
170
+ def page_get_iid
171
+ swf_iid || (page_match(/iid[\s|'|:]*(\S+)/)[1])
155
172
  end
156
173
 
157
- def parse_html
158
- conn = Faraday.new
159
- res = conn.get html_url
160
- @info = res.body
174
+ def page_get_icode
175
+ if swf_iid
176
+ if url_type == "oplay"
177
+ swf_info["ablumItemCode"]
178
+ else
179
+ swf_info["code"]
180
+ end
181
+ else
182
+ if url_type == "albumplay" || url_type == "listplay"
183
+ item_info["code"]
184
+ else
185
+ page_match(/icode[\s|'|:]*(\S+)'/)[1]
186
+ end
187
+ end
188
+ end
189
+
190
+ def page_get_vcode
191
+ if swf_iid
192
+ swf_info["vcode"]
193
+ else
194
+ if url_type == "albumplay"
195
+ item_info["vcode"]
196
+ else
197
+ page_match(/vcode[\s|'|:]*(\S+)'/)[1]
198
+ end
199
+ end
200
+ end
201
+
202
+ def page_get_title
203
+ if swf_iid
204
+ swf_info["title"]
205
+ else
206
+ if url_type == "albumplay"
207
+ item_info["albumItemShortDescription"]
208
+ elsif url_type == "listplay"
209
+ item_info["title"]
210
+ else
211
+ page_match(/,kw[\s|'|:]*(\S+.*)'/)[1]
212
+ end
213
+ end
161
214
  end
215
+
216
+ def page_get_pic
217
+ if swf_iid
218
+ swf_info["snap_pic"]
219
+ else
220
+ if url_type == "albumplay" || url_type == "listplay"
221
+ item_info["picUrl"]
222
+ else
223
+ page_match(/pic[\s|'|:]*(\S+)'/)[1]
224
+ end
225
+ end
226
+ end
227
+
228
+ def page_match(regex)
229
+ data = page.match(regex)
230
+ data || []
231
+ end
232
+
233
+ def view_media(type = nil)
234
+ video_list = {}
235
+ video_type = {
236
+ "2" => "f4v_sd",
237
+ "3" => "f4v_hd",
238
+ "4" => "f4v",
239
+ "5" => "f4v_fhd",
240
+ "52" => "mp4"
241
+ }
242
+ conn = Faraday.new "http://v2.tudou.com"
243
+ conn.headers["User-Agent"] = ""
244
+ response.each do |key, val|
245
+ type = video_type[key] || key
246
+ video_list[type] = []
247
+ val.each do |video|
248
+ video_list[type] << Getvideo::Response.new(conn.get("http://v2.tudou.com/f?id=#{video["k"]}", )).parsed["f"]["__content__"]
249
+ end
250
+ end
251
+ return video_list
252
+ end
253
+
254
+ def albumplay_media(type = nil)
255
+ video_list = {}
256
+ response["data"][0]["streamfileids"].each_key do |type|
257
+ stream = parse_stream(type)
258
+ video_list[type] = []
259
+ segs(type).each do |s|
260
+ video_list[type] << "http://f.youku.com/player/getFlvPath/sid/" + sid + "/st/#{type}/fileid/#{stream[0..8]+s["no"].to_i.to_s(16)+stream[10..-1]}_0#{s["no"].to_i.to_s(16)}?K="+s["k"] if s["k"] != -1
261
+ end
262
+ end
263
+ return video_list
264
+ end
265
+
266
+ def sid
267
+ Time.now.to_i.to_s + rand(10..99).to_s+ "1000" + rand(30..80).to_s+"00"
268
+ end
269
+
270
+ def segs(type)
271
+ response["data"][0]["segs"][type]
272
+ end
273
+
274
+ def videoid
275
+ response["data"][0]["videoid"]
276
+ end
277
+
278
+ def parse_stream(type)
279
+ seed = response["data"][0]["seed"]
280
+ stream_fileids = response["data"][0]["streamfileids"][type]
281
+ random_text = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890'
282
+
283
+ text = ""
284
+ random_text.each_char do |t|
285
+ seed = (seed * 211 + 30031) % 65536
286
+ cuch = ((seed / 65536.0) * random_text.length).to_i
287
+ char = random_text[cuch]
288
+ text = text + char
289
+ random_text = random_text.gsub(char, "")
290
+ end
291
+
292
+ real_text = ""
293
+ stream_fileids.split("*").each do |s|
294
+ real_text = real_text + text.to_s[s.to_i]
295
+ end
296
+ return real_text
297
+ end
298
+
299
+ attr_accessor :page, :swf_iid, :url_type, :item_info, :swf_info
162
300
  end
163
301
  end