Last active
February 13, 2023 12:55
-
-
Save marsz/5231235 to your computer and use it in GitHub Desktop.
convert and map bit.ly's "links" + "bundle" to kippt's "clips" + "list"
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
# encoding: utf-8 | |
require 'active_support/core_ext/object' | |
require "net/https" | |
require "uri" | |
require "kippt" | |
require "hashie" | |
# install kippt api client gems: | |
# | |
# gem install kippt | |
# | |
# see more about kippt gem: | |
# | |
# https://github.com/vesan/kippt | |
# | |
# install requied gems: | |
# | |
# gem install hashie | |
# gem install activesupport ( if you have not installed rails gem) | |
# | |
class BitlyToKippt | |
attr_reader :bitly, :kippt | |
# options = { | |
# :bitly => { :user => "xxx", :access_token => "123456789" }, | |
# :kippt => { :username => "xxx", :password => "123456789" } | |
# } | |
def initialize(options) | |
@bitly = options[:bitly] | |
@kippt = options[:kippt] | |
end | |
def bundles | |
data = request_bitly("bundle/bundles_by_user", :user => bitly[:user]) | |
data["data"]["bundles"].map{ |v| Hashie::Mash.new(v) } | |
end | |
def find_bundle(bundle_link) | |
data = request_bitly("bundle/contents", :bundle_link => bundle_link) | |
Hashie::Mash.new(data["data"]["bundle"]) | |
end | |
def find_list(name) | |
@lists = {} | |
@lists[name] = kippt_client.lists.all(:limit => 300).select{ |l| l.title == name }.first unless @lists[name] | |
@lists[name] | |
end | |
def create_list(list_title) | |
unless find_list(list_title) | |
list = kippt_client.lists.build | |
list.title = list_title | |
list.save | |
end | |
end | |
def create_clip(url, list_title) | |
clip = kippt_client.clips.build | |
clip.url = url | |
if list = find_list(list_title) | |
clip.list = list.resource_uri | |
end | |
clip.save | |
end | |
def convert_bundles_to_lists | |
bundles.each do |bundle| | |
puts "creating bundle: #{bundle.title}" | |
create_list(bundle.title) | |
end | |
end | |
def convert_links_in_bundles | |
bundles.each do |b| | |
convert_bundle_links(b.bundle_link) | |
end | |
end | |
def convert_bundle_links(bundle_link) | |
bundle = find_bundle(bundle_link) | |
bundle.links.each do |link| | |
puts "creating link: #{link.long_url}" | |
create_clip(link.long_url, bundle.title) | |
end | |
end | |
private | |
def request_bitly(path, querys = {}) | |
querys[:access_token] = bitly[:access_token] | |
url = "https://api-ssl.bit.ly/v3/#{path}?#{querys.to_query}" | |
uri = URI.parse(url) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
request = Net::HTTP::Get.new(uri.request_uri) | |
response = http.request(request) | |
JSON.parse(response.body) | |
end | |
def kippt_client | |
Kippt::Client.new(:username => kippt[:username], :password => kippt[:password]) | |
end | |
end | |
options = { | |
:bitly => { | |
# bitly access token can be generated by: | |
# | |
# curl -u "user:password" -X POST "https://api-ssl.bitly.com/oauth/access_token" | |
# | |
:access_token => "bitly token", | |
:user => "bitly user" | |
}, | |
:kippt => { | |
:username => "kippt username", | |
:password => "kippt password" | |
} | |
} | |
x = BitlyToKippt.new(options) | |
# x.convert_bundles_to_lists | |
# x.convert_links_in_bundles | |
# x.convert_bundle("http://bitly.com/bundles/marsz330/1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment