Status: UNCHANGED
+-------------------+--------------------------+
| Application XXXX |
+-------------------+--------------------------+
| Application Type | Commodity Classification |
| Review Status | Completed |
| Registration Date | 01/01/2015 |
| Completion Date | 01/02/2015 |
| Final Decision | |
+-------------------+--------------------------+
Last active
August 29, 2015 14:16
-
-
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
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
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