nicorepo 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: 8aa463841c71be718b4908e77b2450805fdfa8b9
4
- data.tar.gz: 3801987c1e20a697ea4638693aba1f2705074d64
3
+ metadata.gz: e69cda5670a0c001d0683c3879ec4a513d399e69
4
+ data.tar.gz: 632dd4545dd0798eb7067f95ff76cd2b989f2f2a
5
5
  SHA512:
6
- metadata.gz: b1ecf7b7d40f706f3237ea39374c19c02b32784c13557abd134583d75964c964c99ec289d177e642667d49ea4cfc063fcbc83f1417689b3e184f84263657c233
7
- data.tar.gz: 174b50aa05f1f9a73e84acbcda0d32284daa98095a7540f11352cd37ce9c6b8577fa3efe1abba120d2c54ea74d081ba0f5d24ac4befd6e2c16fb1fa482771c85
6
+ metadata.gz: ab7793b6f85e1567274ab1605b849116518af0dff0f4ecf4316004930b3cfebb100c05dc1b221490194d8c76c83264be02ae696837b41d3c28dc19a3fa196c99
7
+ data.tar.gz: 5b7c1a4f70748ddd4dd9f7272cb78174f3bb098b0dce617f1d20b4193fa3637ade902864dd93398e94b1581aee0580259c9acd3e2442374d4e589e0a2cc4cb4f
data/CHANGELOG.md CHANGED
@@ -1,7 +1,23 @@
1
+ ## 0.0.5 (2014/05/11)
2
+
3
+ ### Features
4
+
5
+ #### CLI
6
+ * Add options `--latest`, `--hours=N`, `days=N` for fetch command
7
+ * Reports is cached and it can recalled by `show` command
8
+ * `show` command recalls recent reports by default
9
+ * You can specify `--more` option and it shows all report caches
10
+ * Caches are cleared when you exit the cli
11
+ * Reports will be colorfully decorated
12
+
13
+ ### API
14
+ * Enable to fetch since specific time
15
+
1
16
  ## 0.0.4 (2014-05-04)
2
17
 
3
- CLI Features:
18
+ ### Features
4
19
 
20
+ #### CLI
5
21
  * Add thor and reconstruct CLI
6
22
  * Simplify CLI by removing exec modes without interactive mode
7
23
  * Now you can launch interaction cli without any exec options
data/README.md CHANGED
@@ -36,11 +36,24 @@ machine nicovideo.jp
36
36
  password your-password
37
37
  ```
38
38
 
39
- ### Start nicorepo cli as interactive mode
39
+ ### Start nicorepo cli
40
40
 
41
- $ nicorepo i
41
+ $ nicorepo
42
42
 
43
43
  You can use following commands in interactive cli to fetch nicorepos.
44
+
45
+ command | alias | description
46
+ ----------------------|-------|---------------------------------------------------------
47
+ all | a | fetch all reports
48
+ videos | v | fetch only video reports
49
+ lives | li | fetch only live reports
50
+ show | s | show current reports
51
+ open REPORT-NUMBER | o | open the report url specified by number in your browser
52
+ help [COMMAND] | h | Describe available commands or one specific command
53
+ login | lo | re-login if your session is expired
54
+ exit | e | exit interactive prompt
55
+
56
+ Some commands have specific options `-n, --request-num=N`, `-p, --limit-page=N`.
44
57
  For example, if you want 20 video reports by searcing over 5 pages, the command will be,
45
58
 
46
59
  > video -n20 -p5
@@ -59,22 +72,10 @@ You can omit it as you like then each commad uses default value.
59
72
  > v -p5
60
73
  # => `video -n20 -p5`
61
74
 
62
- **Commands**
63
-
64
- command | alias | description
65
- ----------------------|-------|---------------------------------------------------------
66
- all | a | fetch all reports
67
- videos | v | fetch only video reports
68
- lives | li | fetch only live reports
69
- show | s | show current reports
70
- open REPORT-NUMBER | o | open the report url specified by number in your browser
71
- help [COMMAND] | h | Describe available commands or one specific command
72
- login | lo | re-login if your session is expired
73
- exit | e | exit interactive prompt
74
-
75
- Some commands have specific options `-n, --request-num=N`, `-p, --limit-page=N`.
76
- Please check it out using `help [COMMAND]`.
75
+ And also, you can use `-l, --latest`, `-h, --hours`, `-d, --days` options to find reports by the specific period.
77
76
 
77
+ > all -l
78
+ > lives -h 1
78
79
 
79
80
  ### Configuration
80
81
 
@@ -21,16 +21,8 @@ class Nicorepo
21
21
  class ReportExistenceError < StandardError; end
22
22
 
23
23
  class << self
24
- attr_accessor :reports
25
- attr_reader :repo, :conf
26
-
27
24
  def start(*)
28
- @repo ||= Nicorepo.new
29
- @conf ||= Nicorepo::Cli::Configuration.new
30
- @reports ||= []
31
-
32
- login unless logined?
33
-
25
+ login unless repo.logined?
34
26
  super
35
27
  end
36
28
 
@@ -43,16 +35,22 @@ class Nicorepo
43
35
  mail, pass = Netrc.read["nicovideo.jp"]
44
36
  raise LoginAccountError, "machine nicovideo.jp is not defined in .netrc" if mail.nil? || pass.nil?
45
37
  begin
46
- @repo.login(mail, pass)
38
+ repo.login(mail, pass)
47
39
  rescue
48
40
  raise LoginAccountError, "invalid mail or pass: mail = #{mail}"
49
41
  end
50
- @logined = true
51
42
  end
52
43
 
53
- # TODO: Nicorepo側に持たせる
54
- def logined?
55
- @logined
44
+ def repo
45
+ @repo ||= Nicorepo.new
46
+ end
47
+
48
+ def conf
49
+ @conf ||= Nicorepo::Cli::Configuration.new
50
+ end
51
+
52
+ def cache
53
+ @cache ||= {}
56
54
  end
57
55
  end
58
56
 
@@ -63,38 +61,54 @@ class Nicorepo
63
61
 
64
62
  desc "all", "fetch all reports"
65
63
  option :"request-num", type: :numeric, aliases: :n
64
+ option :"latest", type: :boolean, aliases: :l
65
+ option :"days", type: :numeric, aliases: :d
66
+ option :"hours", type: :numeric, aliases: :h
66
67
  def all
67
68
  request_num = options[:"request-num"] || conf.request_num("all")
68
- cache(repo.all(request_num))
69
+ request_options = { since: parse_since_options(options) }
70
+ cache(repo.all(request_num, request_options))
69
71
  show
70
72
  end
71
73
 
72
74
  desc "videos", "fetch only video reports"
73
75
  option :"request-num", type: :numeric, aliases: :n
74
76
  option :"limit-page", type: :numeric, aliases: :p
77
+ option :"latest", type: :boolean, aliases: :l
78
+ option :"days", type: :numeric, aliases: :d
79
+ option :"hours", type: :numeric, aliases: :h
75
80
  def videos
76
81
  request_num = options[:"request-num"] || conf.request_num("videos")
77
82
  limit_page = options[:"limit-page"] || conf.limit_page("videos")
78
- cache(repo.videos(request_num, limit_page))
83
+ request_options = { since: parse_since_options(options) }
84
+ cache(repo.videos(request_num, limit_page, request_options))
79
85
  show
80
86
  end
81
87
 
82
88
  desc "lives", "fetch only live reports"
83
89
  option :"request-num", type: :numeric, aliases: :n
84
90
  option :"limit-page", type: :numeric, aliases: :p
91
+ option :"latest", type: :boolean, aliases: :l
92
+ option :"days", type: :numeric, aliases: :d
93
+ option :"hours", type: :numeric, aliases: :h
85
94
  def lives
86
95
  request_num = options[:"request-num"] || conf.request_num("lives")
87
96
  limit_page = options[:"limit-page"] || conf.limit_page("lives")
88
- cache(repo.lives(request_num, limit_page))
97
+ request_options = { since: parse_since_options(options) }
98
+ cache(repo.lives(request_num, limit_page, request_options))
89
99
  show
90
100
  end
91
101
 
92
102
  desc "show", "show current reports"
103
+ option :more, type: :boolean, aliases: :m
93
104
  def show
94
- current_reports.each.with_index(1) do |report, i|
95
- puts "[#{i}] #{report.body} on #{report.date.to_s}"
96
- puts " '#{report.title}' (#{report.url})"
105
+ showed_reports = options[:more] ? cached_reports : cached_reports[0, recent_tail]
106
+ showed_reports.each.with_index(1) do |report, i|
107
+ say "--- MORE ---", :blue if i == recent_tail + 1
108
+ say "[#{i}] #{report.body} at #{report.date.to_s}"
109
+ say " #{report.title} (#{report.url})", :green
97
110
  end
111
+ say "* last fetch time: #{cached_at}", :blue
98
112
  end
99
113
 
100
114
  desc "open REPORT-NUMBER", "open the report url specified by number in your browser"
@@ -117,16 +131,39 @@ class Nicorepo
117
131
  self.class.conf
118
132
  end
119
133
 
120
- def current_reports
121
- self.class.reports
134
+ def cached_reports
135
+ self.class.cache[:reports] ||= []
136
+ end
137
+
138
+ def recent_tail
139
+ self.class.cache[:recent_tail]
140
+ end
141
+
142
+ def cached_at
143
+ self.class.cache[:cached_at]
122
144
  end
123
145
 
124
146
  def cache(reports)
125
- self.class.reports = reports
147
+ cached_reports.unshift(reports).flatten!
148
+ self.class.cache[:recent_tail] = reports.size
149
+ self.class.cache[:cached_at] = Time.now
150
+ end
151
+
152
+ def parse_since_options(options)
153
+ case
154
+ when options[:latest]
155
+ cached_at
156
+ when options[:days]
157
+ Time.now - options[:days] * 24 * 60 * 60
158
+ when options[:hours]
159
+ Time.now - options[:hours] * 60 * 60
160
+ else
161
+ nil
162
+ end
126
163
  end
127
164
 
128
165
  def open_numbered_link(request_num)
129
- url = current_reports[request_num - 1].url
166
+ url = cached_reports[request_num - 1].url
130
167
  raise ReportExistenceError, "report existence error: please fetch reports" if url.nil?
131
168
 
132
169
  Launchy.open(url, options) do |exception|
@@ -15,25 +15,42 @@ class Nicorepo
15
15
  @reports = []
16
16
  end
17
17
 
18
- def fetch(request_num, limit_page)
19
- @reports = fetch_recursively(request_num, limit_page)
20
- end
21
-
22
- def fetch_and_select_with(filter, request_num, limit_page)
18
+ def fetch(request_num, limit_page, since: nil)
19
+ filter = {}
20
+ filter[:kind] = selected_kind
21
+ filter[:since] =
22
+ case since
23
+ when String
24
+ Time.parse since
25
+ when Time
26
+ since
27
+ else
28
+ nil
29
+ end
23
30
  @reports = fetch_recursively(request_num, limit_page, filter)
24
31
  end
25
32
 
26
33
  private
27
34
 
28
- def fetch_recursively(request_num, limit_page, filter = nil, url = TOP_URL)
35
+ def selected_kind
36
+ nil
37
+ end
38
+
39
+ def fetch_recursively(request_num, limit_page, filter = {}, url = TOP_URL)
29
40
  return [] unless limit_page > 0
30
41
 
31
42
  # fetch current page reports
32
43
  page = @parser.parse_page(url)
33
44
  reports = page[:reports_attrs].map { |attrs| Report.new(attrs) }
34
- reports.select!{ |report| report.kind =~ /#{filter}/ } if filter
45
+
46
+ if filter[:since]
47
+ reach_oldest_page = (reports.last.date < filter[:since])
48
+ reports.reject! { |report| report.date < filter[:since] }
49
+ end
50
+ reports.select! { |report| report.kind =~ /#{filter[:kind]}/ } if filter[:kind]
35
51
 
36
52
  return reports[0, request_num] if reports.size >= request_num
53
+ return reports if filter[:since] && reach_oldest_page
37
54
 
38
55
  # recursively fetch next reports
39
56
  next_reports = fetch_recursively(request_num - reports.size, limit_page - 1, filter, page[:next_url])
@@ -43,14 +60,18 @@ class Nicorepo
43
60
  end
44
61
 
45
62
  class VideoReports < Reports
46
- def fetch(request_num, limit_page)
47
- fetch_and_select_with('video-upload', request_num, limit_page)
63
+ private
64
+
65
+ def selected_kind
66
+ 'video-upload'
48
67
  end
49
68
  end
50
69
 
51
70
  class LiveReports < Reports
52
- def fetch(request_num, limit_page)
53
- fetch_and_select_with('live', request_num, limit_page)
71
+ private
72
+
73
+ def selected_kind
74
+ 'live'
54
75
  end
55
76
  end
56
77
  end
@@ -1,3 +1,3 @@
1
1
  class Nicorepo
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/nicorepo.rb CHANGED
@@ -15,24 +15,35 @@ class Nicorepo
15
15
  @agent.ssl_version = 'SSLv3'
16
16
  @agent.request_headers = { 'accept-language' => 'ja-JP', 'content-language' => 'ja-JP' }
17
17
  @parser = Parser.new(@agent)
18
+ @logined = false
18
19
  end
19
20
 
20
21
  def login(mail, pass)
21
22
  page = @agent.post(LOGIN_URL, mail: mail, password: pass)
22
- raise LoginError, "Failed to login" if page.header["x-niconico-authflag"] == '0'
23
+ if page.header["x-niconico-authflag"] == '0'
24
+ raise LoginError, "Failed to login"
25
+ else
26
+ @logined = true
27
+ end
23
28
  end
24
29
 
25
- def all(request_num = PER_PAGE)
30
+ def logined?
31
+ # TODO: page.header["x-niconico-auth-flag"] をチェックする?
32
+ # 現状一度ログインしたらfalseにならない
33
+ @logined
34
+ end
35
+
36
+ def all(request_num = PER_PAGE, since: nil)
26
37
  limit_page = request_num / PER_PAGE + 1
27
- Reports.new(@parser).fetch(request_num, limit_page)
38
+ Reports.new(@parser).fetch(request_num, limit_page, since: since)
28
39
  end
29
40
 
30
- def videos(request_num = 3, limit_page = 5)
31
- VideoReports.new(@parser).fetch(request_num, limit_page)
41
+ def videos(request_num = 3, limit_page = 5, since: nil)
42
+ VideoReports.new(@parser).fetch(request_num, limit_page, since: since)
32
43
  end
33
44
 
34
- def lives(request_num = 3, limit_page = 5)
35
- LiveReports.new(@parser).fetch(request_num, limit_page)
45
+ def lives(request_num = 3, limit_page = 5, since: nil)
46
+ LiveReports.new(@parser).fetch(request_num, limit_page, since: since)
36
47
  end
37
48
  end
38
49
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nicorepo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - upinetree
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-04 00:00:00.000000000 Z
11
+ date: 2014-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize