예제 #1
0
        private BuiltinArtistEntry FindBuiltInArtistInsideInArtistMetadata(string artist, string primaryLocale)
        {
            if (!IsInitialized)
            {
                return(null);
            }
            if (string.IsNullOrWhiteSpace(artist))
            {
                return(null);
            }

            BuiltinArtistEntry result = null;

            foreach (BuiltinArtistEntry builtinArtistEntry in builtinArtistEntries)
            {
                if (artist.Contains("(" + builtinArtistEntry.Name + ")"))
                {
                    result = builtinArtistEntry;
                    break;
                }

                if (builtinArtistEntry.AltNames != null)
                {
                    if (builtinArtistEntry.AltNames.Any(x => artist.Contains("(" + x.Name + ")")))
                    {
                        result = builtinArtistEntry;
                        break;
                    }
                }
            }

            return(result);
        }
예제 #2
0
        public BuiltinArtistEntry FindBuiltInArtist(string artistName, string stationLocale)
        {
            if (!IsInitialized)
            {
                return(null);
            }

            //Try and find an entry with the same name as the artist.
            BuiltinArtistEntry builtInMatch = builtinArtistEntries.FirstOrDefault(x => x.Name.ToLower().Equals(artistName.ToLower()));

            if (builtInMatch == null) //If we don't find a direct match, we'll need to do some digging.
            {
                builtInMatch = builtinArtistEntries.FirstOrDefault(x =>
                {
                    //Figure out if we should check using locale as an additional test case. If the country of origin (on the artist) or station locale (on the station) isn't defined, we just return true.
                    bool countryLocaleMatches = (!string.IsNullOrWhiteSpace(x.CountryOfOrigin) && !string.IsNullOrWhiteSpace(stationLocale) ? x.CountryOfOrigin.Equals(stationLocale) : true);


                    //string lastNameFirstNameSwappedName = string.Join(" ", artistName.Split(' ').Reverse()); //splices, reverses and joins: "Ayumi Hamasaki" -> ["Ayumi","Hamasaki"] -> ["Hamasaki", "Ayumi"] -> "Hamasaki Ayumi"

                    //Checks all alternative names listed for the artist to see if they roughly match.
                    return(x.AltNames != null ? (x.AltNames.Any(y => y.Name.ToLower().Equals(artistName.ToLower())) && countryLocaleMatches) : false);
                });
            }

            return(builtInMatch);
        }
        /// <summary>
        /// Finds an artist on JPopAsia.com.
        /// </summary>
        /// <param name="artistName">The name of the artist to search for.</param>
        /// <param name="stationLocale">The locale of the artist to search for.</param>
        /// <returns></returns>
        public static async Task <JPopAsiaArtistData> FindArtistDataOnJPopAsiaAsync(string artistName, string stationLocale)
        {
            //Sets up an http client and response object.
            HttpClient          http         = new HttpClient();
            HttpResponseMessage httpResponse = null;

            BuiltinArtistEntry builtInMatch = NepApp.MetadataManager.FindBuiltInArtist(artistName, stationLocale);

            //If we found a match, access it here.
            if (builtInMatch != null)
            {
                if (builtInMatch.JPopAsiaUrl != null)
                {
                    //Sends the request to the match.
                    httpResponse = await http.GetAsync(builtInMatch.JPopAsiaUrl);

                    if (httpResponse.IsSuccessStatusCode)
                    {
                        //The request was successful as noted by the response. Try and scrape the page here.
                        return(await ParseArtistPageForDataAsync(artistName, httpResponse));
                    }
                }
            }

            //Some artists are available directly by putting their name into the url. We create a URL for that here.
            Uri directUri = new Uri(
                string.Format("http://www.jpopasia.com/{0}/",
                              Uri.EscapeUriString(
                                  artistName.Replace(" ", "")
                                  .Replace("!", "")
                                  .Replace("-", "")
                                  .Replace("*", "")))); //try and use a direct url. this works for artists like "Superfly" or "Perfume"

            try
            {
                //Try to access the artist via a direct url.
                httpResponse = await http.GetAsync(directUri);

                if (httpResponse.IsSuccessStatusCode)
                {
                    //The artist was successfuly reached from the direct url, we can scrape the page here.
                    return(await ParseArtistPageForDataAsync(artistName, httpResponse));
                }
                else
                {
                    //We couldn't get to the artist from a direct url here, We're gonna have to search

                    //Manually search if we reach this point.
                    //TODO manually search.
                }
            }
            catch (Exception)
            {
                //Upon an error, return null.
                return(null);
            }
            finally
            {
                //Clean up http objects here.
                if (httpResponse != null)
                {
                    httpResponse.Dispose();
                    httpResponse = null;
                }
                http.Dispose();
            }

            //If all else fails, return null.
            return(null);
        }
예제 #4
0
        /// <summary>
        /// Retrieves a list of artists from BuiltinArtists.xml
        /// </summary>
        /// <returns>A list of artists.</returns>
        private async Task <BuiltinArtistEntry[]> LoadBuiltinArtistEntriesAsync()
        {
            XDocument xmlDoc = null;
            IRandomAccessStreamWithContentType reader = null; //local file only

            try
            {
                using (HttpClient http = new HttpClient())
                {
                    var xmlText = await http.GetStringAsync("https://raw.githubusercontent.com/Amrykid/Neptunium-ArtistsDB/master/BuiltinArtists.xml");

                    xmlDoc = XDocument.Parse(xmlText);
                }
            }
            catch (Exception)
            {
                //Opens up an XDocument for reading the xml file.

                var file = builtInArtistsFile;
                reader = await file.OpenReadAsync();

                xmlDoc = XDocument.Load(reader.AsStream());
            }

            //Creates a list to hold the artists.
            List <BuiltinArtistEntry> artists = new List <BuiltinArtistEntry>();

            //Looks through the <Artist> elements in the file, creating a BuiltinArtistEntry for each one.
            foreach (var artistElement in xmlDoc.Element("Artists").Elements("Artist"))
            {
                var artistEntry = new BuiltinArtistEntry();

                artistEntry.Name = artistElement.Attribute("Name").Value;

                if (artistElement.Attribute("JPopAsiaUrl") != null)
                {
                    artistEntry.JPopAsiaUrl = new Uri(artistElement.Attribute("JPopAsiaUrl").Value);
                }

                if (artistElement.Elements("AltName") != null)
                {
                    artistEntry.AltNames = artistElement.Elements("AltName").Select(altNameElement =>
                    {
                        string name  = altNameElement.Value;
                        string lang  = "en";
                        string sayAs = null;

                        if (altNameElement.Attribute("Lang") != null)
                        {
                            lang = altNameElement.Attribute("Lang").Value;
                        }

                        if (altNameElement.Attribute("SayAs") != null)
                        {
                            sayAs = altNameElement.Attribute("SayAs").Value;
                        }

                        return(new BuiltinArtistEntryAltName(name, lang, sayAs));
                    }).ToArray();
                }

                if (artistElement.Elements("Related") != null)
                {
                    artistEntry.RelatedArtists = artistElement.Elements("Related").Select(relatedElement =>
                    {
                        return(relatedElement.Value);
                    }).ToArray();
                }

                if (artistElement.Attribute("FanArtTVUrl") != null)
                {
                    artistEntry.FanArtTVUrl = new Uri(artistElement.Attribute("FanArtTVUrl").Value);
                }

                if (artistElement.Attribute("MusicBrainzUrl") != null)
                {
                    artistEntry.MusicBrainzUrl = new Uri(artistElement.Attribute("MusicBrainzUrl").Value);
                }

                if (artistElement.Attribute("OriginCountry") != null)
                {
                    artistEntry.CountryOfOrigin = artistElement.Attribute("OriginCountry").Value;
                }

                if (artistElement.Attribute("NameLanguage") != null)
                {
                    artistEntry.NameLanguage = artistElement.Attribute("NameLanguage").Value;
                }
                else
                {
                    artistEntry.NameLanguage = "en";
                }

                if (artistElement.Attribute("SayAs") != null)
                {
                    artistEntry.NameSayAs = artistElement.Attribute("SayAs").Value;
                }

                artistEntry.IsBuiltIn = true;

                //Adds the artist entry to the list.
                artists.Add(artistEntry);
            }

            //Clean up.
            xmlDoc = null;
            reader?.Dispose();

            return(artists.ToArray());
        }