Exemplo n.º 1
0
        private async Task InitMoviesAsync() {
            if (File.Exists(MoviesPath)) {
                try {
                    string fileData;
                    using (var reader = File.OpenText(MoviesPath)) {
                        fileData = await reader.ReadToEndAsync();
                    }
                    JsonConvert.PopulateObject(fileData, _movies);
                    _logger.Debug("Movies loaded");
                }
                catch (Exception ex) {
                    _logger.Error(ex, "Loading movies failed");
                }
            }

            var assetClient = new AssetClient();
            var assets = await assetClient.GetAssetsAsync();
            var changed = false;
            foreach (var asset in assets) {
                if (_movies.All(a => a.AssetId != asset.Id)) {
                    var movie = new Movie {
                        AssetId = asset.Id,
                        DownloadUrl = asset.Url,
                        Location = asset.AccessibilityLabel,
                        TimeOfDay = asset.TimeOfDay
                    };
                    _movies.Add(movie);
                    changed = true;
                }
            }

            if (Settings.Instance.ShouldCacheMovies) {
                foreach (var movie in _movies.Where(m => string.IsNullOrEmpty(m.LocalPath))) {
                    var localPath = GetLocalPathForMovie(movie);
                    if (File.Exists(localPath)) {
                        movie.LocalPath = localPath;
                        changed = true;
                    }
                }

                changed |= DownloadMovies();
            }

            if (changed) {
                Save();
            }
        }
Exemplo n.º 2
0
 private string GetLocalPathForMovie(Movie movie) => Path.Combine(Settings.Instance.CachePath, Path.GetFileName(movie.DownloadUrl));