Beispiel #1
0
 private void AssertArtists(NicoTitleParseResult result, params string[] artists)
 {
     Assert.AreEqual(artists.Length, result.Artists.Count, "Number of artists");
     foreach (var artist in artists)
     {
         Assert.IsTrue(result.Artists.Any(a => a.DefaultName == artist), string.Format("Has artist {0}", artist));
     }
 }
Beispiel #2
0
 private void AssertArtists(NicoTitleParseResult result, params string[] artists)
 {
     result.Artists.Count.Should().Be(artists.Length, "Number of artists");
     foreach (var artist in artists)
     {
         result.Artists.Any(a => a.DefaultName == artist).Should().BeTrue($"Has artist {artist}");
     }
 }
Beispiel #3
0
        /// <summary>
        /// Parse song and find duplicates in the database.
        ///
        /// Duplicates are searched based on the entered names and PVs.
        /// Additionally, if PVs are provided, those are parsed for song information such as title and artists.
        /// Titles from the PVs will be used for name matching as well.
        /// </summary>
        /// <param name="anyName">Song names to be searched for duplicates. Cannot be null. Can be empty.</param>
        /// <param name="anyPv">List of PVs to be searched for duplicates and parsed for song info. Cannot be null. Can be empty.</param>
        /// <param name="getPVInfo">Whether to load song metadata based on the PVs. If this is false, only matching for duplicates is done.</param>
        /// <returns>Result of the check. Cannot be null.</returns>
        public NewSongCheckResultContract FindDuplicates(string[] anyName, string[] anyPv, bool getPVInfo)
        {
            var names       = anyName.Where(n => !string.IsNullOrWhiteSpace(n)).Select(n => n.Trim()).ToArray();
            var firstNicoPV = getPVInfo ? anyPv.FirstOrDefault(p => VideoService.NicoNicoDouga.IsValidFor(p)) : null;             // For downloading video info

            // Parse PV URLs (gets ID and service for each PV). Metadata will be parsed only for the first Nico PV, and only if it's needed.
            var pvs = anyPv.Select(p => pvParser.ParseByUrl(p, getPVInfo && p == firstNicoPV, PermissionContext)).Where(p => p.IsOk).ToArray();

            if (!names.Any() && !pvs.Any())
            {
                return(new NewSongCheckResultContract());
            }

            return(HandleQuery(ctx => {
                NicoTitleParseResult titleParseResult = null;
                if (getPVInfo)
                {
                    var nicoPV = pvs.FirstOrDefault(p => p.Service == PVService.NicoNicoDouga);

                    titleParseResult = ParseNicoPV(ctx.OfType <PVForSong>(), nicoPV);

                    if (titleParseResult != null && !string.IsNullOrEmpty(titleParseResult.Title))
                    {
                        names = names.Concat(new[] { titleParseResult.Title }).ToArray();
                    }
                }

                var nameMatchIds = (names.Any() ? ctx.OfType <SongName>().Query()
                                    .Where(n => names.Contains(n.Value) && !n.Song.Deleted)
                                    .Select(n => n.Song.Id)
                                    .OrderBy(s => s)
                                    .Distinct()
                                    .Take(10)
                                    .ToArray() : new int[0]);

                var nameMatches = (nameMatchIds.Any() ? ctx.Query().Where(s => nameMatchIds.Contains(s.Id)).ToArray()
                                   .Select(d => new Tuple <Song, SongMatchProperty>(d, SongMatchProperty.Title)) : new Tuple <Song, SongMatchProperty>[] { });

                var pvMatches = pvs.Select(pv => ctx.OfType <PVForSong>().Query()
                                           .Where(p => p.PVId == pv.Id && p.Service == pv.Service)
                                           .Select(n => n.Song)
                                           .FirstOrDefault(n => !n.Deleted))
                                .Where(p => p != null)
                                .Select(d => new Tuple <Song, SongMatchProperty>(d, SongMatchProperty.PV));


                var matches = pvMatches.Union(nameMatches, new SongTupleEqualityComparer <SongMatchProperty>())
                              .Select(s => new DuplicateEntryResultContract <SongMatchProperty>(new EntryRefWithCommonPropertiesContract(s.Item1, PermissionContext.LanguagePreference), s.Item2))
                              .ToArray();

                return new NewSongCheckResultContract(matches, titleParseResult, PermissionContext.LanguagePreference);
            }));
        }
        public NewSongCheckResultContract(DuplicateEntryResultContract <SongMatchProperty>[] matches, NicoTitleParseResult titleParseResult, ContentLanguagePreference languagePreference)
        {
            Matches = matches;

            if (titleParseResult != null)
            {
                Artists       = titleParseResult.Artists.Where(a => a != null).Select(a => new ArtistContract(a, languagePreference)).ToArray();
                SongType      = titleParseResult.SongType;
                Title         = titleParseResult.Title;
                TitleLanguage = titleParseResult.TitleLanguage;
            }
        }