Created
July 1, 2015 01:53
-
-
Save vividtone/151f0c65036a0d844bd0 to your computer and use it in GitHub Desktop.
RedmineからREST APIを使って特定のプロジェクトのWikiページのソースをファイルに保存する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
# | |
# RedmineからREST APIを使って特定のプロジェクトのWikiページの | |
# ソースをファイルに保存する。 | |
# | |
require 'rest-client' # gem install rest-client | |
require 'open-uri' | |
require 'json' | |
REDMINE_USER = 'manager' | |
REDMINE_PASSWORD = 'manager' | |
REDMINE_URL = 'https://my.redmine.jp/demo/' | |
REDMINE_PROJECT = 'demo' | |
OUTPUT_DIR = 'output' | |
# Wikiページの一覧を取得 | |
io = open( | |
"#{REDMINE_URL}/projects/#{REDMINE_PROJECT}/wiki/index.json", | |
{:http_basic_authentication => [REDMINE_USER, REDMINE_PASSWORD]}) | |
pages_json = JSON.load(io.read) | |
if pages_json.size > 0 | |
begin | |
Dir.mkdir(OUTPUT_DIR) | |
rescue Errno::EEXIST | |
STDERR.puts "ERROR: ディレクトリ #{OUTPUT_DIR} が既に存在します。" | |
exit 1 | |
end | |
end | |
# 各ページの内容を取得してファイルに出力 | |
pages_count = pages_json['wiki_pages'].size | |
pages_json['wiki_pages'].each_with_index do |p, i| | |
title = p['title'] | |
puts "処理中(#{i + 1}/#{pages_count}): #{title}" | |
io = open( | |
"#{REDMINE_URL}/projects/#{REDMINE_PROJECT}/wiki/#{URI.encode(title)}.json", | |
{:http_basic_authentication => [REDMINE_USER, REDMINE_PASSWORD]}) | |
page_json = JSON.load(io.read) | |
File.open(File.join(OUTPUT_DIR, title + '.txt'), 'w') do |io_out| | |
io_out.write(page_json['wiki_page']['text']) | |
end | |
end | |
puts "\nプロジェクト #{REDMINE_PROJECT} の #{pages_json['wiki_pages'].size} 件のWikiページをディレクトリ #{OUTPUT_DIR} に出力しました。" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment