Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 87 lines (72 sloc) 2.19 KB
#! /usr/bin/env ruby
# Copyright (c) 2009-2014 Michael Kohl
require "rubygems" # for the 1.8 users
require "logger"
require "net/http"
require "progressbar"
require_relative "../lib/pinboard_client.rb"
require_relative "../lib/utilities.rb"
opts = Utilities.get_options
pinboard_client = PinboardClient.new(opts)
# if the first arg is nil, Net::HTTP:Proxy returns a Net::HTTP object
http_client = Net::HTTP::Proxy(*opts.values_at(:proxy_host,
:proxy_port,
:proxy_user,
:proxy_pass))
logger = Logger.new(opts[:logfile])
logger.info "Unsavory started: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
puts "Retrieving URLs"
urls = pinboard_client.get_urls
unless urls
puts "\nCould not retrieve URLs!\nPlease check your login credentials."
exit 1
end
puts "\n#{opts[:user]} has #{urls.length} bookmarks."
if opts[:dry_run]
puts "You are using dry run mode. No links will be deleted!\n\n"
end
pbar = ProgressBar.new("Checking", urls.size)
moved = 0
urls.each do |url|
pbar.inc
delete = false
use_ssl = false
# Hackety fix for issue #1
uri = URI.parse(url) rescue next
if uri.scheme == "https"
use_ssl = true
end
begin
http_client.start(uri.host, uri.port, :use_ssl => use_ssl) do |http|
response = http.head(uri.path.empty? ? '/' : uri.path)
if response.is_a?(Net::HTTPRedirection)
moved += 1
logger.info "#{url} redirects to #{response['location']}"
next
end
if response.code == '404' && !opts[:dry_run]
delete = true
elsif response.code != '200'
logger.info "#{response.code}: #{url}"
end
end
rescue Errno::ENOENT => e
if opts[:dry_run]
logger.info "#{e.message}: #{url}"
else
delete = true
end
# In 1.8, TimeoutError does not inherit from StandardError
rescue StandardError, Timeout::Error => e
logger.error "#{e.message}: #{url}"
end
if delete
pinboard_client.delete(url)
logger.info "Deleted #{url}"
end
end
pbar.finish
logger.close
if moved > 0
puts "\n#{moved} URIs are redirecting to new locations, you might want to fix them."
end