Exemplo n.º 1
0
 public Task <bool> Update(CancellationToken cancellationToken = default(CancellationToken))
 {
     return(Task.Run(async() =>
     {
         try
         {
             // TODO: Implement Login to pixiv.
             _logger.Info("Get data from " + LiveListUrl);
             var ret = await _client.GetAsync(LiveListUrl, cancellationToken);
             _logger.Info("Wait response");
             if (!ret.IsSuccessStatusCode)
             {
                 throw new Exception($"HTTP Error:{ret.StatusCode}");
             }
             using (var html = await ret.Content.ReadAsStreamAsync())
             {
                 _logger.Info("Complete");
                 // Parsing HTML
                 LiveItem[] ProcessedLives = ProcessHtml(html);
                 // Update lives
                 // Remove old, Enumerate after remove operation.
                 var removed = Lives.Where(x => !ProcessedLives.Contains(x)).ToArray();
                 removed.All(x => Lives.Remove(x));
                 // Add new lives
                 var added = ProcessedLives.Where(x => !Lives.Contains(x));
                 _logger.Info($"Remove {removed.Length} Add {added.Count()}");
                 Lives.AddRange(added);
             }
             return true;
         }
         catch (Exception ex)
         {
             _logger.Error(ex);
             return false;
         }
     }));
 }
Exemplo n.º 2
0
        public Task <bool> Update(CancellationToken cancellationToken = default(CancellationToken))
        {
            return(Task.Run(() =>
            {
                List <LiveItem> ProcessedLives = new List <LiveItem>();

                try
                {
                    _logger.Info("Get Data");
                    if (!System.IO.File.Exists(_TestFile))
                    {
                        return false;
                    }
                    using (System.IO.StreamReader s = new System.IO.StreamReader(_TestFile))
                    {
                        dynamic d = DynamicJson.Parse(s.ReadToEnd());
                        foreach (var i in d.streams)
                        {
                            ProcessedLives.Add(new LiveItem(i.id, DateTime.Now, new Uri(i.url), new string[] { i.title, i.user_name }));
                        }
                    }
                    // Update lives
                    // Remove old
                    Lives.Where(x => !ProcessedLives.Contains(x)).ToArray().All(x => Lives.Remove(x));
                    // Add new lives
                    Lives.AddRange(ProcessedLives.Where(x => !Lives.Contains(x)));
                    _logger.Info("JSON Parse End");
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                    return false;
                }
                return true;
            }));
        }