diff options
| -rw-r--r-- | brisket.hy | 60 |
1 files changed, 58 insertions, 2 deletions
@@ -23,8 +23,8 @@ (setv __version__ "0.1.0: Kansas City-style") (import [configparser [ConfigParser]]) -(import [os [getenv makedirs]]) -(import [os.path [dirname expanduser]]) +(import [os [getcwd getenv makedirs]]) +(import [os.path [dirname expanduser join]]) (import [sys [argv exit stderr]]) (import [nntplib [NNTP-SSL]]) (import [subprocess [Popen PIPE]]) @@ -35,6 +35,7 @@ (setv *config-path* (expanduser "~/.config/brisket/config.ini")) (setv *recognized-commands* ["describe-group" "help" + "fetch-posts" "list-groups" "list-posts" "version"]) @@ -108,6 +109,61 @@ usage: describe-group [group]" (.get overview "from") (.get overview "date")))))) +(defn fetch-single-article [session dest write-to-stdout group article-id] + "Fetches ARTICLE-ID in GROUP. + +Writes to stdout if WRITE-TO-STDOUT is non-nil, or to a file named after the +post id in DEST otherwise." + (.group session group) + (let [info (second (.article session article-id))] + (if write-to-stdout + (for [line info.lines] + (print (.decode line))) + (with [out (open (join dest info.message-id) "w+")] + (for [line info.lines] + (.write out (.decode line)) + (.write out "\n")))))) + +(defn fetch-group-articles [session dest write-to-stdout group] + "Fetches all articles in GROUP. + +Writes to stdout if WRITE-TO-STDOUT is non-nil, or to a file named after the +post id in DEST otherwise." + (let [res (.group session group) + first-post (nth res 2) + last-post (nth res 3)] + (for [i (range first-post (+ 1 last-post))] + (fetch-single-article session dest write-to-stdout group i)))) + +(defn fetch-all-articles [session dest write-to-stdout] + "Fetches all articles on the server. + +Writes to stdout if WRITE-TO-STDOUT is non-nil, or to a file named after the +post id in DEST otherwise." + (let [groups (second (.list session))] + (for [group groups] + (let [group group.group + dest (join dest group)] + (makedirs dest :exist-ok True) + (fetch-group-articles session dest write-to-stdout group))))) + +(defn fetch-posts [session &rest args] + "Fetch an article on the server. + +Writes to stdout if '-' appears in the list of arguments, or to an file named +after the post id otherwise. + +usage: fetch-posts (group [optional]) (article-id [optional])" + (let [write-to-stdout (in "-" args) + ;; Consume the "-" argument. + args (list (filter (fn [arg] (!= arg "-")) args)) + dest (getcwd) + group (if (> (len args) 0) (first args)) + article-id (if (> (len args) 1) (second args))] + (cond [article-id (fetch-single-article session dest write-to-stdout group article-id)] + [group (fetch-group-articles session dest write-to-stdout group)] + [True (fetch-all-articles session dest write-to-stdout)]))) + (defn make-config [] "Create a default configuration file. |