{"id":22,"date":"2024-09-11T10:06:34","date_gmt":"2024-09-11T15:06:34","guid":{"rendered":"https:\/\/danielbondurantcybersecurityportfolio.online\/?p=22"},"modified":"2024-11-07T10:05:46","modified_gmt":"2024-11-07T16:05:46","slug":"daily-weather-forecast-email","status":"publish","type":"post","link":"https:\/\/danielbondurantcybersecurityportfolio.online\/?p=22","title":{"rendered":"Daily Weather Forecast Email"},"content":{"rendered":"\n<p>This post will be about automating a daily weather forecast utilizing a modified version of the Python Email script from the previous post.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXcYHl16sis2skLS8rd9ENp0LZ9B0uCA7f1mCo0JtanjJQWofLp-DQYuxXop_Pj6UgzLohr3zUmp0ywiWwh9SrxBd9dTVv1daEQSXWtD6PFEx3wdWaeT-ZnZZFLGqRGq79CBYcSmTeuY244yiuohxKqU5QI?key=TD4t6X1vWJtUTintZ6OWiA\" width=\"544\" height=\"610\"><\/p>\n\n\n\n<p>The steps for this project are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Find a way to get weather forecast data<\/li>\n\n\n\n<li>Get a script to call that data<\/li>\n\n\n\n<li>Incorporate the script into the email code file<\/li>\n<\/ul>\n\n\n\n<p>For the first step, we will look for an API that will give us weather data. I searched for a weather API on Google and found the WeatherAPI.com through RapidAPI.com.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXf9N9uohksjGwtivvO-aSwjID0KtaClE69au8UnaENCQWUNadn58Gxaw7907QqznG6Jo1Wg9Ajh47K8kIGXX7-aCvSzGJFUpDkO95aOtYRnSO_WkSmhNttXgK68GPXTQIoyccHSzPYxXMlMxzbs9FDzbppZ?key=pdj1z32PDP11ixQUBw-8ng\" width=\"366\" height=\"202\"><\/p>\n\n\n\n<p>After subscribing to the API, we can get a Python script to call forecast data:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"403\" src=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-7.png\" alt=\"\" class=\"wp-image-79\" style=\"width:682px;height:auto\" srcset=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-7.png 651w, https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-7-300x186.png 300w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<p>We can run a test in the workspace to see how to get what we want. I&#8217;ll use Denver, CO for this example.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"290\" height=\"483\" src=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-8.png\" alt=\"\" class=\"wp-image-80\" style=\"width:354px;height:auto\" srcset=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-8.png 290w, https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-8-180x300.png 180w\" sizes=\"auto, (max-width: 290px) 100vw, 290px\" \/><\/figure>\n\n\n\n<p>As we can see, the data is presented as multiple dictionaries within dictionaries, as well as a list thrown in there. To make accessing the data a bit simpler, I will define a variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data = response.json()<\/code><\/pre>\n\n\n\n<p>This way we can call the dictionary keys from the &#8216;data&#8217; variable, depending on which weather attribute we want. Lets say we want max temperature in Fahrenheit. The dictionary keys to call are:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data&#91;'forecast']&#91;'forecastday']&#91;0]&#91;'day']&#91;maxtemp_f']<\/code><\/pre>\n\n\n\n<p>To test, we run this through the CLI:<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfVepPxDEpidVWDN8bPuGBrYpLe2z2SrAbVU4k2oBHgj8_WV44inmIC6ffFTy86wPxwcuEuxu5bnQntghwcU-xsGyJ_6ELwk7O1nWthkfPI4dfRPHSC5cgNpsaeHSUSXBvIMsGW6eU5EyKSsw3BoNgc4s8Q?key=pdj1z32PDP11ixQUBw-8ng\" width=\"624\" height=\"421\"><\/p>\n\n\n\n<p>And we get 96.4, same as from the workspace test run above.<\/p>\n\n\n\n<p>We know how to extract the data, so now we need to construct the email we want to send. We can do that with the method of sending a sample email and copying the HTML element into a new file. I will call it <code>weather_template.html<\/code><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXe7ddqdqxHk3tPv3Ku_K8wBEGN-cB03JICltH3-AJq62wFArAIvDMgdbebPYobPQxpICX2EGbVsj-dx6m8b5x5gQkN7usffqjij9CRF03XtpBU_dcsjAvMaiNYMkuXR-jrCFWz5SzhGs9Hekh_q7VQc3BkS?key=pdj1z32PDP11ixQUBw-8ng\" width=\"612\" height=\"262\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>For this email, we will be needing the date, city, state, high temp, low temp, chance of rain, total amount of rain, and max wind. All we have to do is call the correct dictionary keys for each and assign them to a corresponding variable.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"622\" height=\"216\" src=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/image-3.png\" alt=\"\" class=\"wp-image-26\" srcset=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/image-3.png 622w, https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/image-3-300x104.png 300w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\" \/><\/figure>\n\n\n\n<p>However, we run into an issue:<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXeYXn8q4r5rrD2DYOGMzc-s3gnP5yyJjsiL_1nU5CdQwN-YVPu5MHAZzYmlT9pNzjkLyvFKNcx8BK25hWnufVC1C7eypeGk0LS1ZS-yCFjfpiBRB8ghcKnqoVs3RrahLiii9_qIT0a7wGKHuHE95YYFRWmE?key=pdj1z32PDP11ixQUBw-8ng\" width=\"624\" height=\"175\"><\/p>\n\n\n\n<p>If we set up the script with these variables, we get an error because the variable values are not strings. This is easy to fix by casting the float and integer values as strings:<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"443\" height=\"309\" data-id=\"27\" src=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/error_variable.png\" alt=\"\" class=\"wp-image-27\" srcset=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/error_variable.png 443w, https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/error_variable-300x209.png 300w\" sizes=\"auto, (max-width: 443px) 100vw, 443px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"483\" height=\"309\" data-id=\"28\" src=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/string_variable.png\" alt=\"\" class=\"wp-image-28\" srcset=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/string_variable.png 483w, https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/string_variable-300x192.png 300w\" sizes=\"auto, (max-width: 483px) 100vw, 483px\" \/><\/figure>\n<\/figure>\n\n\n\n<p>The full script now is this:<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdAWOFgXnDcQ7kiobehUGb9lYuVTxtfv3QpxQJoiEDNzjANDDSwVPgsj8fNtfz7gh2pFW2_dl4rchx8iIalOaM3AYjujdtpbYYrZvvVBU2G0_j52w68bo-NhwnkjmJawCpTlJktyFMAGNX9B6hyc8ScCHfC?key=pdj1z32PDP11ixQUBw-8ng\" width=\"624\" height=\"983\"><\/p>\n\n\n\n<p>Running the script should now send the email.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdCd5E27Xv8IBICFQTlYtLkkUuPR4ur5eRt9WyphoPYvffd1EVZ3JW345z2rU7GtsEC2dsI_PAWSZrixjE-5qlrX6wVhlFFqdSJoG_G_HYjiH2DZSZqsf0aocS88IjXkUdEU5dZHKaeYmMUuuXcn3fkCSY?key=pdj1z32PDP11ixQUBw-8ng\" width=\"492\" height=\"368\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Looks like we are good to go. The next post will be about setting the email up to be sent automatically with cron.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post will be about automating a daily weather forecast utilizing a modified version of the Python Email script from the previous post. The steps for this project are: For the first step, we will look for an API that will give us weather data. I searched for a weather API on Google and found [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-linux","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22"}],"version-history":[{"count":8,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/22\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}