Last active
October 2, 2015 00:03
-
-
Save soramugi/10797920 to your computer and use it in GitHub Desktop.
pocketからurl選り分けてニコニコ動画の動画リンクをpocketに追加しなおす
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 'pocket-ruby' | |
gem 'nokogiri' |
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 'open-uri' | |
require 'net/http' | |
require 'uri' | |
require 'bundler' | |
Bundler.require | |
class Nico | |
def client | |
Pocket.client( | |
consumer_key: ENV['CONSUMER_KEY'], | |
access_token: ENV['ACCESS_TOKEN'], | |
) | |
end | |
def retrieve | |
client.retrieve(detailType: :simple) | |
end | |
def convert | |
retrieve['list'].each do |id,item| | |
url = expand_url(item['given_url']) | |
if url.include?('http://cobachica.hateblo.jp') || url.include?('http://smileaddiction.net') | |
doc = Nokogiri::HTML URI.parse(url).open | |
doc.css('iframe').each do |iframe| | |
iframe.attributes.each do |name,attr| | |
next unless name == 'src' | |
next unless (url = attr.value).include?('nicovideo.jp/thumb') | |
item = add(url) | |
next if item.nil? | |
archive id | |
p item | |
end | |
end | |
end | |
end | |
end | |
def add var | |
matchs = var.match(/(sm[0-9]+)|(nm[0-9]+)/) | |
nico_id = matchs.captures.compact.first unless matchs.nil? | |
client.add(url: "http://www.nicovideo.jp/watch/#{nico_id}") if nico_id | |
end | |
def archive id | |
client.modify [ { action: 'archive', item_id: id, } ] | |
end | |
def expand_url(url) | |
uri = url.kind_of?(URI) ? url : URI.parse(url) | |
Net::HTTP.start(uri.host, uri.port) do |io| | |
r = io.head(uri.path) | |
r['Location'] || uri.to_s | |
end | |
rescue | |
url | |
end | |
end | |
Nico.new.convert |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment