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 +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +23 -12
- data/bin/nicorepo +1 -1
- data/lib/nicorepo/cli/cli.rb +103 -116
- data/lib/nicorepo/cli/config.rb +2 -2
- data/lib/nicorepo/reports.rb +9 -26
- data/lib/nicorepo/version.rb +1 -1
- data/lib/nicorepo.rb +2 -2
- data/nicorepo.gemspec +1 -0
- data/spec/cli/cli_config_spec.rb +3 -3
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8aa463841c71be718b4908e77b2450805fdfa8b9
|
4
|
+
data.tar.gz: 3801987c1e20a697ea4638693aba1f2705074d64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
46
|
+
> video -n20 -p5
|
47
47
|
|
48
48
|
Or you can also use aliases.
|
49
49
|
|
50
|
-
> v
|
50
|
+
> v -n20 -p5
|
51
51
|
|
52
|
-
|
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
|
60
|
-
|
61
|
-
all
|
62
|
-
videos
|
63
|
-
lives
|
64
|
-
|
65
|
-
|
66
|
-
|
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
data/lib/nicorepo/cli/cli.rb
CHANGED
@@ -1,151 +1,138 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'netrc'
|
1
3
|
require 'launchy'
|
2
4
|
require 'readline'
|
3
|
-
require '
|
5
|
+
require 'nicorepo/cli/config'
|
4
6
|
|
5
7
|
class Nicorepo
|
6
|
-
|
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
|
9
|
-
|
19
|
+
class Interactor < Thor
|
20
|
+
class LoginAccountError < StandardError; end
|
21
|
+
class ReportExistenceError < StandardError; end
|
10
22
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@conf = Nicorepo::Cli::Config.new
|
15
|
-
end
|
23
|
+
class << self
|
24
|
+
attr_accessor :reports
|
25
|
+
attr_reader :repo, :conf
|
16
26
|
|
17
|
-
|
18
|
-
|
19
|
-
|
27
|
+
def start(*)
|
28
|
+
@repo ||= Nicorepo.new
|
29
|
+
@conf ||= Nicorepo::Cli::Configuration.new
|
30
|
+
@reports ||= []
|
20
31
|
|
21
|
-
|
32
|
+
login unless logined?
|
22
33
|
|
23
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
59
|
+
desc "login", "re-login if your session is expired"
|
60
|
+
def login
|
61
|
+
login
|
66
62
|
end
|
67
63
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
79
|
-
|
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
|
-
|
82
|
-
|
83
|
-
|
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
|
-
|
86
|
-
|
87
|
-
|
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
|
-
|
93
|
-
|
94
|
-
|
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
|
-
|
106
|
-
end
|
110
|
+
private
|
107
111
|
|
108
|
-
|
109
|
-
|
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
|
-
|
119
|
-
|
120
|
-
|
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
|
-
|
127
|
-
|
128
|
-
|
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
|
-
|
136
|
-
|
137
|
-
|
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
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
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
|
data/lib/nicorepo/cli/config.rb
CHANGED
data/lib/nicorepo/reports.rb
CHANGED
@@ -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
|
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
|
-
|
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
|
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
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
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
|
-
|
53
|
+
fetch_and_select_with('live', request_num, limit_page)
|
71
54
|
end
|
72
55
|
end
|
73
56
|
end
|
data/lib/nicorepo/version.rb
CHANGED
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
|
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"
|
data/spec/cli/cli_config_spec.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Nicorepo::Cli::
|
4
|
-
let(:conf) { Nicorepo::Cli::
|
3
|
+
describe Nicorepo::Cli::Configuration do
|
4
|
+
let(:conf) { Nicorepo::Cli::Configuration.new }
|
5
5
|
|
6
6
|
before do
|
7
|
-
Nicorepo::Cli::
|
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.
|
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
|
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
|