Пример #1
0
 internal GeniusController(ITerminalConfiguration settings) : base(settings)
 {
     if (settings.GatewayConfig is GeniusConfig)
     {
         _gatewayConfig = settings.GatewayConfig as GeniusConfig;
     }
     else
     {
         throw new ConfigurationException("GatewayConfig must be of type GeniusConfig for this device type.");
     }
 }
Пример #2
0
 public GeniusHttpInterface(ITerminalConfiguration settings)
 {
     _settings      = settings;
     _gatewayConfig = settings.GatewayConfig as GeniusConfig;
 }
Пример #3
0
        /// <summary>
        /// Scrapes genius lyrics
        /// </summary>
        /// <param name="query">
        /// Any song query
        /// Should also return the most popular song of an artist if one is provided
        /// </param>
        /// <returns>
        /// The lyrics of the first result that is returned by the genius api for the search or null
        /// </returns>
        public async Task <string> ScrapeGeniusLyricsAsync(string query)
        {
            var config = Database.Load <GeniusConfig>(GeniusConfig.DocumentName());

            if (config == null || config.Authorization == null)
            {
                return(null);
            }

            try
            {
                var request = new HttpRequestMessage(HttpMethod.Get, $"https://api.genius.com/search?q={query}");
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", config.Authorization);

                //Use the genius api to make a song query.
                var search = await HttpClient.SendAsync(request);

                if (!search.IsSuccessStatusCode)
                {
                    return(null);
                }

                var token = JToken.Parse(await search.Content.ReadAsStringAsync());
                var hits  = token.Value <JToken>("response").Value <JArray>("hits");

                if (!hits.HasValues)
                {
                    return(null);
                }

                //Try to get the genius url of the lyrics page
                var first = hits.First();
                //Access the page qualifier of the first result that was returned
                var result  = first.Value <JToken>("result");
                var pathStr = result.Value <JToken>("path").ToString();

                //Load and scrape the web page content.
                var webHtml = await HttpClient.GetStringAsync($"https://genius.com{pathStr}");

                var doc = new HtmlDocument();
                doc.LoadHtml(webHtml);
                //Find the lyrics node if possible
                var lyricsDivs = doc.DocumentNode.SelectNodes("//div[contains(@class, 'lyrics')]");
                if (!lyricsDivs.Any())
                {
                    return(null);
                }

                var firstDiv = lyricsDivs.First();
                var text     = firstDiv.InnerText;

                //Filter out the spacing between verses
                var regex2 = new Regex("\n{2}");
                text = regex2.Replace(text, "\n");

                //strip out additional parts which are prefixed with or contain only spaces
                var regex3 = new Regex("\n +");
                text = regex3.Replace(text, "");
                //Fix up the bracketed content that are at the start of verses
                text = text.Replace("[", "\n[");
                text = text.Replace("&amp;", "&", StringComparison.InvariantCultureIgnoreCase);

                //Strip the additional genius content found at the end of the lyrics
                var indexEnd = text.IndexOf("More on genius", StringComparison.InvariantCultureIgnoreCase);
                if (indexEnd != -1)
                {
                    text = text.Substring(0, indexEnd);
                }

                //Remove additional whitespace at the start and end of the response
                text = text.Trim();

                return(text);
            }
            catch
            {
                return(null);
            }
        }