Class: PinboardTools::Tagger
- Inherits:
-
Object
- Object
- PinboardTools::Tagger
- Defined in:
- lib/pinboard_tools/pinboard_tagger.rb
Overview
Replaces keywords on select Pinboard articles via Embed.ly
Instance Attribute Summary collapse
-
#pb_pass ⇒ Object
readonly
Returns the value of attribute pb_pass.
-
#pb_user ⇒ Object
readonly
Returns the value of attribute pb_user.
-
#pinboard ⇒ Object
readonly
Returns the value of attribute pinboard.
Instance Method Summary collapse
-
#get_metadata(url) ⇒ Hash
Handles downloading new metadata for articles via Embed.ly Extract API.
-
#initialize(args) ⇒ Tagger
constructor
A new instance of Tagger.
-
#run ⇒ Object
Initiates new Pinboard session, fetches articles to be re-tagged, and applies new tags.
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.("../../../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_pass ⇒ Object (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_user ⇒ Object (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 |
#pinboard ⇒ Object (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
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, , error_code, type = "", "", Array.new, nil, nil = Embedly::API.new :key => @embedly_key # single url obj = .extract :url => url obj.each do |i| i.keywords.each do |k| .push k["name"] unless k["name"].is_number? end << "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: .take(5), title: title, excerpt: description, error_code: error_code, type: type} end |
#run ⇒ Object
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 = 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 .increment! if @verbose end puts "#{@errors} errors." end |