Skip to content

Instantly share code, notes, and snippets.

@YDKK
Last active December 14, 2017 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YDKK/49129c103998f4fd0371011632733a48 to your computer and use it in GitHub Desktop.
Save YDKK/49129c103998f4fd0371011632733a48 to your computer and use it in GitHub Desktop.
Fix Qiita:Team link to Crowi link. http://ydkk.hateblo.jp/entry/2017/12/14/235727
//
// QiitaLinkFixer.cs
//
// Created by YDKK on 2017/12/14.
// Copyright © 2017 YDKK. Licensed under MIT.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Codeplex.Data;
namespace QiitaLinkFixer
{
class Program
{
const string accessToken = "CrowiへのAccessToken";
static async Task Main(string[] args)
{
var client = new HttpClient();
const string baseUrl = "https://crowi.example.jp";//CrowiのUrl
const string titlePrefix = $"{baseUrl}/qiita/";//CrowiのUrl + Prefix
var qiitaJson = File.ReadAllText("1507689122.json");//QiitaからエクスポートしたJSON
var qiita = DynamicJson.Parse(qiitaJson);
var crowiJson = File.ReadAllText("pages.json");//https://crowi.example.jp/_api/pages.list?user=hoge を保存したもの
var crowi = DynamicJson.Parse(crowiJson);
var dict = new Dictionary<string, string>();
foreach (dynamic article in (object[])qiita.articles)
{
dict.Add(article.url, titlePrefix + article.user.id + "/" + ReplaceTitle(article.title));
}
foreach (dynamic page in (object[])crowi.pages)
{
var newBody = (string)page.revision.body;
newBody = dict.Aggregate(newBody, (current, pair) => current.Replace(pair.Key, pair.Value));
if (newBody == page.revision.body) continue;
var param = new Dictionary<string, string>
{
{ "access_token", accessToken },
{ "body", newBody },
{ "page_id", page.id }
};
var content = new FormUrlEncodedContent(param);
var result = await client.PostAsync($"{baseUrl}/_api/pages.update", content);
var res = DynamicJson.Parse(await result.Content.ReadAsStringAsync());
if (res.ok != true)
{
Console.WriteLine("[ERROR] Failed to update page.");
Console.WriteLine("Path: " + page.path);
}
}
}
private static string ReplaceTitle(string title)
{
return title
.Replace('^', '^')
.Replace('$', '$')
.Replace('*', '*')
.Replace('%', '%')
.Replace('?', '?')
.Replace('/', '/');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment