serialboard/serialboard.rb

56 lines
1.4 KiB
Ruby
Executable File

#!/usr/bin/env ruby
#
# Use pinboard to keep your place in web serials.
require 'pinboard'
# Initialize the config.
config = YAML.load_file('config.yml')
users = []
config.each do |user_config|
user = {}
user['active'] = user_config['active_tag_name'] ||= 'serial'
user['deleted'] = user_config['deleted_tag_name'] ||= 'serial_deleted'
if user_config['api_token'].nil?
puts "Couldn't find API token. Skipping user. Please see the Configuration section of Readme.md."
else
user['token'] = user_config['api_token']
users << user
end
end
users.each do |user|
# Set up metrics
metric = {}
metric[:posts] = 0
metric[:posts_deleted] = 0
# Get our data from the API
api = Pinboard.new(token: user['token'])
serial_posts = api.posts(tag: user['active'])
serial_posts.sort! { |x,y| y.time <=> x.time }
domains = []
serial_posts.each do |post|
metric[:posts] += 1
# Extract the domain from the full URL
domain = post.href.sub(/^https?:\/\/(.*?)\/.*$/, "\\1")
if !domains.include? domain
# Record that we've seen the domain now.
domains.push domain
else
# If this is a duplicate, soft-delete it.
post.tag.delete user['active']
post.tag.push user['deleted']
post.shared = false
api.add post.api_hash(true)
metric[:posts_deleted] += 1
end
end
puts "#{metric[:posts]} posts found. #{metric[:posts_deleted]} posts deleted."
end