Last active
September 22, 2015 10:54
-
-
Save soramugi/52f885e08a58864b6b7e to your computer and use it in GitHub Desktop.
pocketに入ってるニコ動をmp3にしてDropboxに入れるやつ(マイリス対応もしてる
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
source 'https://rubygems.org' | |
gem 'niconico' | |
gem 'dropbox-sdk' | |
gem 'pocket-ruby' |
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
require 'dropbox_sdk' | |
require 'niconico' | |
require 'pocket-ruby' | |
require 'fileutils' | |
require 'pp' | |
user = YAML.load_file(File.expand_path(File.dirname(__FILE__)+"/user.yml")) | |
FileUtils.mkdir('flv') unless File.exists?('flv') | |
FileUtils.mkdir('mp3') unless File.exists?('mp3') | |
niconico = Niconico.new(user['mail'], user['pass']) | |
niconico.login | |
path = '/nico' | |
dropbox_client = DropboxClient.new(user['access_token']) | |
client = Pocket.client(consumer_key: user['pocket_key'], access_token: user['pocket_token']) | |
retrieve = client.retrieve(sort: 'newest', search: 'nicovideo.jp/watch') | |
retrieve['list'].each do |id,info| | |
url = info['resolved_url'] | |
next unless url =~ /nicovideo\.jp\/watch/ | |
nico_id = url.to_s.gsub(Niconico::URL[:watch],'') | |
video = niconico.video(nico_id) | |
next unless video.available? | |
flv_name_path = "flv/#{video.id}.#{video.type}" | |
mp3_name = "#{video.id}.mp3" | |
mp3_name_path = "mp3/#{mp3_name}" | |
open(flv_name_path, 'w') {|f| f.write video.get_video } | |
if video.type == 'swf' | |
next unless system("swfextract -m #{flv_name_path} -o #{mp3_name_path}") | |
else | |
# flv | |
next unless system("ffmpeg -y -i #{flv_name_path} #{mp3_name_path}") | |
end | |
dropbox_client.put_file("#{path}/#{mp3_name}", open(mp3_name_path)) | |
client.modify([{action: 'archive', item_id: id}]) | |
end | |
FileUtils.rm_rf('flv') | |
FileUtils.rm_rf('mp3') | |
entry = dropbox_client.metadata(path + '/mylist') | |
if entry['is_dir'] | |
entry['contents'].each do |c| | |
nico_id = File.basename(c['path'], '.*') | |
video = niconico.video(nico_id) | |
begin | |
video.add_to_mylist(user['mylist']) | |
rescue Niconico::NicoAPI::ApiError => e | |
raise e unless e.message.include?('EXIST') # 登録済み | |
end | |
dropbox_client.file_delete(c['path']) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment