youku_video_utility 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +8 -8
  2. data/lib/youku_video_utility.rb +69 -17
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDdjYTA2MzMwZmJjOTQ4N2RjMDMxNTU3NWVlZDY1ZTVmOWJkM2Y1Ng==
4
+ YzkwZWI5ZTlhYWU1NjRjODc3Y2Q0MGNjZWMyNDcwNjk0YTQxYjNiMQ==
5
5
  data.tar.gz: !binary |-
6
- N2MwYjZkOGMyYTM3NmU4ZWU1MzVlODUwMzc5YTI5NmI1MzliY2NjOQ==
6
+ YjE5Nzg0NGMwMGZlNDA3NDU4Y2RmNjNhNzFiMDY0N2E4ZDk5ZmQ1Zg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NzM4Y2U3OTExZTZmNWNkZjIwMTRjNTI2MjliOTI5ZjMwNTc3MDE0NWQ1MjUw
10
- NmRiZTQ5Y2EzNWI1OTY4ZDYyN2E3OGVjOTliNTU4MzBjOWJlOTNlMGMyNzY1
11
- ZDhlNTA3NjRmMzNkMmY4ZjQ2ZDdlZDJjNzY2ODBiNzgwNmRlYmM=
9
+ ZDI3YzE1YTU4ZjM1YmVjZmMzMDg3YTljYjc3NTcxOGE5NDI3YmY4NTExODcx
10
+ MmI0YTU2OGZmODM4ZTZhYjc4MDUwOTI2YTYzMjkyZDQ2ZDA3YTFkNzkyMGY0
11
+ ZDdiODU5MzZjNmQ0YzhlZDgwMGUwMzM1ZTA1OTU5NDU5MjIzMGM=
12
12
  data.tar.gz: !binary |-
13
- ZWZkN2QwMDg5M2U5YjkwM2Y3OGY1MmIwNmZlMTQxMjQ3NmM0MjU5MjRjNmRl
14
- YTJlMzFiOTU0ZWNiZjU5M2I0YmEzYzZlMzkzZjU3Y2E1NjM5MzBjMjZkZmFl
15
- NzIzMzU4NTc0ZDA3YjE5YWRkOTVlOWE0ZWY5ZTJlYTJkY2UzOWM=
13
+ ZjIyNDVlNmQ0ODE5YWQzNTNlNTZiNzM1MmZjMDQ2MjU2YjliMDFiN2U2OTYx
14
+ ZmRhMzNmYmI2OTlhMzk0NGI5ZDIyMWFlY2JiNjE1ZGE3NTNmYTM4MGIyNjk3
15
+ YzkzMWRkZTE0YTBlZTE3NWM3NTk5ZDQyYWE5ZDUwZDdmNDJmZDI=
@@ -1,5 +1,12 @@
1
1
  require 'base64'
2
+ require 'httparty'
3
+ require 'json'
4
+ require 'active_support/all'
5
+
2
6
  class YoukuVideoUtility
7
+ CONTENT_TYPE_VIDEO = 'video'
8
+ CONTENT_TYPE_SHOW = 'show'
9
+ HOST = '10.103.88.54'
3
10
  def self.get_encoded_video_id_from_integer integer
4
11
  shifted_integer = Integer(integer) << 2 rescue ''
5
12
  encoded_video_id = Base64.encode64 "#{shifted_integer}"
@@ -21,23 +28,68 @@ class YoukuVideoUtility
21
28
  result
22
29
  end
23
30
 
24
- def get_extra_info show_id
25
- host = '10.103.88.54'
26
- url = '/show.show'
27
- query = {
28
- :query => {
29
- :ft => 'json',
30
- :q => "showid:#{show_id}",
31
- :fd => "showid pk_odshow showname showtotal_vv showcategory
32
- avg_rating showsubtitle show_thumburl show_vthumburl completed episode_total
33
- episode_last releasedate_youku lastepisode_videoid showdesc
34
- "
35
- }
31
+ def self.update_extra_information(video_object, options = {})
32
+ options = {:content_id_attribute => :show_id}.merge(options)
33
+ content_id = video_object.send options[:content_id_attribute]
34
+ content_type = get_content_type_from_content_id content_id
35
+ json = send "get_extra_information_for_#{content_type}", content_id
36
+ send "set_extra_information_for_#{content_type}", json, video_object.extra_information
37
+ end
38
+
39
+ private
40
+ def self.get_content_type_from_content_id content_id
41
+ if content_id[0] == "X" && content_id.length == 13
42
+ result = CONTENT_TYPE_VIDEO
43
+ elsif (content_id[0] == "z" && content_id.length == 21) || (content_id.length == 20)
44
+ result = CONTENT_TYPE_SHOW
45
+ else
46
+ ''
47
+ end
48
+ return result
49
+ end
50
+ def self.set_extra_information_for_show json, extra_info
51
+ extra_info.seconds = Integer(json['showlength']) * 60
52
+ extra_info.minutes_and_seconds = "#{json['showlength']}:00"
53
+ extra_info.total_vv = json['showtotal_vv']
54
+ extra_info.total_episode = json['episode_total']
55
+ extra_info.last_episode_index = json['episode_last']
56
+ extra_info.reputation = json['avg_rating']
57
+ end
58
+
59
+ def self.set_extra_information_for_video json, extra_info
60
+ extra_info.seconds = json['seconds'].to_i
61
+ extra_info.minutes_and_seconds = "#{extra_info.seconds / 60}:#{extra_info.seconds % 60}"
62
+ extra_info.total_vv = Integer json['total_vv']
63
+ end
64
+ def self.get_extra_information_for_show show_id
65
+ url = '/show.show'
66
+ query = {
67
+ :query => {
68
+ :ft => 'json',
69
+ :q => "showid:#{show_id}",
70
+ :fd => "showid pk_odshow showname showtotal_vv showcategory " +
71
+ " avg_rating showsubtitle show_thumburl show_vthumburl completed episode_total" +
72
+ " episode_last releasedate_youku lastepisode_videoid showdesc showlength"
36
73
  }
37
- response = HTTParty.get("http://#{host}#{url}", query)
38
- return nil if response.code >= 400
39
- json = JSON.parse response.body
40
- return nil if json['results'].blank? || json['results'].first.blank?
41
- return json['results'].first
74
+ }
75
+ get_response_from_media_lib(url, query)
76
+ end
77
+ def self.get_extra_information_for_video video_id
78
+ url = '/video.show'
79
+ query = {
80
+ :query => {
81
+ :ft => 'json',
82
+ :q => "videoid:#{video_id}",
83
+ :fd => "videoid seconds total_vv "
84
+ }
85
+ }
86
+ get_response_from_media_lib(url, query)
87
+ end
88
+ def self.get_response_from_media_lib url, query
89
+ response = HTTParty.get("http://#{HOST}#{url}", query)
90
+ return nil if response.code >= 400
91
+ json = JSON.parse response.body
92
+ return nil if json['results'].blank? || json['results'].first.blank?
93
+ return json['results'].first
42
94
  end
43
95
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youku_video_utility
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Siwei
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-20 00:00:00.000000000 Z
11
+ date: 2013-12-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: youku video utility
14
14
  email: shensiwei@youku.com