Last active
December 16, 2015 04:48
-
-
Save chsh/5379263 to your computer and use it in GitHub Desktop.
Example to create Podio app item using ruby api.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# usage: bookmark.rb URL [tag1 [tag2]...] | |
# | |
require 'rubygems' | |
require 'bundler/setup' | |
require 'podio' | |
require 'nokogiri' | |
require 'uri' | |
require 'open-uri' | |
api_key = 'API_KEY' | |
api_secret = 'API_SECRET' | |
app_id = 'APP_ID' | |
app_token = 'APP_TOKEN' | |
Podio.setup api_key: api_key, api_secret: api_secret | |
r = nil | |
begin | |
r = Podio.client.authenticate_with_app app_id, app_token | |
rescue => e | |
puts e.inspect | |
end | |
exit(1) unless r | |
class Entry | |
attr_accessor :body, :title, :url | |
def crawl_url_and_fill_content | |
doc = crawl_url_doc self.url | |
self.title = doc.title | |
doc = remove_decoration_tags(doc) | |
self.body = doc.text.gsub(/\s+/, ' ').strip | |
end | |
private | |
def crawl_url_doc(url) | |
uri = URI.parse(url) | |
uri.fragment = nil | |
url = uri.to_s | |
c = open(url).read | |
Nokogiri::HTML(c) | |
end | |
def remove_decoration_tags(doc) | |
doc.css('script').remove | |
doc.css('style').remove | |
doc.xpath('//comment()').remove | |
doc | |
end | |
end | |
url = ARGV.shift | |
embed = Podio::Embed.create url | |
entry = Entry.new | |
entry.url = url | |
entry.crawl_url_and_fill_content | |
fields = { | |
"url" => [{embed: embed.id}], | |
"title" => entry.title, | |
"content" => entry.body | |
} | |
tags = ARGV | |
item = Podio::Item.create app_id, fields: fields, tags: tags | |
puts item.inspect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem 'podio' | |
gem 'nokogiri' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment