nicorepo 0.0.3 → 0.0.4

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: 4d0c8ac38ef205c58bc61e5014580cf41126433f
4
- data.tar.gz: 64fe13068e72c8edae3d9feb8ddab9e3f5fcf1d1
3
+ metadata.gz: 8aa463841c71be718b4908e77b2450805fdfa8b9
4
+ data.tar.gz: 3801987c1e20a697ea4638693aba1f2705074d64
5
5
  SHA512:
6
- metadata.gz: 149c12f8221bee89fd75e8ae6ea344557df39c16a2e86c0aa4bf4c49872125a56b535907bf6e93e2dc8f58016c740b7b9e00ee65b5a30abec743aebc0ea3889e
7
- data.tar.gz: 6e76250ea97bc1a16bba04c8323e911972d93edd814f4b35942fdea4de3c1a7f2d3f5cb8997709e2e006cee08aa8879435817c899476e0c25ff3378b13b1088d
6
+ metadata.gz: b1ecf7b7d40f706f3237ea39374c19c02b32784c13557abd134583d75964c964c99ec289d177e642667d49ea4cfc063fcbc83f1417689b3e184f84263657c233
7
+ data.tar.gz: 174b50aa05f1f9a73e84acbcda0d32284daa98095a7540f11352cd37ce9c6b8577fa3efe1abba120d2c54ea74d081ba0f5d24ac4befd6e2c16fb1fa482771c85
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ ## 0.0.4 (2014-05-04)
2
+
3
+ CLI Features:
4
+
5
+ * Add thor and reconstruct CLI
6
+ * Simplify CLI by removing exec modes without interactive mode
7
+ * Now you can launch interaction cli without any exec options
8
+ * Use option keyword `-n` and `-p` to specify request-num and limit-page
9
+ * Change aliases
10
+ * Cache last fetched reports
11
+ * Add show command: it shows cached current reports
12
+
data/README.md CHANGED
@@ -43,27 +43,38 @@ machine nicovideo.jp
43
43
  You can use following commands in interactive cli to fetch nicorepos.
44
44
  For example, if you want 20 video reports by searcing over 5 pages, the command will be,
45
45
 
46
- > video 20 5
46
+ > video -n20 -p5
47
47
 
48
48
  Or you can also use aliases.
49
49
 
50
- > v 20 5
50
+ > v -n20 -p5
51
51
 
52
- And each commad has default value so it is simply used like,
52
+ `-n` and `-p` are specifing options.
53
+ You can omit it as you like then each commad uses default value.
53
54
 
54
- # it means `video 10 3`
55
55
  > v
56
+ # => `video -n10 -p3`
57
+ > v -n20
58
+ # => `video -n20 -p3`
59
+ > v -p5
60
+ # => `video -n20 -p5`
56
61
 
57
62
  **Commands**
58
63
 
59
- command | alias | params | description
60
- ---------|-------|------------------------|-------------------------------------
61
- all | a | request_num | all reports
62
- videos | v | request_num limit_page | only videos
63
- lives | l | request_num limit_page | only lives
64
- open | o | report_num | open the specified report url in the browser
65
- login | | | re-login
66
- exit | | | exit nicorepo
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]`.
77
+
67
78
 
68
79
  ### Configuration
69
80
 
data/bin/nicorepo CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  require 'nicorepo'
4
4
 
5
- Nicorepo::Cli.new.run(ARGV)
5
+ Nicorepo::Cli::Runner.run
6
6
 
@@ -1,151 +1,138 @@
1
+ require 'thor'
2
+ require 'netrc'
1
3
  require 'launchy'
2
4
  require 'readline'
3
- require 'netrc'
5
+ require 'nicorepo/cli/config'
4
6
 
5
7
  class Nicorepo
6
- class Cli
8
+ module Cli
9
+ class Runner
10
+ def self.run
11
+ loop do
12
+ args = Readline::readline("nicorepo > ", true).split
13
+ ret = Interactor.start(args)
14
+ break if ret == :exit
15
+ end
16
+ end
17
+ end
7
18
 
8
- class ReportExistenceError < StandardError; end
9
- class LoginAccountError < StandardError; end
19
+ class Interactor < Thor
20
+ class LoginAccountError < StandardError; end
21
+ class ReportExistenceError < StandardError; end
10
22
 
11
- def initialize
12
- @repo = Nicorepo.new
13
- @reports = nil
14
- @conf = Nicorepo::Cli::Config.new
15
- end
23
+ class << self
24
+ attr_accessor :reports
25
+ attr_reader :repo, :conf
16
26
 
17
- def run(argv)
18
- cmd, request_num, limit_page = parse(argv)
19
- help if cmd == 'help'
27
+ def start(*)
28
+ @repo ||= Nicorepo.new
29
+ @conf ||= Nicorepo::Cli::Configuration.new
30
+ @reports ||= []
20
31
 
21
- login
32
+ login unless logined?
22
33
 
23
- reports = exec_command(cmd, request_num, limit_page)
24
- if reports
25
- disp reports
26
- else
27
- case cmd
28
- when 'interactive' then interactive_run
29
- else help
34
+ super
30
35
  end
31
- end
32
- end
33
36
 
34
- # returns true when exit
35
- def interactive_run
36
- loop do
37
- argv = Readline::readline("nicorepo > ", true).split
38
- cmd, request_num, limit_page = parse(argv)
39
-
40
- reports = exec_command(cmd, request_num, limit_page)
41
- if reports
42
- @reports = reports
43
- disp @reports
44
- else
45
- case cmd
46
- when 'open' then open_url(@reports, request_num)
47
- when 'login' then login
48
- when 'exit' then return true
49
- else help_interactive; next
37
+ # replaces by a blank for help because Interactor dosen't require the basename
38
+ def basename
39
+ ''
40
+ end
41
+
42
+ def login
43
+ mail, pass = Netrc.read["nicovideo.jp"]
44
+ raise LoginAccountError, "machine nicovideo.jp is not defined in .netrc" if mail.nil? || pass.nil?
45
+ begin
46
+ @repo.login(mail, pass)
47
+ rescue
48
+ raise LoginAccountError, "invalid mail or pass: mail = #{mail}"
50
49
  end
50
+ @logined = true
51
51
  end
52
- end
53
- end
54
52
 
55
- # options is now just for testing
56
- def open_url(reports, request_num, options = {})
57
- url = reports[request_num - 1].url
58
- if url.nil?
59
- puts "report existence error: please fetch reports"
60
- raise ReportExistenceError
53
+ # TODO: Nicorepo側に持たせる
54
+ def logined?
55
+ @logined
56
+ end
61
57
  end
62
58
 
63
- Launchy.open(url, options) do |exception|
64
- puts "Attempted to open #{url} and failed because #{exception}"
65
- raise exception
59
+ desc "login", "re-login if your session is expired"
60
+ def login
61
+ login
66
62
  end
67
63
 
68
- return true
69
- end
70
-
71
- private
64
+ desc "all", "fetch all reports"
65
+ option :"request-num", type: :numeric, aliases: :n
66
+ def all
67
+ request_num = options[:"request-num"] || conf.request_num("all")
68
+ cache(repo.all(request_num))
69
+ show
70
+ end
72
71
 
73
- def parse(argv)
74
- cmd = translate(argv.shift || 'help')
75
- request_num = (argv.shift || @conf.request_num(cmd)).to_i
76
- limit_page = (argv.shift || @conf.limit_page(cmd)).to_i
72
+ desc "videos", "fetch only video reports"
73
+ option :"request-num", type: :numeric, aliases: :n
74
+ option :"limit-page", type: :numeric, aliases: :p
75
+ def videos
76
+ request_num = options[:"request-num"] || conf.request_num("videos")
77
+ limit_page = options[:"limit-page"] || conf.limit_page("videos")
78
+ cache(repo.videos(request_num, limit_page))
79
+ show
80
+ end
77
81
 
78
- return cmd, request_num, limit_page
79
- end
82
+ desc "lives", "fetch only live reports"
83
+ option :"request-num", type: :numeric, aliases: :n
84
+ option :"limit-page", type: :numeric, aliases: :p
85
+ def lives
86
+ request_num = options[:"request-num"] || conf.request_num("lives")
87
+ limit_page = options[:"limit-page"] || conf.limit_page("lives")
88
+ cache(repo.lives(request_num, limit_page))
89
+ show
90
+ end
80
91
 
81
- def login
82
- mail, pass = Netrc.read["nicovideo.jp"]
83
- raise LoginAccountError, "machine nicovideo.jp is not defined in .netrc" if mail.nil? || pass.nil?
92
+ desc "show", "show current reports"
93
+ 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})"
97
+ end
98
+ end
84
99
 
85
- begin
86
- @repo.login(mail, pass)
87
- rescue
88
- raise LoginAccountError, "invalid mail or pass: mail = #{mail}"
100
+ desc "open REPORT-NUMBER", "open the report url specified by number in your browser"
101
+ def open(report_number)
102
+ open_numbered_link(report_number.to_i)
89
103
  end
90
- end
91
104
 
92
- # it returns
93
- # - reports if succeed to exec exepcted command
94
- # - nil if unexpected command given
95
- def exec_command(cmd, request_num, limit_page)
96
- reports = nil
97
-
98
- case cmd
99
- when 'all' then reports = @repo.all request_num
100
- when 'videos' then reports = @repo.videos request_num, limit_page
101
- when 'lives' then reports = @repo.lives request_num, limit_page
102
- else return nil
105
+ desc "exit", "exit interactive prompt"
106
+ def exit
107
+ :exit
103
108
  end
104
109
 
105
- return reports
106
- end
110
+ private
107
111
 
108
- ALIAS = {"a" => "all", "v" => "videos", "l" => "lives",
109
- "o" => "open", "i" => "interactive"}
110
- def translate(cmd)
111
- if ALIAS.has_key?(cmd)
112
- ALIAS[cmd]
113
- else
114
- cmd
112
+ def repo
113
+ self.class.repo
115
114
  end
116
- end
117
115
 
118
- def help
119
- puts ' usage: nicorepo command [params]'
120
- puts ' command:'
121
- help_commands
122
- puts ' interactive, i - begin interactive mode'
123
- exit 1
124
- end
116
+ def conf
117
+ self.class.conf
118
+ end
125
119
 
126
- def help_interactive
127
- puts ' usage: command [params]'
128
- puts ' command:'
129
- help_commands
130
- puts ' open, o [report_num] - open url of given report number'
131
- puts ' login'
132
- puts ' exit'
133
- end
120
+ def current_reports
121
+ self.class.reports
122
+ end
134
123
 
135
- def help_commands
136
- puts <<-"EOS"
137
- all, a [request_num]
138
- videos, v [request_num] [limit_page]
139
- lives, l [request_num] [limit_page]
140
- *request_num - number of reports to fetch from nicovideo (default = 10)
141
- *limit_page - limit page to fetch reports(default = 3)
142
- EOS
143
- end
124
+ def cache(reports)
125
+ self.class.reports = reports
126
+ end
144
127
 
145
- def disp(reports)
146
- reports.each.with_index(1) do |report, i|
147
- puts "[#{i}] #{report.body} on #{report.date.to_s}"
148
- puts " '#{report.title}' (#{report.url})"
128
+ def open_numbered_link(request_num)
129
+ url = current_reports[request_num - 1].url
130
+ raise ReportExistenceError, "report existence error: please fetch reports" if url.nil?
131
+
132
+ Launchy.open(url, options) do |exception|
133
+ puts "Attempted to open #{url} and failed because #{exception}"
134
+ raise exception
135
+ end
149
136
  end
150
137
  end
151
138
  end
@@ -1,8 +1,8 @@
1
1
  require 'yaml'
2
2
 
3
3
  class Nicorepo
4
- class Cli
5
- class Config
4
+ module Cli
5
+ class Configuration
6
6
 
7
7
  class ReadError < StandardError; end
8
8
 
@@ -1,13 +1,10 @@
1
1
  require 'nicorepo/report'
2
- require 'nicorepo/parser'
3
2
  require 'forwardable'
4
3
 
5
4
  class Nicorepo
6
5
  class Reports
7
6
  extend Forwardable
8
7
 
9
- class ReportsAccessError < StandardError; end
10
-
11
8
  TOP_URL = 'http://www.nicovideo.jp/my/top'
12
9
 
13
10
  attr_reader :reports
@@ -22,7 +19,7 @@ class Nicorepo
22
19
  @reports = fetch_recursively(request_num, limit_page)
23
20
  end
24
21
 
25
- def fetch_with_filtere(filter, request_num, limit_page)
22
+ def fetch_and_select_with(filter, request_num, limit_page)
26
23
  @reports = fetch_recursively(request_num, limit_page, filter)
27
24
  end
28
25
 
@@ -31,43 +28,29 @@ class Nicorepo
31
28
  def fetch_recursively(request_num, limit_page, filter = nil, url = TOP_URL)
32
29
  return [] unless limit_page > 0
33
30
 
34
- # fetch current reports
31
+ # fetch current page reports
35
32
  page = @parser.parse_page(url)
36
- begin
37
- reports = page[:reports_attrs].map { |attrs| Report.new(attrs) }
38
- rescue
39
- raise ReportsAccessError
40
- end
33
+ reports = page[:reports_attrs].map { |attrs| Report.new(attrs) }
41
34
  reports.select!{ |report| report.kind =~ /#{filter}/ } if filter
42
35
 
43
- if reports.size > request_num then
44
- return reports[0, request_num]
45
- end
36
+ return reports[0, request_num] if reports.size >= request_num
46
37
 
47
38
  # recursively fetch next reports
48
- if reports.size < request_num then
49
- begin
50
- next_reports = fetch_recursively(request_num - reports.size, limit_page - 1, filter, page[:next_url])
51
- rescue
52
- return reports
53
- else
54
- reports += next_reports unless next_reports.nil?
55
- end
56
- end
57
-
58
- return reports
39
+ next_reports = fetch_recursively(request_num - reports.size, limit_page - 1, filter, page[:next_url])
40
+ reports += next_reports unless next_reports.nil?
41
+ reports
59
42
  end
60
43
  end
61
44
 
62
45
  class VideoReports < Reports
63
46
  def fetch(request_num, limit_page)
64
- fetch_with_filtere('video-upload', request_num, limit_page)
47
+ fetch_and_select_with('video-upload', request_num, limit_page)
65
48
  end
66
49
  end
67
50
 
68
51
  class LiveReports < Reports
69
52
  def fetch(request_num, limit_page)
70
- fetch_with_filtere('live', request_num, limit_page)
53
+ fetch_and_select_with('live', request_num, limit_page)
71
54
  end
72
55
  end
73
56
  end
@@ -1,3 +1,3 @@
1
1
  class Nicorepo
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/nicorepo.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'mechanize'
2
2
  require 'nicorepo/reports'
3
+ require 'nicorepo/parser'
3
4
 
4
5
  class Nicorepo
5
6
  class LoginError < StandardError; end
@@ -35,7 +36,6 @@ class Nicorepo
35
36
  end
36
37
  end
37
38
 
38
- require "nicorepo/version"
39
39
  require 'nicorepo/cli/cli'
40
- require 'nicorepo/cli/config'
40
+ require "nicorepo/version"
41
41
 
data/nicorepo.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency "mechanize", "~> 2.7"
23
23
  spec.add_dependency "launchy", "~> 2.4"
24
24
  spec.add_dependency "netrc", "~> 0.7"
25
+ spec.add_dependency "thor", "~> 0.19"
25
26
 
26
27
  spec.add_development_dependency "bundler", "~> 1.6"
27
28
  spec.add_development_dependency "pry"
@@ -1,10 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Nicorepo::Cli::Config do
4
- let(:conf) { Nicorepo::Cli::Config.new }
3
+ describe Nicorepo::Cli::Configuration do
4
+ let(:conf) { Nicorepo::Cli::Configuration.new }
5
5
 
6
6
  before do
7
- Nicorepo::Cli::Config.any_instance.stub(:load_config).and_return(config_values)
7
+ Nicorepo::Cli::Configuration.any_instance.stub(:load_config).and_return(config_values)
8
8
  end
9
9
 
10
10
  describe "#request_num" do
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - upinetree
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-29 00:00:00.000000000 Z
11
+ date: 2014-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.19'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.19'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -119,6 +133,7 @@ files:
119
133
  - ".gitignore"
120
134
  - ".nicorepo.yaml.sample"
121
135
  - ".rspec"
136
+ - CHANGELOG.md
122
137
  - Gemfile
123
138
  - LICENSE.txt
124
139
  - README.md