From 1d5d9d39da322e7a829dcadd8d99a72bb65d6bc5 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Tue, 10 Jan 2017 16:40:14 -0500 Subject: [PATCH] Add the ability to run serialboard on multiple accounts. --- Readme.md | 12 ++++++++ config.yml.example | 6 ++-- serialboard.rb | 74 ++++++++++++++++++++++++++-------------------- 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/Readme.md b/Readme.md index c3b60ae..47883de 100644 --- a/Readme.md +++ b/Readme.md @@ -12,6 +12,18 @@ First install ruby, then just: Now copy config.yml.example to config.yml, and edit it to use your Pinboard API key. +## Configuration + +Copy the `config.yml.example` to `config.yml` and edit it to use your API token. You can add additional tokens to process multiple accounts as well, e.g.: + +``` +- api_token: api_token_2 + active_tag_name: comics + deleted_tag_name: archived +- api_token: api_token_3 +``` + +`active_tag_name` and `deleted_tag_name` are optional, and will default to 'serial' and 'serial_deleted', respectively. ## Usage diff --git a/config.yml.example b/config.yml.example index 875fd87..c9d43f4 100644 --- a/config.yml.example +++ b/config.yml.example @@ -1,3 +1,3 @@ -api_token: replace_with_your_api_token -active_tag_name: serial -deleted_tag_name: serial_deleted +- api_token: replace_with_your_api_token + active_tag_name: serial + deleted_tag_name: serial_deleted diff --git a/serialboard.rb b/serialboard.rb index ac411ea..08990e4 100755 --- a/serialboard.rb +++ b/serialboard.rb @@ -4,42 +4,52 @@ require 'pinboard' -# Set up metrics -metric = {} -metric[:posts] = 0 -metric[:posts_deleted] = 0 - -# Initialize the config +# Initialize the config. config = YAML.load_file('config.yml') -config['active_tag_name'] ||= 'serial' -config['deleted_tag_name'] ||= 'serial_deleted' -if config['api_token'].nil? - puts "Couldn't find API token. Please see the Configuration section of Readme.md." - return -end -# Get our data from the API -api = Pinboard.new(token: config['api_token']) -serial_posts = api.posts(tag: config['active_tag_name']) -serial_posts.sort! { |x,y| y.time <=> x.time } +users = [] +config.each do |user_config| + user = {} + user['active'] = user_config['active_tag_name'] ||= 'serial' + user['deleted'] = user_config['deleted_tag_name'] ||= 'serial_deleted' -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 + if user_config['api_token'].nil? + puts "Couldn't find API token. Skipping user. Please see the Configuration section of Readme.md." else - # If this is a duplicate, soft-delete it. - post.tag.delete config['active_tag_name'] - post.tag.push config['deleted_tag_name'] - post.shared = false - api.add post.api_hash(true) - metric[:posts_deleted] += 1 + user['token'] = user_config['api_token'] + users << user end end -puts "#{metric[:posts]} posts found. #{metric[:posts_deleted]} posts deleted." +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