Last active
January 10, 2017 16:45
-
-
Save gouf/8d2b0956e4ed178bfcdd6c0f30da9249 to your computer and use it in GitHub Desktop.
Niconico にログインしてニコレポ マイリストの情報から動画タイトル・リンクを取得する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mechanize' | |
module MechanizeScraper | |
class NicoVideo | |
TOP_PAGE = 'http://www.nicovideo.jp/'.freeze | |
LOGIN_PAGE = 'https://account.nicovideo.jp/login'.freeze | |
MY_PAGE = 'http://www.nicovideo.jp/my/top'.freeze | |
def initialize(email_or_tel, password) | |
@mechanize = Mechanize.new | |
@page = @mechanize.get(TOP_PAGE) | |
@email_or_tel = email_or_tel | |
@password = password | |
end | |
def my_page | |
login unless logged_in? | |
@page = @page.link_with(href: '/my/top').click | |
end | |
def nico_repo_mylist | |
my_page | |
@page = @page.link_with(href: 'my/top/mylist').click | |
end | |
private | |
def login | |
page = @mechanize.get(LOGIN_PAGE) | |
filled_login_form = | |
proc do |page, id, pass| | |
form = page.form_with(id: 'login_form') | |
form.field_with(id: 'input__mailtel').value = id | |
form.field_with(id: 'input__password').value = pass | |
form | |
end | |
@page = filled_login_form.call(page, @email_or_tel, @password).submit | |
end | |
def logged_in? | |
[email protected] | |
.map { |elm| elm.href } | |
.any? { |href| href.eql?('http://www.nicovideo.jp/login') } | |
end | |
end | |
end | |
id = '[email protected]' | |
password = 'password' | |
nico = MechanizeScraper::NicoVideo.new(id, password) | |
pp nico.nico_repo_mylist | |
.links_with(href: /nicorepo_mylist_added_video/) | |
.find_all { |link| ((!link.href.empty? && !link.text.to_s.strip.empty?) && link.href.to_s.match?('watch')) } | |
.map { |link| [link.text, link.href.sub(/\?.+$/, '')] } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment