Esempio n. 1
0
 private void OnNowPlayingChanged(NowPlayingInfo newInfo)
 {
     if (_lastNowPlayingInfo != newInfo &&
         _lastNowPlayingInfo?.IsEquivalentTo(newInfo) != true)
     {
         NowPlayingChanged?.Invoke(this, new NowPlayingChangedEventArgs(_lastNowPlayingInfo, newInfo));
         _lastNowPlayingInfo = newInfo;
     }
 }
Esempio n. 2
0
        private async void TimerCallback(object sender, ElapsedEventArgs args)
        {
            var timeoutCancellationTokenSource = new CancellationTokenSource(_configuration.PollingInterval * 5);
            CancellationTokenSource cancellationTokenSource;

            if (_cancellationTokenSource != null)
            {
                cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
                    _cancellationTokenSource.Token,
                    timeoutCancellationTokenSource.Token);
            }
            else
            {
                cancellationTokenSource = timeoutCancellationTokenSource;
            }

            NowPlayingInfo nowPlayingInfo = null;

            try
            {
                var web = new HtmlWeb();
                var doc = await web.LoadFromWebAsync(
                    _configuration.SourceUrl,
                    Encoding.UTF8,
                    cancellationTokenSource.Token);

                var infoDiv = doc.DocumentNode.FirstDescendantWithClass("div", "card horizontal");
                if (infoDiv != null)
                {
                    var imgUrl = infoDiv.Descendants("img").FirstOrDefault()?.GetAttributeValue("src", default(string));

                    var content = infoDiv.FirstDescendantWithClass("div", "card-content")?.InnerText;
                    if (content != null)
                    {
                        var lines = content
                                    .Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                                    .Select(x => x.Trim())
                                    .Take(2)
                                    .ToList();

                        var song   = lines[0];
                        var artist = lines[1];
                        nowPlayingInfo = new NowPlayingInfo(artist, song, imgUrl);
                    }
                }
            }
            catch (TaskCanceledException)
            {
                // Ignore - these are expected during timeouts or while quitting
            }
            catch (Exception ex)
            {
                _logger?.Log(ex);
            }
            finally
            {
                timeoutCancellationTokenSource.Dispose();
                cancellationTokenSource.Dispose();
            }
            OnNowPlayingChanged(nowPlayingInfo);
        }