private async Task TestItem(ChannelItemInfo item, ListingResults results, CancellationToken cancellationToken) { foreach (var media in (item.MediaSources ?? new List<ChannelMediaInfo>())) { try { await TestMediaInfo(media, results, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { _logger.ErrorException("Error testing media info", ex); } } }
private async Task TestMediaInfo(ChannelMediaInfo item, ListingResults results, CancellationToken cancellationToken) { var key = item.Path; if (string.IsNullOrEmpty(key)) { return; } ListingResult result; if (results.TestResults.TryGetValue(key, out result)) { // Was validated recently if (result.IsValid && (DateTime.UtcNow - result.DateTested).TotalDays <= 30) { return; } // Already known to be bad if (!result.IsValid) { return; } } var options = new HttpRequestOptions { Url = item.Path, CancellationToken = cancellationToken, BufferContent = false }; foreach (var header in item.RequiredHttpHeaders) { options.RequestHeaders[header.Key] = header.Value; } var isOk = false; try { var response = await _httpClient.SendAsync(options, "HEAD").ConfigureAwait(false); try { if (response.ContentType.StartsWith("video", StringComparison.OrdinalIgnoreCase)) { isOk = true; } } finally { response.Content.Dispose(); } } catch (HttpException ex) { if (!ex.StatusCode.HasValue) { throw; } if (ex.StatusCode.Value != HttpStatusCode.NotFound && ex.StatusCode.Value != HttpStatusCode.Forbidden) { throw; } } results.TestResults[key] = new ListingResult { IsValid = isOk, DateTested = DateTime.UtcNow }; }
private async Task TestItems(IEnumerable<ChannelItemInfo> items, ListingResults results, CancellationToken cancellationToken) { foreach (var item in items) { await TestItem(item, results, cancellationToken).ConfigureAwait(false); } }
private async Task<ListingResults> TestResults(IEnumerable<ChannelItemInfo> items, string savePath, CancellationToken cancellationToken) { ListingResults results = null; try { using (var stream = GetType().Assembly.GetManifestResourceStream(GetType().Namespace + ".Listings.testresults.txt")) { if (stream != null) { results = _json.DeserializeFromStream<ListingResults>(stream); } } } catch { } if (results == null) { results = new ListingResults(); } await TestItems(items, results, cancellationToken).ConfigureAwait(false); await _fileLock.WaitAsync(cancellationToken).ConfigureAwait(false); try { _json.SerializeToFile(results, Path.Combine(savePath, "testresults.txt")); } finally { _fileLock.Release(); } return results; }
private bool Filter(ChannelMediaInfo item, ListingResults results) { var key = item.Path; if (string.IsNullOrEmpty(key)) { return false; } ListingResult result; if (results.TestResults.TryGetValue(key, out result)) { return result.IsValid; } // Should never get here. return false return false; }
private bool Filter(ChannelItemInfo item, ListingResults results) { item.MediaSources = item.MediaSources .Where(i => Filter(i, results)) .ToList(); return item.MediaSources.Count > 0; }
private List<ChannelItemInfo> CreateFilteredListings(IEnumerable<ChannelItemInfo> items, ListingResults results) { return items.Where(i => Filter(i, results)).ToList(); }