public static bool SaveHistory(string path, GatheringHistory history) { try { if (!File.Exists(path)) { File.Create(path); } File.WriteAllBytes(path, MessagePackSerializer.Serialize(history)); } catch (IOException e) { Console.WriteLine($"Gather履歴「{path}」を書き込めませんでした:"); Console.WriteLine(e.Message); return(false); } return(true); }
static void Gather(Tokens tokens, List <Tym> Tyms) { Console.WriteLine("Gather: 戸山生を収集します。"); Console.WriteLine(); GatheringSettings settings = GatheringSettings.CreateSettings(Tyms); GatheringHistory History = GatheringHistory.GetHistory("history.dat"); switch (settings.SearchOrigin) { case GatheringSettings.SearchOriginType.Specified: case GatheringSettings.SearchOriginType.Direct: SingleGathering(tokens, Tyms, settings, History); break; case GatheringSettings.SearchOriginType.Indirect: IndirectGathering(tokens, Tyms, settings, History); break; default: break; } Tym.SaveTymList("tyms.dat", Tyms, true); }
static void IndirectGathering(Tokens tokens, List <Tym> Tyms, GatheringSettings settings, GatheringHistory History) { GatheringHistory.IndirectGatheringState State = History.GetState(settings) as GatheringHistory.IndirectGatheringState; int startIndex = 0; if (State != null) { Console.WriteLine("以前にこの設定で途中まで検索を行った履歴があります。途中から再開しますか?[y/n]"); switch (Console.ReadLine()) { case "n": State = new GatheringHistory.IndirectGatheringState(null, null); break; default: int index = Tyms.IndexOf(State.CurrentTym); if (index == -1) { startIndex = 0; } else { startIndex = index; } break; } } else { State = new GatheringHistory.IndirectGatheringState(null, null); } History.SetState(settings, State); GatheringHistory.SaveHistory("history.dat", History); int TymCounter = 0; for (int i = 0; i < Tyms.Count; i++) { if (startIndex <= i) { //Process State.CurrentTym = Tyms[i]; History.SetState(settings, State); GatheringHistory.SaveHistory("history.dat", History); while (true) { Tym.SaveTymList("tyms.dat", Tyms, false); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("累計収集戸山生数:" + TymCounter.ToString()); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"これから処理する戸山生:{State.CurrentTym.Name} (@{State.CurrentTym.Screen_name})"); Console.ForegroundColor = ConsoleColor.Gray; Cursored <User> gathereds = null; try { gathereds = InternalGather(tokens, State.Cursor, State.CurrentTym, settings.SearchGroup); } catch (Exception) { return; } if (gathereds == null) { continue; } foreach (User gathered in gathereds) { if (IsTym(gathered)) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("●戸山生っぽい!"); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine($"{gathered.Name} ({gathered.ScreenName})"); Console.WriteLine(gathered.Description); Tym tempTym = new Tym(gathered, false); if (!Tyms.Contains(tempTym)) { TymCounter++; Tyms.Add(tempTym); } } else { Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("●戸山生じゃないかも……"); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine($"{gathered.Name} ({gathered.ScreenName})"); Console.WriteLine(gathered.Description); } } if (gathereds.NextCursor == 0) { //Last Page State.Cursor = null; History.SetState(settings, State); GatheringHistory.SaveHistory("history.dat", History); break; } else { State.Cursor = gathereds.NextCursor; History.SetState(settings, State); GatheringHistory.SaveHistory("history.dat", History); } } } } Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("収集終了"); Console.WriteLine("累計収集戸山生数:" + TymCounter.ToString()); Console.ForegroundColor = ConsoleColor.Gray; }
static void SingleGathering(Tokens tokens, List <Tym> Tyms, GatheringSettings settings, GatheringHistory History) { GatheringHistory.SingleGatheringState State = History.GetState(settings) as GatheringHistory.SingleGatheringState; if (State != null) { Console.WriteLine("以前にこの設定で途中まで検索を行った履歴があります。途中から再開しますか?[y/n]"); switch (settings.SearchOrigin) { case GatheringSettings.SearchOriginType.Direct: Console.WriteLine($"({State.CurrentTym.Name}, @{State.CurrentTym.Screen_name})"); break; case GatheringSettings.SearchOriginType.Specified: Console.WriteLine($"(@{State.SpecifiedScreenName})"); break; } switch (Console.ReadLine()) { case "n": switch (settings.SearchOrigin) { case GatheringSettings.SearchOriginType.Direct: State = new GatheringHistory.SingleGatheringState(null, new Tym(currentUser, true)); break; case GatheringSettings.SearchOriginType.Specified: Console.WriteLine("検索の起点となるユーザーのIDを入力してください"); Console.Write("@"); State = new GatheringHistory.SingleGatheringState(null, Console.ReadLine()); break; default: State = new GatheringHistory.SingleGatheringState(null, new Tym(currentUser, true)); break; } break; default: break; } } else { switch (settings.SearchOrigin) { case GatheringSettings.SearchOriginType.Direct: State = new GatheringHistory.SingleGatheringState(null, new Tym(currentUser, true)); break; case GatheringSettings.SearchOriginType.Specified: Console.WriteLine("検索の起点となるユーザーのIDを入力してください"); Console.Write("@"); State = new GatheringHistory.SingleGatheringState(null, Console.ReadLine()); break; default: State = new GatheringHistory.SingleGatheringState(null, new Tym(currentUser, true)); break; } } History.SetState(settings, State); GatheringHistory.SaveHistory("history.dat", History); int TymCounter = 0; while (true) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("累計収集戸山生数:" + TymCounter.ToString()); Cursored <User> gathereds = null; try { switch (settings.SearchOrigin) { case GatheringSettings.SearchOriginType.Direct: gathereds = InternalGather(tokens, State.Cursor, State.CurrentTym, settings.SearchGroup); break; case GatheringSettings.SearchOriginType.Specified: gathereds = InternalGather(tokens, State.Cursor, State.SpecifiedScreenName, settings.SearchGroup); break; } } catch (Exception) { return; } if (gathereds == null) { continue; } foreach (User gathered in gathereds) { if (IsTym(gathered)) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("●戸山生っぽい!"); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine($"{gathered.Name} ({gathered.ScreenName})"); Console.WriteLine(gathered.Description); Tym tym; switch (settings.SearchOrigin) { case GatheringSettings.SearchOriginType.Direct: tym = new Tym(gathered, (settings.SearchGroup == GatheringSettings.SearchGroupType.Follow ? true : false)); break; case GatheringSettings.SearchOriginType.Specified: default: tym = new Tym(gathered, false); break; } if (!Tyms.Contains(tym)) { TymCounter++; Tyms.Add(tym); } } else { Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("●戸山生じゃないかも……"); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine($"{gathered.Name} ({gathered.ScreenName})"); Console.WriteLine(gathered.Description); } } if (gathereds.NextCursor == 0) { //Last Page State = null; History.SetState(settings, State); GatheringHistory.SaveHistory("history.dat", History); break; } else { State.Cursor = gathereds.NextCursor; History.SetState(settings, State); GatheringHistory.SaveHistory("history.dat", History); } } Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("収集終了"); Console.WriteLine("累計収集戸山生数:" + TymCounter.ToString()); Console.ForegroundColor = ConsoleColor.Gray; }