LoginSignup
8
4

More than 3 years have passed since last update.

Qiitaの記事を退避(保存)するスクリプト書いた

Posted at

しかもCommon Lispでな!!!

使い方

Common Lispの処理系マネージャ・スクリプティング環境のRoswellスクリプトとして書かれています。
動作確認はSBCL (roswellデフォルトの処理系)で確認しました。CCLではなんか日本語ファイル名がつかえないのか動かなかったです。

ユーザ名*user*と保存先ディレクトリ*target-directory*は適宜書き換えて実行してください。ユーザ名 - 記事タイトル.mdのファイル名で保存されます。

スクリプト自体

#!/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* "xxxxxxxxxxxxxx")
(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:

では、happy hacking!

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4