public void ProcessMetaDatas(List <MetaData> metadatas) { //process genres foreach (MetaData meta in metadatas) { foreach (string genre in meta.Genres) { if (!GenreColumnMap.ContainsKey(genre)) //if not already in map { GenreColumnMap.Add(genre, GenreColumnCount++); } } } //process ways to watch foreach (MetaData meta in metadatas) { WayToWatch watch = meta.WayToWatch; foreach (OptionGroup group in watch.Groups) { foreach (WatchOption option in group.Options) { string optionStr = $"{group.DisplayName} {option.Primary}"; if (!WayToWatchColumnMap.ContainsKey(optionStr)) //if not already in map { WayToWatchColumnMap.Add(optionStr, WayToWatchColumnCount++); } } } } // COLUMNS: //Id //Title //StartYear //EndYear //NumEpisodes //RunningTime //Rating //Rating Count //Certificate //Genre columns //Ways to watch columns MainTable = new String[metadatas.Count + 1, 9 + GenreColumnCount + WayToWatchColumnCount]; //generate headings int count = 0; MainTable[0, count++] = "Id"; MainTable[0, count++] = "Title"; MainTable[0, count++] = "StartYear"; MainTable[0, count++] = "EndYear"; MainTable[0, count++] = "NumEpisodes"; MainTable[0, count++] = "RunningTime"; MainTable[0, count++] = "Rating"; MainTable[0, count++] = "RatingCount"; MainTable[0, count++] = "Certificate"; foreach (String genre in GenreColumnMap.Keys) { MainTable[0, count++] = genre; } foreach (String wayToWatch in WayToWatchColumnMap.Keys) { MainTable[0, count++] = wayToWatch; } //add data int rowNum = 1; foreach (MetaData meta in metadatas) { count = 0; MainTable[rowNum, count++] = meta.Id; MainTable[rowNum, count++] = meta.Title; MainTable[rowNum, count++] = meta.StartYear.ToString(); MainTable[rowNum, count++] = meta.EndYear.ToString(); MainTable[rowNum, count++] = meta.NumEpisodes.ToString(); MainTable[rowNum, count++] = meta.AverageRunningTimeMinutes.ToString(); MainTable[rowNum, count++] = meta.Rating.ToString(); MainTable[rowNum, count++] = meta.RatingCount.ToString(); MainTable[rowNum, count++] = meta.Certificate; for (int i = 0; i < meta.Genres.Length; i++) { int colInd = -1; if (GenreColumnMap.TryGetValue(meta.Genres[i], out colInd)) { MainTable[rowNum, count + colInd] = (i + 1).ToString(); } } count += GenreColumnCount; foreach (OptionGroup group in meta.WayToWatch.Groups) { foreach (WatchOption watchOption in group.Options) { int colInd = -1; if (WayToWatchColumnMap.TryGetValue($"{group.DisplayName} {watchOption.Primary}", out colInd)) { MainTable[rowNum, count + colInd] = watchOption.Secondary; } } } count += WayToWatchColumnCount; rowNum++; } }
static MetaData ReadMetaData(String text) { int startIndex = 0, endIndex = 0; var objectName = "title"; var titleObject = GetObjectJSON(objectName, text, out startIndex, out endIndex); text = text.Substring(endIndex); objectName = "ratings"; var ratingsObject = GetObjectJSON(objectName, text, out startIndex, out endIndex); text = text.Substring(endIndex); objectName = "metacritic"; var metacriticObject = GetObjectJSON(objectName, text, out startIndex, out endIndex); text = text.Substring(endIndex); objectName = "releaseDate"; var releaseDate = GetValue(objectName, text, out startIndex, out endIndex); text = text.Substring(endIndex); objectName = "popularity"; var popularity = GetObjectJSON(objectName, text, out startIndex, out endIndex); text = text.Substring(endIndex); objectName = "waysToWatch"; var waysToWatch = GetObjectJSON(objectName, text, out startIndex, out endIndex); text = text.Substring(endIndex); objectName = "genres"; var genres = GetArrayJSON(objectName, text, out startIndex, out endIndex); text = text.Substring(endIndex); var genreArray = genres.Split(","); for (int i = 0; i < genreArray.Length; i++) { genreArray[i] = Clean(genreArray[i]); } objectName = "certificate"; var certificate = GetValue(objectName, text, out startIndex, out endIndex); text = text.Substring(endIndex); //Get values //TV Show String imdbID = GetValue("id", titleObject); String title = GetValue("title", titleObject); String startYear = GetValue("seriesStartYear", titleObject); String endYear = GetValue("seriesEndYear", titleObject); String numEpisodes = GetValue("numberOfEpisodes", titleObject); String runningTime = GetValue("runningTimeInMinutes", titleObject); //Rating String rating = GetValue("rating", ratingsObject); String ratingCount = GetValue("ratingCount", ratingsObject); //WaysToWatch WayToWatch ways = new WayToWatch(); ways.Id = GetValue("id", waysToWatch); List <OptionGroup> optionGroups = new List <OptionGroup>(); //Streaming options String options = GetArrayJSON("optionGroups", waysToWatch); options = RemoveArrayBrackets(options); while (options.Length > 0) //loop until it breaks. Exception driven development { string thisOption = GetFirstObjectFrom(options, 0); options = options.Substring(thisOption.Length); OptionGroup group = new OptionGroup(); group.DisplayName = GetValue("displayName", thisOption); String watchOptions = GetArrayJSON("watchOptions", thisOption); watchOptions = RemoveArrayBrackets(watchOptions); List <WatchOption> watchOptionsList = new List <WatchOption>(); while (watchOptions.Length > 0) { string thisWatchOption = GetFirstObjectFrom(watchOptions, 0); watchOptions = watchOptions.Substring(thisWatchOption.Length); var watchOption = new WatchOption(); watchOption.Primary = GetValue("primaryText", thisWatchOption); watchOption.Secondary = GetValue("secondaryText", thisWatchOption); var linkJSON = GetObjectJSON("link", thisWatchOption); watchOption.UriLink = GetValue("uri", linkJSON); watchOptionsList.Add(watchOption); } group.Options = watchOptionsList; optionGroups.Add(group); } ways.Groups = optionGroups; MetaData metadata = new MetaData() { Id = imdbID, Title = title, StartYear = Int32.Parse(startYear), EndYear = Int32.Parse(endYear), NumEpisodes = Int32.Parse(numEpisodes), AverageRunningTimeMinutes = Int32.Parse(runningTime), Rating = Decimal.Parse(rating), RatingCount = Int64.Parse(ratingCount), WayToWatch = ways, Genres = genreArray, Certificate = certificate }; return(metadata); }