コード例 #1
0
        /// <summary>
        /// Fetch lyrics from either local file cache or Internet.
        /// </summary>
        /// <param name="candidates">
        /// A list collection receiving online lyrics candidates.
        /// </param>
        /// <param name="ignoreCache">Do not read local cache.</param>
        /// <returns>Parsed lyrics</returns>
        public static async Task <ParsedLrc> FetchLyricsAsync(string title, string artist, IList <ExternalLrcInfo> candidates, string cacheName, bool ignoreCache = false)
        {
            if (candidates == null)
            {
                throw new ArgumentNullException(nameof(candidates));
            }

            var lrcFolder = await GetLyricsSaveFolderAsync();

            if (lrcFolder != null)
            {
                var existentFile = (await lrcFolder.TryGetItemAsync(cacheName)) as IStorageFile;
                if (existentFile != null && !ignoreCache)
                {
                    return(LrcParser.Parse(await existentFile.OpenStreamForReadAsync(), false));
                }
            }

            // No existent lrc file. Query for it on Internet.
            List <ExternalLrcInfo> sorted = new List <ExternalLrcInfo>();

            foreach (var s in SourceScriptManager.GetAllScripts())
            {
                var infos = (await s.LookupLrcAsync(title, artist));
                sorted.AddRange(infos);
            }

            sorted.Sort(new SimilarityComparer(title, artist));
            sorted.ForEach((info) => candidates.Add(info));

            return(candidates.Count > 0 ?
                   await FetchLyricsAsync(candidates[0], cacheName) : null);
        }
コード例 #2
0
        /// <summary>
        /// Fetch lyrics by <see cref="ExternalLrcInfo"/> from
        /// previous call of <see cref="FetchLyricsAsync(string, string, IList{ExternalLrcInfo})"/>.
        /// </summary>
        /// <param name="info">Obtained from previous call</param>
        /// <returns>Parsed lyrics</returns>
        public static async Task <ParsedLrc> FetchLyricsAsync(ExternalLrcInfo info, string cacheName)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            var source = info.Source;

            if (string.IsNullOrEmpty(source))
            {
                throw new ArgumentNullException(nameof(info.Source), "Where do you come from?");
            }

            var s       = SourceScriptManager.GetScript(source);
            var lrcText = await s.DownloadLrcAsync(info);

            await SaveLyricsInternalAsync(
                await GetLyricsSaveFolderAsync(),
                cacheName, lrcText);

            if (!string.IsNullOrEmpty(lrcText))
            {
                try
                {
                    return(LrcParser.Parse(lrcText));
                }
                catch { }
            }

            return(null);
        }
コード例 #3
0
        public static async Task <ParsedLrc> ImportLyricsAsync(string title, string artist, StorageFile lrcFile)
        {
            if (lrcFile == null)
            {
                throw new ArgumentNullException(nameof(lrcFile));
            }
            var lrcFolder = await GetLyricsSaveFolderAsync();

            if (lrcFolder != null)
            {
                await lrcFile.CopyAsync(lrcFolder,
                                        BuildLyricsFilename(title, artist),
                                        NameCollisionOption.ReplaceExisting);
            }

            return(LrcParser.Parse(await lrcFile.OpenStreamForReadAsync(), false));
        }