good old friend, pip<\/a>. Other then checking the source code to find related modules and install them one by one, in Python, we can take advantage of a “requirements.txt” file. A “requirements.txt” file is a list of python modules to be installed by pip in plain text format. So we can put all our required modules in a “requirements.txt”, and let pip to install and setup our development environment.<\/p>\npip install -r requirements.txt<\/pre>\nInside the Code<\/h3>\n As usual, we import required modules at the top of our program:<\/p>\n
from geopy.geocoders import Nominatim\r\nfrom datetime import datetime,timedelta\r\nimport sys, requests\r\n<\/pre>\nThis time, we use\u00a0geopy<\/em> python module to get location information such as address, latitude and longitude.<\/p>\nDARK_SKY_API_KEY = \"YOUR_DARK_SKY_API_KEY_HERE\"\r\noption_list = \"exclude=currently,minutely,hourly,alerts&units=si\"\r\n<\/pre>\nFill you Dark Sky API KEY on line 4. And you can change Celsius\/Fahrenheit setting on “units” at line 5.<\/p>\n
location = Nominatim().geocode(sys.argv[1], language='en_US')\r\nd_from_date = datetime.strptime(sys.argv[2] , '%Y-%m-%d')\r\nd_to_date = datetime.strptime(sys.argv[3] , '%Y-%m-%d')\r\n<\/pre>\nWe get the destination, start date and end date from the command line arguments. Please note that both start date and end date are in “YYYY-MM-DD” format.<\/p>\n
delta = d_to_date - d_from_date\r\nlatitude = str(location.latitude)\r\nlongitude = str(location.longitude)\r\n<\/pre>\nNow we calculate the number of day between start date and end date, and get the latitude and longitude using Nominatim tool from OpenStreetMap.org.<\/p>\n
The Weather Forecast Magic Starts Here<\/h3>\n We have API key, location and the date range. What we do next is just call the Dark Sky API, then sit back, grab a popcorn and let the API do the weather forecast magic for us.<\/p>\n
print(\"\\nLocation: \"+ location.address)\r\nfor i in range(delta.days+1):\r\n new_date = (d_from_date + timedelta(days=i)).strftime('%Y-%m-%d')\r\n search_date = new_date+\"T00:00:00\"\r\n response = requests.get(\"https:\/\/api.darksky.net\/forecast\/\"+DARK_SKY_API_KEY+\"\/\"+latitude+\",\"+longitude+\",\"+search_date+\"?\"+option_list)\r\n json_res = response.json()\r\n<\/pre>\nWe use a for loop with the day range of our start date and end date, then make a call to the Dark Sky API. Please note that a timedelta<\/em> object is used to create the time duration, i.e.<\/p>\n\nd_from_date + timedelta(days=<\/em>1) will be one day after d_from_date<\/em><\/li>\nd_from_date + timedelta(days=<\/em>3) will be three days after d_from_date<\/em><\/li>\n<\/ul>\nAfter sending the latitude, longitude and the date to the Dark Sky API, it will return a JSON object with weather information and we pick our information from it.<\/p>\n
\r\n print(\"\\n\"+(d_from_date + timedelta(days=i)).strftime('%Y-%m-%d %A'))\r\n unit_type = '\u00b0F' if json_res['flags']['units'] == 'us' else '\u00b0C'\r\n print(\"Min temperature: \"+str(json_res['daily']['data'][0]['apparentTemperatureMin'])+unit_type)\r\n print(\"Max temperature: \"+str(json_res['daily']['data'][0]['apparentTemperatureMax'])+unit_type)\r\n print(\"Summary: \" + json_res['daily']['data'][0]['summary'])\r\n precip_type = None\r\n precip_prob = None\r\n if'precipProbability' in json_res['daily']['data'][0] and 'precipType' in json_res['daily']['data'][0]:\r\n precip_type = json_res['daily']['data'][0]['precipType']\r\n precip_prob = json_res['daily']['data'][0]['precipProbability']\r\n if (precip_type == 'rain' and precip_prob != None):\r\n precip_prob *= 100\r\n print(\"Chance of rain: %.2f%%\" % (precip_prob))\r\n<\/pre>\nLike I have mentioned before, I would like to get only the information I am interested. Thus I have both the max and min-imum “feel like” temperatures, a daily summary and the chance of rain. The Dark Sky JSON object contains much more information, you can check out their document to add the information you need.<\/p>\n
Show Time<\/h3>\n The programming part is all done, it is time to run our program. As I said, I will go to Sapporo at the end of August, then I can input:<\/p>\n
python weather_tool.py Sapporo 2017-08-30 2017-09-03\r\n<\/pre>\nAnd we got:<\/p>\n
Location: Sapporo, Ishikari, Hokkaido, Japan\r\n\r\n2017-08-30 Wednesday\r\nMin temperature: 18.47\u00b0C\r\nMax temperature: 23.84\u00b0C\r\nSummary: Mostly cloudy throughout the day.\r\n\r\n........\r\n\r\n2017-09-03 Sunday\r\nMin temperature: 17.96\u00b0C\r\nMax temperature: 23.31\u00b0C\r\nSummary: Mostly cloudy throughout the day.\r\n<\/pre>\nLovely, that is what I wanted. We have used around 30 lines to write a weather forecast tool, you can modify it to make your own weather tool.<\/p>\n
What we have learnt<\/h3>\n Of course, we have learnt the usage of the Dark Sky API. Other than that, you may notice we have used the Software Development Life Cycle (SDLC<\/strong>) phases as well. Firstly, we define a problem to solve (planning<\/strong>), then think about inputs and outputs (design<\/strong>), code a program (Implementation<\/strong>), \u00a0have a test run (testing<\/strong>) and launch our software here (deployment<\/strong>). You can see, SDLC is nothing special, it can be applied to our daily experience.<\/p>\nCoding is, just a part of our life.<\/p><\/blockquote>\n
<\/p>\n
The complete source code can be found at\u00a0https:\/\/github.com\/codeastar\/weather_on_trip<\/a>\u00a0.<\/p>\n","protected":false},"excerpt":{"rendered":"Since I am going to have a 5-day trip to Sapporo, Hokkaido, Japan at the end of August, I would like to have a weather forecast for my trip. “So why don’t you check from a weather forecast site for free?” Yes, but I would like to get the exact information that I want to […]<\/p>\n","protected":false},"author":1,"featured_media":408,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[2],"tags":[15,5,8,16,33,32],"class_list":["post-387","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-we-code-therefore-we-are","tag-api","tag-code","tag-python","tag-requests","tag-sdlc","tag-weather-forecast"],"jetpack_publicize_connections":[],"yoast_head":"\n
Easy Weather Forecast Tool in Python ⋆ Code A Star<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n