Exemplo n.º 1
0
        static public async void Start()
        {
            IPEndPoint ipPoint      = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
            Socket     listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            listenSocket.Bind(ipPoint);
            listenSocket.Listen(10);
            Console.WriteLine("Сервер запущен. Ожидание подключений...");

            while (true)
            {
                Socket handler = listenSocket.Accept();
#pragma warning disable CS4014 // Так как этот вызов не ожидается, выполнение существующего метода продолжается до тех пор, пока вызов не будет завершен
                Task.Run(async() =>
                {
                    using (NetworkStream stream = new NetworkStream(handler))
                    {
                        var str  = ReadMessage(stream);
                        var str1 = str.Split(':');
                        if (str1[0] == "Create")
                        {
                            var m = str1[1].Split('.');
                            dataBase.CreateUsers(m);
                        }
                        if (str1[0] == "Console")
                        {
                            while (true)
                            {
                                var message = ReadMessage(stream).Split(':');
                                switch (message[0])
                                {
                                case "Movie":
                                    {
                                        string r  = "Display:" + Convert.ToBase64String(Encoding.UTF8.GetBytes(await ShowRezult("movie", message[1], Find.Search.Show))) + "<EOF>";
                                        var data1 = Encoding.UTF8.GetBytes(r);
                                        stream.Write(data1, 0, data1.Length);
                                        stream.Flush();
                                    }
                                    break;

                                case "TV":
                                    {
                                        string r  = "Display:" + Convert.ToBase64String(Encoding.UTF8.GetBytes(await ShowRezult("tv", message[1], Find.Search.Show))) + "<EOF>";
                                        var data1 = Encoding.UTF8.GetBytes(r);
                                        stream.Write(data1, 0, data1.Length);
                                        stream.Flush();
                                    }
                                    break;
                                }
                            }
                        }
                        var answer = GoToDatabase(str);
                        if (answer == "ok<EOF>")
                        {
                            if (dataBase.IsAdmin(str.Split('.')[0]))
                            {
                                var mess  = $"Admin:{Find.Search.API_key}<EOF>";
                                var data1 = Encoding.UTF8.GetBytes(mess);
                                stream.Write(data1, 0, data1.Length);
                                stream.Flush();
                            }
                            else
                            {
                                var data = Encoding.UTF8.GetBytes(answer);
                                stream.Write(data, 0, data.Length);
                                stream.Flush();
                            }
                            while (true)
                            {
                                try
                                {
                                    var message = ReadMessage(stream).Split(':');
                                    switch (message[0])
                                    {
                                    case "GetTV":
                                        {
                                            var mess  = "Display:" + Convert.ToBase64String(Encoding.UTF8.GetBytes(await Program.ShowRezult("tv", message[1], Display))) + "<EOF>";
                                            var data1 = Encoding.UTF8.GetBytes(mess);
                                            stream.Write(data1, 0, data1.Length);
                                            stream.Flush();
                                        }
                                        break;

                                    case "GetMovie":
                                        {
                                            var mess  = "Display:" + Convert.ToBase64String(Encoding.UTF8.GetBytes(await Program.ShowRezult("movie", message[1], Display))) + "<EOF>";
                                            var data1 = Encoding.UTF8.GetBytes(mess);
                                            stream.Write(data1, 0, data1.Length);
                                            stream.Flush();
                                        }
                                        break;

                                    case "FindComment":
                                        {
                                            var comm            = dataBase.Select($"select User_comm,Text,Date from Comments where Id_content={FromB64String(message[1])}");
                                            Comments[] comments = new Comments[comm.Rows.Count];
                                            for (int i = 0; i < comm.Rows.Count; i++)
                                            {
                                                comments[i] = new Comments((string)comm.Rows[i].ItemArray[0], (string)comm.Rows[i].ItemArray[1], (string)comm.Rows[i].ItemArray[2]);
                                            }
                                            var mess  = "FindComment:" + Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comments))) + "<EOF>";
                                            var data1 = Encoding.UTF8.GetBytes(mess);
                                            stream.Write(data1, 0, data1.Length);
                                            stream.Flush();
                                        }
                                        break;

                                    case "CreateComment":
                                        {
                                            var sss = message[1].Split('.');
                                            dataBase.Insert($"Insert into Comments(User_comm,Text,Id_content,Date) values('{sss[0]}','{FromB64String(sss[1])}',{FromB64String(sss[2])},'{DateTime.Now}')");
                                            var comm            = dataBase.Select($"select User_comm,Text,Date from Comments where Id_content={FromB64String(sss[2])}");
                                            Comments[] comments = new Comments[comm.Rows.Count];
                                            for (int i = 0; i < comm.Rows.Count; i++)
                                            {
                                                comments[i] = new Comments((string)comm.Rows[i].ItemArray[0], (string)comm.Rows[i].ItemArray[1], (string)comm.Rows[i].ItemArray[2]);
                                            }
                                            var mess  = "CreateComment:" + Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comments))) + "<EOF>";
                                            var data1 = Encoding.UTF8.GetBytes(mess);
                                            stream.Write(data1, 0, data1.Length);
                                            stream.Flush();
                                        }
                                        break;

                                    case "AdminAPI":
                                        {
                                            var sss = message[1];
                                            using (StreamWriter writer = new StreamWriter("API.txt", false))
                                            {
                                                writer.WriteLine(sss);
                                                Find.Search.API_key = sss;
                                            }
                                        }
                                        break;
                                    }
                                }
                                catch (Exception e)
                                {
                                    IKernel kernel = new StandardKernel();
                                    ModuleLoader.Load(kernel, LoggerType.File);
                                    DIExperiment dI = kernel.Get <DIExperiment>();
                                    dI.UseLogger(e.Message);
                                }
                            }
                        }
                    }
                });
#pragma warning restore CS4014 // Так как этот вызов не ожидается, выполнение существующего метода продолжается до тех пор, пока вызов не будет завершен
            }
        }
Exemplo n.º 2
0
        public static async Task <string> ShowRezult(string type, string question, Find.Sh sh)
        {
            DataBase dataBase = new DataBase();

            string content = "";

            try
            {
                question.Replace(' ', '+');

                string url = $"https://api.themoviedb.org/3/search/{type}?api_key={Find.Search.API_key}&query={question}";
                if ((content = dataBase.Search(question, type, sh)) == null)
                {
                    if (type == "movie")
                    {
                        Find.AnswerMovie rez = await Find.Search.GetRezaltAsync <Find.AnswerMovie>(url);

                        Find.Genres genres = await Find.Search.GetRezaltAsync <Find.Genres>($"https://api.themoviedb.org/3/genre/movie/list?api_key={Find.Search.API_key}&language=en-US");

                        dataBase.Insert($"insert into Search_History(Text_Of_Search,Type,User_search) values('{question}','{type}','vlad')");

                        for (int o = 0; o < rez.results.Count; o++)
                        {
                            string genrez = "";
                            for (int q = 0; q < rez.results[o].genre_ids.Length; q++)
                            {
                                genrez += rez.results[o].genre_ids[q].ToString();
                                genrez += ",";
                            }
                            dataBase.Insert($"Insert into Content (Genre_ids,Popularity,Vote_count,Backdrop_path,Original_language,Vote_average,Overview," +
                                            $"Poster_path,Adult,Release_date,Original_title,Title,Video) values" +
                                            $"('{genrez}',{rez.results[o].popularity.ToString(CultureInfo.CreateSpecificCulture("en-US"))},{rez.results[o].vote_count},'{rez.results[o].backdrop_path}','{rez.results[o].original_language}'" +
                                            $",{rez.results[o].vote_average.ToString(CultureInfo.CreateSpecificCulture("en-US"))}, '{rez.results[o].overview.Replace("’", "").Replace("'", "")}','{rez.results[o].poster_path}'," +
                                            $"{(rez.results[o].adult ? 1 : 0)},'{rez.results[o].release_date}','{rez.results[o].original_title.Replace("’", "").Replace("'", "")}','{rez.results[o].title.Replace("’", "").Replace("'", "")}',{(rez.results[o].video ? 1 : 0)})");
                            dataBase.Insert($"insert into Content_Answer(Search_Id,Content_Id) values((select top(1) Id from Search_History where Text_Of_Search='{question}' and Type='{type}'),(select top(1) Id from Content where Original_title='{rez.results[o].original_title}'))");
                        }

                        for (int g = 0; g < genres.genres.Count; g++)
                        {
                            try
                            {
                                dataBase.Insert($"Insert into Genres(Id,Name) values('{genres.genres[g].id}','{genres.genres[g].name}')");
                            }
                            catch (Exception) { }
                        }
                        content = sh(rez, null, genres);
                    }
                    else
                    {
                        Find.AnswerTV reza = await Find.Search.GetRezaltAsync <Find.AnswerTV>(url);

                        Find.Genres genress = await Find.Search.GetRezaltAsync <Find.Genres>($"https://api.themoviedb.org/3/genre/tv/list?api_key={Find.Search.API_key}&language=en-US");

                        dataBase.Insert($"insert into Search_History(Text_Of_Search,Type,User_search) values('{question}','{type}','vlad')");

                        for (int o = 0; o < reza.results.Count; o++)
                        {
                            string genrez = "", countries = "";
                            for (int q = 0; q < reza.results[o].genre_ids.Length; q++)
                            {
                                genrez += reza.results[o].genre_ids[q].ToString();
                                genrez += ",";
                            }
                            for (int q = 0; q < reza.results[o].origin_country.Length; q++)
                            {
                                countries += reza.results[o].origin_country[q].ToString();
                                countries += ",";
                            }
                            dataBase.Insert($"Insert into Content(Genre_ids,Popularity,Vote_count,Backdrop_path,Original_language,Vote_average," +
                                            $"Overview,Poster_path,Original_name,Name,Origin_country,First_air_date)" +
                                            $" values('{genrez}',{reza.results[o].popularity.ToString(CultureInfo.CreateSpecificCulture("en-US"))}," +
                                            $"{reza.results[o].vote_count},'{reza.results[o].backdrop_path}','{reza.results[o].original_language}'," +
                                            $"{reza.results[o].vote_average.ToString(CultureInfo.CreateSpecificCulture("en-US"))}," +
                                            $"'{reza.results[o].overview.Replace("’", "").Replace("'", "")}','{reza.results[o].poster_path}','{reza.results[o].original_name.Replace("’", "").Replace("'", "")}','{reza.results[o].name.Replace("’", "").Replace("'", "")}','{countries}','{reza.results[o].first_air_date}')");

                            dataBase.Insert($"insert into Content_Answer(Search_Id,Content_Id) values((select top(1) Id from Search_History where Text_Of_Search='{question}' and Type='{type}'),(select top(1) Id from Content where Original_name='{reza.results[o].original_name}'))");
                        }
                        for (int g = 0; g < genress.genres.Count; g++)
                        {
                            try
                            {
                                dataBase.Insert($"Insert into Genres(Id,Name) values('{genress.genres[g].id}','{genress.genres[g].name}')");
                            }
                            catch (Exception) { }
                        }
                        content = sh(null, reza, genress);
                    }
                }
            }
            catch (Exception e)
            {
                IKernel kernel = new StandardKernel();
                ModuleLoader.Load(kernel, LoggerType.File);
                DIExperiment dI = kernel.Get <DIExperiment>();
                dI.UseLogger(e.Message);
            }
            return(content);
        }