private void UpdateChords(string[] token, SuiteСhord suiteСhordToUpdate)
        {
            if (token == null)
            {
                suiteСhordToUpdate.Fingerings = new List <Fingering>();
                return;
            }

            var selectedChordsHS    = new HashSet <string>(token);
            var suiteChordFingering = new HashSet <string>
                                          (suiteСhordToUpdate.Fingerings.Select(c => c.Name));

            foreach (var chord in db.Fingerings)
            {
                if (selectedChordsHS.Contains(chord.Name.ToString()))
                {
                    if (!suiteChordFingering.Contains(chord.Name))
                    {
                        suiteСhordToUpdate.Fingerings.Add(chord);
                    }
                }
                else
                {
                    if (suiteChordFingering.Contains(chord.Name))
                    {
                        suiteСhordToUpdate.Fingerings.Remove(chord);
                    }
                }
            }
        }
        public ActionResult UpdateSong(int id)
        {
            string token = string.Join(",", db.Fingerings.Select(x => x.Name).ToList());

            @ViewBag.Fingerings = token.ToString().Split(',');
            SuiteСhord song = db.SuiteСhords.First(s => s.SuiteСhordId == id);

            return(View("UpdateSong", song));
        }
        public static void GetSongs(HtmlDocument doc, string link)
        {
            HtmlWeb hw        = new HtmlWeb();
            var     repeaters = doc.DocumentNode.SelectNodes("//table[@id='tablesort']/tr");//*[@id="tablesort"]/tbody/tr

            if (repeaters != null)
            {
                foreach (var repeater in repeaters)
                {
                    if (repeater != null)
                    {
                        string name       = repeater.SelectSingleNode(".//td/a/text()").InnerText;
                        var    rep        = repeater.SelectSingleNode(".//td/a[@href]");
                        string linkToText = "http:" + rep.Attributes["href"].Value.Substring(0, rep.Attributes["href"].Value.Length - 1);
                        doc = hw.Load(linkToText);
                        if (doc.DocumentNode.InnerText == "Too Many Requests")
                        {
                            Thread.Sleep(5000);
                            doc = hw.Load(linkToText);
                        }
                        using (var context = new ApplicationDbContext())
                        {
                            rep = repeater.SelectSingleNode(".//td[@class='number icon']/i[@class='fa fa-youtube-play']");
                            if (!context.SuiteСhords.Any(p => p.LinkToText == linkToText))
                            {
                                string video      = GetVideo(rep, doc);
                                string countViews = repeater.SelectSingleNode(".//td[@class = 'number hidden-phone']").InnerText;
                                string text       = GetText(doc);
                                context.SuiteСhords.Add(new SuiteСhord(name, countViews, video, text, linkToText, context.Singers.Where(p => p.LinkToSinger == link).First()));
                                context.SaveChanges();
                            }
                            else
                            {
                                SuiteСhord suiteChord = context.SuiteСhords.First(p => p.LinkToText == linkToText);
                                if (suiteChord.Text == null)
                                {
                                    context.Entry(suiteChord).State = EntityState.Modified;
                                    suiteChord.Text = GetText(doc);
                                    context.SaveChanges();
                                }
                                if (suiteChord.Video == null)
                                {
                                    context.Entry(suiteChord).State = EntityState.Modified;
                                    suiteChord.Video = GetVideo(rep, doc);
                                    context.SaveChanges();
                                }
                            }
                            int id = context.SuiteСhords.First(p => p.LinkToText == linkToText).SuiteСhordId;
                            ListFingerings.GetFingering(doc, id);
                        }
                    }
                }
            }
        }
        public ActionResult UpdateSong(int?id, string text, string tokenfield)
        {
            string[] token = tokenfield.ToString().Replace("  ", string.Empty).Split(',');
            for (int i = 0; i < token.Length; i++)
            {
                token[i] = token[i].Trim();
            }
            SuiteСhord suiteСhordToUpdate = db.SuiteСhords.First(s => s.SuiteСhordId == id);

            suiteСhordToUpdate.Text = text;
            UpdateChords(token, suiteСhordToUpdate);
            db.SaveChanges();
            return(RedirectToAction("Song", new { id = suiteСhordToUpdate.SingerId, ids = suiteСhordToUpdate.SuiteСhordId }));
        }
Ejemplo n.º 5
0
        public static void GetFingering(HtmlDocument doc, int id)
        {
            var repeaters = doc.DocumentNode.SelectNodes("//div[@id='song_chords']/img");

            if (repeaters != null)
            {
                foreach (var repeater in repeaters)
                {
                    if (repeater != null)
                    {
                        string name = repeater.Attributes["alt"].Value;
                        name = name.Substring(7, name.Length - 7);
                        using (var context = new ApplicationDbContext())
                        {
                            if (!context.Fingerings.Any(p => p.Name == name))
                            {
                                string     picture    = "http:" + repeater.Attributes["src"].Value;
                                SuiteСhord suiteChord = context.SuiteСhords.First(p => p.SuiteСhordId == id);
                                Fingering  fingering  = context.Fingerings.Add(new Fingering(name, picture, suiteChord));
                                context.SaveChanges();
                                context.Entry(suiteChord).State = EntityState.Modified;
                                suiteChord.Fingerings.Add(fingering);
                                context.SaveChanges();
                            }
                            else
                            {
                                //string picture = "http:" + repeater.SelectSingleNode(".//img[@src]").Attributes["src"].Value;
                                Fingering  fingering  = context.Fingerings.First(p => p.Name == name);
                                SuiteСhord suiteChord = context.SuiteСhords.First(p => p.SuiteСhordId == id);
                                context.Entry(suiteChord).State = EntityState.Modified;
                                context.Entry(fingering).State  = EntityState.Modified;
                                suiteChord.Fingerings.Add(fingering);
                                fingering.SuiteСhords.Add(suiteChord);
                                context.SaveChanges();
                            }
                            context.SaveChanges();
                        }
                    }
                }
            }
        }