public async Task BeginJobModule(IStorageClient storageClient) { _storageClient = storageClient; _thousandsLists = new Dictionary <string, ThousandsList>(); // Get every file name in the main directory object lockObject = new object(); string[] allFileNames = _storageClient.GetAllFileNames(); Parallel.ForEach(allFileNames, async fileName => { string text = (await _storageClient.GetFileContents(fileName, FileType.Json)).Trim(); if (String.IsNullOrEmpty(text)) { return; } NYTQuery movies = null; try { movies = JsonConvert.DeserializeObject <NYTQuery>(text); } catch { } // Just continue for now if (movies == null || movies.results == null || movies.results.Count == 0) { return; } foreach (var movie in movies.results) { int yesNo; if (!int.TryParse(movie.thousand_best, out yesNo)) { continue; } DateTime date; if (!DateTime.TryParse(movie.publication_date, out date)) { continue; } string key = Convert.ToString(date.Date.Year); lock (lockObject) { if (!_thousandsLists.ContainsKey(key)) { _thousandsLists.Add(key, new ThousandsList { Yes = new Yes { Count = 0 }, No = new No { Count = 0 } }); } switch (yesNo) { case 0: _thousandsLists[key].No.Count++; break; case 1: _thousandsLists[key].Yes.Count++; break; } } } }); string json = JsonConvert.SerializeObject(_thousandsLists, Formatting.Indented); await _storageClient.WriteFileToStorageAsync(json, "thousands_list", FileType.Json, true); }
public async Task BeginJobModule(IStorageClient storageClient) { _storageClient = storageClient; _mpaaList = new Dictionary <string, MPAA>(); // Get every file name in the main directory object lockObject = new object(); string[] allFileNames = _storageClient.GetAllFileNames(); Parallel.ForEach(allFileNames, async fileName => { string text = (await _storageClient.GetFileContents(fileName, FileType.Json)).Trim(); if (String.IsNullOrEmpty(text)) { return; } NYTQuery movies = null; try { movies = JsonConvert.DeserializeObject <NYTQuery>(text); } catch { } // Just continue for now if (movies == null || movies.results == null || movies.results.Count == 0) { return; } foreach (var movie in movies.results) { DateTime date; if (!DateTime.TryParse(movie.publication_date, out date)) { continue; } string key = Convert.ToString(date.Date.Year); lock (lockObject) { if (!_mpaaList.ContainsKey(key)) { _mpaaList.Add(key, new MPAA { G = new G { Count = 0 }, PG = new PG { Count = 0 }, PG13 = new PG13 { Count = 0 }, R = new R { Count = 0 }, None = new None { Count = 0 } }); } switch (movie.mpaa_rating) { case "G": _mpaaList[key].G.Count++; break; case "PG": _mpaaList[key].PG.Count++; break; case "PG13": _mpaaList[key].PG13.Count++; break; case "R": _mpaaList[key].R.Count++; break; default: _mpaaList[key].None.Count++; break; } } } }); string json = JsonConvert.SerializeObject(_mpaaList, Formatting.Indented); await _storageClient.WriteFileToStorageAsync(json, "mpaa_analyzer", FileType.Json, true); }