// 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); }
public void SaveRawSDProgramResponses(IEnumerable <ProgramCache.SDProgram> programs) { Console.WriteLine("Saving Program JSON Responses to local DB"); const string kProgramIDParam = "@programID"; const string kProgramResponseParam = "@programResponse"; string sql = string.Format("insert or replace into {0} ({1}, {2}) values ({3}, {4})", kProgramsTableName, kProgramIdColumnName, kProgramRawResponseColumnName, kProgramIDParam, kProgramResponseParam); using (var transaction = connection_.BeginTransaction()) { foreach (var program in programs) { if (program.code > 0) { continue; } Dictionary <string, string> paramDictionary = new Dictionary <string, string>(); int rowCount = ExecuteSQLCommand(sql, new Dictionary <string, string>() { { kProgramIDParam, program.programID }, { kProgramResponseParam, JSONClient.Serialize(program) }, }); if (rowCount != 1) { throw new Exception("Expected exactly 1 row changed, got " + rowCount); } } transaction.Commit(); } }
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); } }
public void SaveProgramImages(IEnumerable <SDProgramImageResponse> programImages) { Console.WriteLine("Saving program images to local DB"); const string kImageIdParam = "@programID"; const string kImageResponseParam = "@json"; string sql = string.Format("insert or replace into {0} ({1}, {2}) values ({3}, {4});", kImagesTableName, kImageIdColumnName, kImageDataColumnName, kImageIdParam, kImageResponseParam); using (var transaction = connection_.BeginTransaction()) { foreach (var programImage in programImages) { if (programImage.code > 0) { continue; } int rowCount = ExecuteSQLCommand(sql, new Dictionary <string, string>() { { kImageIdParam, programImage.programID }, { kImageResponseParam, JSONClient.Serialize(programImage) } }); if (rowCount != 1) { throw new Exception("Expected exactly 1 row changed, got " + rowCount); } } transaction.Commit(); } }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); foreach (var d in new Devices(ChannelEditing.object_store)) { Device device = d as Device; Console.WriteLine("Device id: {0}\n\tname: {1}\n\ttype: {2}\n\tuids: {3}", device.Id, device.Name, device.DeviceType, string.Join(",", device.UIds)); } // FindUnencryptedQAMChannels(); SDTokenManager token_manager = LoginForm.LoginAndGetTokenManager(); StatusResponse status = new SDStatusReader(token_manager).GetSchedulesDirectStatus(); JSONClient.DisplayJSON(status); if (!status.IsOnline()) { MessageBox.Show("SchedulesDirect JSON API is currently offline. Try again later."); Application.Exit(); } /* var schedule_responses = SDSchedules.GetStationScheduleResponses(new List<string>{ * "58623", "62420" }); * HashSet<string> programIDs = new HashSet<string>(); * foreach(var schedule_response in schedule_responses) * { * if (schedule_response.programs != null) * foreach(var program in schedule_response.programs) * programIDs.Add(program.programID); * } * List<SDProgram> programs = SDProgramFetcher.FetchPrograms(programIDs); */ // SDAccountManagement.AddLineupToAccount("USA-NY67791-QAM"); ProviderCreator.ListProviders(); Application.Run(new ConfigForm(token_manager, status)); }
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); }
public StatusResponse GetSchedulesDirectStatus() { StatusResponse status_response = JSONClient.GetJSONResponse <StatusResponse>(kStatusRequestUrl, new JSONClient.EmptyRequest(), token_manager_.token); return(status_response); }
public void SaveProgramData(IEnumerable <DBProgram> programs) { Console.WriteLine("Saving program data to local DB"); const string kProgramIDParam = "@programID"; const string kProgramDataParam = "@programData"; const string kMD5Param = "@md5"; string sql = string.Format("insert or replace into {0} ({1}, {2}, {3}) values ({4}, {5}, {6})", kProgramsTableName, kProgramIdColumnName, kProgramDataColumnName, kMD5ColumnName, kProgramIDParam, kProgramDataParam, kMD5Param); using (var transaction = connection_.BeginTransaction()) { foreach (var program in programs) { Dictionary <string, string> paramDictionary = new Dictionary <string, string>(); int rowCount = ExecuteSQLCommand(sql, new Dictionary <string, string>() { { kProgramIDParam, program.programID }, { kProgramDataParam, JSONClient.Serialize(program) }, { kMD5Param, program.md5 } }); if (rowCount != 1) { throw new Exception("Expected exactly 1 row changed, got " + rowCount); } } transaction.Commit(); } }
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); }
public void SaveConfig() { using (Stream output_stream = File.OpenWrite(config_path_)) { byte[] bytes = Encoding.UTF8.GetBytes(JSONClient.Serialize(config_)); output_stream.Write(bytes, 0, bytes.Length); output_stream.Flush(); output_stream.Close(); } }
internal static void RemoveLineupFromAccount(string lineup) { LineupSubscriptionChangeReponse response = JSONClient.GetJSONResponse <LineupSubscriptionChangeReponse>( UrlBuilder.BuildWithAPIPrefix("/lineups/" + lineup), null, SDTokenManager.token_manager.token, "DELETE"); if (!response.Succeeded()) { throw new Exception("Failed to remove lineup from account!"); } }
public static void AddLineupToAccount(string lineup) { LineupSubscriptionChangeReponse response = JSONClient.GetJSONResponse <LineupSubscriptionChangeReponse>( UrlBuilder.BuildWithAPIPrefix("/lineups/" + lineup), null, SDTokenManager.token_manager.token, "PUT"); if (!response.Succeeded()) { throw new Exception("Failed to add lineup to account!"); } }
public static List <Country> GetCountryListFromUri(string uri) { Dictionary <string, Country[]> countries_by_region = JSONClient.GetJSONResponse <Dictionary <string, Country[]> >( UrlBuilder.BuildWithBasePrefix(uri), JSONClient.empty_request); List <Country> countries = new List <Country>(); foreach (Country[] region_countries in countries_by_region.Values) { countries.AddRange(region_countries); } return(countries); }
private void SelectedLineupsListBox_SelectedValueChanged(object sender, EventArgs e) { RemoveSelectedLineupButton.Enabled = selected_lineup_for_removal != null; if (selected_lineup_for_removal != null) { var channel_list = SDChannelReader.GetChannelListByLineupUri(selected_lineup_for_removal.sdLineup.uri); JSONClient.DisplayJSON(channel_list); string serialized = JSONClient.Serialize(channel_list); System.IO.File.WriteAllBytes("channel_list.json", Encoding.UTF8.GetBytes(serialized)); } }
private IEnumerable <SDProgramImageResponse> DownloadProgramImages(IEnumerable <string> programIDs) { const int kBatchSize = 500; Console.WriteLine("Downloading list of program images to download."); HashSet <string> idsToFetch = new HashSet <string>(); foreach (string programID in programIDs) { string key = GetSDImageIDByProgramID(programID); if (!cachedImageData_.ContainsKey(key)) { idsToFetch.Add(key); } } Console.WriteLine("Downloading program image URLs"); List <string> batchProgramIds = new List <string>(); List <SDProgramImageResponse> fetchedImages = new List <SDProgramImageResponse>(); Func <int> DownloadBatch = new Func <int>(() => { List <SDProgramImageResponse> responses = JSONClient.GetJSONResponse <List <SDProgramImageResponse> >( UrlBuilder.BuildWithAPIPrefix("/metadata/programs/"), batchProgramIds, SDTokenManager.token_manager.token); foreach (var response in responses) { if (!response.OK()) { Console.WriteLine("Some images failed to download, code: {0} programID: {2} message: {1}", response.code, response.message, response.programID); continue; } fetchedImages.Add(response); } batchProgramIds.Clear(); return(0); }); foreach (var programID in idsToFetch) { batchProgramIds.Add(programID); if (batchProgramIds.Count >= kBatchSize) { DownloadBatch(); } } if (batchProgramIds.Count > 0) { DownloadBatch(); } DBManager.instance.SaveProgramImages(fetchedImages); return(fetchedImages); }
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); }
public static List <SDLineup> GetLineupsForZip(SDTokenManager token_manager, string country, string zip) { string url = String.Format(UrlBuilder.BuildWithAPIPrefix("/headends?country={0}&postalcode={1}"), country, zip); List <HeadEnd> headends = JSONClient.GetJSONResponse <List <HeadEnd> >(url, JSONClient.empty_request, token_manager.token); List <SDLineup> lineups = new List <SDLineup>(); foreach (HeadEnd headend in headends) { if (headend.lineups != null) { foreach (SDLineup lineup in headend.lineups) { lineup.transport = headend.transport; } lineups.AddRange(headend.lineups.ToArray()); } } return(lineups); }
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); }
private static Dictionary <string, SDGenericProgramDescription> DownloadSeriesByIDs(IEnumerable <string> seriesIds) { const int kBatchSize = 500; Console.WriteLine("Downloading series infos from SchedulesDirect"); Dictionary <string, SDGenericProgramDescription> downloadedGenericDescriptions = new Dictionary <string, SDGenericProgramDescription>(); List <string> currentBatch = new List <string>(); Func <int> DownloadBatch = new Func <int>(() => { var batchRepsonse = JSONClient.GetJSONResponse <Dictionary <string, SDGenericProgramDescription> >( UrlBuilder.BuildWithAPIPrefix("/metadata/description/"), currentBatch, SDTokenManager.token_manager.token); foreach (var kv in batchRepsonse) { var genericDescription = kv.Value; if (genericDescription.code > 0) { Console.WriteLine("Failed to download generic description for program ID: {0}\ncode: {1}\nmessage: {2}", kv.Key, genericDescription.code, genericDescription.message); continue; } downloadedGenericDescriptions[kv.Key] = genericDescription; } currentBatch.Clear(); return(0); }); foreach (string seriesId in seriesIds) { currentBatch.Add(seriesId); if (currentBatch.Count >= kBatchSize) { DownloadBatch(); } } if (currentBatch.Count > 0) { DownloadBatch(); } return(downloadedGenericDescriptions); }
private List <SDStationScheduleResponse> GetStationScheduleResponses(IDictionary <string, List <string> > daysByStationID) { Console.WriteLine("Downloading station schedules from SchedulesDirect"); const int kBatchSize = 5000; List <SDScheduleStationRequest> request = new List <SDScheduleStationRequest>(); List <SDStationScheduleResponse> responses = new List <SDStationScheduleResponse>(); Func <int> DoBatch = new Func <int>(() => { var batchResponses = JSONClient.GetJSONResponse <List <SDStationScheduleResponse> >(UrlBuilder.BuildWithAPIPrefix("/schedules"), request, SDTokenManager.token_manager.token); // Some of the reponses may be errors! Loop through and exclude these so we don't replace good data! foreach (var response in batchResponses) { if (response.code > 0) { Console.WriteLine( "Portions of the schedule for station ID {0} failed to download, SchedulesDirect response code: {1} - {2}", response.stationID, response.code, response.reponse); continue; } responses.Add(response); } request.Clear(); return(0); }); foreach (var stationIDAndDays in daysByStationID) { request.Add(new SDScheduleStationRequest(stationIDAndDays.Key, stationIDAndDays.Value)); if (request.Count > kBatchSize) { DoBatch(); } } if (request.Count > 0) { DoBatch(); } return(responses); }
private string GetNewToken() { TokenRequest token_request = new TokenRequest(username_, pwhash_); TokenResponse token_response = JSONClient.GetJSONResponse <TokenResponse>(kTokenRequestUrl, token_request); switch (token_response.code) { case 0: token_ = token_response.token; Console.WriteLine("Successfully requested access token: {0}", token_); last_updated_ = DateTime.Now; token_manager_ = this; return(token_); case 3000: throw new ServerDownException(token_response.response_code, token_response.message); default: throw new Exception("Unrecognized error - bad password? response_code:" + token_response.response_code + " message: " + token_response.message + " code: " + token_response.code); } }
internal void SaveSeriesInfos(IEnumerable <DBSeriesInfo> seriesInfos) { Console.WriteLine("Saving series info to local DB"); const string kSeriesIdParam = "@seriesID"; const string kSeriesDataParam = "@seriesData"; string sql = string.Format("insert or replace into {0} ({1}, {2}) values ({3}, {4})", kSeriesInfoTableName, kSeriesIdColumnName, kSeriesDataColumnName, kSeriesIdParam, kSeriesDataParam); using (var transsaction = connection_.BeginTransaction()) { foreach (var seriesInfo in seriesInfos) { int rowCount = ExecuteSQLCommand(sql, new Dictionary <string, string>() { { kSeriesIdParam, seriesInfo.id }, { kSeriesDataParam, JSONClient.Serialize(seriesInfo) } }); } transsaction.Commit(); } }
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); }
internal void SaveSchedules(IEnumerable <SDStationScheduleResponse> stationSchedules) { Console.WriteLine("Saving schedules to local DB"); const string kStationIDParam = "@stationID"; const string kDayParam = "@day"; const string kScheduleEntriesParam = "@scheduleEntries"; const string kMD5Param = "@md5"; string sql = string.Format("insert or replace into {0} ({1}, {2}, {3}, {4}) values ({5}, {6}, {7}, {8});", kSchedulesTableName, kStationIDColumnName, kDayColumnName, kScheduleEntriesColumnName, kMD5ColumnName, kStationIDParam, kDayParam, kScheduleEntriesParam, kMD5Param); using (var transaction = connection_.BeginTransaction()) { foreach (var stationSchedule in stationSchedules) { if (stationSchedule.code > 0) { continue; } int rowCount = ExecuteSQLCommand(sql, new Dictionary <string, string>() { { kStationIDParam, stationSchedule.stationID }, { kDayParam, stationSchedule.metadata.startDate }, { kScheduleEntriesParam, JSONClient.Serialize(stationSchedule) }, { kMD5Param, stationSchedule.metadata.md5 } }); if (rowCount != 1) { throw new Exception("Expected exactly 1 row changed, got " + rowCount); } } transaction.Commit(); } PruneSchedules(); }
public static SDChannelList GetChannelListByLineupUri(string lineupuri) { return(JSONClient.GetJSONResponse <SDChannelList>( UrlBuilder.BuildWithBasePrefix(lineupuri), JSONClient.empty_request, SDTokenManager.token_manager.token)); }
public static SubscribedLineupsResponse GetSubscribedLineups() { return(JSONClient.GetJSONResponse <SubscribedLineupsResponse>(UrlBuilder.BuildWithAPIPrefix("/lineups"), null, SDTokenManager.token_manager.token)); }