Beispiel #1
0
        public async Task <ObjectResult> FindTrack([FromBody] User item)
        {
            string link = commands.GetTrack(item);

            string response;

            using var client = new HttpClient();
            response         = await client.GetStringAsync(link);

            User useritem = JsonConvert.DeserializeObject <User>(response);

            ClassResults searchtrack = JsonConvert.DeserializeObject <ClassResults>(response);

            for (int i = searchtrack.results.trackmatches.track.Count - 1; i > 9; i--)
            {
                searchtrack.results.trackmatches.track.RemoveAt(i);
            }

            return(new ObjectResult(searchtrack));
        }
Beispiel #2
0
        public async Task <ObjectResult> AddFavorite([FromBody] User item)
        {
            string link = commands.GetTrack(item);

            string response;

            using var client = new HttpClient();
            response         = await client.GetStringAsync(link);

            User useritem = JsonConvert.DeserializeObject <User>(response);

            ClassResults searchtrack = JsonConvert.DeserializeObject <ClassResults>(response);

            if (searchtrack.results.trackmatches.track.Count == 0)
            {
                return(new ObjectResult("It's bad"));
            }

            for (int i = 0; i < searchtrack.results.trackmatches.track.Count - 1; i++)
            {
                //...
                if (searchtrack.results.trackmatches.track[i].name == item.Name && searchtrack.results.trackmatches.track[i].artist == item.Artist)
                {
                    SearchTrackMain dbtrack = searchtrack.results.trackmatches.track[i];
                    searchtrack.results.trackmatches.track.Clear();
                    searchtrack.results.trackmatches.track.Add(dbtrack);

                    DBFavs dbfavs = new DBFavs();
                    dbfavs.id = item.Id;
                    dbfavs.jsresults.Add(searchtrack.results.trackmatches.track[i]);


                    DBListofFavs dBUserItemsDB = new DBListofFavs();
                    dBUserItemsDB.dbresults.Add(dbfavs);
                    DBListofFavs dBUserItemsDBFind = JsonConvert.DeserializeObject <DBListofFavs>(System.IO.File.ReadAllText(path));

                    bool contains = false;
                    int  indx     = 0;

                    for (int j = 0; j < dBUserItemsDBFind.dbresults.Count; j++)
                    {
                        if (dBUserItemsDBFind.dbresults[j].id == item.Id)
                        {
                            indx     = j;
                            contains = true;
                            break;
                        }
                    }

                    if (contains == false)
                    {
                        dBUserItemsDBFind.dbresults.Add(dbfavs);
                        System.IO.File.WriteAllText(path, JsonConvert.SerializeObject(dBUserItemsDBFind));
                    }
                    else
                    {
                        bool TrackContains = false;
                        List <SearchTrackMain> userItemadding = dBUserItemsDBFind.dbresults[indx].jsresults;
                        dBUserItemsDBFind.dbresults[indx].jsresults = userItemadding;
                        for (int k = 0; k < dBUserItemsDBFind.dbresults[indx].jsresults.Count; k++)
                        {
                            if (dBUserItemsDBFind.dbresults[indx].jsresults[k].name == dbtrack.name & dBUserItemsDBFind.dbresults[indx].jsresults[k].artist == dbtrack.artist)
                            {
                                TrackContains = true;
                            }
                        }

                        if (TrackContains == false)
                        {
                            dBUserItemsDBFind.dbresults[indx].jsresults.Add(dbtrack);
                            System.IO.File.WriteAllText(path, JsonConvert.SerializeObject(dBUserItemsDBFind));
                            break;
                        }
                        else
                        {
                            return(new ObjectResult("BAD")); //уже есть в списке
                        }
                    }
                    break;
                }
            }
            return(new ObjectResult("OK"));
        }
        private async static void BotOnMessageReceived(object sender, MessageEventArgs e)
        {
            var message = e.Message;

            if (message == null || message.Type != MessageType.Text)
            {
                return;
            }
            string name = $"{message.From.FirstName } {message.From.LastName}";

            Console.WriteLine($"{name} sent text: '{message.Text}'");


            User            userrr = new User();
            ClassStatusList st     = JsonConvert.DeserializeObject <ClassStatusList>(File.ReadAllText(stpath));

            int        index     = 0;
            bool       contains  = false;
            UserStatus usrstatus = new UserStatus();


            for (int i = 0; i < st.statuses.Count; i++)
            {
                if (st.statuses[i].Id == Convert.ToString(message.From.Id))
                {
                    contains  = true;
                    usrstatus = st.statuses[i];
                    index     = i;
                    break;
                }
            }

            if (contains == false)
            {
                usrstatus.status1 = 0;
                usrstatus.status2 = 0;
                usrstatus.Id      = Convert.ToString(message.From.Id);
                st.statuses.Add(usrstatus);
            }



            switch (st.statuses[index].status1)
            {
            case 0:
            {
                switch (message.Text)
                {
                case "/start":
                {
                    string text =
                        @"Список команд:
/gettoptracks - Топ-10 треков
/gettrack - Найти трек по названию
/getartistinfo - Информация об исполнителе
/getfavorite - Список избранных";

                    await Bot.SendTextMessageAsync(message.From.Id, text);

                    usrstatus.status1  = 0;
                    usrstatus.status2  = 0;
                    usrstatus.Id       = Convert.ToString(message.From.Id);
                    st.statuses[index] = usrstatus;
                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));
                    break;
                }

                case "/gettoptracks":
                {
                    var json = JsonConvert.SerializeObject(userrr);
                    var data = new StringContent(json, Encoding.UTF8, "application/json");

                    userrr.Id = Convert.ToString(message.From.Id);

                    HttpClient client   = new HttpClient();
                    var        response = await client.PostAsync($"https://mymusicbot.azurewebsites.net/api/Music/GetTopTracks", data);

                    string      result   = response.Content.ReadAsStringAsync().Result;
                    ClassTracks useritem = JsonConvert.DeserializeObject <ClassTracks>(result);

                    List <List <InlineKeyboardButton> > inlineKeyboardList = new List <List <InlineKeyboardButton> >();


                    foreach (var track in useritem.tracks.track)
                    {
                        //if (track.url.Length <= 60)
                        {
                            List <InlineKeyboardButton> ts = new List <InlineKeyboardButton>();
                            ts.Add(InlineKeyboardButton.WithUrl(track.name + " - " + track.artist.name, track.url));
                            ts.Add(InlineKeyboardButton.WithCallbackData("+", Convert.ToString(message.From.Id) + "⁜" + track.name + "⁜" + track.artist.name));
                            inlineKeyboardList.Add(ts);
                        }
                    }
                    var inline = new InlineKeyboardMarkup(inlineKeyboardList);

                    await Bot.SendTextMessageAsync(message.From.Id, "Топ-10 треков:", replyMarkup : inline);


                    usrstatus.status1  = 0;
                    usrstatus.status2  = 1;
                    st.statuses[index] = usrstatus;

                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                    break;
                }

                case "/getartistinfo":
                {
                    await Bot.SendTextMessageAsync(e.Message.Chat.Id, "Введите исполнителя");

                    st.statuses[index].status1 = 1;
                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                    break;
                }

                case "/gettrack":
                {
                    await Bot.SendTextMessageAsync(e.Message.Chat.Id, "Введите название песни");


                    st.statuses[index].status1 = 2;
                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                    break;
                }

                case "/getfavorite":
                {
                    userrr.Id = Convert.ToString(message.From.Id);

                    var json = JsonConvert.SerializeObject(userrr);
                    var data = new StringContent(json, Encoding.UTF8, "application/json");


                    HttpClient client   = new HttpClient();
                    var        response = await client.PostAsync($"https://mymusicbot.azurewebsites.net/api/Music/GetFavorite", data);

                    string result = response.Content.ReadAsStringAsync().Result;

                    if (result == "BAD")
                    {
                        await Bot.SendTextMessageAsync(e.Message.Chat.Id, "Списка нету");

                        break;
                    }


                    List <SearchTrackMain> js = JsonConvert.DeserializeObject <List <SearchTrackMain> >(result);

                    List <List <InlineKeyboardButton> > inlineKeyboardList = new List <List <InlineKeyboardButton> >();


                    foreach (var track in js)
                    {
                        List <InlineKeyboardButton> ts = new List <InlineKeyboardButton>();
                        ts.Add(InlineKeyboardButton.WithUrl(track.name + " - " + track.artist, track.url));
                        ts.Add(InlineKeyboardButton.WithCallbackData("х", Convert.ToString(message.From.Id) + "⁜" + track.name + "⁜" + track.artist));
                        inlineKeyboardList.Add(ts);
                    }
                    var inline = new InlineKeyboardMarkup(inlineKeyboardList);
                    if (js.Count != 0)
                    {
                        await Bot.SendTextMessageAsync(message.From.Id, "Список фаворитов:", replyMarkup : inline);
                    }
                    else
                    {
                        await Bot.SendTextMessageAsync(message.From.Id, "Список фаворитов пуст.");
                    }

                    usrstatus.status1  = 0;
                    usrstatus.status2  = 2;
                    st.statuses[index] = usrstatus;

                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                    break;
                }

                default:
                {
                    break;
                }
                }
                break;
            }

            case 1:     //для getartistinfo
            {
                try
                {
                    st.statuses[index].status1 = 0;
                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                    userrr.Id     = Convert.ToString(message.From.Id);
                    userrr.Artist = e.Message.Text;

                    var json = JsonConvert.SerializeObject(userrr);
                    var data = new StringContent(json, Encoding.UTF8, "application/json");

                    HttpClient client   = new HttpClient();
                    var        response = await client.PostAsync($"https://mymusicbot.azurewebsites.net/api/Music/GetArtistInfo", data);

                    string result = response.Content.ReadAsStringAsync().Result;
                    ClassListArtistInfo useritem = JsonConvert.DeserializeObject <ClassListArtistInfo>(result);

                    await Bot.SendTextMessageAsync(message.From.Id, $"Artist: {useritem.artist.name}\n\nBio: {useritem.artist.bio.summary}.");

                    break;
                }
                catch
                {
                    await Bot.SendTextMessageAsync(message.From.Id, "Было введено некорректное имя исполнителя.");

                    break;
                }
            }

            case 2:     //для gettrack
            {
                st.statuses[index].status1 = 0;
                st.statuses[index].status2 = 1;

                userrr.Id   = Convert.ToString(message.From.Id);
                userrr.Name = e.Message.Text;

                var json = JsonConvert.SerializeObject(userrr);
                var data = new StringContent(json, Encoding.UTF8, "application/json");


                HttpClient client   = new HttpClient();
                var        response = await client.PostAsync($"https://mymusicbot.azurewebsites.net/api/Music/GetTrack", data);

                string       result   = response.Content.ReadAsStringAsync().Result;
                ClassResults useritem = JsonConvert.DeserializeObject <ClassResults>(result);

                List <List <InlineKeyboardButton> > inlineKeyboardList = new List <List <InlineKeyboardButton> >();


                foreach (var track in useritem.results.trackmatches.track)
                {
                    if (track.url.Length <= 60)
                    {
                        List <InlineKeyboardButton> ts = new List <InlineKeyboardButton>();
                        ts.Add(InlineKeyboardButton.WithUrl(track.name + " - " + track.artist, track.url));
                        ts.Add(InlineKeyboardButton.WithCallbackData("+", Convert.ToString(message.From.Id) + "⁜" + track.name + "⁜" + track.artist));
                        inlineKeyboardList.Add(ts);
                    }
                }
                var inline = new InlineKeyboardMarkup(inlineKeyboardList);
                if (result == "It's bad")
                {
                    await Bot.SendTextMessageAsync(message.From.Id, "Ничего не найдено. Попробуйте ввести название по-другому.");
                }
                else
                {
                    await Bot.SendTextMessageAsync(message.From.Id, "Найденные треки:", replyMarkup : inline);
                }
                File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                break;
            }

            default:
            {
                goto case 0;
            }
            }
        }
Beispiel #4
0
        public List <StudentResults> GetAll()
        {
            // Note - this is just for illustrative purposes.  Normally we'd go to a database to fetch these results.
            // This is to demonstrate a RESTful API in dotnet and its results.
            List <StudentResults> studentResultsList = new List <StudentResults>();

            StudentResults studentResultsClark = new StudentResults();

            studentResultsClark.Date         = DateTime.Now;
            studentResultsClark.FirstName    = "Super";
            studentResultsClark.LastName     = "Man";
            studentResultsClark.Id           = 1243454;
            studentResultsClark.SummaryNotes = "Your student is a pleasure to have in class";
            studentResultsClark.ClassResults = new List <ClassResults>();
            studentResultsClark.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsClark.Id, "Flying", 4.0f));
            studentResultsClark.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsClark.Id, "Laser beam vision", 3.1f));
            studentResultsClark.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsClark.Id, "Kryptonite Resistance", 0.1f));
            studentResultsList.Add(studentResultsClark);

            StudentResults studentResultsLois = new StudentResults();

            studentResultsLois.Date         = DateTime.Now;
            studentResultsLois.FirstName    = "Lois";
            studentResultsLois.LastName     = "Lane";
            studentResultsLois.Id           = 9834543;
            studentResultsLois.SummaryNotes = "Lois is smart and resourceful.";
            studentResultsLois.ClassResults = new List <ClassResults>();
            studentResultsLois.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsLois.Id, "Flying", 0.0f));
            studentResultsLois.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsLois.Id, "Journalism", 4.0f));
            studentResultsLois.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsLois.Id, "Establishing personal boundaries", 2.4f));
            studentResultsList.Add(studentResultsLois);

            StudentResults studentResultsGreenLantern = new StudentResults();

            studentResultsGreenLantern.Date         = DateTime.Now;
            studentResultsGreenLantern.FirstName    = "Green";
            studentResultsGreenLantern.LastName     = "Lantern";
            studentResultsGreenLantern.Id           = 0025214;
            studentResultsGreenLantern.SummaryNotes = "Green Lantern was a pleasure to have in class.  He might be a little too cocky for his own good but in the end he does the right thing.";
            studentResultsGreenLantern.ClassResults = new List <ClassResults>();
            studentResultsGreenLantern.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsGreenLantern.Id, "Flying", 3.2f));
            studentResultsGreenLantern.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsGreenLantern.Id, "Alter-Ego", 3.9f));
            studentResultsGreenLantern.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsGreenLantern.Id, "Green Power Ring", 4.0f));
            studentResultsList.Add(studentResultsGreenLantern);

            StudentResults studentResultsWonderWoman = new StudentResults();

            studentResultsWonderWoman.Date         = DateTime.Now;
            studentResultsWonderWoman.FirstName    = "Wonder";
            studentResultsWonderWoman.LastName     = "Woman";
            studentResultsWonderWoman.Id           = 0215487;
            studentResultsWonderWoman.SummaryNotes = "Wonder Woman brings true fighting spirit and the feeling of hope and unity to the classroom.";
            studentResultsWonderWoman.ClassResults = new List <ClassResults>();
            studentResultsWonderWoman.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsWonderWoman.Id, "Flying", 3.6f));
            studentResultsWonderWoman.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsWonderWoman.Id, "Bracers of Themiscyra", 4.0f));
            studentResultsWonderWoman.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsWonderWoman.Id, "Lasso of Hestia", 4.0f));
            studentResultsWonderWoman.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultsWonderWoman.Id, "Sword of the Amazons", 4.0f));
            studentResultsList.Add(studentResultsWonderWoman);

            StudentResults studentResultBatman = new StudentResults();

            studentResultBatman.Date         = DateTime.Now;
            studentResultBatman.FirstName    = "Bat";
            studentResultBatman.LastName     = "Man";
            studentResultBatman.Id           = 9854758;
            studentResultBatman.SummaryNotes = "Batman is the worlds best detective and a formidible opponent in combat.  I would encourage Batman to let his guard down and allow himself to be vulnerable and build positive, lasting relationships.";
            studentResultBatman.ClassResults = new List <ClassResults>();
            studentResultBatman.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultBatman.Id, "Batarang", 3.6f));
            studentResultBatman.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultBatman.Id, "Detective Skills - Advanced", 4.0f));
            studentResultBatman.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultBatman.Id, "Martial Arts", 3.7f));
            studentResultBatman.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultBatman.Id, "Building Loving Relationships", 1.7f));
            studentResultsList.Add(studentResultBatman);

            StudentResults studentResultTheFlash = new StudentResults();

            studentResultTheFlash.Date         = DateTime.Now;
            studentResultTheFlash.FirstName    = "The";
            studentResultTheFlash.LastName     = "Flash";
            studentResultTheFlash.Id           = 9854758;
            studentResultTheFlash.SummaryNotes = "Gets his work done quicky! (who would have thought?).  Could maybe take some time and slow down and appreciate life more.";
            studentResultTheFlash.ClassResults = new List <ClassResults>();
            studentResultTheFlash.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultTheFlash.Id, "Super-speed", 3.9f));
            studentResultTheFlash.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultTheFlash.Id, "Time shifting", 3.1f));
            studentResultTheFlash.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultTheFlash.Id, "Phasing", 3.5f));
            studentResultTheFlash.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultTheFlash.Id, "Art Appreciation", 1.2f));
            studentResultsList.Add(studentResultTheFlash);

            StudentResults studentResultGreenArrow = new StudentResults();

            studentResultGreenArrow.Date         = DateTime.Now;
            studentResultGreenArrow.FirstName    = "Green";
            studentResultGreenArrow.LastName     = "Arrow";
            studentResultGreenArrow.Id           = 9854758;
            studentResultGreenArrow.SummaryNotes = "Green Arrow is a wonderful student.  Bow skills are fantastic.  Martial arts prowess top notch.  Look forward to him finding a real super power.";
            studentResultGreenArrow.ClassResults = new List <ClassResults>();
            studentResultGreenArrow.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultGreenArrow.Id, "Archery", 4.0f));
            studentResultGreenArrow.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultGreenArrow.Id, "Martial Arts", 3.4f));
            studentResultGreenArrow.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultGreenArrow.Id, "Stealth", 3.8f));
            studentResultGreenArrow.ClassResults.Add(ClassResults.BuildClassResults(DateTime.Now, studentResultGreenArrow.Id, "Invulnerability", 0.2f));
            studentResultsList.Add(studentResultGreenArrow);

            return(studentResultsList);
        }