nicovideo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.txt ADDED
@@ -0,0 +1,37 @@
1
+ = Nicovideo
2
+
3
+ Nicovideo is scraping NICONICO DOUGA utility for Ruby
4
+ (NICONICO DOUGA is Japanese Video Sharing Service)
5
+
6
+ - NICONICO DOUGA : http://www.nicovideo.jp/
7
+
8
+ == Installation
9
+
10
+ $ sudo gem install nicovideo
11
+
12
+ == Usage
13
+
14
+ See sample/nv_download.rb
15
+
16
+ == Author
17
+
18
+ - emergent ( http://d.hatena.ne.jp/emergent )
19
+ - thanks for many bloggers who wrote scraping scripts
20
+
21
+ == License
22
+
23
+ - 2-clause BSD License
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/config/hoe.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'nicovideo/version'
2
+
3
+ AUTHOR = 'emergent' # can also be an array of Authors
4
+ EMAIL = "emergent22 (at) livedoor.com"
5
+ DESCRIPTION = "utils for nicovideo"
6
+ GEM_NAME = 'nicovideo' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'nicovideo' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "unknown"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+
31
+ REV = nil
32
+ # UNCOMMENT IF REQUIRED:
33
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34
+ VERS = Nicovideo::VERSION::STRING + (REV ? ".#{REV}" : "")
35
+ RDOC_OPTS = ['--quiet', '--title', 'nicovideo documentation',
36
+ "--opname", "index.html",
37
+ "--line-numbers",
38
+ "--main", "README",
39
+ "--inline-source"]
40
+
41
+ class Hoe
42
+ def extra_deps
43
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44
+ @extra_deps
45
+ end
46
+ end
47
+
48
+ # Generate all the Rake tasks
49
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
50
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
51
+ p.author = AUTHOR
52
+ p.description = DESCRIPTION
53
+ p.email = EMAIL
54
+ p.summary = DESCRIPTION
55
+ p.url = HOMEPATH
56
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
57
+ p.test_globs = ["test/**/test_*.rb"]
58
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
59
+
60
+ # == Optional
61
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\\n\\n")
62
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
63
+ p.extra_deps = [
64
+ ['mechanize', '>=0.6.0']
65
+ ]
66
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
67
+ end
68
+
69
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
70
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
71
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
@@ -0,0 +1,17 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
16
+
17
+ require 'nicovideo'
data/lib/nicovideo.rb ADDED
@@ -0,0 +1,11 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'nicovideo/mechanize-ext'
4
+ require 'nicovideo/base'
5
+ require 'nicovideo/video'
6
+ require 'nicovideo/comments'
7
+ #require 'nicovideo/tags'
8
+ #require 'nicovideo/ichiba'
9
+ #require 'nicovideo/rank'
10
+ #require 'nicovideo/feed'
11
+
@@ -0,0 +1,145 @@
1
+ require 'cgi'
2
+ require 'kconv'
3
+
4
+ module Nicovideo
5
+
6
+ class NicovideoArgError < StandardError ; end
7
+ class NicovideoLoginError < StandardError ; end
8
+ class NicovideoNotFoundError < StandardError ; end
9
+
10
+ class Base
11
+ NV_DEBUG_LEVEL = 0
12
+ TITLE_NICONICO = 'ニコニコ動画\(RC\)‐'.toutf8
13
+
14
+ def initialize mail=nil, password=nil
15
+ @mail = mail
16
+ @password = password
17
+ @logged_in = false
18
+ @agent = WWW::Mechanize.new()
19
+
20
+ # for parameters current video
21
+ @video_id = nil
22
+ @params = nil
23
+ @page = nil
24
+ self
25
+ end
26
+
27
+ def login mail=nil, password=nil
28
+ if !@logged_in
29
+ @mail = mail unless @mail
30
+ @password = password unless @password
31
+ account = {'mail' => @mail, 'password' => @password }
32
+
33
+ begin
34
+ @page = @agent.post('https://secure.nicovideo.jp/secure/login?site=niconico', account)
35
+ puts_debug @page.title
36
+ @logged_in = true
37
+ rescue
38
+ @logged_in = false
39
+ raise NicovideoLoginError
40
+ end
41
+ end
42
+ self
43
+ end
44
+
45
+ def watch(video_id)
46
+ @video_id = video_id
47
+ if block_given?
48
+ yield self
49
+ end
50
+ end
51
+
52
+ def logged_in?() @logged_in end
53
+ def comments(num=500) self.get_comments(@video_id, num) end
54
+ def flv() self.get_flv @video_id end
55
+ def title() self.get_title @video_id end
56
+ def tags() self.get_tags @video_id end
57
+
58
+ def get_tags video_id
59
+ page = get_page(video_id)
60
+ div = page.parser.search("div#video_tags")
61
+ tags = div.to_html.scan(/<a href=\"tag\/[\w\%]+?\">(.+?)<\/a>/ou).inject([]) {|arr, v|
62
+ puts_debug v[0]
63
+ arr << v[0]
64
+ }
65
+ tags
66
+ end
67
+
68
+ def get_title video_id
69
+ page = get_page(video_id)
70
+ title = page.title
71
+ puts_info title.toutf8.sub(/^#{TITLE_NICONICO}/ou, '')
72
+ title.toutf8.sub(/^#{TITLE_NICONICO}/ou, '')
73
+ end
74
+
75
+ def get_video video_id=nil
76
+ puts_info 'getting video : id = ' + video_id
77
+ self.get_flv video_id
78
+ end
79
+
80
+ def get_flv video_id=nil
81
+ puts_info 'getting flv : id = ' + video_id if video_id
82
+ begin
83
+ params = get_params video_id
84
+
85
+ video_url = CGI.unescape(params['url'])
86
+ video_flv = @agent.get_file(video_url)
87
+ Video.new(video_id, video_flv)
88
+ end
89
+ end
90
+
91
+ def get_comments video_id, num=500
92
+ puts_info 'getting comment xml : id = ' + video_id
93
+ begin
94
+ params = get_params video_id
95
+ raise unless params['ms']
96
+
97
+ thread_id = params['thread_id']
98
+ body = %!<thread res_from="-#{num}" version="20061206" thread="#{thread_id}" />!
99
+ post_url = CGI.unescape(params['ms'])
100
+ comment_xml = @agent.post_data(post_url, body).body
101
+ puts_debug comment_xml
102
+ Comments.new(video_id, comment_xml)
103
+ end
104
+ end
105
+
106
+ private
107
+ def get_params video_id=nil
108
+ self.login unless @logged_in
109
+
110
+ begin
111
+ if !((video_id == @video_id) && @params)
112
+ puts_info 'getting params : id = ' + video_id
113
+ @video_id = video_id
114
+ page = get_page video_id
115
+ content = @agent.get_file('http://www.nicovideo.jp/api/getflv?v=' + video_id)
116
+ puts_debug content
117
+ @params = content.scan(/([^&]+)=([^&]*)/).inject({}){|h, v| h[v[0]] = v[1]; h}
118
+ else
119
+ puts_info 'params are already in cache : id = ' + video_id
120
+ end
121
+ @params
122
+ rescue
123
+ raise NicovideoNotFoundError
124
+ end
125
+ end
126
+
127
+ def get_page video_id
128
+ puts_info 'getting HTML page : id = ' + video_id
129
+ @agent.get('http://www.nicovideo.jp/watch/' + video_id)
130
+ end
131
+
132
+ def puts_error str ; puts str if (NV_DEBUG_LEVEL >= 1) ; end
133
+ def puts_info str ; puts str if (NV_DEBUG_LEVEL >= 2) ; end
134
+ def puts_debug str ; puts str if (NV_DEBUG_LEVEL >= 3) ; end
135
+
136
+ end
137
+
138
+ def Nicovideo.new(mail, password)
139
+ Base.new(mail, password)
140
+ end
141
+
142
+ def Nicovideo.login(mail, password)
143
+ Base.new(mail, password).login
144
+ end
145
+ end
@@ -0,0 +1,9 @@
1
+ class Nicovideo::Comments
2
+ def initialize video_id, xml
3
+ @video_id = video_id
4
+ @xml = xml
5
+ end
6
+
7
+ def to_s() @xml.to_s end
8
+ def to_xml() @xml.to_s end
9
+ end
@@ -0,0 +1,20 @@
1
+ # file: mechanize-ext.rb
2
+ require 'mechanize'
3
+
4
+ module WWW
5
+ class Mechanize
6
+ self.class_eval {
7
+ def post_data(url, data='', enctype=nil)
8
+ cur_page = current_page || Page.new( nil, {'content-type'=>'text/html'})
9
+ request_data = data
10
+ abs_url = to_absolute_uri(url, cur_page)
11
+ request = fetch_request(abs_url, :post)
12
+ request.add_field('Content-Length', request_data.size.to_s)
13
+
14
+ page = fetch_page(abs_url, request, cur_page, [request_data])
15
+ add_to_history(page)
16
+ page
17
+ end
18
+ }
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module Nicovideo #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ require 'nicovideo/base'
2
+
3
+ module Nicovideo
4
+ class Video
5
+ def initialize video_id, data
6
+ @video_id = video_id
7
+ @data = data
8
+ end
9
+
10
+ def id() @video_id end
11
+ def flv() @data end
12
+ def video() @data end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'nicovideo'
3
+ require 'yaml'
4
+
5
+ video_ids = ARGV
6
+
7
+ # set account
8
+ account = YAML.load_file(ENV['HOME'] + '/.nicovideo/account.yml')
9
+ mail = account['mail']
10
+ password = account['password']
11
+
12
+ # you must login to Nicovideo
13
+ nv = Nicovideo.new(mail, password).login
14
+
15
+ # get videos and comments
16
+ video_ids.each {|video_id|
17
+
18
+ nv.watch(video_id) {|v|
19
+ puts 'title = ' + v.title
20
+ puts 'tags = ' + v.tags.join(' ')
21
+ puts 'getting comments xml'
22
+ File.open("#{video_id}.xml", "wb") {|f| f.write v.comments(100).to_xml }
23
+ puts 'getting flv file'
24
+ File.open("#{video_id}.flv", "wb") {|f| f.write v.flv }
25
+ }
26
+
27
+ sleep 3
28
+ }
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/nicovideo'
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestNicovideo < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: nicovideo
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2008-01-06 00:00:00 +09:00
8
+ summary: utils for nicovideo
9
+ require_paths:
10
+ - lib
11
+ email: emergent22 (at) livedoor.com
12
+ homepage: http://nicovideo.rubyforge.org
13
+ rubyforge_project: nicovideo
14
+ description: utils for nicovideo
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - emergent
31
+ files:
32
+ - README.txt
33
+ - Rakefile
34
+ - config/hoe.rb
35
+ - config/requirements.rb
36
+ - lib/nicovideo.rb
37
+ - lib/nicovideo/version.rb
38
+ - lib/nicovideo/mechanize-ext.rb
39
+ - lib/nicovideo/base.rb
40
+ - lib/nicovideo/video.rb
41
+ - lib/nicovideo/comments.rb
42
+ - test/test_helper.rb
43
+ - test/test_nicovideo.rb
44
+ - sample/nv_download.rb
45
+ test_files:
46
+ - test/test_nicovideo.rb
47
+ - test/test_helper.rb
48
+ rdoc_options:
49
+ - --main
50
+ - README.txt
51
+ extra_rdoc_files:
52
+ - README.txt
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ requirements: []
58
+
59
+ dependencies:
60
+ - !ruby/object:Gem::Dependency
61
+ name: mechanize
62
+ version_requirement:
63
+ version_requirements: !ruby/object:Gem::Version::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 0.6.0
68
+ version: