Created
August 17, 2011 04:06
-
-
Save stassats/1150794 to your computer and use it in GitHub Desktop.
Postage tracking
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
(eval-when (:compile-toplevel :load-toplevel :execute) | |
(:asd :drakma) | |
(:asd :babel) | |
(:asd :cl-json)) | |
(defpackage post | |
(:use :cl)) | |
(in-package :post) | |
(defvar *url* "http://www.emspost.ru/tracking.aspx/TrackOne") | |
(defvar *in-terminal* #+swank nil #-swank t) | |
(defvar *config-file* (merge-pathnames ".config/postage-tracking" | |
(user-homedir-pathname))) | |
(defun retrieve (id) | |
(babel:octets-to-string | |
(drakma:http-request *url* | |
:method :post | |
:user-agent :firefox | |
:content (json:encode-json-plist-to-string | |
(list "id" id)) | |
:content-type "application/json" | |
:force-binary t))) | |
(defun retrieve-data (json) | |
(cdr (assoc :*operations | |
(cdr (assoc :d | |
(json:decode-json-from-string json)))))) | |
(defun longest-lines (list-of-lines) | |
(let ((lengths (make-list (length (car list-of-lines)) | |
:initial-element 0))) | |
(flet ((process-line (line) | |
(loop for element in line | |
for length on lengths | |
do (setf (car length) | |
(max (length (prin1-to-string element)) | |
(car length)))))) | |
(mapc #'process-line list-of-lines) | |
lengths))) | |
(defun make-table (json) | |
(loop for row in json | |
collect | |
(loop for (nil . value) in row | |
collect (typecase value | |
(null "") | |
(t value))))) | |
(defun print-table (json) | |
(let* ((table (make-table json)) | |
(longest-lines (longest-lines table))) | |
(loop for line in table | |
do | |
(loop for elt in line | |
for length in longest-lines | |
do (format t "~vA" length elt)) | |
(terpri)) | |
(finish-output))) | |
(defun print-info (id) | |
(print-table (retrieve-data (retrieve id)))) | |
(defun read-config () | |
(with-open-file (stream *config-file* :if-does-not-exist nil) | |
(loop for line = (read-line stream nil) | |
while line | |
collect line))) | |
(defun start-from-image () | |
(loop for id in (read-config) | |
do (write-line id) | |
(print-info id) | |
(terpri))) | |
#+ (or) | |
(ccl:save-application "post" :toplevel-function | |
(lambda () | |
(unwind-protect (post::start-from-image) | |
(ccl:quit))) | |
:prepend-kernel t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment