コード例 #1
0
        public void Publish(HpbStatistic hpbStatistic)
        {
            string post = "Total Cases - " + hpbStatistic.LocalTotalCases + "\n";

            post += "Active Cases - " + hpbStatistic.LocalActiveCases + "\n";
            post += "New Cases - " + hpbStatistic.LocalNewCases + "\n";
            post += "In Hospitals - " + hpbStatistic.LocalTotalNumberOfIndividualsInHospitals + "\n";
            post += "Total Recoverd - " + hpbStatistic.LocalRecoverd + "\n";
            post += "Total Deaths - " + hpbStatistic.LocalNewDeaths + "\n";
            post += "Updated on - " + hpbStatistic.LastUpdate + "\n";
            post += "More info visit https://www.hpb.health.gov.lk/" + "\n";
            post += "#lka #COVID19SL #COVID19";

            var         requestContent = new MultipartFormDataContent();
            HttpContent stringContent2 = new StringContent(post);

            HttpContent fileStreamContent = new StreamContent(new FileStream("status_update_" + hpbStatistic.Id + ".jpg", FileMode.Open, FileAccess.Read));

            fileStreamContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");

            requestContent.Add(stringContent2, "message");
            requestContent.Add(fileStreamContent, "file", "status_update_" + hpbStatistic.Id + ".jpg");

            var token    = Configuration["Facebook:Token"];
            var response = Client.PostAsync($"https://graph.facebook.com/104399301213657/photos?access_token={token}", requestContent).Result;

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine("Error Posting Facebook " + response.StatusCode);
            }
        }
コード例 #2
0
        public string CreateStatusUpdatePoster(HpbStatistic hpbStatistic)
        {
            using (Image image = Image.Load(Configuration["HBP:LatestUpdateImage"]))
            {
                FontCollection fonts      = new FontCollection();
                FontFamily     fontFamily = fonts.Install(Configuration["HBP:font"]);

                String dateAndTime = hpbStatistic.LastUpdate.ToString("dd.MM.yyyy") + " - " + hpbStatistic.LastUpdate.ToString("hh.mm tt");

                image.Mutate(ctx => ctx.DrawText(dateAndTime, new Font(fontFamily, 60, FontStyle.Bold), Color.White, new PointF(50, 230)));

                image.Mutate(ctx => ctx.DrawText(hpbStatistic.LocalTotalCases.ToString(),
                                                 new Font(fontFamily, 80, FontStyle.Bold), Color.FromRgb(210, 9, 61), new PointF(40, 350)));

                image.Mutate(ctx => ctx.DrawText("New Cases : " + hpbStatistic.LocalNewCases.ToString(),
                                                 new Font(fontFamily, 25, FontStyle.Bold), Color.FromRgb(210, 9, 61), new PointF(30, 430)));

                image.Mutate(ctx => ctx.DrawText(hpbStatistic.LocalActiveCases.ToString(),
                                                 new Font(fontFamily, 80, FontStyle.Bold), Color.FromRgb(210, 9, 61), new PointF(40, 610)));

                image.Mutate(ctx => ctx.DrawText(hpbStatistic.LocalTotalNumberOfIndividualsInHospitals.ToString(),
                                                 new Font(fontFamily, 80, FontStyle.Bold), Color.FromRgb(210, 9, 61), new PointF(40, 850)));

                image.Mutate(ctx => ctx.DrawText(hpbStatistic.LocalRecoverd.ToString(),
                                                 new Font(fontFamily, 80, FontStyle.Bold), Color.ForestGreen, new PointF(40, 1110)));

                image.Mutate(ctx => ctx.DrawText(hpbStatistic.LocalDeaths.ToString(),
                                                 new Font(fontFamily, 90, FontStyle.Bold), Color.FromRgb(210, 9, 61), new PointF(80, 1355)));

                String imageName = "status_update_" + hpbStatistic.Id + ".jpg";

                image.Save(imageName);
                return(imageName);
            }
        }
コード例 #3
0
        public async Task TriggerNotification(HpbStatistic hpbStatistic)
        {
            Console.WriteLine("Total Cases " + hpbStatistic.LocalTotalCases);
            Console.WriteLine("New Cases " + hpbStatistic.LocalNewCases);
            Console.WriteLine("In Hospitals " + hpbStatistic.LocalTotalNumberOfIndividualsInHospitals);
            Console.WriteLine("Total Recoverd " + hpbStatistic.LocalRecoverd);
            Console.WriteLine("Total Deaths " + hpbStatistic.LocalNewDeaths);
            Console.WriteLine("More info visit https://www.hpb.health.gov.lk/");

            foreach (var notification in Notifications)
            {
                notification.Publish(hpbStatistic);
            }
        }
コード例 #4
0
        public void Publish(HpbStatistic hpbStatistic)
        {
            string tweet = "Total Cases - " + hpbStatistic.LocalTotalCases + "\n";

            tweet += "Active Cases - " + hpbStatistic.LocalActiveCases + "\n";
            tweet += "New Cases - " + hpbStatistic.LocalNewCases + "\n";
            tweet += "In Hospitals - " + hpbStatistic.LocalTotalNumberOfIndividualsInHospitals + "\n";
            tweet += "Total Recoverd - " + hpbStatistic.LocalRecoverd + "\n";
            tweet += "Total Deaths - " + hpbStatistic.LocalDeaths + "\n";
            tweet += "Updated on - " + hpbStatistic.LastUpdate + "\n";
            tweet += "More info visit https://www.hpb.health.gov.lk/" + "\n";
            tweet += "@HPBSriLanka #lka #COVID19SL #COVID19LK #COVID19";

            PostTweet(hpbStatistic, tweet, TwitterNotificationTypes.STATUS_UPDATE);
        }
コード例 #5
0
        public async Task NotificationTriggerCheck(HpbStatistic hpbStatistic)
        {
            var lastRecord = await DataContext.HpbStatistic
                             .OrderByDescending(d => d.LastUpdate)
                             .FirstOrDefaultAsync();

            // if there is no record trigger the notification
            if (lastRecord == null)
            {
                await TriggerNotification(hpbStatistic);
            }
            else
            {
                // Local Cases increase or decrese
                if (lastRecord.LocalTotalCases != hpbStatistic.LocalTotalCases ||
                    lastRecord.LocalActiveCases != hpbStatistic.LocalActiveCases ||
                    lastRecord.LocalTotalNumberOfIndividualsInHospitals != hpbStatistic.LocalTotalNumberOfIndividualsInHospitals ||
                    lastRecord.LocalRecoverd != hpbStatistic.LocalRecoverd)
                {
                    await TriggerNotification(hpbStatistic);
                }
            }
        }
コード例 #6
0
        public void PostTweet(HpbStatistic hpbStatistic, string message, TwitterNotificationTypes type)
        {
            var userCredentials = Auth.CreateCredentials(Configuration["Twitter:ConsumerKey"], Configuration["Twitter:ConsumerSecret"],
                                                         Configuration["Twitter:UserToken"], Configuration["Twitter:UserSecret"]);

            AuthenticatedUser = User.GetAuthenticatedUser(userCredentials);

            if (type == TwitterNotificationTypes.STATUS_UPDATE)
            {
                byte[] status_update_post = File.ReadAllBytes("status_update_" + hpbStatistic.Id + ".jpg");

                var publishedTweet = Auth.ExecuteOperationWithCredentials(userCredentials, () =>
                {
                    var publishOptions = new PublishTweetOptionalParameters();
                    if (status_update_post != null)
                    {
                        publishOptions.MediaBinaries.Add(status_update_post);
                    }

                    return(Tweet.PublishTweet(message, publishOptions));
                });
            }
        }
コード例 #7
0
        public async Task MapToDataContext(HpbStatisticResponse hpbStatisticResponse)
        {
            var response = hpbStatisticResponse.Data;
            // Check if the record existing in the database using the last updated time
            var flag = await DataContext.HpbStatistic
                       .Where(u => u.LastUpdate.Equals(response.update_date_time))
                       .AsNoTracking()
                       .CountAsync();

            // if the record alredy existing skip this record;
            if (flag != 0)
            {
                return;
            }

            // Add the record to the Statistic table
            HpbStatistic hpbStatistic = new HpbStatistic
            {
                LastUpdate       = response.update_date_time,
                LocalNewCases    = response.local_new_cases,
                LocalDeaths      = response.local_deaths,
                LocalNewDeaths   = response.local_new_deaths,
                LocalRecoverd    = response.local_recovered,
                LocalTotalCases  = response.local_total_cases,
                LocalActiveCases = response.local_active_cases,
                LocalTotalNumberOfIndividualsInHospitals = response.local_total_number_of_individuals_in_hospitals,
                GlobalDeaths     = response.global_deaths,
                GlobalNewCases   = response.global_new_cases,
                GlobalNewDeaths  = response.global_new_deaths,
                GlobalRecovered  = response.global_recovered,
                GlobalTotalCases = response.global_total_cases
            };

            DataContext.HpbStatistic.Add(hpbStatistic);
            hpbStatistic.HospitalStatuses = new List <HpbHospitalStatus>();
            // Add Hospital Status
            foreach (var data in response.hospital_data)
            {
                // check if the hospital exisiting
                var hospital = await DataContext.HpbHospital
                               .Where(i => i.Id.Equals(data.hospital.id))
                               .FirstOrDefaultAsync();

                if (hospital == null)
                {
                    // if not create a new hospital
                    HpbHospital hpbHospital = new HpbHospital
                    {
                        Id          = data.hospital.id,
                        Name        = data.hospital.name,
                        NameSinhala = data.hospital.name_si,
                        NameTamil   = data.hospital.name_ta,
                        CreatedAt   = data.hospital.created_at,
                        UpdatedAt   = data.hospital.updated_at,
                        DeletedAt   = data.hospital.deleted_at,
                    };

                    DataContext.HpbHospital.Add(hpbHospital);
                    hospital = hpbHospital;
                }


                HpbHospitalStatus hpbHospitalStatus = new HpbHospitalStatus
                {
                    CreatedAt         = data.created_at,
                    CumulativeForeign = data.cumulative_foreign,
                    CumulativeLocal   = data.cumulative_local,
                    CumulativeTotal   = data.cumulative_total,
                    DeletedAt         = data.deleted_at,
                    UpdatedAt         = data.updated_at,
                    TreatmentForeign  = data.treatment_foreign,
                    TreatmentLocal    = data.treatment_local,
                    TreatmentTotal    = data.treatment_total,
                    HpbHospital       = hospital
                };

                hpbStatistic.HospitalStatuses.Add(hpbHospitalStatus);
            }

            await NotificationTriggerCheck(hpbStatistic);

            await DataContext.SaveChangesAsync();
        }
コード例 #8
0
 public void Publish(HpbStatistic hpbStatistic)
 {
     CreateStatusUpdatePoster(hpbStatistic);
 }