Class: PinboardTools::Tagger

Inherits:
Object
  • Object
show all
Defined in:
lib/pinboard_tools/pinboard_tagger.rb

Overview

Replaces keywords on select Pinboard articles via Embed.ly

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Tagger

Returns a new instance of Tagger.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pinboard_tools/pinboard_tagger.rb', line 24

def initialize(args)
  config_path = File.expand_path("../../../config/pinboard.yml", __FILE__)
  @config = YAML.load_file(config_path)
  begin
    @embedly_key = @config[:embedly_key].to_s
    @pb_user = @config[:pinboard_user]
    @pb_pass = @config[:pinboard_pass]
  rescue
    puts "Credentials not found. Please run `pinboardtools init`"
    exit
  end
  @errors = 0
  # @_tag = Array.new
  @_tag = args[:tag]
  @verbose = args[:verbose]
  @pinboard = {}
end

Instance Attribute Details

#pb_passObject (readonly)

Returns the value of attribute pb_pass.



22
23
24
# File 'lib/pinboard_tools/pinboard_tagger.rb', line 22

def pb_pass
  @pb_pass
end

#pb_userObject (readonly)

Returns the value of attribute pb_user.



22
23
24
# File 'lib/pinboard_tools/pinboard_tagger.rb', line 22

def pb_user
  @pb_user
end

#pinboardObject (readonly)

Returns the value of attribute pinboard.



22
23
24
# File 'lib/pinboard_tools/pinboard_tagger.rb', line 22

def pinboard
  @pinboard
end

Instance Method Details

#get_metadata(url) ⇒ Hash

Handles downloading new metadata for articles via Embed.ly Extract API

Parameters:

  • url (String)

    URL of article to fetch keywords for

Returns:

  • (Hash)

    A collection of important metadata tags for article: keywords [Array] 5 most relevant keywords for article title [String] the most likely title for the article excert [String] an excerpt or description for the article



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pinboard_tools/pinboard_tagger.rb', line 50

def (url)
  title, description, tags, error_code, type = "", "", Array.new, nil, nil
  embedly_api = Embedly::API.new :key => @embedly_key
  # single url
  obj = embedly_api.extract :url => url
  obj.each do |i|
    i.keywords.each do |k|
      tags.push k["name"] unless k["name"].is_number?
    end
    tags << "unread"
    title = i.title
    desription = i.description
    error_code = ""
    error_code = i.respond_to?(:error_code) ? i.error_code : 200
    # p i
    type = i.type
  end
  return {keywords: tags.take(5), title: title, excerpt: description, error_code: error_code, type: type}
end

#runObject

Initiates new Pinboard session, fetches articles to be re-tagged, and applies new tags



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pinboard_tools/pinboard_tagger.rb', line 72

def run
  pinboard = Pinboard::Client.new(:username => @pb_user, password: @pb_pass)
  posts = []
  # unless @_tag.empty? && @_tag.is_a Array
  if @_tag.is_a? Array
    @_tag.each do |t|
      pinboard.posts(tag: t).each {|p| posts << p}
    end
  else
    begin
      posts = pinboard.posts(:tag => @_tag)
    rescue Exception => e
      posts = pinboard.posts
    end
  end
  # else
  # posts = pinboard.posts(:results => 10)
  # end
  bar = ProgressBar.new(posts.size)
  posts.each do |post|

    tag_list = Array.new
     = (post.href)
    params = {
      url: post.href,
      description: [:title],
      tags: [:keywords],
      replace: true,
      :public => true,
      :toread => true,
      :type => [:type]
    }
    if [:error_code] == 404
      pinboard.delete(post.href)
    elsif params[:type] != "html"
      pinboard.add(url: post.href, tags: [params[:type]], description: post.href)
    else
      pinboard.add(params) rescue @errors += 1
    end
    bar.increment! if @verbose
  end
  puts "#{@errors} errors."
end