twicas_stream 1.0.0 → 1.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d657ce62377fdadac0733f9e3419092332e3484f
4
- data.tar.gz: 64e75337098cd48153c11576abbb3f664c88f767
3
+ metadata.gz: 190ddd6a8dba8840e8105796fdaac070e0948193
4
+ data.tar.gz: d6d19ed51e0bdc9025708ed56a14afdd93b267f6
5
5
  SHA512:
6
- metadata.gz: 4b46a27061c258843864d9b1fdb03db741892f121f5da735a582f9ba33235b22cdd07e1fb3a2bec28c611d229f2afdcc37fd7f7b8d862b302f32b947bd693fcf
7
- data.tar.gz: '0299009ee34a060b34f3dc51ded4e8266db96278f1499ef9519e91d8014e20c350624f322be71722ff95807c0c150f801942f3c551ca7221ff463e0896f8901b'
6
+ metadata.gz: a4b2c4fb6bde367d127fd335998e7111546b66d0f2e61d04908425e127abf853e95b541ffb38a7c449d74543b681d0aa176566d4add79f6053b3ae2d2934f338
7
+ data.tar.gz: 93e562700f7e02d92dffab2da8cd1e6dd96c6942432c6235480c2137bfc794c2cfc7716dd59c0c35c0544f6d34962d6dbf873505a50b4f03cc15f30bbe486f4c
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # twicas_stream
2
2
 
3
- twicas_stream provides simple Ruby access to [TwitCasting](https://twitcasting.tv/) API.
3
+ [twicas_stream](https://rubygems.org/gems/twicas_stream) provides simple Ruby access to [TwitCasting](https://twitcasting.tv/) API.
4
4
 
5
5
  ## Installation
6
6
 
@@ -97,18 +97,18 @@ Current version supports following requests.
97
97
  |:---------------|:-------------------------|:------------:|
98
98
  | User | Get User Info | Support |
99
99
  | | Verify Credentials | Support |
100
- | Live Thumbnail | Get Live Thumbnail Image | Not Support |
100
+ | Live Thumbnail | Get Live Thumbnail Image | Support |
101
101
  | Movie | Get Movie Info | Support |
102
102
  | | Get Movies by User | Not Support |
103
103
  | | Get Current Live | Not Support |
104
104
  | Comment | Get Comments | Support |
105
105
  | | Post Comment | Not Support |
106
106
  | | Delete Comment | Not Support |
107
- | Supporter | Get Supporting Status | Not Support |
107
+ | Supporter | Get Supporting Status | Support |
108
108
  | | Support User | Not Support |
109
109
  | | Unsupport User | Not Support |
110
- | | Supporting List | Not Support |
111
- | | Supporter List | Not Support |
110
+ | | Supporting List | Support |
111
+ | | Supporter List | Support |
112
112
  | Category | Get Categories | Support |
113
113
  | Search | Search Users | Support |
114
114
  | | Search Live Movies | Support |
@@ -280,17 +280,3 @@ end
280
280
  └─ README.md
281
281
  ```
282
282
 
283
- ### Test
284
-
285
- #### execution of all test
286
-
287
- ```rb
288
- ruby Test.rb
289
- ```
290
-
291
- #### execution of each test
292
-
293
- ```rb
294
- ruby TestUser.rb
295
- ```
296
-
@@ -0,0 +1,16 @@
1
+ #! /opt/local/bin/ruby
2
+ # coding: utf-8
3
+
4
+ if $0 == __FILE__
5
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/twicas_stream')
6
+
7
+ path = File.expand_path(File.dirname(__FILE__)) + '/'
8
+ user_id = 'twitcasting_jp'
9
+ api = TwicasStream::LiveThumbnail::GetLiveThumbnailImage.new(path, user_id)
10
+
11
+ exit(0) if api.filepath.empty?
12
+
13
+ STDOUT.puts 'saved image is here.'
14
+ STDOUT.puts api.filepath
15
+
16
+ end
@@ -44,7 +44,7 @@ module TwicasStream
44
44
 
45
45
  param['lang'] = lang
46
46
 
47
- url = BASE_URL + '/' + PREFIX_URL + TwicasStream.make_query_string(param)
47
+ url = [BASE_URL, PREFIX_URL].join('/') + TwicasStream.make_query_string(param)
48
48
  # => 'https://apiv2.twitcasting.tv/categories?lang=ja'
49
49
 
50
50
  @response = TwicasStream.parse(TwicasStream.get(url))
@@ -60,7 +60,7 @@ module TwicasStream
60
60
  param['limit'] = limit
61
61
  param['slice_id'] = slice_id unless slice_id == DEFAULT_SLICE_ID
62
62
 
63
- url = BASE_URL + '/' + PREFIX_URL + '/' + movie_id + '/' + SUFFIX_URL + TwicasStream.make_query_string(param)
63
+ url = [BASE_URL, PREFIX_URL, movie_id, SUFFIX_URL].join('/') + TwicasStream.make_query_string(param)
64
64
  # => 'https://apiv2.twitcasting.tv/movies/:movie_id/comments?offset=0&limit=10'
65
65
 
66
66
  @response = TwicasStream.parse(TwicasStream.get(url))
@@ -4,6 +4,52 @@
4
4
  module TwicasStream
5
5
  module LiveThumbnail
6
6
  class GetLiveThumbnailImage
7
+ attr_reader :filepath
8
+
9
+ PREFIX_URL = 'users'
10
+
11
+ INFIX_URL = 'live'
12
+
13
+ SUFFIX_URL = 'thumbnail'
14
+
15
+ DEFAULT_SIZE = 'small'
16
+
17
+ SIZE_LIMITATION = ['large', 'small']
18
+
19
+ DEFAULT_POSITION = 'latest'
20
+
21
+ POSITION_LIMITATION = ['beginning', 'latest']
22
+
23
+ def initialize path, user_id, size = DEFAULT_SIZE, position = DEFAULT_POSITION
24
+ @filepath = ''
25
+ param = Hash.new
26
+
27
+ unless File.exists?(path)
28
+ STDERR.puts "#{__FILE__}:#{__LINE__}:Error: no such file or directory - #{path}"
29
+ return
30
+ end
31
+
32
+ unless File::ftype(path) == 'directory'
33
+ STDERR.puts "#{__FILE__}:#{__LINE__}:Error: not directory - #{path}"
34
+ return
35
+ end
36
+
37
+ unless SIZE_LIMITATION.include?(size)
38
+ STDERR.puts "#{__FILE__}:#{__LINE__}:Warning: out of limitation. support size are '#{SIZE_LIMITATION.join("' or '")}'."
39
+ end
40
+
41
+ unless POSITION_LIMITATION.include?(position)
42
+ STDERR.puts "#{__FILE__}:#{__LINE__}:Warning: out of limitation. support position are '#{POSITION_LIMITATION.join("' or '")}'."
43
+ end
44
+
45
+ param['size'] = size
46
+ param['position'] = position
47
+
48
+ url = [BASE_URL, PREFIX_URL, user_id, INFIX_URL, SUFFIX_URL].join('/') + TwicasStream.make_query_string(param)
49
+ # => 'https://apiv2.twitcasting.tv/users/twitcasting_jp/live/thumbnail?size=small&position=latest'
50
+
51
+ @filepath = TwicasStream.get_image(url, path)
52
+ end
7
53
  end
8
54
  end
9
55
  end
@@ -53,7 +53,7 @@ module TwicasStream
53
53
  def initialize movie_id
54
54
  @response = Hash.new
55
55
 
56
- url = BASE_URL + '/' + PREFIX_URL + '/' + movie_id
56
+ url = [BASE_URL, PREFIX_URL, movie_id].join('/')
57
57
  # => 'https://apiv2.twitcasting.tv/movies/:movie_id'
58
58
 
59
59
  @response = TwicasStream.parse(TwicasStream.get(url))
@@ -73,7 +73,7 @@ module TwicasStream
73
73
  param['limit'] = limit
74
74
  param['lang'] = lang
75
75
 
76
- url = BASE_URL + '/' + PREFIX_URL + '/' + SUFFIX_URL + TwicasStream.make_query_string(param)
76
+ url = [BASE_URL, PREFIX_URL, SUFFIX_URL].join('/') + TwicasStream.make_query_string(param)
77
77
  # => 'https://apiv2.twitcasting.tv/search/users?words=ツイキャス+公式&limit=10&lang=ja'
78
78
 
79
79
  @response = TwicasStream.parse(TwicasStream.get(url))
@@ -168,7 +168,7 @@ module TwicasStream
168
168
  =end
169
169
  param['lang'] = lang
170
170
 
171
- url = BASE_URL + '/' + PREFIX_URL + '/' + SUFFIX_URL + TwicasStream.make_query_string(param)
171
+ url = [BASE_URL, PREFIX_URL, SUFFIX_URL].join('/') + TwicasStream.make_query_string(param)
172
172
  # => 'https://apiv2.twitcasting.tv/search/lives?limit=10&type=recommend&lang=ja'
173
173
 
174
174
  @response = TwicasStream.parse(TwicasStream.get(url))
@@ -7,6 +7,23 @@ require File.expand_path(File.dirname(__FILE__) + '/twicas_api_object/supporter_
7
7
  module TwicasStream
8
8
  module Supporter
9
9
  class GetSupportingStatus
10
+ attr_reader :response
11
+
12
+ PREFIX_URL = 'users'
13
+
14
+ SUFFIX_URL = 'supporting_status'
15
+
16
+ def initialize user_id, target_user_id
17
+ @response = Hash.new
18
+ param = Hash.new
19
+
20
+ param['target_user_id'] = target_user_id
21
+
22
+ url = [BASE_URL, PREFIX_URL, user_id, SUFFIX_URL].join('/') + TwicasStream.make_query_string(param)
23
+ # => 'https://apiv2.twitcasting.tv/users/:user_id/supporting_status?target_user_id=casma_jp'
24
+
25
+ @response = TwicasStream.parse(TwicasStream.get(url))
26
+ end
10
27
  end
11
28
 
12
29
  class SupportUser
@@ -16,9 +33,88 @@ module TwicasStream
16
33
  end
17
34
 
18
35
  class SupportingList
36
+ attr_reader :response
37
+
38
+ PREFIX_URL = 'users'
39
+
40
+ SUFFIX_URL = 'supporting'
41
+
42
+ DEFAULT_OFFSET = 0
43
+
44
+ LOWER_OFFSET = 0
45
+
46
+ DEFAULT_LIMIT = 20
47
+
48
+ LOWER_LIMIT = 1
49
+
50
+ UPPER_LIMIT = 20
51
+
52
+ def initialize user_id, offset = DEFAULT_OFFSET, limit = DEFAULT_LIMIT
53
+ @response = Hash.new
54
+ param = Hash.new
55
+
56
+ if offset < LOWER_OFFSET
57
+ STDERR.puts "#{__FILE__}:#{__LINE__}:Warning: out of limitation. offset should be over than #{LOWER_OFFSET}."
58
+ end
59
+
60
+ unless limit >= LOWER_LIMIT and limit <= UPPER_LIMIT
61
+ STDERR.puts "#{__FILE__}:#{__LINE__}:Warning: out of limitation. limitation range is #{LOWER_LIMIT} ~ #{UPPER_LIMIT}."
62
+ end
63
+
64
+ param['offset'] = offset
65
+ param['limit'] = limit
66
+
67
+ url = [BASE_URL, PREFIX_URL, user_id, SUFFIX_URL].join('/') + TwicasStream.make_query_string(param)
68
+ # => 'https://apiv2.twitcasting.tv/users/twitcasting_jp/supporting?offset=10&limit=20'
69
+
70
+ @response = TwicasStream.parse(TwicasStream.get(url))
71
+ end
19
72
  end
20
73
 
21
74
  class SupporterList
75
+ attr_reader :response
76
+
77
+ PREFIX_URL = 'users'
78
+
79
+ SUFFIX_URL = 'supporters'
80
+
81
+ DEFAULT_OFFSET = 0
82
+
83
+ LOWER_OFFSET = 0
84
+
85
+ DEFAULT_LIMIT = 20
86
+
87
+ LOWER_LIMIT = 1
88
+
89
+ UPPER_LIMIT = 20
90
+
91
+ SORT_LIMITATION = ['new', 'ranking']
92
+
93
+ def initialize user_id, offset = DEFAULT_OFFSET, limit = DEFAULT_LIMIT, sort
94
+ @response = Hash.new
95
+ param = Hash.new
96
+
97
+ if offset < LOWER_OFFSET
98
+ STDERR.puts "#{__FILE__}:#{__LINE__}:Warning: out of limitation. offset should be over than #{LOWER_OFFSET}."
99
+ end
100
+
101
+ unless limit >= LOWER_LIMIT and limit <= UPPER_LIMIT
102
+ STDERR.puts "#{__FILE__}:#{__LINE__}:Warning: out of limitation. limitation range is #{LOWER_LIMIT} ~ #{UPPER_LIMIT}."
103
+ end
104
+
105
+ unless SORT_LIMITATION.include?(sort)
106
+ STDERR.puts "#{__FILE__}:#{__LINE__}:Warning: out of limitation. sort is '#{SORT_LIMITATION.join("' or '")}'."
107
+ end
108
+
109
+ param['offset'] = offset
110
+ param['limit'] = limit
111
+ param['sort'] = sort
112
+
113
+ url = [BASE_URL, PREFIX_URL, user_id, SUFFIX_URL].join('/') + TwicasStream.make_query_string(param)
114
+ # => 'https://apiv2.twitcasting.tv/users/twitcasting_jp/supporting?offset=10&limit=20'
115
+
116
+ @response = TwicasStream.parse(TwicasStream.get(url))
117
+ end
22
118
  end
23
119
  end
24
120
  end
@@ -4,6 +4,25 @@
4
4
  module TwicasStream
5
5
  module TwicasApiObject
6
6
  class SupporterUser
7
+ attr_reader :object
8
+
9
+ def initialize elements = {}
10
+ @object = {
11
+ :id => elements['id'],
12
+ :screen_id => elements['screen_id'],
13
+ :name => elements['name'],
14
+ :image => elements['image'],
15
+ :profile => elements['profile'],
16
+ :level => elements['level'],
17
+ :last_movie_id => elements['last_movie_id'],
18
+ :is_live => elements['is_live'],
19
+ :supporter_count => elements['supporter_count'],
20
+ :supporting_count => elements['supporting_count'],
21
+ :created => elements['created'],
22
+ :point => elements['point'],
23
+ :total_point => elements['total_point']
24
+ }
25
+ end
7
26
  end
8
27
  end
9
28
  end
@@ -14,7 +14,7 @@ module TwicasStream
14
14
  def initialize user_id
15
15
  @response = Hash.new
16
16
 
17
- url = BASE_URL + '/' + PREFIX_URL + '/' + user_id
17
+ url = [BASE_URL, PREFIX_URL, user_id].join('/')
18
18
  # => 'https://apiv2.twitcasting.tv/users/:user_id'
19
19
 
20
20
  @response = TwicasStream.parse(TwicasStream.get(url))
@@ -29,7 +29,7 @@ module TwicasStream
29
29
  def initialize
30
30
  @response = Hash.new
31
31
 
32
- url = BASE_URL + '/' + PREFIX_URL
32
+ url = [BASE_URL, PREFIX_URL].join('/')
33
33
  # => 'https://apiv2.twitcasting.tv/verify_credentials'
34
34
 
35
35
  @response = TwicasStream.parse(TwicasStream.get(url))
@@ -2,5 +2,5 @@
2
2
  # coding: utf-8
3
3
 
4
4
  module TwicasStream
5
- VERSION = '1.0.0'
5
+ VERSION = '1.1.0'
6
6
  end
data/lib/twicas_stream.rb CHANGED
@@ -17,6 +17,7 @@ require File.expand_path(File.dirname(__FILE__) + '/twicas_stream/twicas_api_obj
17
17
  require 'active_support/inflector'
18
18
  require 'curb'
19
19
  require 'json'
20
+ require 'open-uri'
20
21
 
21
22
  module TwicasStream
22
23
  extend RequestHeader
@@ -25,10 +26,11 @@ module TwicasStream
25
26
 
26
27
  class << self
27
28
  def parse result
28
- hash = Hash.new
29
+ response = Hash.new
29
30
 
30
- response = response(result)
31
+ response_code = response_code(result)
31
32
  body = body(result)
33
+ header = header(result)
32
34
 
33
35
  body.each{ |key, value|
34
36
  # here is the plural form of TwicasStream
@@ -45,31 +47,39 @@ module TwicasStream
45
47
  array.push tmp
46
48
  }
47
49
 
48
- hash[key.to_sym] = array
50
+ response[key.to_sym] = array
49
51
 
50
52
  else
51
- hash[key.to_sym] = value.map{ |e| parse_deep(key, e) }
53
+ response[key.to_sym] = value.map{ |i| parse_deep(key, i) }
52
54
 
53
55
  end
56
+ # following keys is actually TwicasApiObject as 'SupporterUser', but is_TwicasApiObject?(key) = false
57
+ # so, we prepare special support as below
58
+ elsif key == 'supporting' or key == 'supporters'
59
+ response[key.to_sym] = value.map{ |i| parse_deep('SupporterUser', i) }
54
60
 
55
61
  # here is a singular form of TwicasApiObject, also others are here
56
62
  else
57
- hash[key.to_sym] = parse_deep(key, value)
63
+ response[key.to_sym] = parse_deep(key, value)
58
64
 
59
65
  end
60
66
  }
61
67
 
62
- hash
68
+ response
63
69
  end
64
70
 
65
- def response result
66
- result[:response]
71
+ def response_code result
72
+ result[:response_code]
67
73
  end
68
74
 
69
75
  def body result
70
76
  result[:body]
71
77
  end
72
78
 
79
+ def header result
80
+ result[:header]
81
+ end
82
+
73
83
  def get url
74
84
  c = Curl.get(url) do |curl|
75
85
  TwicasStream.configure do |request_header|
@@ -79,7 +89,19 @@ module TwicasStream
79
89
  end
80
90
  end
81
91
 
82
- { :body => JSON.parse(c.body_str), :response => c.response_code }
92
+ response, *headers = c.header_str.split(/[\r\n]+/).map(&:strip)
93
+ headers = Hash[headers.flat_map{ |s| s.scan(/^(\S+): (.+)/) }]
94
+
95
+ { :header => headers, :body => JSON.parse(c.body_str), :response_code => c.response_code }
96
+ end
97
+
98
+ def get_image url, path
99
+ c = Curl.get(url)
100
+
101
+ response, *headers = c.header_str.split(/[\r\n]+/).map(&:strip)
102
+ headers = Hash[headers.flat_map{ |s| s.scan(/^(\S+): (.+)/) }]
103
+
104
+ save_image(path, headers['Location'])
83
105
  end
84
106
 
85
107
  def make_query_string options
@@ -94,13 +116,13 @@ module TwicasStream
94
116
  private
95
117
  def parse_deep key, value
96
118
  if is_TwicasApiObject?(key)
97
- str = [self.to_s, 'TwicasApiObject', key.singularize.capitalize].join('::')
119
+ str = [self.to_s, 'TwicasApiObject', key.singularize.camelize].join('::')
98
120
  api = Object.const_get(str).new(value)
99
121
 
100
122
  return api.object
101
123
 
102
- # called by 'GetMovieInfo' method
103
- elsif key == 'broadcaster'
124
+ # called by 'GetMovieInfo' or 'GetSupportingStatus' method
125
+ elsif key == 'broadcaster' or key == 'target_user'
104
126
  str = [self.to_s, 'TwicasApiObject', 'User'].join('::')
105
127
  api = Object.const_get(str).new(value)
106
128
 
@@ -114,9 +136,27 @@ module TwicasStream
114
136
 
115
137
  private
116
138
  def is_TwicasApiObject? str
117
- TwicasApiObject.constants.include?(str.singularize.capitalize.to_sym)
139
+ TwicasApiObject.constants.include?(str.singularize.camelize.to_sym)
118
140
  # TwicasApiObject.constants
119
141
  # => [:App, :User, :Movie, :Comment, :SupporterUser, :SubCategory, :Category, :Error]
120
142
  end
143
+
144
+ private
145
+ def save_image path, image_url
146
+ begin
147
+ filepath = path + File.basename(image_url)
148
+ rescue Exception => e
149
+ STDERR.puts "#{__FILE__}:#{__LINE__}:Error: #{e.message}"
150
+ filepath = ''
151
+ else
152
+ File.open(filepath, 'wb') do |file|
153
+ open(image_url) do |data|
154
+ file.write(data.read)
155
+ end
156
+ end
157
+ end
158
+
159
+ filepath
160
+ end
121
161
  end
122
162
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twicas_stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuya Sato
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-23 00:00:00.000000000 Z
11
+ date: 2018-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -112,6 +112,7 @@ files:
112
112
  - bin/setup
113
113
  - examples/get_categories.rb
114
114
  - examples/get_comments.rb
115
+ - examples/get_live_thumbnail_image.rb
115
116
  - examples/get_movie_info.rb
116
117
  - examples/get_user_info.rb
117
118
  - examples/search_live_movies.rb