<commit_before>﻿using CoreTweet;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Diagnostics;

namespace Shanghai
{
    class TwitterCheck
    {
        private static readonly string[] BlackList = {
            "Mewdra", "nippy", "metto0226",
        };
        private static readonly int SettingMax = 100;
        private readonly ReadOnlyCollection<string> BlackWords, WhiteWords;

        public TwitterCheck()
        {
            var settings = ConfigurationManager.AppSettings;

            var blackWords = new List<string>();
            var whiteWords = new List<string>();
            for (int i = 1; i <= SettingMax; i++)
            {
                string black = settings["twitter.blackwords." + i];
                if (black != null)
                {
                    foreach (var elem in black.Split(','))
                    {
                        blackWords.Add(elem);
                    }
                }
                string white = settings["twitter.whitewords." + i];
                if (white != null)
                {
                    foreach (var elem in white.Split(','))
                    {
                        whiteWords.Add(elem);
                    }
                }
            }
            BlackWords = blackWords.AsReadOnly();
            WhiteWords = whiteWords.AsReadOnly();
            Log.Trace.TraceEvent(TraceEventType.Information, 0,
                "{0} black words loaded", BlackWords.Count);
            Log.Trace.TraceEvent(TraceEventType.Information, 0,
               "{0} white words loaded", WhiteWords.Count);
        }

        private bool IsBlack(Status status, long masterId)
        {
            // not master
            if (status.User.Id == masterId)
            {
                return false;
            }
            if (Array.IndexOf(BlackList, status.User.Name) < 0)
            {
                return false;
            }

            bool black = false;
            foreach (var word in BlackWords)
            {
                black = black || status.Text.Contains(word);
            }

            const int AfterHour = 21;
            const int BeforeHour = 5;
            DateTimeOffset localTime = status.CreatedAt.ToLocalTime();
            black = black && (localTime.Hour >= AfterHour || localTime.Hour <= BeforeHour);

            return black;
        }

        private bool IsWhite(Status status)
        {
            // master only
            if (status.User.Id != TwitterManager.MasterTokens.UserId)
            {
                return false;
            }

            bool white = false;
            foreach (var word in WhiteWords)
            {
                white = white || status.Text.Contains(word);
            }

            return white;
        }

        public void CheckBlack(TaskServer server, string taskName)
        {
            const int SearchCount = 200;
            long masterId = TwitterManager.MasterTokens.Account.VerifyCredentials().Id ?? 0;

            var timeline = TwitterManager.MasterTokens.Statuses.HomeTimeline(count: SearchCount);
            foreach (var status in timeline)
            {
                if (IsBlack(status, masterId))
                {
                    if (!(status.IsFavorited ?? false))
                    {
                        Log.Trace.TraceEvent(TraceEventType.Information, 0,
                            "[{0}] Find black: {1} - {2}", taskName, status.User.Name, status.Text);
                        TwitterManager.Favorite(status.Id);
                        TwitterManager.Update(
                            string.Format("@{0} ブラック", status.User.Name),
                            status.Id);
                    }
                    else
                    {
                        Log.Trace.TraceEvent(TraceEventType.Information, 0, "Skip");
                    }
                }
            }
        }

        public void CheckMention(TaskServer server, string taskName)
        {
            const int SearchCount = 200;

            var timeline = TwitterManager.Tokens.Statuses.MentionsTimeline(count: SearchCount);
            foreach (var status in timeline)
            {<commit_msg>ホワイト実装<commit_after>﻿using CoreTweet;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Diagnostics;

namespace Shanghai
{
    class TwitterCheck
    {
        private static readonly string[] BlackList = {
            "Mewdra", "nippy", "metto0226",
        };
        private static readonly int SettingMax = 100;
        private readonly ReadOnlyCollection<string> BlackWords, WhiteWords;

        public TwitterCheck()
        {
            var settings = ConfigurationManager.AppSettings;

            var blackWords = new List<string>();
            var whiteWords = new List<string>();
            for (int i = 1; i <= SettingMax; i++)
            {
                string black = settings["twitter.blackwords." + i];
                if (black != null)
                {
                    foreach (var elem in black.Split(','))
                    {
                        blackWords.Add(elem);
                    }
                }
                string white = settings["twitter.whitewords." + i];
                if (white != null)
                {
                    foreach (var elem in white.Split(','))
                    {
                        whiteWords.Add(elem);
                    }
                }
            }
            BlackWords = blackWords.AsReadOnly();
            WhiteWords = whiteWords.AsReadOnly();
            Log.Trace.TraceEvent(TraceEventType.Information, 0,
                "{0} black words loaded", BlackWords.Count);
            Log.Trace.TraceEvent(TraceEventType.Information, 0,
               "{0} white words loaded", WhiteWords.Count);
        }

        private bool IsBlack(Status status, long masterId)
        {
            // not master
            if (status.User.Id == masterId)
            {
                return false;
            }
            if (Array.IndexOf(BlackList, status.User.Name) < 0)
            {
                return false;
            }

            bool black = false;
            foreach (var word in BlackWords)
            {
                black = black || status.Text.Contains(word);
            }

            const int AfterHour = 21;
            const int BeforeHour = 5;
            DateTimeOffset localTime = status.CreatedAt.ToLocalTime();
            black = black && (localTime.Hour >= AfterHour || localTime.Hour <= BeforeHour);

            return black;
        }

        private bool IsWhite(Status status, long masterId)
        {
            // master only
            if (status.User.Id != masterId)
            {
                return false;
            }

            bool white = false;
            foreach (var word in WhiteWords)
            {
                white = white || status.Text.Contains(word);
            }

            return white;
        }

        public void CheckBlack(TaskServer server, string taskName)
        {
            const int SearchCount = 200;
            long masterId = TwitterManager.MasterTokens.Account.VerifyCredentials().Id ?? 0;

            var timeline = TwitterManager.MasterTokens.Statuses.HomeTimeline(count: SearchCount);
            foreach (var status in timeline)
            {
                if (IsBlack(status, masterId))
                {
                    Log.Trace.TraceEvent(TraceEventType.Information, 0,
                        "[{0}] Find black: {1} - {2}", taskName, status.User.Name, status.Text);
                    try
                    {
                        TwitterManager.Favorite(status.Id);
                    }
                    catch (TwitterException)
                    {
                        continue;
                    }
                    TwitterManager.Update(
                        string.Format("@{0} ブラック", status.User.Name),
                        status.Id);
                }
                else if (IsWhite(status, masterId))
                {
                    Log.Trace.TraceEvent(TraceEventType.Information, 0,
                        "[{0}] Find white: {1} - {2}", taskName, status.User.Name, status.Text);
                    try
                    {
                        TwitterManager.Favorite(status.Id);
                    }
                    catch (TwitterException)
                    {
                        continue;
                    }
                    TwitterManager.Update(
                        string.Format("@{0} ホワイト！", status.User.Name),
                        status.Id);
                }
            }
        }

        public void CheckMention(TaskServer server, string taskName)
        {
            const int SearchCount = 200;

            var timeline = TwitterManager.Tokens.Statuses.MentionsTimeline(count: SearchCount);
            foreach (var status in timeline)
            {
{'fraction_non_alphanumeric': 0.09019004723717455, 'fraction_numerical': 0.006810941447874327, 'mean_word_length': 1.2280959373470386, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6403247', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2094, 'n_words': 503}