internal IDictionary <string, IDictionary <string, SDStationScheduleResponse> > GetStationSchedules(ISet <string> stationIDs) { Console.WriteLine("Reading schedules from local DB"); Dictionary <string, IDictionary <string, SDStationScheduleResponse> > stationSchedules = new Dictionary <string, IDictionary <string, SDStationScheduleResponse> >(); foreach (string stationID in stationIDs) { stationSchedules[stationID] = new Dictionary <string, SDStationScheduleResponse>(); } using (SQLiteCommand command = new SQLiteCommand( string.Format("select {1}, {2} from {0};", kSchedulesTableName, kStationIDColumnName, kScheduleEntriesColumnName), connection_)) { SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) { string stationID = reader[0].ToString(); if (stationIDs.Contains(stationID)) { SDStationScheduleResponse dailyScheduleResponse = JSONClient.Deserialize <SDStationScheduleResponse>(reader[1].ToString()); stationSchedules[stationID][dailyScheduleResponse.metadata.startDate] = dailyScheduleResponse; } } } return(stationSchedules); }
// Note: image IDs should be used here, not program IDs. public IDictionary <string, SDProgramImageResponse> GetImagesByIds(ISet <string> imageIDs) { Console.WriteLine("Reading program images from DB"); Dictionary <string, SDProgramImageResponse> imagesByID = new Dictionary <string, SDProgramImageResponse>(); foreach (string id in imageIDs) { imageIDs.Add(Misc.LimitString(id, 10)); } using (SQLiteCommand command = new SQLiteCommand(string.Format("select {1}, {2} from {0};", kImagesTableName, kImageIdColumnName, kImageDataColumnName), connection_)) { SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) { string id = reader[0].ToString(); if (imageIDs.Contains(id)) { imagesByID[id] = JSONClient.Deserialize <SDProgramImageResponse>(reader[1].ToString()); } } } return(imagesByID); }
private ConfigManager(string config_path) { this.config_path_ = config_path; config_ = new SDGrabberConfig(); try { using (Stream input_stream = File.OpenRead(config_path_)) { using (StreamReader reader = new StreamReader(input_stream)) { config_ = JSONClient.Deserialize <SDGrabberConfig>(reader.ReadToEnd()); } } if (config_.lineups == null) { config_.lineups = new List <LineupConfig>(); } if (config_.scanned_lineups == null) { config_.scanned_lineups = new List <ScannedLineupConfig>(); } config_.ValidateScannedLineupConfigs(); } catch (Exception ex) { Misc.OutputException(ex); } }
private List <DBProgram> DownloadPrograms(IEnumerable <string> programIDs) { const int kBatchSize = 5000; List <string> currentBatch = new List <string>(); List <DBProgram> fetchedPrograms = new List <DBProgram>(); Func <int> FetchCurrentBatch = new Func <int>(() => { Console.WriteLine("Downloading program info from SchedulesDirect"); List <object> response = JSONClient.GetJSONResponse <List <object> >( UrlBuilder.BuildWithAPIPrefix("/programs"), currentBatch, SDTokenManager.token_manager.token); List <DBProgram> programsThisBatch = new List <DBProgram>(); // Only include programs fetched successfully, so any errors do not replace good data. List <SDProgram> successfulResponses = new List <SDProgram>(); foreach (object genericJson in response) { SDProgram sdProgram = null; try { sdProgram = JSONClient.Deserialize <SDProgram>(genericJson.ToString()); } catch (Exception exc) { Console.WriteLine("Failed to deserialize program JSON: {0}", genericJson); Misc.OutputException(exc); continue; } if (sdProgram.code > 0) { Console.WriteLine("Failed to download updated program info for programID {0}, response {1}, message: {2}", sdProgram.programID, sdProgram.response, sdProgram.message); continue; } successfulResponses.Add(sdProgram); var dbProgram = new DBProgram(sdProgram); fetchedPrograms.Add(dbProgram); programsThisBatch.Add(dbProgram); } DBManager.instance.SaveRawSDProgramResponses(successfulResponses); DBManager.instance.SaveProgramData(programsThisBatch); currentBatch.Clear(); return(0); }); foreach (string id in programIDs) { currentBatch.Add(id); if (currentBatch.Count >= kBatchSize) { FetchCurrentBatch(); } } if (currentBatch.Count > 0) { FetchCurrentBatch(); } return(fetchedPrograms); }
private IDictionary <string, Dictionary <string, SDStationMD5Response> > GetStationMD5Responses(IEnumerable <string> stationIDs) { // TODO: batch by 5000 const int kBatchSize = 5000; List <Dictionary <string, string> > request = new List <Dictionary <string, string> >(); Dictionary <string, Dictionary <string, SDStationMD5Response> > stationMD5Responses = new Dictionary <string, Dictionary <string, SDStationMD5Response> >(); Func <int> DoBatch = new Func <int>(() => { //var batchResponse = JSONClient.GetJSONResponse<Dictionary<string, Dictionary<string, SDStationMD5Response>>>( var batchResponse = JSONClient.GetJSONResponse <Dictionary <string, object> >( UrlBuilder.BuildWithAPIPrefix("/schedules/md5"), request, SDTokenManager.token_manager.token); foreach (var keyval in batchResponse) { string stationID = keyval.Key; try { Dictionary <string, SDStationMD5Response> dailyResponses = JSONClient.Deserialize <Dictionary <string, SDStationMD5Response> >(keyval.Value.ToString()); stationMD5Responses[stationID] = dailyResponses; } catch (Exception exc) { Console.WriteLine("Failed to deserialize schedule MD5s for station {0}, JSON: {1}", stationID, keyval.Value); Misc.OutputException(exc); } } request.Clear(); return(0); }); foreach (string stationID in stationIDs) { request.Add(new Dictionary <string, string>() { { "stationID", stationID } }); if (request.Count >= kBatchSize) { DoBatch(); } } if (request.Count > 0) { DoBatch(); } return(stationMD5Responses); }
internal IDictionary <string, DBSeriesInfo> GetSeriesInfoByIDs(ISet <string> seriesIds) { Console.WriteLine("Reading series info from local DB"); Dictionary <string, DBSeriesInfo> seriesById = new Dictionary <string, DBSeriesInfo>(); using (SQLiteCommand command = new SQLiteCommand( string.Format("select {1}, {2} from {0};", kSeriesInfoTableName, kSeriesIdColumnName, kSeriesDataColumnName), connection_)) { SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) { string seriesId = reader[0].ToString(); if (seriesIds.Contains(seriesId)) { seriesById[seriesId] = JSONClient.Deserialize <DBSeriesInfo>(reader[1].ToString()); } } } return(seriesById); }
public IDictionary <string, DBProgram> GetProgramsByIds(ISet <string> programIDs) { Console.WriteLine("Reading program info from local DB"); Dictionary <string, DBProgram> programsByID = new Dictionary <string, DBProgram>(); using (SQLiteCommand command = new SQLiteCommand( string.Format("select {1}, {2} from {0};", kProgramsTableName, kProgramIdColumnName, kProgramDataColumnName), connection_)) { SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) { string programID = reader[0].ToString(); string json = reader[1].ToString(); if (programIDs.Contains(programID) && !string.IsNullOrEmpty(json)) { programsByID[programID] = JSONClient.Deserialize <DBProgram>(json); } } } return(programsByID); }