Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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!");
            }
        }
Esempio n. 3
0
        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!");
            }
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
        }
        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);
        }
Esempio n. 8
0
 public static SubscribedLineupsResponse GetSubscribedLineups()
 {
     return(JSONClient.GetJSONResponse <SubscribedLineupsResponse>(UrlBuilder.BuildWithAPIPrefix("/lineups"),
                                                                   null, SDTokenManager.token_manager.token));
 }
Esempio n. 9
0
        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);
        }