Created
August 5, 2012 22:01
-
-
Save raa0121/3267266 to your computer and use it in GitHub Desktop.
ニコニコ動画の検索APIで特定のタグの投稿の新しい順に検索し、情報を拾ってくるスクリプト その2
This file contains 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
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
if RUBY_VERSION < '1.9' | |
require 'rubygems' | |
end | |
require 'json' | |
require 'mechanize' | |
require 'cgi' | |
require 'rexml/document' | |
class NicoVideo | |
def getVideoList | |
nico_id = '' | |
nico_pass = '' | |
secure_uri = 'https://secure.nicovideo.jp/secure/login?site=niconico' | |
agent = Mechanize.new | |
agent.user_agent = 'NicoLiveAlertTwitterBot 0.2' | |
agent.post(secure_uri,'mail' => nico_id ,'password' => nico_pass) | |
cookie = agent.cookie_jar | |
search_uri = 'http://ext.nicovideo.jp/api/search/tag/' | |
search_word = CGI.escape('アイドルマスター') | |
51200.times do |i| | |
i += 1 | |
html = agent.get("#{search_uri}#{search_word}?mode=watch&order=d&page=#{i}&sort=f", {'Cookie' => cookie} ) | |
str = html.body | |
str.gsub!(/\\u([0-9a-f]{4})/) { | |
[$1.hex].pack("U") | |
} | |
json = JSON.parser.new(str) | |
hash = json.parse() | |
lists = hash['list'] | |
next unless lists | |
File::open("Nicovideo_List.csv","w"){|f| | |
lists.map do |list| | |
thumb = agent.get("http://ext.nicovideo.jp/api/getthumbinfo/#{list['id']}") | |
next unless thumb | |
source = thumb.body | |
next unless source | |
info = REXML::Document.new source | |
next unless info | |
f.puts "#{list['id']},#{list['title']},#{info.elements['nicovideo_thumb_response/thumb/user_id'].text},#{list['view_counter']},#{list['num_res']},#{list['mylist_counter']}" | |
puts info.elements['nicovideo_thumb_response/thumb/title'].text | |
break if list['id'] == "sm15177209" | |
sleep 0.1 | |
end | |
} | |
end | |
end | |
end | |
nicovideo = NicoVideo.new | |
nicovideo.getVideoList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment