nicoquery 0.0.1.4 → 0.0.2

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: 15d6f2118588b2124758dc3642306e06a00b9d69
4
- data.tar.gz: 55813fd196a22de63b674cc9499208e04ee03217
3
+ metadata.gz: 4f0ddf47b8f763c5fc064122517e69f31680ca13
4
+ data.tar.gz: dc5b0711ae9be3cfcc2ad20d789f31d89c407146
5
5
  SHA512:
6
- metadata.gz: 8a4af2d7651431930e523409113dac51189184edd9b4e26a969e9f96f6291ec5ca80246a8c13db06362500d596df88d2166a8b2cc9a7030cc77ac841a716abca
7
- data.tar.gz: a6db57d557c28d24d007df3669ac3fbff4033ef56f09708321ec925de279d96750f1f4eb503a56d2b10d28f011ac051d89a18e284ca88cca4c0676f632080ff3
6
+ metadata.gz: 97c1ab0f74bf11b527a4a2ac976af6f55c3e70a2e4697d44d936539ddf51b15dc9292f11343540c6d6ddd005ee26c279cd83eccd5dca4b8d83a6e8931ae8a962
7
+ data.tar.gz: 1b9801240a7427f2ba1bcfa7bb14935c77ebf17f25183ecdcfb7ed555c5decdbd61b5f0e8791d29225eeb840f5f40010c3430893c4c2d3ec84efec4b4b6c2789
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nicoquery (0.0.1.4)
4
+ nicoquery (0.0.2)
5
5
  activesupport (~> 4.0.0)
6
6
  i18n
7
7
  nicoapi
@@ -27,7 +27,7 @@ GEM
27
27
  mini_portile (0.5.1)
28
28
  minitest (4.7.5)
29
29
  multi_json (1.7.8)
30
- nicoapi (0.0.4)
30
+ nicoapi (0.0.4.1)
31
31
  activesupport (~> 4.0.0)
32
32
  i18n
33
33
  rest-client
@@ -0,0 +1,33 @@
1
+ require 'rest_client'
2
+
3
+
4
+ module NicoQuery
5
+ module Api
6
+ class Base
7
+ def scheme
8
+ 'http'
9
+ end
10
+
11
+ def params
12
+ @params_array.join('&')
13
+ end
14
+
15
+ def path
16
+ if @dynamic_segment.present?
17
+ static_segment + '/' + @dynamic_segment
18
+ else
19
+ static_segment
20
+ end
21
+ end
22
+
23
+ def uri
24
+ _uri = scheme + "://" + [ ([host, path].join('/')), params].join('?')
25
+ URI.escape _uri
26
+ end
27
+
28
+ def get
29
+ RestClient.get uri.to_s
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ require 'nicoquery/api/base'
2
+
3
+
4
+ module NicoQuery
5
+ module Api
6
+ class GetThumbInfo < NicoQuery::Api::Base
7
+ def initialize(video_id)
8
+ @dynamic_segment = video_id
9
+ @params_array = []
10
+ end
11
+
12
+ private
13
+ def host
14
+ 'ext.nicovideo.jp'
15
+ end
16
+
17
+ def static_segment
18
+ 'api/getthumbinfo'
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ require 'active_support/core_ext'
2
+ require 'nicoquery/api/base'
3
+
4
+
5
+ module NicoQuery
6
+ module Api
7
+ class MylistRSS < NicoQuery::Api::Base
8
+
9
+ def initialize(mylist_id)
10
+ @dynamic_segment = mylist_id.to_s
11
+ @params_array = params_array
12
+ end
13
+
14
+ private
15
+ def host
16
+ 'www.nicovideo.jp'
17
+ end
18
+
19
+ def static_segment
20
+ 'mylist'
21
+ end
22
+
23
+ def params_array
24
+ [format]
25
+ end
26
+
27
+ def format
28
+ "rss=2.0"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,66 @@
1
+ require 'active_support/core_ext'
2
+ require 'nicoquery/api/base'
3
+
4
+
5
+ module NicoQuery
6
+ module Api
7
+ class TagSearchRss < NicoQuery::Api::Base
8
+
9
+ def initialize(tag: tag, sort: sort, order: order, page: page)
10
+ @dynamic_segment = tag
11
+ @params_array = params_array(sort, order, page)
12
+ end
13
+
14
+ private
15
+ def host
16
+ 'www.nicovideo.jp'
17
+ end
18
+
19
+ def static_segment
20
+ 'tag'
21
+ end
22
+
23
+ def params_array(sort, order, page)
24
+ [
25
+ sort_param(sort),
26
+ order_param(order),
27
+ page_param(page),
28
+ format,
29
+ "numbers=1", # it enables to catch view/comment/mylist cum.
30
+ ]
31
+ end
32
+
33
+ def format
34
+ "rss=2.0"
35
+ end
36
+
37
+ def sort_param(sort)
38
+ sort_string = case sort
39
+ when :commented_at then nil
40
+ when :view_count then 'v'
41
+ when :comment_num then 'r'
42
+ when :mylist_count then 'm'
43
+ when :published_at then 'f'
44
+ when :length then 'l'
45
+ else nil
46
+ end
47
+
48
+ sort_string.present? ? "sort=#{sort_string}" : ''
49
+ end
50
+
51
+ def order_param(order)
52
+ order_string = case order
53
+ when :asc then 'order=a'
54
+ when :desc then 'order=d'
55
+ else nil
56
+ end
57
+
58
+ order_string.presence || ''
59
+ end
60
+
61
+ def page_param(page)
62
+ "page=#{page}"
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,28 @@
1
+ require 'nicoquery/api/base'
2
+
3
+
4
+ module NicoQuery
5
+ module Api
6
+ class VideoArray < NicoQuery::Api::Base
7
+ def initialize(video_id_array)
8
+ @video_id_array = video_id_array
9
+ @dynamic_segment = ''
10
+ @params_array = params_array @video_id_array
11
+ end
12
+
13
+ private
14
+ def host
15
+ 'i.nicovideo.jp'
16
+ end
17
+
18
+ def static_segment
19
+ 'v3/video.array'
20
+ end
21
+
22
+ # video.array APIは動的セグメントではなく、URIパラメータとして動画を指定する。
23
+ def params_array(video_id_array)
24
+ ['v=' + video_id_array.join(',')]
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
- require "nicoapi"
2
- require "nicoquery/parser"
1
+ require "nicoquery/api/tag_search"
2
+ require "nicoquery/object_mapper/tag_search"
3
3
  require "nori"
4
4
 
5
5
 
@@ -7,17 +7,14 @@ module NicoQuery
7
7
  module Crawler
8
8
  module TagSearch
9
9
  def execute(tag: tag, sort: sort, order: order, &block)
10
- parser = NicoQuery::Parser::TagSearch.new
11
10
  page = 0
12
11
 
13
12
  loop do
14
13
  command = nil
15
14
  page += 1
16
15
 
17
- result = NicoAPI.tag_search(tag: tag, sort: sort, order: order, page: page)
18
- parser.parse result
19
-
20
- self.each_movie(parser.items) do |movie|
16
+ tag_search_object = NicoQuery::Object::TagSearch.new tag: tag, sort: sort, order: order, page: page
17
+ self.each_movie(tag_search_object.movies) do |movie|
21
18
  command = block.call movie
22
19
  break if command == :break || command != :continue
23
20
  end
@@ -0,0 +1,77 @@
1
+ require "nicoquery/api/getthumbinfo"
2
+ require "nicoquery/object_mapper/getthumbinfo"
3
+
4
+
5
+ module NicoQuery
6
+ module Object
7
+ class Movie
8
+ attr_reader :video_id
9
+
10
+ [
11
+ 'title',
12
+ 'url',
13
+ # 'thread_id',
14
+ 'view_counter',
15
+ 'comment_num',
16
+ 'mylist_counter',
17
+ 'publish_date',
18
+ 'description',
19
+ ].each do |field_name|
20
+ define_method(field_name) do
21
+ source =
22
+ @source['mylist_rss'].presence ||
23
+ @source['tag_search_rss'].presence ||
24
+ @source['gethumbinfo'].presence ||
25
+ Proc.new do
26
+ source = (NicoQuery::Api::GetThumbInfo.new @video_id).get
27
+ set_getthumbinfo_source(NicoQuery::ObjectMapper::GetThumbInfo.new source)
28
+ end.call
29
+
30
+ source.send field_name
31
+ end
32
+ end
33
+
34
+ [
35
+ 'movie_type',
36
+ 'size_high',
37
+ 'size_low',
38
+ 'last_res_body',
39
+ 'thumb_type',
40
+ 'embeddable',
41
+ 'no_live_play',
42
+ 'user_id',
43
+ 'tags',
44
+ ].each do |field_name|
45
+ define_method(field_name) do
46
+ source =
47
+ @source['gethumbinfo'].presence ||
48
+ @source['video_array'].presence ||
49
+ Proc.new do
50
+ source = (NicoQuery::Api::GetThumbInfo.new @video_id).get
51
+ set_getthumbinfo_source(NicoQuery::ObjectMapper::GetThumbInfo.new source)
52
+ end.call
53
+
54
+ source.send field_name
55
+ end
56
+ end
57
+
58
+ def initialize(video_id)
59
+ @source = {}
60
+ @video_id = video_id
61
+ end
62
+
63
+ def set_getthumbinfo_source(hash)
64
+ @source['getthumbinfo'] ||= hash
65
+ end
66
+
67
+ def set_mylist_rss_source(hash)
68
+ @source['mylist_rss'] ||= hash
69
+ end
70
+
71
+ def set_tag_search_rss_source(hash)
72
+ @source['tag_search_rss'] ||= hash
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,51 @@
1
+ require "nicoquery/api/mylist_rss"
2
+ require "nicoquery/object_mapper/mylist_rss"
3
+ require "nicoquery/object/movie"
4
+
5
+
6
+ module NicoQuery
7
+ module Object
8
+ class Mylist
9
+ attr_accessor :movies
10
+
11
+ [
12
+ 'title',
13
+ 'mylist_id',
14
+ 'url',
15
+ 'link',
16
+ 'description',
17
+ 'publish_date',
18
+ 'last_build_date',
19
+ 'creator',
20
+ ].each do |field_name|
21
+ define_method(field_name) { @hash.meta.send field_name }
22
+ end
23
+
24
+ def initialize(mylist_id)
25
+ @movies = []
26
+ @mylist_id = mylist_id
27
+ source = (NicoQuery::Api::MylistRSS.new mylist_id).get
28
+ @hash = NicoQuery::ObjectMapper::MylistRSS.new source
29
+
30
+ @hash.items.map do |item|
31
+ movie = NicoQuery::Object::Movie.new item.video_id
32
+ movie.set_mylist_rss_source item
33
+ @movies.push movie
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+
41
+ # <title>マイリスト to_test‐ニコニコ動画</title>
42
+ # <link>http://www.nicovideo.jp/mylist/38369702</link>
43
+ # <atom:link rel="self" type="application/rss+xml" href="http://www.nicovideo.jp/mylist/38369702?rss=2.0"/>
44
+ # <description></description>
45
+ # <pubDate>Sat, 17 Aug 2013 22:51:40 +0900</pubDate>
46
+ # <lastBuildDate>Sat, 17 Aug 2013 22:51:40 +0900</lastBuildDate>
47
+ # <generator>ニコニコ動画</generator>
48
+ # <dc:creator>うえおに</dc:creator>
49
+ # <language>ja-jp</language>
50
+ # <copyright>(c) niwango, inc. All rights reserved.</copyright>
51
+ # <docs>http://blogs.law.harvard.edu/tech/rss</docs>
@@ -0,0 +1,48 @@
1
+ require "nicoquery/api/tag_search_rss"
2
+ require "nicoquery/object_mapper/tag_search_rss"
3
+ require "nicoquery/object/movie"
4
+
5
+
6
+ module NicoQuery
7
+ module Object
8
+ class TagSearch
9
+ attr_accessor :movies
10
+
11
+ [
12
+ 'title',
13
+ 'url',
14
+ 'link',
15
+ 'description',
16
+ 'publish_date',
17
+ 'creator',
18
+ ].each do |field_name|
19
+ define_method(field_name) { @hash.meta.send field_name }
20
+ end
21
+
22
+ def initialize(tag: tag, sort: sort, order: order, page: page)
23
+ @movies = []
24
+ source = (NicoQuery::Api::TagSearchRss.new(tag: tag, sort: sort, order: order, page: page)).get
25
+ @hash = NicoQuery::ObjectMapper::TagSearchRss.new source
26
+
27
+ @hash.items.map do |item|
28
+ movie = NicoQuery::Object::Movie.new item.video_id
29
+ movie.set_tag_search_rss_source item
30
+ @movies.push movie
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+
38
+ # <title>マイリスト to_test‐ニコニコ動画</title>
39
+ # <link>http://www.nicovideo.jp/mylist/38369702</link>
40
+ # <atom:link rel="self" type="application/rss+xml" href="http://www.nicovideo.jp/mylist/38369702?rss=2.0"/>
41
+ # <description></description>
42
+ # <pubDate>Sat, 17 Aug 2013 22:51:40 +0900</pubDate>
43
+ # <lastBuildDate>Sat, 17 Aug 2013 22:51:40 +0900</lastBuildDate>
44
+ # <generator>ニコニコ動画</generator>
45
+ # <dc:creator>うえおに</dc:creator>
46
+ # <language>ja-jp</language>
47
+ # <copyright>(c) niwango, inc. All rights reserved.</copyright>
48
+ # <docs>http://blogs.law.harvard.edu/tech/rss</docs>
@@ -0,0 +1,151 @@
1
+ require "nori"
2
+
3
+
4
+ module NicoQuery
5
+ module ObjectMapper
6
+ class GetThumbInfo
7
+ def initialize(xml)
8
+ @xml = xml
9
+ parser = Nori.new
10
+ parsed_xml = parser.parse xml
11
+ @hash = parsed_xml['nicovideo_thumb_response']['thumb']
12
+ end
13
+
14
+ def video_id
15
+ @hash['video_id']
16
+ end
17
+
18
+ def title
19
+ @hash['title']
20
+ end
21
+
22
+ def description
23
+ @hash['description']
24
+ end
25
+
26
+ def thumbnail_url
27
+ @hash['thumbnail_url']
28
+ end
29
+
30
+ def first_retrieve
31
+ @hash['first_retrieve'].to_time
32
+ end
33
+
34
+ def publish_date # alias
35
+ first_retrieve
36
+ end
37
+
38
+ def length
39
+ string = @hash['length'].split(':')
40
+ string[0].to_i * 60 + string[1].to_i
41
+ end
42
+
43
+ def movie_type
44
+ @hash['movie_type']
45
+ end
46
+
47
+ def size_high
48
+ @hash['size_high'].to_i
49
+ end
50
+
51
+ def size_low
52
+ @hash['size_low'].to_i
53
+ end
54
+
55
+ def view_counter
56
+ @hash['view_counter'].to_i
57
+ end
58
+
59
+ def comment_num
60
+ @hash['comment_num'].to_i
61
+ end
62
+
63
+ def mylist_counter
64
+ @hash['mylist_counter'].to_i
65
+ end
66
+
67
+ def last_res_body
68
+ @hash['last_res_body']
69
+ end
70
+
71
+ def url
72
+ @hash['watch_url']
73
+ end
74
+
75
+ def watch_url
76
+ @hash['watch_url']
77
+ end
78
+
79
+ def thumb_type
80
+ @hash['thumb_type']
81
+ end
82
+
83
+ def embeddable
84
+ @hash['embeddable'] == 1
85
+ end
86
+
87
+ def no_live_play
88
+ @hash['no_live_play'] == 1
89
+ end
90
+
91
+ def tags
92
+ xml = @xml.scan(/\<tags domain=\"jp\">\n.+\n\<\/tags\>/m)[0]
93
+ parsed = Nokogiri::XML xml
94
+ parsed.xpath("//tag").map do |tag_object|
95
+ generate_tag_hash_by tag_object
96
+ end
97
+ end
98
+
99
+ def user_id
100
+ @hash['user_id'].to_i
101
+ end
102
+
103
+ def generate_tag_hash_by(nokogiri_xml)
104
+ text = nokogiri_xml.text
105
+
106
+ lock = if nokogiri_xml.attributes['lock'].present?
107
+ nokogiri_xml.attributes['lock'].text.to_i == 1
108
+ else
109
+ false
110
+ end
111
+
112
+ { text: text, lock: lock }
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ # <nicovideo_thumb_response status="ok">
119
+ # <thumb>
120
+ # <video_id>sm9</video_id>
121
+ # <title>新・豪血寺一族 -煩悩解放 - レッツゴー!陰陽師</title>
122
+ # <description>レッツゴー!陰陽師(フルコーラスバージョン)</description>
123
+ # <thumbnail_url>http://tn-skr2.smilevideo.jp/smile?i=9</thumbnail_url>
124
+ # <first_retrieve>2007-03-06T00:33:00+09:00</first_retrieve>
125
+ # <length>5:19</length>
126
+ # <movie_type>flv</movie_type>
127
+ # <size_high>21138631</size_high>
128
+ # <size_low>17436492</size_low>
129
+ # <view_counter>13536149</view_counter>
130
+ # <comment_num>4148196</comment_num>
131
+ # <mylist_counter>133688</mylist_counter>
132
+ # <last_res_body>へん? はははははははははは wwwwwwwwwwwwwwwwwwww ...</last_res_body>
133
+ # <watch_url>http://www.nicovideo.jp/watch/sm9</watch_url>
134
+ # <thumb_type>video</thumb_type>
135
+ # <embeddable>1</embeddable>
136
+ # <no_live_play>0</no_live_play>
137
+ # <tags domain="jp">
138
+ # <tag lock="1">陰陽師</tag>
139
+ # <tag lock="1">レッツゴー!陰陽師</tag>
140
+ # <tag lock="1">公式</tag>
141
+ # <tag lock="1">音楽</tag>
142
+ # <tag lock="1">ゲーム</tag>
143
+ # <tag>β時代の英雄</tag>
144
+ # <tag>運営のお気に入り</tag>
145
+ # <tag>最古の動画</tag>
146
+ # <tag>1000万再生</tag>
147
+ # <tag>弾幕動画</tag>
148
+ # </tags>
149
+ # <user_id>4</user_id>
150
+ # </thumb>
151
+ # </nicovideo_thumb_response>