niconico 1.0.0
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.
- data/.gitignore +4 -0
- data/Gemfile +6 -0
- data/README.mkd +23 -0
- data/Rakefile +1 -0
- data/lib/niconico.rb +43 -0
- data/lib/niconico/ranking.rb +29 -0
- data/lib/niconico/version.rb +3 -0
- data/lib/niconico/video.rb +74 -0
- data/niconico.gemspec +20 -0
- metadata +54 -0
    
        data/.gitignore
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/README.mkd
    ADDED
    
    | @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            # Niconico - yet another nicovideo.gem
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            ## Description
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Wrapper of `Mechanize`, optimized for <http://www.nicovideo.jp/>.
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            ## Feature
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            * Login to nicovideo
         | 
| 10 | 
            +
            * Retrive a ranking page
         | 
| 11 | 
            +
            * Download a video
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            ## Requirements
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            * Ruby 1.9+ (1.9.2+ is supported)
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            ## Install
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                $ gem install niconico
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            ## Author
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            * Shota Fukumori (sora\_h)
         | 
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            require 'bundler/gem_tasks'
         | 
    
        data/lib/niconico.rb
    ADDED
    
    | @@ -0,0 +1,43 @@ | |
| 1 | 
            +
            # -*- coding: utf-8 -*-
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'rubygems'
         | 
| 4 | 
            +
            require 'mechanize'
         | 
| 5 | 
            +
            require 'cgi'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            class Niconico
         | 
| 8 | 
            +
              URL = {
         | 
| 9 | 
            +
                login: 'https://secure.nicovideo.jp/secure/login?site=niconico',
         | 
| 10 | 
            +
                watch: 'http://www.nicovideo.jp/watch/',
         | 
| 11 | 
            +
                getflv: 'http://www.nicovideo.jp/api/getflv'
         | 
| 12 | 
            +
              }
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              TEST_VIDEO_ID = "sm9"
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              attr_reader :agent, :logined
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              def initialize(mail, pass)
         | 
| 19 | 
            +
                @mail = mail
         | 
| 20 | 
            +
                @pass = pass
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                @logined = false
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                @agent = Mechanize.new
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              def login(force=false)
         | 
| 28 | 
            +
                return false if !force && @logined
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                page = @agent.post(URL[:login], 'mail' => @mail, 'password' => @pass)
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                raise LoginError, "Failed to login (x-niconico-authflag is 0)" if page.header["x-niconico-authflag"] == '0'
         | 
| 33 | 
            +
                @logined = true
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
              def inspect
         | 
| 37 | 
            +
                "#<Niconico: #{@mail} (#{@logined ? "" : "not "}logined)>"
         | 
| 38 | 
            +
              end
         | 
| 39 | 
            +
              class LoginError < StandardError; end
         | 
| 40 | 
            +
            end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            require_relative './niconico/video'
         | 
| 43 | 
            +
            require_relative './niconico/ranking'
         | 
| @@ -0,0 +1,29 @@ | |
| 1 | 
            +
            # -*- coding: utf-8 -*-
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class Niconico
         | 
| 4 | 
            +
              # options[:span]    -> :hourly, :daily, :weekly, :monthly, :total
         | 
| 5 | 
            +
              #                or -> :hour, :day, :week, :month, :all
         | 
| 6 | 
            +
              #   default: daily
         | 
| 7 | 
            +
              #
         | 
| 8 | 
            +
              # options[:method] -> :fav,     :view, :comment, :mylist
         | 
| 9 | 
            +
              #                     (or :all)        (or :res)
         | 
| 10 | 
            +
              def ranking(category, options={})
         | 
| 11 | 
            +
                login unless @logined
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                span = options[:span] || :daily
         | 
| 14 | 
            +
                span = :hourly  if span == :hour
         | 
| 15 | 
            +
                span = :daily   if span == :day
         | 
| 16 | 
            +
                span = :weekly  if span == :week
         | 
| 17 | 
            +
                span = :monthly if span == :month
         | 
| 18 | 
            +
                span = :total   if span == :all
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                method = options[:method] || :fav
         | 
| 21 | 
            +
                method = :res if method == :comment
         | 
| 22 | 
            +
                method = :fav if method == :all
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                page = @agent.get(url = "http://www.nicovideo.jp/ranking/#{method}/#{span}/#{category}")
         | 
| 25 | 
            +
                page.search("a.watch").map do |link|
         | 
| 26 | 
            +
                  Video.new(self, link['href'].sub(/^.*?watch\//,""), title: link.inner_text)
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
            end
         | 
| @@ -0,0 +1,74 @@ | |
| 1 | 
            +
            # -*- coding: utf-8 -*-
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class Niconico
         | 
| 4 | 
            +
              def video(video_id)
         | 
| 5 | 
            +
                login unless @logined
         | 
| 6 | 
            +
                Video.new(self, video_id)
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              class Video
         | 
| 10 | 
            +
                DEFFERABLES = [:id, :title, :url, :video_url, :type]
         | 
| 11 | 
            +
                DEFFERABLES_VAR = DEFFERABLES.map{|k| :"@#{k}" }
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                DEFFERABLES.zip(DEFFERABLES_VAR).each do |(k,i)|
         | 
| 14 | 
            +
                  define_method(k) do
         | 
| 15 | 
            +
                    instance_variable_get(i) || (get && instance_variable_get(i))
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                def initialize(parent, video_id, defer=nil)
         | 
| 20 | 
            +
                  @parent = parent
         | 
| 21 | 
            +
                  @agent = parent.agent
         | 
| 22 | 
            +
                  @id = video_id
         | 
| 23 | 
            +
                  @url = "#{Niconico::URL[:watch]}#{@id}"
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                  if defer
         | 
| 26 | 
            +
                    defer.each do |k,v|
         | 
| 27 | 
            +
                      next unless DEFFERABLES.include?(k)
         | 
| 28 | 
            +
                      instance_variable_set :"@#{k}", v
         | 
| 29 | 
            +
                    end
         | 
| 30 | 
            +
                    @page = nil
         | 
| 31 | 
            +
                  else
         | 
| 32 | 
            +
                    @page = get()
         | 
| 33 | 
            +
                  end
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                def economy?; @eco; end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                def get
         | 
| 39 | 
            +
                  begin
         | 
| 40 | 
            +
                    @page = @agent.get(@url)
         | 
| 41 | 
            +
                  rescue Mechanize::ResponseCodeError => e
         | 
| 42 | 
            +
                    raise NotFound, "#{@id} not found" if e.message == "404 => Net::HTTPNotFound"
         | 
| 43 | 
            +
                    raise e
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
                  getflv = Hash[@agent.get_file("#{Niconico::URL[:getflv]}?v=#{@id}").scan(/([^&]+)=([^&]+)/).map{|(k,v)| [k.to_sym,CGI.unescape(v)] }]
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                  @title = @page.at("#video_title").inner_text
         | 
| 48 | 
            +
                  @video_url = getflv[:url]
         | 
| 49 | 
            +
                  @eco = !(/low$/ =~ @video_url).nil?
         | 
| 50 | 
            +
                  @type = case @video_url.match(/^http:\/\/(.+\.)?nicovideo\.jp\/smile\?(.+?)=.*$/).to_a[2]
         | 
| 51 | 
            +
                          when 'm'; :mp4
         | 
| 52 | 
            +
                          when 's'; :swf
         | 
| 53 | 
            +
                          else;     :flv
         | 
| 54 | 
            +
                          end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                  @page
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                def get_video
         | 
| 60 | 
            +
                  @agent.get_file(@video_url)
         | 
| 61 | 
            +
                end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                def get_video_by_other
         | 
| 64 | 
            +
                  {cookie: @agent.cookie_jar.cookies(URI.parse(@video_url)).join(";"),
         | 
| 65 | 
            +
                   url: @video_url}
         | 
| 66 | 
            +
                end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                def inspect
         | 
| 69 | 
            +
                  "#<Niconico::Video: #{@id}.#{@type} \"#{@title}\"#{@eco ? " low":""}>"
         | 
| 70 | 
            +
                end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                class NotFound < StandardError; end
         | 
| 73 | 
            +
              end
         | 
| 74 | 
            +
            end
         | 
    
        data/niconico.gemspec
    ADDED
    
    | @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 2 | 
            +
            $:.push File.expand_path("../lib", __FILE__)
         | 
| 3 | 
            +
            require "niconico/version"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Gem::Specification.new do |s|
         | 
| 6 | 
            +
              s.name        = "niconico"
         | 
| 7 | 
            +
              s.version     = Niconico::VERSION
         | 
| 8 | 
            +
              s.authors     = ["Shota Fukumori (sora_h)"]
         | 
| 9 | 
            +
              s.email       = ["sorah@tubusu.net"]
         | 
| 10 | 
            +
              s.homepage    = ""
         | 
| 11 | 
            +
              s.summary     = "wrapper of Mechanize, optimized for nicovideo."
         | 
| 12 | 
            +
              s.description = "wrapper of Mechanize, optimized for nicovideo. :)"
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              s.rubyforge_project = "niconico"
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              s.files         = `git ls-files`.split("\n")
         | 
| 17 | 
            +
              s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
         | 
| 18 | 
            +
              s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
         | 
| 19 | 
            +
              s.require_paths = ["lib"]
         | 
| 20 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,54 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: niconico
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 1.0.0
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 | 
            +
            platform: ruby
         | 
| 7 | 
            +
            authors:
         | 
| 8 | 
            +
            - Shota Fukumori (sora_h)
         | 
| 9 | 
            +
            autorequire: 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
            date: 2011-10-12 00:00:00.000000000 Z
         | 
| 13 | 
            +
            dependencies: []
         | 
| 14 | 
            +
            description: wrapper of Mechanize, optimized for nicovideo. :)
         | 
| 15 | 
            +
            email:
         | 
| 16 | 
            +
            - sorah@tubusu.net
         | 
| 17 | 
            +
            executables: []
         | 
| 18 | 
            +
            extensions: []
         | 
| 19 | 
            +
            extra_rdoc_files: []
         | 
| 20 | 
            +
            files:
         | 
| 21 | 
            +
            - .gitignore
         | 
| 22 | 
            +
            - Gemfile
         | 
| 23 | 
            +
            - README.mkd
         | 
| 24 | 
            +
            - Rakefile
         | 
| 25 | 
            +
            - lib/niconico.rb
         | 
| 26 | 
            +
            - lib/niconico/ranking.rb
         | 
| 27 | 
            +
            - lib/niconico/version.rb
         | 
| 28 | 
            +
            - lib/niconico/video.rb
         | 
| 29 | 
            +
            - niconico.gemspec
         | 
| 30 | 
            +
            homepage: ''
         | 
| 31 | 
            +
            licenses: []
         | 
| 32 | 
            +
            post_install_message: 
         | 
| 33 | 
            +
            rdoc_options: []
         | 
| 34 | 
            +
            require_paths:
         | 
| 35 | 
            +
            - lib
         | 
| 36 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
              none: false
         | 
| 38 | 
            +
              requirements:
         | 
| 39 | 
            +
              - - ! '>='
         | 
| 40 | 
            +
                - !ruby/object:Gem::Version
         | 
| 41 | 
            +
                  version: '0'
         | 
| 42 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 43 | 
            +
              none: false
         | 
| 44 | 
            +
              requirements:
         | 
| 45 | 
            +
              - - ! '>='
         | 
| 46 | 
            +
                - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                  version: '0'
         | 
| 48 | 
            +
            requirements: []
         | 
| 49 | 
            +
            rubyforge_project: niconico
         | 
| 50 | 
            +
            rubygems_version: 1.8.11
         | 
| 51 | 
            +
            signing_key: 
         | 
| 52 | 
            +
            specification_version: 3
         | 
| 53 | 
            +
            summary: wrapper of Mechanize, optimized for nicovideo.
         | 
| 54 | 
            +
            test_files: []
         |