Skip to content

Instantly share code, notes, and snippets.

@raa0121
Created July 25, 2012 22:00
Show Gist options
  • Save raa0121/3178970 to your computer and use it in GitHub Desktop.
Save raa0121/3178970 to your computer and use it in GitHub Desktop.
ニコニコ動画の検索APIで特定のタグの投稿の新しい順に検索し、情報を拾ってくるスクリプト
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
if RUBY_VERSION < '1.9'
require 'rubygems'
end
require 'json'
require 'mechanize'
require 'cgi'
class NicoVideo
def getVideoList
nico_id = 'Enter Your ID'
nico_pass = 'Enter Your Password'
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
puts search_uri = 'http://ext.nicovideo.jp/api/search/tag/'
puts 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|
f.puts "#{list['id']},#{list['view_counter']},#{list['num_res']},#{list['mylist_counter']}"
puts list['title']
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