Skip to content

Commit

Permalink
添加项目文件。
Browse files Browse the repository at this point in the history
  • Loading branch information
BluesDawn576 committed May 23, 2023
1 parent cf4e285 commit c00f62f
Show file tree
Hide file tree
Showing 16 changed files with 901 additions and 0 deletions.
401 changes: 401 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions Common/FileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.IO;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WordPress2Valine.Common
{
public class FileReader
{
public static JArray ReadJson(string path)
{
using (StreamReader file = File.OpenText(path))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
JArray o = (JArray)JToken.ReadFrom(reader);
return o;
}
}
}
public static string ReadFileFromDialog()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;
dialog.Filter = "WordPress Data(*.json)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
return dialog.FileName;
}
return null;
}
}
}
26 changes: 26 additions & 0 deletions Common/FileWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.IO;
using Newtonsoft.Json;

namespace WordPress2Valine.Common
{
public class FileWriter
{
public static void SaveComment(string path, Valine[] v)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string fp = string.Format("{0}\\Comment_{1}.json", path, DateTime.Now.ToString("yyyyMMdd_HHmmss"));
if (!File.Exists(fp))
{
FileStream fs = new FileStream(fp, FileMode.Create, FileAccess.ReadWrite);
fs.Close();
}
File.WriteAllText(fp, JsonConvert.SerializeObject(v));
Console.WriteLine();
Console.WriteLine(fp);
}
}
}
78 changes: 78 additions & 0 deletions Common/UrlDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace WordPress2Valine.Common
{
class UrlDictionary
{
public static Dictionary<string, string> dic;

private static void CreateDic()
{
dic = new Dictionary<string, string>();
}
public static void AutoAddDic(JArray o)
{
int c = o.Count;
for (int i = 0; i < c; i++)
{
AddDic(o[i]["comment_post_ID"].ToString());
}
}
public static void AddDic(string wordpressId)
{
AddDic(wordpressId, wordpressId);
}
public static void AddDic(string wordpressId, string hexoId)
{
if (dic == null)
{
CreateDic();
}
if (dic.ContainsKey(wordpressId))
{
if (dic[wordpressId] == "/" + hexoId + "/") return;
dic[wordpressId] = "/" + hexoId + "/";
Console.WriteLine("WordPress = {0} => Valine = {1}", wordpressId, dic[wordpressId]);
}
else
{
dic.Add(wordpressId, "/" + hexoId + "/");
}
}
/// <summary>
/// comment_post_ID 转 Hexo 链接后缀
/// </summary>
/// <param name="id">comment_post_ID</param>
public static string GetDic(string id)
{
if (dic == null)
{
CreateDic();
}

if (dic.ContainsKey(id))
{
return dic[id];
}
else
{
return null;
}
}
public static void GetAllDic()
{
if (dic == null)
{
CreateDic();
}
Console.WriteLine("---------------");
foreach (var d in dic)
{
Console.WriteLine("WordPress = {0} => Valine = {1}", d.Key, d.Value);
}
Console.WriteLine("---------------");
}
}
}
24 changes: 24 additions & 0 deletions Common/Valine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace WordPress2Valine.Common
{
public class Valine
{
public string comment;
public string createdAt;
public InsertedAt insertedAt = new InsertedAt();
public string ip;
public string link;
public string mail;
public string nick;
public string objectId;
public string ua;
public string updatedAt;
public string url;
public string rid = null;
}

public class InsertedAt
{
public string __type = "Date";
public string iso;
}
}
55 changes: 55 additions & 0 deletions Common/WordpressToValine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Newtonsoft.Json.Linq;
using WordPress2Valine.Utils;

namespace WordPress2Valine.Common
{
public class WordPressToValine
{
public static bool Convert(JArray o, string path)
{
try
{
int c = o.Count;
Valine[] va = new Valine[c];
for (int i = 0; i < c; i++)
{
va[i] = new Valine();
va[i].comment = string.Format("<p>{0}</p>", Unicode.Decode(o[i]["comment_content"].ToString()));
va[i].createdAt = TimeConvert.Format(o[i]["comment_date_gmt"].ToString());
va[i].insertedAt.iso = TimeConvert.Format(o[i]["comment_date_gmt"].ToString());
va[i].ip = o[i]["comment_author_IP"].ToString();
va[i].link = o[i]["comment_author_url"].ToString();
va[i].mail = o[i]["comment_author_email"].ToString();
va[i].nick = Unicode.Decode(o[i]["comment_author"].ToString());
va[i].objectId = o[i]["comment_ID"].ToString();
va[i].ua = o[i]["comment_agent"].ToString();
va[i].updatedAt = TimeConvert.Format(o[i]["comment_date_gmt"].ToString());
va[i].url = UrlDictionary.GetDic(o[i]["comment_post_ID"].ToString());
if (o[i]["comment_parent"].ToString() != "0")
{
va[i].rid = o[i]["comment_parent"].ToString();
//遍历评论id,寻找回复层对应的主评论
for (int j = 0; j < c; j++)
{
if (va[i].rid == o[j]["comment_ID"].ToString())
{
var parent_id = o[j]["comment_parent"].ToString();
if (parent_id == "0") break;
va[i].rid = parent_id;
}
}
}
Console.WriteLine(string.Format("[Info] Email: {0}, NickName: {1}, Comment: {2}", va[i].mail, va[i].nick, va[i].comment));
}
FileWriter.SaveComment(path, va);
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
}
}
}
20 changes: 20 additions & 0 deletions I18n/I18n.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Globalization;
using System.Resources;

namespace WordPress2Valine.Common
{
public static class I18n
{
public static CultureInfo GetLanguageResource()
{
switch (CultureInfo.CurrentCulture.Name)
{
case "zh-CN":
return new CultureInfo("zh-CN");
default:
return new CultureInfo("en-US");
}
}
}
}
Binary file added I18n/en_US/message.mo
Binary file not shown.
53 changes: 53 additions & 0 deletions I18n/en_US/message.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
msgid ""
msgstr ""
"Project-Id-Version: WordPress2Valine\n"
"POT-Creation-Date: 2023-05-23 14:29+0800\n"
"PO-Revision-Date: 2023-05-23 14:38+0800\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: en_US\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 3.3.1\n"
"X-Poedit-Basepath: ..\n"
"X-Poedit-KeywordsList: GetDic\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-SearchPath-0: .\n"

#: Program.cs:18
msgid "打开WordPress评论数据(.json)"
msgstr "Open WordPress comment file (.json)"

#: Program.cs:23
msgid "输入要替换的文章/页面id"
msgstr "Please enter the article/page id you want to replace."

#: Program.cs:24
msgid "例如 42,friend (不要添加\"/\")"
msgstr "Example: 42,friend (Don't add \"/\")"

#: Program.cs:25
msgid "输入 end 结束"
msgstr "You can type \"end\" to end it."

#: Program.cs:28
msgid "替换:"
msgstr "The value to replace: "

#: Program.cs:34
#, csharp-format
msgid "已替换 {0}"
msgstr "Replaced {0}"

#: Program.cs:41
msgid "转换完成"
msgstr "Completed."

#: Program.cs:46
msgid "转换失败"
msgstr "Failed to convert."

#: Program.cs:48
msgid "请按任意键退出..."
msgstr "Please press any key to exit..."
56 changes: 56 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using WordPress2Valine.Common;
using GetText;

namespace WordPress2Valine
{
class Program
{
public static readonly string savePath = Environment.CurrentDirectory + "\\Comment";

[STAThread]
static void Main(string[] args)
{
var localization = new Catalog("message", "./I18n", I18n.GetLanguageResource());
try
{
Console.WriteLine(localization.GetString("打开WordPress评论数据(.json)"));
string wordpressComments = FileReader.ReadFileFromDialog();
var content = FileReader.ReadJson(wordpressComments);
UrlDictionary.AutoAddDic(content);
UrlDictionary.GetAllDic();
Console.Write(localization.GetString("输入要替换的文章/页面id") + "\n"
+ localization.GetString("例如 42,friend (不要添加\"/\")") + "\n"
+ localization.GetString("输入 end 结束")+ "\n\n");
while (true)
{
Console.Write(localization.GetString("替换:"));
string value = Console.ReadLine();
if (value == "end") break;
else if (value.IndexOf(',') == -1) continue;
string[] vs = value.Split(',');
UrlDictionary.AddDic(vs[0], vs[1]);
}
var result = WordPressToValine.Convert(content, savePath);
if (result)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(localization.GetString("转换完成"));
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(localization.GetString("转换失败"));
}
Console.WriteLine(localization.GetString("请按任意键退出..."));
Console.ReadKey(true);
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine(localization.GetString("请按任意键退出..."));
Console.ReadKey(true);
}
}
}
}
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# WordPress2Valine

[简体中文](https://github.com/BluesDawn576/WordPress2Valine/blob/main/README_CN.md) | English

This is NOT a WordPress plugin.

## How to use

### Export the `wp_comments` table as `json` using phpmyadmin or other software

⚠️ATTENTION: Each post or page in WordPress has a unique internal identifier.

If the original page identifier is different from the current page, the comments on that page will not be displayed.
Please replace the changed identifier manually.

> Suppose the permalink has been set to `/?p=%post_id%`
> https://example.com/?p=42 => https//example.com/friend/
```
Example:
WordPress = 42 => Valine = /42/
```
```
The value to replace: 42,friend
```
```
WordPress = 42 => Valine = /friend/
```

### Import comments (LeanCloud)


Data Storage - Import/export - Import

![leancloud_en.png](https://s2.loli.net/2023/05/23/jazWBcxs53ZlFir.png)
Loading

0 comments on commit c00f62f

Please sign in to comment.