0
0

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 1 year has passed since last update.

kakaotalkのオープンチャット履歴のコメント集計

Last updated at Posted at 2022-11-17

概要

カカオトークのオープンチャットのチャット履歴をエクスポートしてそのテキストファイルから集計するものです。
日単位、月単位で各ユーザ毎にコメント数を表示するツールになります。

事前事項

・パソコンのカカオトークアプリケーションからチャット履歴をエクスポートが必要です。
・ファイル名は「kakaotalk.txt」に変更が必要です。(固定で指定しています。)
・実行ファイルと同じディレクトリにチャット履歴のファイルを保存が必要です。

実際のソース

まだ性能的には問題があるかもしれないですが、以下の通りです。

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace kakaotalk_txt
{
    class Program
    {
        public static List<string> personList = new List<string>();
        public static string head = "";
        public static string datestr = "";
        public static string path = System.Environment.CurrentDirectory + @"\kakaotalk.txt";
        public static string temp = "";
        public static List<string> nameList = new List<string>();
        public static List<string> chkList = new List<string>();
        public static List<string> dayList = new List<string>();

        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                if (args[0].ToString() == "M")
                {
                    monthOfCount();
                }
                else if (args[0].ToString() == "MU")
                {
                    monthOfPerson();
                }
                else if (args[0].ToString() == "U")
                {
                    userList();
                }
                else if (args[0].ToString() == "D")
                {
                    dayOfCount();
                }
                else
                {
                    Console.WriteLine("Usage)filename.exe [M]onth count |[D]ay count |[U]ser list");
                }
            }
            else
            {
                Console.WriteLine("Usage)filename.exe [M]onth count |[D]ay count |[U]ser list");
            }
        }
        /// <summary>
        /// ユーザ一覧
        /// </summary>
        static void userList()
        {
            int counter = 0;

            foreach (string line in File.ReadLines(path))
            {
                if (line == "" || (line.Length <= 0)) continue;

                if (line.IndexOf("---------------") >= 0)
                {
                    datestr = line.Replace("---------------", "");
                    datestr = datestr.Replace(" ", "");
                }

                if (counter < 2)
                {
                    head = head + line + "\r\n";
                    counter++;
                    continue;
                }
                else
                {
                    if (line.IndexOf("入室しました。") > 0 ||
                        line.IndexOf("들어왔습니다.") > 0 || 
                        line.IndexOf("退室しました。") > 0 ||
                        line.IndexOf("나갔습니다.") > 0) continue;
                    temp = line.Replace("] [", "]$[");
                    string[] tmparray = temp.Split('$');

                    if (tmparray[0].IndexOf("[") >= 0)
                    {

                        if (!nameList.Contains(tmparray[0]))
                        {
                            nameList.Add(tmparray[0]);
                        }
                    }
                }
                counter++;
            }

            //保存するファイル名
            string fileName = "userList.txt";
            try
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                //ファイルをオープンする
                using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.GetEncoding("UTF-8")))
                {
                    foreach (string name in nameList)
                    {
                        Console.WriteLine(name);
                        //テキストを書き込む
                        sw.WriteLine(name);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("error = " + ex.Message);
            }
        }
        /// <summary>
        /// 毎月のユーザリスト
        /// </summary>
        static void monthOfPerson()
        {
            int counter = 0;

            foreach (string line in File.ReadLines(path))
            {
                if (line == "" || (line.Length <= 0)) continue;

                if (line.IndexOf("---------------") >= 0)
                {
                    datestr = line.Replace("---------------", "");
                    datestr = datestr.Replace(" ", "");
                }

                if (counter < 2)
                {
                    head = head + line + "\r\n";
                    counter++;
                    continue;
                }
                else
                {
                    if (line.IndexOf("入室しました。") > 0 ||
                        line.IndexOf("들어왔습니다.") > 0 ||
                        line.IndexOf("退室しました。") > 0 ||
                        line.IndexOf("나갔습니다.") > 0) continue;
                    temp = line.Replace("] [", "]$[");
                    string[] tmparray = temp.Split('$');

                    if (tmparray[0].IndexOf("[") >= 0)
                    {
                        if (datestr.IndexOf("월") > 0)
                        {
                            if (!nameList.Contains(datestr.Split('월')[0] + "월" + "|" + tmparray[0]))
                            {
                                nameList.Add(datestr.Split('월')[0] + "월" + "|" + tmparray[0]);
                            }
                        }
                        else if (datestr.IndexOf("月") > 0)
                        {
                            if (!nameList.Contains(datestr.Split('月')[0] + "月" + "|" + tmparray[0]))
                            {
                                nameList.Add(datestr.Split('月')[0] + "月" + "|" + tmparray[0]);
                            }
                        }
                    }
                }
                counter++;
            }

            //保存するファイル名
            string fileName = "monthOfUser.txt";
            try
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                //ファイルをオープンする
                using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.GetEncoding("UTF-8")))
                {
                    foreach (string name in nameList)
                    {
                        Console.WriteLine(name);
                        //テキストを書き込む
                        sw.WriteLine(name);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("error = " + ex.Message);
            }
        }
        /// <summary>
        /// 毎日のチャットカウント
        /// </summary>
        static void dayOfCount()
        {
            int counter = 0;

            foreach (string line in File.ReadLines(path))
            {
                if (line == "" || (line.Length <= 0)) continue;

                if (line.IndexOf("---------------") >= 0)
                {
                    datestr = line.Replace("---------------", "");
                    datestr = datestr.Replace(" ", "");
                }

                if (counter < 2)
                {
                    head = head + line + "\r\n";
                    counter++;
                    continue;
                }
                else
                {
                    if (line.IndexOf("入室しました。") > 0 ||
                        line.IndexOf("들어왔습니다.") > 0 ||
                        line.IndexOf("退室しました。") > 0 ||
                        line.IndexOf("나갔습니다.") > 0) continue;
                    temp = line.Replace("] [", "]$[");
                    string[] tmparray = temp.Split('$');

                    if (tmparray[0].IndexOf("[") >= 0)
                    {

                        if (!nameList.Contains(tmparray[0]))
                        {
                            nameList.Add(tmparray[0]);
                        }

                        personList.Add(datestr + "|" + tmparray[0]);

                        if (!dayList.Contains(datestr.Split('|')[0]))
                        {
                            dayList.Add(datestr.Split('|')[0]);
                        }
                    }
                }
                counter++;
            }

            //保存するファイル名
            string fileName = "dayOfCount.txt";
            try
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                //ファイルをオープンする
                using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.GetEncoding("UTF-8")))
                {
                    //ユニークなユーザ名
                    foreach (string name in nameList)
                    {
                        foreach (string day in dayList)
                        {
                            Console.WriteLine("<<<" + day + ">>>");

                            long chatcnt = 0;
                            foreach (string person in personList)
                            {
                                if (person.IndexOf(day) >= 0 && person.IndexOf(name) > 0)
                                {
                                    ++chatcnt;
                                }
                            }

                            if (chatcnt > 0)
                            {
                                //Console.WriteLine(day + ":" + tmpname + "=" + chatcnt);
                                sw.WriteLine(day + ":" + name + "=" + chatcnt);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("error = " + ex.Message);
            }
        }

        /// <summary>
        /// 月別チャットカウント
        /// </summary>
        static void monthOfCount()
        {
            int counter = 0;

            foreach (string line in File.ReadLines(path))
            {
                if (line == "" || (line.Length <= 0)) continue;

                if (line.IndexOf("---------------") >= 0)
                {
                    datestr = line.Replace("---------------", "");
                    datestr = datestr.Replace(" ", "");
                }

                if (counter < 2)
                {
                    head = head + line + "\r\n";
                    counter++;
                    continue;
                }
                else
                {
                    if (line.IndexOf("入室しました。") > 0 ||
                        line.IndexOf("들어왔습니다.") > 0 ||
                        line.IndexOf("退室しました。") > 0 ||
                        line.IndexOf("나갔습니다.") > 0) continue;
                    temp = line.Replace("] [", "]$[");
                    string[] tmparray = temp.Split('$');

                    if (tmparray[0].IndexOf("[") >= 0)
                    {

                        if (!nameList.Contains(tmparray[0]))
                        {
                            nameList.Add(tmparray[0]);
                        }

                        personList.Add(datestr + "|" + tmparray[0]);

                        if (datestr.IndexOf("월") > 0)
                        {
                            if (!dayList.Contains(datestr.Split('월')[0] + "월"))
                            {
                                dayList.Add(datestr.Split('월')[0] + "월");
                            }
                        }else if (datestr.IndexOf("月") > 0)
                        {
                            if (!dayList.Contains(datestr.Split('月')[0] + "月"))
                            {
                                dayList.Add(datestr.Split('月')[0] + "月");
                            }
                        }
                    }
                }
                counter++;
            }

            //保存するファイル名
            string fileName = "monthOfCount.txt";
            try
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                //ファイルをオープンする
                using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.GetEncoding("UTF-8")))
                {
                    //ユニークなユーザ名
                    foreach (string name in nameList)
                    {
                        Console.WriteLine("<<<" + name + ">>>");
                        foreach (string month in dayList)
                        {
                            Console.WriteLine("<<<" + month + ">>>");

                            long chatcnt = 0;
                            foreach (string person in personList)
                            {
                                if (person.IndexOf(month) >= 0 && person.IndexOf(name) > 0)
                                {
                                    ++chatcnt;
                                }
                            }
                            if (chatcnt > 0)
                            {
                                //Console.WriteLine(month + ":" + tmpname + "=" + chatcnt);
                                sw.WriteLine(month + ":" + name + "=" + chatcnt);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("error = " + ex.Message);
            }
        }
    }
}

実行結果

コンソールにも表示されていますが、ファイルに保存されている内容を記載しています。

●コマンド
C:\prj\kakaotalk>kakaotalk_txt.exe
Usage)filename.exe [M]onth count |[D]ay count |[U]ser list

●ユーザ一覧

C:\prj\kakaotalk>kakaotalk_txt.exe U
[テスト1/東京北区/女]
[テスト2/六本木/男]
[テスト3/東京/男]
[テスト4/多摩市/男]

●毎月のユーザリスト

C:\prj\kakaotalk>kakaotalk_txt.exe MU
2022年8月|[テスト1/東京北区/女]
2022年8月|[テスト2/六本木/男]
2022年8月|[テスト3/東京/男]
2022年8月|[テスト4/多摩市/男]

●毎日のチャットカウント

C:\prj\kakaotalk>kakaotalk_txt.exe D
2022年8月1日月曜日:[テスト1/東京北区/女]=74
2022年8月2日火曜日:[テスト1/東京北区/女]=96
2022年8月3日水曜日:[テスト1/東京北区/女]=8
2022年8月4日木曜日:[テスト1/東京北区/女]=25

●月別チャットカウント

C:\prj\kakaotalk>kakaotalk_txt.exe M
2022年8月:[テスト1/東京北区/女]=858
2022年9月:[テスト1/東京北区/女]=554
2022年10月:[テスト1/東京北区/女]=644
2022年11月:[テスト1/東京北区/女]=32
2022年8月:[テスト2/六本木/男]=704
2022年9月:[テスト2/六本木/男]=1011
2022年10月:[テスト2/六本木/男]=1024
2022年11月:[テスト2/六本木/男]=427

終わりに

元々は韓国語であるチャットを実行した結果ですが、日本語に年、月、日などを変更しました。
本当の目的としては期間を指定してその期間の中で一言も発信していないユーザを抽出するものを作成する予定でしたが、日・月別のコメント数集計を作成することになりましたね。
今日はここまでです。
おやすみなさい~。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?