/// <summary> /// Asynchronously downloads the requested information from the JSON api and deserializes the response to the requested <typeparam name="TE">Type</typeparam>. /// This method can save the response to local cache, if a valid path is passed in <paramref name="saveCacheFile"/>. /// </summary> /// <typeparam name="TE">Target type</typeparam> /// <param name="url">Url to download</param> /// <param name="saveCacheFile">Optional name for saving response to cache</param> /// <returns>Downloaded object</returns> public async Task <TE> DownloadAsync <TE>(string url, string saveCacheFile = null, bool allowCached = true) { var writeLock = !string.IsNullOrEmpty(saveCacheFile) ? await _jsonLock.WriterLockAsync(saveCacheFile).ConfigureAwait(false) : null; using (writeLock) { if (allowCached) { TE cached = ReadCacheInternal <TE>(saveCacheFile); if (cached != null) { return(cached); } } string json = await DownloadJSON(url).ConfigureAwait(false); if (string.IsNullOrEmpty(json)) { return(default(TE)); } //Console.WriteLine("JSON: {0}", json); if (!string.IsNullOrEmpty(saveCacheFile)) { WriteCache(saveCacheFile, json); } return(JsonConvert.DeserializeObject <TE>(json)); } }
public async Task <bool> TrySaveFanArt(Guid mediaItemId, string title, string fanArtType, TrySaveFanArtAsyncDelegate saveDlgt) { string fanArtCacheDirectory = GetFanArtDirectory(mediaItemId); string fanArtTypeSubDirectory = GetFanArtTypeDirectory(fanArtCacheDirectory, fanArtType); using (var writer = await _fanArtSync.WriterLockAsync(mediaItemId).ConfigureAwait(false)) { if (!await InitCache(fanArtCacheDirectory, fanArtTypeSubDirectory, title).ConfigureAwait(false)) { return(false); } FanArtCount currentCount = await _fanArtCounts.GetValue(CreateFanArtTypeId(mediaItemId, fanArtType), _ => CreateFanArtCount(mediaItemId, fanArtType)).ConfigureAwait(false); if (currentCount.Count < GetMaxFanArtCount(fanArtType) && await saveDlgt(fanArtTypeSubDirectory).ConfigureAwait(false)) { currentCount.Count++; return(true); } } return(false); }
/// <summary> /// Donwload a file from given <paramref name="url"/> and save it to <paramref name="downloadFile"/>. /// </summary> /// <param name="url">Url to download</param> /// <param name="downloadFile">Target file name</param> /// <returns><c>true</c> if successful</returns> public async Task <bool> DownloadFileAsync(string url, string downloadFile) { if (string.IsNullOrEmpty(downloadFile)) { return(false); } if (File.Exists(downloadFile)) { return(true); } using (await _fileLock.WriterLockAsync(downloadFile).ConfigureAwait(false)) { try { if (File.Exists(downloadFile)) { return(true); } byte[] data = null; using (WebClient webClient = new CompressionWebClient()) { foreach (KeyValuePair <string, string> headerEntry in Headers) { webClient.Headers[headerEntry.Key] = headerEntry.Value; } data = await webClient.DownloadDataTaskAsync(url).ConfigureAwait(false); } if (data?.LongLength > 0) { using (FileStream sourceStream = new FileStream(downloadFile, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true)) await sourceStream.WriteAsync(data, 0, data.Length); return(true); } ServiceRegistration.Get <ILogger>().Warn("OnlineLibraries.Downloader: No data received when downloading file {0} from {1}", downloadFile, url); return(false); } catch (Exception ex) { ServiceRegistration.Get <ILogger>().Warn("OnlineLibraries.Downloader: Exception when downloading file {0} from {1} ({2})", downloadFile, url, ex.Message); return(false); } } }