From 3834063020c019dc624aa37152af4596d7585b93 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Wed, 16 Jun 2010 20:37:19 +1000 Subject: [PATCH] Script to add short url to article meta data --- script/bitly | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 script/bitly diff --git a/script/bitly b/script/bitly new file mode 100755 index 0000000..77faa93 --- /dev/null +++ b/script/bitly @@ -0,0 +1,35 @@ +#!/usr/bin/env ruby + +require 'pathname' +$:.unshift (Pathname(__FILE__).parent.parent + 'lib').to_s + +require 'nanoc3' +require 'articles' +require 'bitly' + +include Nanoc3::Helpers::Blogging + +site = Nanoc3::Site.new(Pathname(__FILE__).parent.parent.to_s) + +Bitly.use_api_version_3 +bitly = Bitly.new('wezm', 'R_f2bfdace56c886671086eb0c8acb9ce7') + +site.load_data +site.items.select { |item| item[:kind] == 'article' }.each do |article| + meta = YAML.load_file(article[:meta_filename]) || {} + url = site.config[:base_url] + article.path + unless meta['short_url'] + begin + u = bitly.shorten(url) + meta['short_url'] = u.short_url + rescue BitlyError + puts "BitlyError" + end + end + + # Write the meta data back out + File.open(article[:meta_filename], 'w') do |file| + YAML.dump(meta, file) + end + puts "#{url} => #{meta['short_url']}" +end