Skip to content

Instantly share code, notes, and snippets.

@benbalter
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbalter/2622ad28d07609b8d762 to your computer and use it in GitHub Desktop.
Save benbalter/2622ad28d07609b8d762 to your computer and use it in GitHub Desktop.
Ruby script to check if the status of an export license application (STELA request) has changed and output the result
Status: UNCHANGED
+-------------------+--------------------------+
|             Application XXXX                 |
+-------------------+--------------------------+
| Application Type  | Commodity Classification |
| Review Status     | Completed                |
| Registration Date | 01/01/2015               |
| Completion Date   | 01/02/2015               |
| Final Decision    |                          |
+-------------------+--------------------------+
require 'open-uri'
require 'nokogiri'
require 'terminal-table'
require 'colorator'
require 'digest/md5'
require 'dotenv'
Dotenv.load
url = "https://snapr.bis.doc.gov/stela/view-application?number=#{ENV["APPLICATION_NUMBER"]}"
# Grab the application status page
html = open(url).read
# On first load, one link gets a jsession id. Strip it to keep the MD5 hash accurate
html.gsub! /;jsessionid=[a-z0-9]+/i, ""
# Parse the doc
doc = Nokogiri::HTML(html)
# Generate an MD5 of the normalized HTML
md5 = Digest::MD5.hexdigest(doc.to_html)
begin
old_md5 = File.open(".md5").read
rescue Errno::ENOENT
old_md5 = nil
end
if md5 == old_md5
puts "Status: " + "UNCHANGED".green
else
puts "Status: " + "UPDATED".red
File.write ".md5", md5
end
# Grab the application info from the table
rows = doc.css("#appInfo tr")
fields = {}
rows.each do |row|
cells = row.css("td")
next unless cells.count == 2
key = cells[0].text.to_s.strip.to_sym
value = cells[1].text.strip.downcase.split(" ").map(&:capitalize).join(" ")
fields[key] = value
end
# Interagency referalls is a single two-column td with a UL of statuses
key = rows.last.text.lstrip.split("\n").first.to_sym
value = rows.last.css("li").map { |l| l.text.strip }.join("\n")
fields[key] = value
# Table flip
puts Terminal::Table.new :rows => fields, :title => "Application #{ENV["APPLICATION_NUMBER"]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment