Skip to content

Instantly share code, notes, and snippets.

@t-sin
Created March 26, 2020 02:09
Show Gist options
  • Save t-sin/1537bfd12d76c0de908e342bd76a036e to your computer and use it in GitHub Desktop.
Save t-sin/1537bfd12d76c0de908e342bd76a036e to your computer and use it in GitHub Desktop.
#!/bin/sh
#|-*- mode:lisp -*-|#
#|
exec ros -Q -- $0 "$@"
|#
(progn ;;init forms
(ros:ensure-asdf)
#+quicklisp(ql:quickload '(:jonathan :dexador) :silent t)
)
(defpackage :ros.script.get-all-qiita-items.3794175262
(:use :cl))
(in-package :ros.script.get-all-qiita-items.3794175262)
(defparameter *user* "t-sin")
(defparameter *target-directory* (truename "."))
(defun list-all-items (user)
(let ((url (format nil "https://qiita.com/api/v2/users/~a/items" user)))
(jojo:parse (dex:get url))))
(defun get-id-and-title (item)
(list :id (getf item :|id|)
:title (getf item :|title|)))
(defun get-item-as-markdown (user item-id)
(let ((url (format nil "https://qiita.com/~a/items/~a.md" user item-id)))
(dex:get url)))
(defun save-item (md title)
(let ((path (merge-pathnames (format nil "~a - ~a.md" *user* title) *target-directory*)))
(format t "save as '~a'.~%" path)
(with-open-file (out path
:direction :output
:external-format :utf-8
:if-exists :supersede)
(write-line md out))))
(defun main (&rest argv)
(declare (ignorable argv))
(let ((items (mapcar (lambda (item)
(append (get-id-and-title item)
(list :md (get-item-as-markdown *user* (getf item :|id|)))))
(list-all-items *user*))))
(format t "; target articles~%")
(mapc (lambda (item) (format t "; - ~a~%" (getf item :title))) items)
(when (yes-or-no-p "save those into ~s? " *target-directory*)
(loop
:for item :in items
:do (save-item (getf item :md) (getf item :title)))))
)
;;; vim: set ft=lisp lisp:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment