DEV Community

KOGA Mitsuhiro
KOGA Mitsuhiro

Posted on • Originally published at qiita.com

Qiitaの記事とストックをバックアップする

はじめに

最近PowerShellを使っているので力試しにQiitaの記事とストックをバックアップするスクリプトを作りました。
LGTM一覧は残念ながらQiita APIにないのでWebページをスクレイピングしないと取得できなさそうです。

動作環境

  • Windows PowerShell 5
  • PowerShell Core 6
  • PowerShell 7

使い方

記事のバックアップ

qiita-item-exporter.ps1

  1. ファイルの先頭に$userIdを自分のアカウント名に変更します。
  2. 記事中の画像ファイルのURLはデフォルトでファイル名のみに置換しますが他のURLに置き換えたい場合に設定します。 たとえば$imageFolder = "https://example.com/images/"にすると記事中の画像のURLが以下のように置換されます。
    • 変更前 https://qiita-image-store.s3.amazonaws.com/0/00000/ffffffff-ffff-ffff-ffff-ffffffffffff.png
    • 変更後 https://example.com/images/ffffffff-ffff-ffff-ffff-ffffffffffff.png
$userId = 'your-user-name'
$imageFolder = "" # replace image path

qiita-item-exporter.ps1を実行すると以下のような構造で記事と画像が保存されます。

- items
  +- readme.md                                   # 各記事へのリンク
  +- xxxxxxxxxxxxxxxxxxxx                        # 記事のID
     +- xxxxxxxxxxxxxxxxxxxx.md                  # ダウンロードした記事
     +- readme.md                                # 画像のURLを置換した記事
     +- ffffffff-ffff-ffff-ffff-ffffffffffff.png # 記事中の画像
     +- meta.json                                # 作成日時、更新日時、タイトル、タグを保存したJSON

ストックのバックアップ

qiita-stock-exporter.ps1

  1. $userId$imageFolderqiita-item-exporter.ps1と同様の設定です。
  2. ストックの総数はQiita APIで取得できないのでストック一覧の一番下からページ数を確認して$pagesを設定します。 このスクショでは29がページ数です。 image.png
$userId = 'your-user-name'
$pages = 1 # your-stock-pages
$imageFolder = "" # replace image path

qiita-stock-exporter.ps1を実行すると以下のような構造で記事と画像が保存されます。

- stokcs
  +- readme.md                                   # 各記事へのリンク
  +- xxxxxxxxxxxxxxxxxxxx                        # 記事のID
     +- xxxxxxxxxxxxxxxxxxxx.md                  # ダウンロードした記事
     +- readme.md                                # 画像のURLを置換した記事
     +- ffffffff-ffff-ffff-ffff-ffffffffffff.png # 記事中の画像
     +- meta.json                                # ユーザーID、ユーザー情報、タイトル、タグを保存したJSON

さいごに

PowerShellは文法こそ独特なもののC#のLINQのようにパイプで文字列だけではなくオブジェクトを流せるのでデータの加工が楽でした。

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay