-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
43 lines (32 loc) · 995 Bytes
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require 'bundler'
Bundler.require(:default)
Dotenv.load
namespace :shortinator do
desc "port links from bitly"
task :migrate_from_bitly, [:bitly_key, :host] do |t, args|
require 'open-uri'
require 'json'
raise "bitly_key required" unless args.bitly_key
raise "host required" unless args.host
records = 50
page = 0
while records == 50 do
json = open("https://api-ssl.bitly.com/v3/user/link_history?access_token=#{args.bitly_key}&offset=#{records * page}") { |r| r.read }
hash = JSON.parse(json)
store = Shortinator::Store.new
records = hash['data']['link_history'].length
page = page + 1
puts "#{page} #{records}"
hash['data']['link_history'].each do |entry|
id = entry['link'].sub(args.host, '')
url = entry['long_url']
begin
store.insert(id, url)
puts "inserted #{id}"
rescue => e
puts "#{id} #{e.message}"
end
end
end
end
end