Permalink
Browse files

initial commit

  • Loading branch information...
bhoggard committed Jul 1, 2011
0 parents commit 8e990103631f7ae7e1e2972dd35f548c01fae567
Showing with 91 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +31 −0 README
  3. +58 −0 parse-storify
@@ -0,0 +1,2 @@
+.rvmrc
+
31 README
@@ -0,0 +1,31 @@
+== About this code
+
+Script for parsing JSON feed for a storify.com story and writing out a new JSON file suitable for use in a widget. Content is "twitterized" so that links for hashtags, users, and URLs work as expected. This runs as a cron, and occasionally there are problems reaching Storify. Unless the verbose flag is set, it will silently stop if it fails to retrieve the JSON URL content.
+
+== Prerequisities
+
+Ruby - I'm using 1.9.2
+
+Gems:
+
+crack
+htmlentities
+json
+trollop
+
+== Usage
+
+./parse-storify -h
+
+./parse-storify -u <storify story JSON url> -o <path to output file>
+
+
+== MIT License
+
+Copyright (c) 2011 Barry Hoggard, barry@tristanmedia.com
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
+#!/usr/bin/env ruby
+
+require 'rubygems'
+require 'crack'
+require 'htmlentities'
+require 'json'
+require 'net/http'
+require 'trollop'
+
+# parse options
+options = Trollop::options do
+ opt :url, "URL of Storify JSON feed", :type => :string
+ opt :output, "Path to output file", :type => :string
+ opt :verbose, "Whether to be verbose"
+end
+
+Trollop::die :url, "is required" unless options[:url]
+Trollop::die :output, "is required" unless options[:output]
+
+def twitterize(s)
+ $coder ||= HTMLEntities.new
+ # hyperlinks
+ s.gsub! /((https?:\/\/|www\.)([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/, %Q{<a target="_blank" href="\\1">\\1</a>}
+ # tweeter
+ s.gsub! /^([_\w]+)?: /, %Q{<a target="_blank" href="http://twitter.com/\\1">\\1</a>: }
+ # usernames
+ s.gsub! /@(\w+)/, %Q{@<a target="_blank" href="http://twitter.com/\\1">\\1</a>}
+ # hashtag
+ s.gsub! /#(\w+)/, %Q{<a target="_blank" href="http://search.twitter.com/search?q=%23\\1">#\\1</a>}
+ $coder.decode(s);
+end
+
+resp = Net::HTTP.get_response(URI.parse(options[:url]))
+if resp.instance_of?(Net::HTTPOK)
+ data = resp.body
+ feed = Crack::JSON.parse(data)
+else
+ puts "Failed to fetch URL: " + resp if options[:verbose]
+ exit
+end
+
+output = []
+feed['elements'].each do |key, e|
+ next if key.to_i > 20
+ author = %Q{<a target="_blank" href="#{e['author']['href']}">#{e['author']['username']}</a>:}
+ content = twitterize(e['description'])
+ output[key.to_i] = { 'content' => author + " " + content }
+end
+
+begin
+ File.open(options[:output], 'w') do |f|
+ f.puts(output.to_json)
+ end
+rescue Exception => e
+ puts "Failed to write output file: #{e.message}"
+end
+
+

0 comments on commit 8e99010

Please sign in to comment.