10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Qiita:Teamの投稿をMarkdownファイルでエクスポート

Posted at

目的 :bulb:

Qiitaの有料クローズド版Qiita:Teamでは、チームの全投稿データを一つのJSONファイルとして、オーナ権限付きユーザがエクスポートできます。退会時も安心ですね。
ただ、JSONファイルのままだと人間が読みにくいので、投稿ごとにMarkdownファイルとして分割することにします。
体裁が必要であれば、そのあとPandocでPDFなどに変換しましょう。

入力 :speech_balloon:

とりあえず投稿のタイトルとユーザ名と本文のみに着目すると、投稿ごとに下記リストのような構造になっています。
Qiitaと共通のAPI v2で取得できるJSONとはキー名など異なるようなので注意が必要です。

  • title
  • user
    • url_name
    • ...
  • raw_baody
  • ...

省略しましたが、JSONファイルにはタグやコメントなどの情報も含まれています。

コード :sparkles:

ScalaのJSONライブラリjson4sを使うことにします。実行環境はWindowsです。

Main.scala
case class QiitaTeamUser(url_name: String)

case class QiitaTeamPost(title: String, user: QiitaTeamUser, raw_body: String)

object Main extends App {

  splitQiitaTeamJson("C:\\TEMP\\123456789.json") // エクスポートしたJSONファイルのパス

  def splitQiitaTeamJson(file: String) {
    using(scala.io.Source.fromFile(file, "UTF-8")) { source =>
      // JSONパース
      implicit val formats = org.json4s.DefaultFormats
      val json = org.json4s.native.JsonMethods.parse(source.mkString)
      val posts = json.children.map(_.extract[QiitaTeamPost])

      // Markdownファイル出力
      posts.foreach { post =>
        val dir = "out\\" + post.user.url_name
        val file = post.title.replaceAll("[\\/:*?\"<>|]", "_") + ".md" // Windowsのパス禁止文字を置換
        new java.io.File(dir).mkdirs()
        writeFile(dir + "\\" + file)(_.print(post.raw_body))
      }
    }

    // 簡易loanパターン
    def using[A <: {def close() : Unit}](s: A)(f: A => Any) {
      try f(s)
      finally s.close()
    }

    def writeFile(file: String)(f: java.io.PrintWriter => Unit) {
      import java.io._
      try using(new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")))(f)
      catch {
        case e: FileNotFoundException => println(e)
      }
    }
  }
}
build.sbt
name := "splitQiitaTeamJson"

version := "1.0"

scalaVersion := "2.11.4"

libraryDependencies += "org.json4s" %% "json4s-native" % "3.2.11"
build.properties
sbt.version=0.13.7

出力 :white_flower:

下記のようなフォルダ構造で出力します。

> tree /F
├─piyo7
│      boost__optionalにモナドのbindを.md
│      ICML 2014 Best Paper斜め読み.md
│      Qiita_Teamの更新をChatWorkに通知する.md
...
├─piyo8
...
10
9
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
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?