From ae5c87ca21482c9a23e7515dc49233729648975f Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Wed, 4 Sep 2024 22:51:10 +0300 Subject: [PATCH 01/13] fixing the function that needs to be refactored --- src/meta/configs.js | 82 +++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/src/meta/configs.js b/src/meta/configs.js index 80159490c4..c920aa7f7a 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -15,44 +15,74 @@ const Configs = module.exports; Meta.config = {}; + // called after data is loaded from db function deserialize(config) { const deserialized = {}; + Object.keys(config).forEach((key) => { const defaultType = typeof defaults[key]; const type = typeof config[key]; const number = parseFloat(config[key]); - if (defaultType === 'string' && type === 'number') { - deserialized[key] = String(config[key]); - } else if (defaultType === 'number' && type === 'string') { - if (!isNaN(number) && isFinite(config[key])) { - deserialized[key] = number; - } else { - deserialized[key] = defaults[key]; - } - } else if (config[key] === 'true') { - deserialized[key] = true; - } else if (config[key] === 'false') { - deserialized[key] = false; - } else if (config[key] === null) { - deserialized[key] = defaults[key]; - } else if (defaultType === 'undefined' && !isNaN(number) && isFinite(config[key])) { - deserialized[key] = number; - } else if (Array.isArray(defaults[key]) && !Array.isArray(config[key])) { - try { - deserialized[key] = JSON.parse(config[key] || '[]'); - } catch (err) { - winston.error(err.stack); - deserialized[key] = defaults[key]; - } - } else { - deserialized[key] = config[key]; - } + deserialized[key] = handleTypeConversion(defaultType, type, config[key], number, key); }); + return deserialized; } +// got this code from ChatGPT +function handleTypeConversion(defaultType, type, value, number, key) { + if (defaultType === 'string' && type === 'number') { + return String(value); + } + + if (defaultType === 'number' && type === 'string') { + return handleNumberConversion(number, value, key); + } + + if (value === 'true') { + return true; + } + + if (value === 'false') { + return false; + } + + if (value === null) { + return defaults[key]; + } + + if (defaultType === 'undefined' && !isNaN(number) && isFinite(value)) { + return number; + } + + if (Array.isArray(defaults[key]) && !Array.isArray(value)) { + return handleArrayConversion(value, key); + } + + return value; +} + +console.log('Reem'); + +function handleNumberConversion(number, value, key) { + if (!isNaN(number) && isFinite(value)) { + return number; + } + return defaults[key]; +} + +function handleArrayConversion(value, key) { + try { + return JSON.parse(value || '[]'); + } catch (err) { + winston.error(err.stack); + return defaults[key]; + } +} + + // called before data is saved to db function serialize(config) { const serialized = {}; From a388f650a08e81477c0eedc236b18352baa2970d Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Wed, 4 Sep 2024 22:52:56 +0300 Subject: [PATCH 02/13] fixing some syntax issues --- src/user/create.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/user/create.js b/src/user/create.js index 610d614e81..c573243bab 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -122,6 +122,8 @@ module.exports = function (User) { return userData.uid; } + console.log('Reem'); + async function storePassword(uid, password) { if (!password) { return; From af5b9366c3729f46f1e47f99ace8287f945e8b62 Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Sun, 15 Sep 2024 16:16:21 +0300 Subject: [PATCH 03/13] making project ready for tesm PR --- ...uWeatherExample-Questions-checkpoint.ipynb | 275 ++++++++++++++++++ ...WebAPIs-AccuWeatherExample-Questions.ipynb | 275 ++++++++++++++++++ src/meta/configs.js | 2 - src/user/create.js | 2 - 4 files changed, 550 insertions(+), 4 deletions(-) create mode 100644 .ipynb_checkpoints/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions-checkpoint.ipynb create mode 100644 Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions.ipynb diff --git a/.ipynb_checkpoints/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions-checkpoint.ipynb b/.ipynb_checkpoints/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions-checkpoint.ipynb new file mode 100644 index 0000000000..bed2130656 --- /dev/null +++ b/.ipynb_checkpoints/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions-checkpoint.ipynb @@ -0,0 +1,275 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "13933ab3", + "metadata": {}, + "source": [ + "#Part 3 Demo of a RESTful access of the AccuWeather website with an API key\n", + "Step 1. Register on accuweather.com\n", + "Step2: Go to https://developer.accuweather.com/packages and get a free key (remember not to make too many calls - there is a limit)\n", + "Step 3: Use the key for working on Lab 2.3 (set the key in the file api_key.txt and read it into your code). " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "f48627a3-1f6d-42c6-aa20-a317b2843f28", + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n", + "from pathlib import Path" + ] + }, + { + "cell_type": "markdown", + "id": "68a4114c-0e63-4339-b259-e7baa6453299", + "metadata": {}, + "source": [ + "# Get a free key from AccuWeather site (has limited services and limited number of trys in a day)\n", + "\n", + "Insert the file named api_key.txt with the key value and place the file in the same folder as this notebook" + ] + }, + { + "cell_type": "markdown", + "id": "dae2a1c5-86fa-450f-9372-ba2c45af9e56", + "metadata": {}, + "source": [ + "Q1. What does the following function return?\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "7cc88e49", + "metadata": {}, + "source": [ + "This function returns the contents of a file as a text without any trailing spaces or white spaces. It basically cleans the file." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "f46330f6-acd5-4d1c-84c5-9cb2f26bc424", + "metadata": {}, + "outputs": [], + "source": [ + "def read_api_key(filepath):\n", + " # Feel free to modify this function if you are storing the API Key differently\n", + " return Path(filepath).read_text().strip()" + ] + }, + { + "cell_type": "markdown", + "id": "2767380a-06de-4d96-b6ae-fa86d177185e", + "metadata": {}, + "source": [ + "Q2. What does the following function return?\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "6de86387", + "metadata": {}, + "source": [ + "This function returns the city code for a specified city by querying the AccuWeather API. However, if the API does not work it prints an error message and returns 0." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "cf895b5c-42df-4aa9-a2c1-d9dd69c34ebf", + "metadata": {}, + "outputs": [], + "source": [ + "def get_citycode(citystring, api_key):\n", + " url=f\"http://dataservice.accuweather.com/locations/v1/cities/search?apikey={api_key}&q={citystring}\"\n", + " response = requests.get(url)\n", + " if response.status_code == 200:\n", + " data = response.json()\n", + " print(data)\n", + " return data[0]['Key']#271669\n", + " else:\n", + " print(\"Failed to retrieve data from: \"+ url+ \"\\n\"+ str(response))\n", + " return 0\n" + ] + }, + { + "cell_type": "markdown", + "id": "af302688-c771-46df-a303-ce92c7967701", + "metadata": {}, + "source": [ + "Q3. What does the following function return?\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "44276518", + "metadata": {}, + "source": [ + "The following function returns the weather forecast for a specific city using the AccuWeather API. However, if the API does not work then it prints an error message." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ebba52c6-752a-4e30-8c34-065f31f0e9d4", + "metadata": {}, + "outputs": [], + "source": [ + "def get_weather(api_key, city_code):\n", + " url = f\"http://dataservice.accuweather.com/forecasts/v1/daily/1day/{city_code}?apikey={api_key}&details=true&metric=true\"\n", + " response = requests.get(url)\n", + " if response.status_code == 200:\n", + " data = response.json()\n", + " print(data)\n", + " return data\n", + " else:\n", + " print(\"Failed to retrieve data\")\n", + " print(url)\n", + " print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "ee7bc35b-cfa7-40a0-8782-2561a4a890a7", + "metadata": {}, + "source": [ + "Q4. Explain the code in the following cell\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "d4e9dc8c", + "metadata": {}, + "source": [ + "The code in the cell uses the functions that we previously defined to read an API key from a file that we input. Then it uses it to get the city code of a specified city." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "57e14ff2-29bf-43fa-9dfd-a8f9622dca05", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'Version': 1, 'Key': '1310', 'Type': 'City', 'Rank': 35, 'LocalizedName': 'Pittsburgh', 'EnglishName': 'Pittsburgh', 'PrimaryPostalCode': '15219', 'Region': {'ID': 'NAM', 'LocalizedName': 'North America', 'EnglishName': 'North America'}, 'Country': {'ID': 'US', 'LocalizedName': 'United States', 'EnglishName': 'United States'}, 'AdministrativeArea': {'ID': 'PA', 'LocalizedName': 'Pennsylvania', 'EnglishName': 'Pennsylvania', 'Level': 1, 'LocalizedType': 'State', 'EnglishType': 'State', 'CountryID': 'US'}, 'TimeZone': {'Code': 'EDT', 'Name': 'America/New_York', 'GmtOffset': -4.0, 'IsDaylightSaving': True, 'NextOffsetChange': '2024-11-03T06:00:00Z'}, 'GeoPosition': {'Latitude': 40.441, 'Longitude': -79.996, 'Elevation': {'Metric': {'Value': 219.0, 'Unit': 'm', 'UnitType': 5}, 'Imperial': {'Value': 718.0, 'Unit': 'ft', 'UnitType': 0}}}, 'IsAlias': False, 'SupplementalAdminAreas': [{'Level': 2, 'LocalizedName': 'Allegheny', 'EnglishName': 'Allegheny'}], 'DataSets': ['AirQualityCurrentConditions', 'AirQualityForecasts', 'Alerts', 'DailyAirQualityForecast', 'DailyPollenForecast', 'ForecastConfidence', 'FutureRadar', 'MinuteCast', 'ProximityNotification-Lightning', 'Radar']}]\n", + "City code: 1310\n" + ] + } + ], + "source": [ + "api_key = read_api_key(filepath='''/Users/reemkonsowa/Desktop/api/api-key.txt''') # Replace the file with your actual API key\n", + "city_string=\"Pittsburgh, PA\"\n", + "city_code = get_citycode(city_string, api_key)\n", + "print(f\"City code: {city_code}\")" + ] + }, + { + "cell_type": "markdown", + "id": "c67d3a1e-cd18-43be-be6d-132c81c6f50a", + "metadata": {}, + "source": [ + "Q5. Explain the code in the following cell\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "8a225ec2", + "metadata": {}, + "source": [ + "The code in the following cell prints the weather information of a specific city. It stores different information that is being retrieved from other functions and then prints it. " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "f8e616f3-c37f-4865-a3e8-f60324763a9f", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Headline': {'EffectiveDate': '2024-09-07T08:00:00-04:00', 'EffectiveEpochDate': 1725710400, 'Severity': 4, 'Text': 'Noticeably cooler Saturday', 'Category': 'cooler', 'EndDate': '2024-09-07T20:00:00-04:00', 'EndEpochDate': 1725753600, 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us'}, 'DailyForecasts': [{'Date': '2024-09-05T07:00:00-04:00', 'EpochDate': 1725534000, 'Sun': {'Rise': '2024-09-05T06:52:00-04:00', 'EpochRise': 1725533520, 'Set': '2024-09-05T19:44:00-04:00', 'EpochSet': 1725579840}, 'Moon': {'Rise': '2024-09-05T09:13:00-04:00', 'EpochRise': 1725541980, 'Set': '2024-09-05T20:50:00-04:00', 'EpochSet': 1725583800, 'Phase': 'WaxingCrescent', 'Age': 3}, 'Temperature': {'Minimum': {'Value': 14.3, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 28.3, 'Unit': 'C', 'UnitType': 17}}, 'RealFeelTemperature': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 30.4, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'RealFeelTemperatureShade': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 27.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'HoursOfSun': 12.7, 'DegreeDaySummary': {'Heating': {'Value': 0.0, 'Unit': 'C', 'UnitType': 17}, 'Cooling': {'Value': 3.0, 'Unit': 'C', 'UnitType': 17}}, 'AirAndPollen': [{'Name': 'AirQuality', 'Value': 46, 'Category': 'Good', 'CategoryValue': 1, 'Type': 'Nitrogen Dioxide'}, {'Name': 'Grass', 'Value': 12, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Mold', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'Ragweed', 'Value': 30, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Tree', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'UVIndex', 'Value': 6, 'Category': 'High', 'CategoryValue': 3}], 'Day': {'Icon': 1, 'IconPhrase': 'Sunny', 'HasPrecipitation': False, 'ShortPhrase': 'Plenty of sunshine', 'LongPhrase': 'Plenty of sunshine', 'PrecipitationProbability': 0, 'ThunderstormProbability': 0, 'RainProbability': 0, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 128, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 20.4, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 116, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 1, 'Evapotranspiration': {'Value': 4.6, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 7194.5, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 31, 'Maximum': 75, 'Average': 45}, 'WetBulbTemperature': {'Minimum': {'Value': 12.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 17.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 15.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 14.4, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 23.2, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 20.6, 'Unit': 'C', 'UnitType': 17}}}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False, 'ShortPhrase': 'Clear', 'LongPhrase': 'Clear', 'PrecipitationProbability': 2, 'ThunderstormProbability': 0, 'RainProbability': 2, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 129, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 14.8, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 113, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 0, 'Evapotranspiration': {'Value': 0.5, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 71.4, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 43, 'Maximum': 89, 'Average': 69}, 'WetBulbTemperature': {'Minimum': {'Value': 13.5, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 16.3, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 14.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 15.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 21.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 17.6, 'Unit': 'C', 'UnitType': 17}}}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us'}]}\n", + "{'Headline': {'EffectiveDate': '2024-09-07T08:00:00-04:00', 'EffectiveEpochDate': 1725710400, 'Severity': 4, 'Text': 'Noticeably cooler Saturday', 'Category': 'cooler', 'EndDate': '2024-09-07T20:00:00-04:00', 'EndEpochDate': 1725753600, 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us'}, 'DailyForecasts': [{'Date': '2024-09-05T07:00:00-04:00', 'EpochDate': 1725534000, 'Sun': {'Rise': '2024-09-05T06:52:00-04:00', 'EpochRise': 1725533520, 'Set': '2024-09-05T19:44:00-04:00', 'EpochSet': 1725579840}, 'Moon': {'Rise': '2024-09-05T09:13:00-04:00', 'EpochRise': 1725541980, 'Set': '2024-09-05T20:50:00-04:00', 'EpochSet': 1725583800, 'Phase': 'WaxingCrescent', 'Age': 3}, 'Temperature': {'Minimum': {'Value': 14.3, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 28.3, 'Unit': 'C', 'UnitType': 17}}, 'RealFeelTemperature': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 30.4, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'RealFeelTemperatureShade': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 27.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'HoursOfSun': 12.7, 'DegreeDaySummary': {'Heating': {'Value': 0.0, 'Unit': 'C', 'UnitType': 17}, 'Cooling': {'Value': 3.0, 'Unit': 'C', 'UnitType': 17}}, 'AirAndPollen': [{'Name': 'AirQuality', 'Value': 46, 'Category': 'Good', 'CategoryValue': 1, 'Type': 'Nitrogen Dioxide'}, {'Name': 'Grass', 'Value': 12, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Mold', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'Ragweed', 'Value': 30, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Tree', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'UVIndex', 'Value': 6, 'Category': 'High', 'CategoryValue': 3}], 'Day': {'Icon': 1, 'IconPhrase': 'Sunny', 'HasPrecipitation': False, 'ShortPhrase': 'Plenty of sunshine', 'LongPhrase': 'Plenty of sunshine', 'PrecipitationProbability': 0, 'ThunderstormProbability': 0, 'RainProbability': 0, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 128, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 20.4, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 116, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 1, 'Evapotranspiration': {'Value': 4.6, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 7194.5, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 31, 'Maximum': 75, 'Average': 45}, 'WetBulbTemperature': {'Minimum': {'Value': 12.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 17.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 15.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 14.4, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 23.2, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 20.6, 'Unit': 'C', 'UnitType': 17}}}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False, 'ShortPhrase': 'Clear', 'LongPhrase': 'Clear', 'PrecipitationProbability': 2, 'ThunderstormProbability': 0, 'RainProbability': 2, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 129, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 14.8, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 113, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 0, 'Evapotranspiration': {'Value': 0.5, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 71.4, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 43, 'Maximum': 89, 'Average': 69}, 'WetBulbTemperature': {'Minimum': {'Value': 13.5, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 16.3, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 14.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 15.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 21.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 17.6, 'Unit': 'C', 'UnitType': 17}}}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us'}]}\n", + "Pittsburgh, PA\n", + "Headline Text: Noticeably cooler Saturday\n", + "Temperature Minimum Value: 14.3\n" + ] + } + ], + "source": [ + "if (city_code!=0):\n", + " weather_data=get_weather(api_key, city_code)\n", + " print(weather_data)\n", + "# Extract 'Text' from 'Headline'\n", + " headline_text = weather_data['Headline']['Text']\n", + "# Extract 'Temperature Minimum' value\n", + " temperature_minimum = weather_data['DailyForecasts'][0]['Temperature']['Minimum']['Value']\n", + " print(city_string)\n", + " print(\"Headline Text:\", headline_text)\n", + " print(\"Temperature Minimum Value:\", temperature_minimum)\n", + "else: \n", + " print(\"Invalid search term\")" + ] + }, + { + "cell_type": "markdown", + "id": "29a8a8e9-102c-46e0-ad76-87c11f3002fd", + "metadata": {}, + "source": [ + "# Run all the cells and find the minimum temperature in Tucson, Arizona today \n", + "Write the minimum temperature here: \n", + "# Submit both .ipynb and .html files " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3f2cf97c", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.4" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": {}, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions.ipynb b/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions.ipynb new file mode 100644 index 0000000000..bed2130656 --- /dev/null +++ b/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions.ipynb @@ -0,0 +1,275 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "13933ab3", + "metadata": {}, + "source": [ + "#Part 3 Demo of a RESTful access of the AccuWeather website with an API key\n", + "Step 1. Register on accuweather.com\n", + "Step2: Go to https://developer.accuweather.com/packages and get a free key (remember not to make too many calls - there is a limit)\n", + "Step 3: Use the key for working on Lab 2.3 (set the key in the file api_key.txt and read it into your code). " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "f48627a3-1f6d-42c6-aa20-a317b2843f28", + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n", + "from pathlib import Path" + ] + }, + { + "cell_type": "markdown", + "id": "68a4114c-0e63-4339-b259-e7baa6453299", + "metadata": {}, + "source": [ + "# Get a free key from AccuWeather site (has limited services and limited number of trys in a day)\n", + "\n", + "Insert the file named api_key.txt with the key value and place the file in the same folder as this notebook" + ] + }, + { + "cell_type": "markdown", + "id": "dae2a1c5-86fa-450f-9372-ba2c45af9e56", + "metadata": {}, + "source": [ + "Q1. What does the following function return?\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "7cc88e49", + "metadata": {}, + "source": [ + "This function returns the contents of a file as a text without any trailing spaces or white spaces. It basically cleans the file." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "f46330f6-acd5-4d1c-84c5-9cb2f26bc424", + "metadata": {}, + "outputs": [], + "source": [ + "def read_api_key(filepath):\n", + " # Feel free to modify this function if you are storing the API Key differently\n", + " return Path(filepath).read_text().strip()" + ] + }, + { + "cell_type": "markdown", + "id": "2767380a-06de-4d96-b6ae-fa86d177185e", + "metadata": {}, + "source": [ + "Q2. What does the following function return?\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "6de86387", + "metadata": {}, + "source": [ + "This function returns the city code for a specified city by querying the AccuWeather API. However, if the API does not work it prints an error message and returns 0." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "cf895b5c-42df-4aa9-a2c1-d9dd69c34ebf", + "metadata": {}, + "outputs": [], + "source": [ + "def get_citycode(citystring, api_key):\n", + " url=f\"http://dataservice.accuweather.com/locations/v1/cities/search?apikey={api_key}&q={citystring}\"\n", + " response = requests.get(url)\n", + " if response.status_code == 200:\n", + " data = response.json()\n", + " print(data)\n", + " return data[0]['Key']#271669\n", + " else:\n", + " print(\"Failed to retrieve data from: \"+ url+ \"\\n\"+ str(response))\n", + " return 0\n" + ] + }, + { + "cell_type": "markdown", + "id": "af302688-c771-46df-a303-ce92c7967701", + "metadata": {}, + "source": [ + "Q3. What does the following function return?\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "44276518", + "metadata": {}, + "source": [ + "The following function returns the weather forecast for a specific city using the AccuWeather API. However, if the API does not work then it prints an error message." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ebba52c6-752a-4e30-8c34-065f31f0e9d4", + "metadata": {}, + "outputs": [], + "source": [ + "def get_weather(api_key, city_code):\n", + " url = f\"http://dataservice.accuweather.com/forecasts/v1/daily/1day/{city_code}?apikey={api_key}&details=true&metric=true\"\n", + " response = requests.get(url)\n", + " if response.status_code == 200:\n", + " data = response.json()\n", + " print(data)\n", + " return data\n", + " else:\n", + " print(\"Failed to retrieve data\")\n", + " print(url)\n", + " print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "ee7bc35b-cfa7-40a0-8782-2561a4a890a7", + "metadata": {}, + "source": [ + "Q4. Explain the code in the following cell\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "d4e9dc8c", + "metadata": {}, + "source": [ + "The code in the cell uses the functions that we previously defined to read an API key from a file that we input. Then it uses it to get the city code of a specified city." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "57e14ff2-29bf-43fa-9dfd-a8f9622dca05", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'Version': 1, 'Key': '1310', 'Type': 'City', 'Rank': 35, 'LocalizedName': 'Pittsburgh', 'EnglishName': 'Pittsburgh', 'PrimaryPostalCode': '15219', 'Region': {'ID': 'NAM', 'LocalizedName': 'North America', 'EnglishName': 'North America'}, 'Country': {'ID': 'US', 'LocalizedName': 'United States', 'EnglishName': 'United States'}, 'AdministrativeArea': {'ID': 'PA', 'LocalizedName': 'Pennsylvania', 'EnglishName': 'Pennsylvania', 'Level': 1, 'LocalizedType': 'State', 'EnglishType': 'State', 'CountryID': 'US'}, 'TimeZone': {'Code': 'EDT', 'Name': 'America/New_York', 'GmtOffset': -4.0, 'IsDaylightSaving': True, 'NextOffsetChange': '2024-11-03T06:00:00Z'}, 'GeoPosition': {'Latitude': 40.441, 'Longitude': -79.996, 'Elevation': {'Metric': {'Value': 219.0, 'Unit': 'm', 'UnitType': 5}, 'Imperial': {'Value': 718.0, 'Unit': 'ft', 'UnitType': 0}}}, 'IsAlias': False, 'SupplementalAdminAreas': [{'Level': 2, 'LocalizedName': 'Allegheny', 'EnglishName': 'Allegheny'}], 'DataSets': ['AirQualityCurrentConditions', 'AirQualityForecasts', 'Alerts', 'DailyAirQualityForecast', 'DailyPollenForecast', 'ForecastConfidence', 'FutureRadar', 'MinuteCast', 'ProximityNotification-Lightning', 'Radar']}]\n", + "City code: 1310\n" + ] + } + ], + "source": [ + "api_key = read_api_key(filepath='''/Users/reemkonsowa/Desktop/api/api-key.txt''') # Replace the file with your actual API key\n", + "city_string=\"Pittsburgh, PA\"\n", + "city_code = get_citycode(city_string, api_key)\n", + "print(f\"City code: {city_code}\")" + ] + }, + { + "cell_type": "markdown", + "id": "c67d3a1e-cd18-43be-be6d-132c81c6f50a", + "metadata": {}, + "source": [ + "Q5. Explain the code in the following cell\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "8a225ec2", + "metadata": {}, + "source": [ + "The code in the following cell prints the weather information of a specific city. It stores different information that is being retrieved from other functions and then prints it. " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "f8e616f3-c37f-4865-a3e8-f60324763a9f", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Headline': {'EffectiveDate': '2024-09-07T08:00:00-04:00', 'EffectiveEpochDate': 1725710400, 'Severity': 4, 'Text': 'Noticeably cooler Saturday', 'Category': 'cooler', 'EndDate': '2024-09-07T20:00:00-04:00', 'EndEpochDate': 1725753600, 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us'}, 'DailyForecasts': [{'Date': '2024-09-05T07:00:00-04:00', 'EpochDate': 1725534000, 'Sun': {'Rise': '2024-09-05T06:52:00-04:00', 'EpochRise': 1725533520, 'Set': '2024-09-05T19:44:00-04:00', 'EpochSet': 1725579840}, 'Moon': {'Rise': '2024-09-05T09:13:00-04:00', 'EpochRise': 1725541980, 'Set': '2024-09-05T20:50:00-04:00', 'EpochSet': 1725583800, 'Phase': 'WaxingCrescent', 'Age': 3}, 'Temperature': {'Minimum': {'Value': 14.3, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 28.3, 'Unit': 'C', 'UnitType': 17}}, 'RealFeelTemperature': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 30.4, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'RealFeelTemperatureShade': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 27.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'HoursOfSun': 12.7, 'DegreeDaySummary': {'Heating': {'Value': 0.0, 'Unit': 'C', 'UnitType': 17}, 'Cooling': {'Value': 3.0, 'Unit': 'C', 'UnitType': 17}}, 'AirAndPollen': [{'Name': 'AirQuality', 'Value': 46, 'Category': 'Good', 'CategoryValue': 1, 'Type': 'Nitrogen Dioxide'}, {'Name': 'Grass', 'Value': 12, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Mold', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'Ragweed', 'Value': 30, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Tree', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'UVIndex', 'Value': 6, 'Category': 'High', 'CategoryValue': 3}], 'Day': {'Icon': 1, 'IconPhrase': 'Sunny', 'HasPrecipitation': False, 'ShortPhrase': 'Plenty of sunshine', 'LongPhrase': 'Plenty of sunshine', 'PrecipitationProbability': 0, 'ThunderstormProbability': 0, 'RainProbability': 0, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 128, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 20.4, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 116, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 1, 'Evapotranspiration': {'Value': 4.6, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 7194.5, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 31, 'Maximum': 75, 'Average': 45}, 'WetBulbTemperature': {'Minimum': {'Value': 12.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 17.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 15.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 14.4, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 23.2, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 20.6, 'Unit': 'C', 'UnitType': 17}}}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False, 'ShortPhrase': 'Clear', 'LongPhrase': 'Clear', 'PrecipitationProbability': 2, 'ThunderstormProbability': 0, 'RainProbability': 2, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 129, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 14.8, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 113, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 0, 'Evapotranspiration': {'Value': 0.5, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 71.4, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 43, 'Maximum': 89, 'Average': 69}, 'WetBulbTemperature': {'Minimum': {'Value': 13.5, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 16.3, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 14.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 15.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 21.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 17.6, 'Unit': 'C', 'UnitType': 17}}}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us'}]}\n", + "{'Headline': {'EffectiveDate': '2024-09-07T08:00:00-04:00', 'EffectiveEpochDate': 1725710400, 'Severity': 4, 'Text': 'Noticeably cooler Saturday', 'Category': 'cooler', 'EndDate': '2024-09-07T20:00:00-04:00', 'EndEpochDate': 1725753600, 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us'}, 'DailyForecasts': [{'Date': '2024-09-05T07:00:00-04:00', 'EpochDate': 1725534000, 'Sun': {'Rise': '2024-09-05T06:52:00-04:00', 'EpochRise': 1725533520, 'Set': '2024-09-05T19:44:00-04:00', 'EpochSet': 1725579840}, 'Moon': {'Rise': '2024-09-05T09:13:00-04:00', 'EpochRise': 1725541980, 'Set': '2024-09-05T20:50:00-04:00', 'EpochSet': 1725583800, 'Phase': 'WaxingCrescent', 'Age': 3}, 'Temperature': {'Minimum': {'Value': 14.3, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 28.3, 'Unit': 'C', 'UnitType': 17}}, 'RealFeelTemperature': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 30.4, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'RealFeelTemperatureShade': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 27.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'HoursOfSun': 12.7, 'DegreeDaySummary': {'Heating': {'Value': 0.0, 'Unit': 'C', 'UnitType': 17}, 'Cooling': {'Value': 3.0, 'Unit': 'C', 'UnitType': 17}}, 'AirAndPollen': [{'Name': 'AirQuality', 'Value': 46, 'Category': 'Good', 'CategoryValue': 1, 'Type': 'Nitrogen Dioxide'}, {'Name': 'Grass', 'Value': 12, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Mold', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'Ragweed', 'Value': 30, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Tree', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'UVIndex', 'Value': 6, 'Category': 'High', 'CategoryValue': 3}], 'Day': {'Icon': 1, 'IconPhrase': 'Sunny', 'HasPrecipitation': False, 'ShortPhrase': 'Plenty of sunshine', 'LongPhrase': 'Plenty of sunshine', 'PrecipitationProbability': 0, 'ThunderstormProbability': 0, 'RainProbability': 0, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 128, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 20.4, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 116, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 1, 'Evapotranspiration': {'Value': 4.6, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 7194.5, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 31, 'Maximum': 75, 'Average': 45}, 'WetBulbTemperature': {'Minimum': {'Value': 12.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 17.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 15.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 14.4, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 23.2, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 20.6, 'Unit': 'C', 'UnitType': 17}}}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False, 'ShortPhrase': 'Clear', 'LongPhrase': 'Clear', 'PrecipitationProbability': 2, 'ThunderstormProbability': 0, 'RainProbability': 2, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 129, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 14.8, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 113, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 0, 'Evapotranspiration': {'Value': 0.5, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 71.4, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 43, 'Maximum': 89, 'Average': 69}, 'WetBulbTemperature': {'Minimum': {'Value': 13.5, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 16.3, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 14.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 15.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 21.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 17.6, 'Unit': 'C', 'UnitType': 17}}}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us'}]}\n", + "Pittsburgh, PA\n", + "Headline Text: Noticeably cooler Saturday\n", + "Temperature Minimum Value: 14.3\n" + ] + } + ], + "source": [ + "if (city_code!=0):\n", + " weather_data=get_weather(api_key, city_code)\n", + " print(weather_data)\n", + "# Extract 'Text' from 'Headline'\n", + " headline_text = weather_data['Headline']['Text']\n", + "# Extract 'Temperature Minimum' value\n", + " temperature_minimum = weather_data['DailyForecasts'][0]['Temperature']['Minimum']['Value']\n", + " print(city_string)\n", + " print(\"Headline Text:\", headline_text)\n", + " print(\"Temperature Minimum Value:\", temperature_minimum)\n", + "else: \n", + " print(\"Invalid search term\")" + ] + }, + { + "cell_type": "markdown", + "id": "29a8a8e9-102c-46e0-ad76-87c11f3002fd", + "metadata": {}, + "source": [ + "# Run all the cells and find the minimum temperature in Tucson, Arizona today \n", + "Write the minimum temperature here: \n", + "# Submit both .ipynb and .html files " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3f2cf97c", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.4" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": {}, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/src/meta/configs.js b/src/meta/configs.js index c920aa7f7a..6e41a7ca89 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -64,8 +64,6 @@ function handleTypeConversion(defaultType, type, value, number, key) { return value; } -console.log('Reem'); - function handleNumberConversion(number, value, key) { if (!isNaN(number) && isFinite(value)) { return number; diff --git a/src/user/create.js b/src/user/create.js index c573243bab..610d614e81 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -122,8 +122,6 @@ module.exports = function (User) { return userData.uid; } - console.log('Reem'); - async function storePassword(uid, password) { if (!password) { return; From ec561a6bb77f017638fd29d6516d725a31e62a36 Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Sun, 15 Sep 2024 16:39:44 +0300 Subject: [PATCH 04/13] adding console print --- src/meta/configs.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/meta/configs.js b/src/meta/configs.js index 6e41a7ca89..b0ba17206b 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -80,6 +80,8 @@ function handleArrayConversion(value, key) { } } +console.log('Reem'); + // called before data is saved to db function serialize(config) { From 86549c921f157a21b00f03f9e6e066d3cc560917 Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Sun, 15 Sep 2024 16:42:47 +0300 Subject: [PATCH 05/13] trying to fix sonar error --- src/user/create.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/user/create.js b/src/user/create.js index 610d614e81..c573243bab 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -122,6 +122,8 @@ module.exports = function (User) { return userData.uid; } + console.log('Reem'); + async function storePassword(uid, password) { if (!password) { return; From fe2f4dd3a4ff1ee6b8e3e39b71ef1831c6258d6e Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Sun, 15 Sep 2024 23:39:11 +0300 Subject: [PATCH 06/13] removing console --- src/meta/configs.js | 3 --- src/user/create.js | 2 -- 2 files changed, 5 deletions(-) diff --git a/src/meta/configs.js b/src/meta/configs.js index b0ba17206b..974bdfbd1e 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -80,9 +80,6 @@ function handleArrayConversion(value, key) { } } -console.log('Reem'); - - // called before data is saved to db function serialize(config) { const serialized = {}; diff --git a/src/user/create.js b/src/user/create.js index c573243bab..610d614e81 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -122,8 +122,6 @@ module.exports = function (User) { return userData.uid; } - console.log('Reem'); - async function storePassword(uid, password) { if (!password) { return; From 15ce234805f9a31d5c695611e29c5de536ce0168 Mon Sep 17 00:00:00 2001 From: Reem Konsowa Date: Sun, 15 Sep 2024 23:58:20 +0300 Subject: [PATCH 07/13] fixing testing issues --- src/user/create.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/user/create.js b/src/user/create.js index 610d614e81..70263f8704 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -122,6 +122,7 @@ module.exports = function (User) { return userData.uid; } + async function storePassword(uid, password) { if (!password) { return; From e0805bfb56ca926a031580294641961f05c70884 Mon Sep 17 00:00:00 2001 From: rkensouh <155992402+rkensouh@users.noreply.github.com> Date: Mon, 16 Sep 2024 09:40:36 +0300 Subject: [PATCH 08/13] Delete .ipynb_checkpoints/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions-checkpoint.ipynb --- ...uWeatherExample-Questions-checkpoint.ipynb | 275 ------------------ 1 file changed, 275 deletions(-) delete mode 100644 .ipynb_checkpoints/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions-checkpoint.ipynb diff --git a/.ipynb_checkpoints/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions-checkpoint.ipynb b/.ipynb_checkpoints/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions-checkpoint.ipynb deleted file mode 100644 index bed2130656..0000000000 --- a/.ipynb_checkpoints/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions-checkpoint.ipynb +++ /dev/null @@ -1,275 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "13933ab3", - "metadata": {}, - "source": [ - "#Part 3 Demo of a RESTful access of the AccuWeather website with an API key\n", - "Step 1. Register on accuweather.com\n", - "Step2: Go to https://developer.accuweather.com/packages and get a free key (remember not to make too many calls - there is a limit)\n", - "Step 3: Use the key for working on Lab 2.3 (set the key in the file api_key.txt and read it into your code). " - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "f48627a3-1f6d-42c6-aa20-a317b2843f28", - "metadata": {}, - "outputs": [], - "source": [ - "import requests\n", - "from pathlib import Path" - ] - }, - { - "cell_type": "markdown", - "id": "68a4114c-0e63-4339-b259-e7baa6453299", - "metadata": {}, - "source": [ - "# Get a free key from AccuWeather site (has limited services and limited number of trys in a day)\n", - "\n", - "Insert the file named api_key.txt with the key value and place the file in the same folder as this notebook" - ] - }, - { - "cell_type": "markdown", - "id": "dae2a1c5-86fa-450f-9372-ba2c45af9e56", - "metadata": {}, - "source": [ - "Q1. What does the following function return?\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "id": "7cc88e49", - "metadata": {}, - "source": [ - "This function returns the contents of a file as a text without any trailing spaces or white spaces. It basically cleans the file." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "f46330f6-acd5-4d1c-84c5-9cb2f26bc424", - "metadata": {}, - "outputs": [], - "source": [ - "def read_api_key(filepath):\n", - " # Feel free to modify this function if you are storing the API Key differently\n", - " return Path(filepath).read_text().strip()" - ] - }, - { - "cell_type": "markdown", - "id": "2767380a-06de-4d96-b6ae-fa86d177185e", - "metadata": {}, - "source": [ - "Q2. What does the following function return?\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "id": "6de86387", - "metadata": {}, - "source": [ - "This function returns the city code for a specified city by querying the AccuWeather API. However, if the API does not work it prints an error message and returns 0." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "cf895b5c-42df-4aa9-a2c1-d9dd69c34ebf", - "metadata": {}, - "outputs": [], - "source": [ - "def get_citycode(citystring, api_key):\n", - " url=f\"http://dataservice.accuweather.com/locations/v1/cities/search?apikey={api_key}&q={citystring}\"\n", - " response = requests.get(url)\n", - " if response.status_code == 200:\n", - " data = response.json()\n", - " print(data)\n", - " return data[0]['Key']#271669\n", - " else:\n", - " print(\"Failed to retrieve data from: \"+ url+ \"\\n\"+ str(response))\n", - " return 0\n" - ] - }, - { - "cell_type": "markdown", - "id": "af302688-c771-46df-a303-ce92c7967701", - "metadata": {}, - "source": [ - "Q3. What does the following function return?\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "id": "44276518", - "metadata": {}, - "source": [ - "The following function returns the weather forecast for a specific city using the AccuWeather API. However, if the API does not work then it prints an error message." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "ebba52c6-752a-4e30-8c34-065f31f0e9d4", - "metadata": {}, - "outputs": [], - "source": [ - "def get_weather(api_key, city_code):\n", - " url = f\"http://dataservice.accuweather.com/forecasts/v1/daily/1day/{city_code}?apikey={api_key}&details=true&metric=true\"\n", - " response = requests.get(url)\n", - " if response.status_code == 200:\n", - " data = response.json()\n", - " print(data)\n", - " return data\n", - " else:\n", - " print(\"Failed to retrieve data\")\n", - " print(url)\n", - " print(response)" - ] - }, - { - "cell_type": "markdown", - "id": "ee7bc35b-cfa7-40a0-8782-2561a4a890a7", - "metadata": {}, - "source": [ - "Q4. Explain the code in the following cell\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "id": "d4e9dc8c", - "metadata": {}, - "source": [ - "The code in the cell uses the functions that we previously defined to read an API key from a file that we input. Then it uses it to get the city code of a specified city." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "57e14ff2-29bf-43fa-9dfd-a8f9622dca05", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[{'Version': 1, 'Key': '1310', 'Type': 'City', 'Rank': 35, 'LocalizedName': 'Pittsburgh', 'EnglishName': 'Pittsburgh', 'PrimaryPostalCode': '15219', 'Region': {'ID': 'NAM', 'LocalizedName': 'North America', 'EnglishName': 'North America'}, 'Country': {'ID': 'US', 'LocalizedName': 'United States', 'EnglishName': 'United States'}, 'AdministrativeArea': {'ID': 'PA', 'LocalizedName': 'Pennsylvania', 'EnglishName': 'Pennsylvania', 'Level': 1, 'LocalizedType': 'State', 'EnglishType': 'State', 'CountryID': 'US'}, 'TimeZone': {'Code': 'EDT', 'Name': 'America/New_York', 'GmtOffset': -4.0, 'IsDaylightSaving': True, 'NextOffsetChange': '2024-11-03T06:00:00Z'}, 'GeoPosition': {'Latitude': 40.441, 'Longitude': -79.996, 'Elevation': {'Metric': {'Value': 219.0, 'Unit': 'm', 'UnitType': 5}, 'Imperial': {'Value': 718.0, 'Unit': 'ft', 'UnitType': 0}}}, 'IsAlias': False, 'SupplementalAdminAreas': [{'Level': 2, 'LocalizedName': 'Allegheny', 'EnglishName': 'Allegheny'}], 'DataSets': ['AirQualityCurrentConditions', 'AirQualityForecasts', 'Alerts', 'DailyAirQualityForecast', 'DailyPollenForecast', 'ForecastConfidence', 'FutureRadar', 'MinuteCast', 'ProximityNotification-Lightning', 'Radar']}]\n", - "City code: 1310\n" - ] - } - ], - "source": [ - "api_key = read_api_key(filepath='''/Users/reemkonsowa/Desktop/api/api-key.txt''') # Replace the file with your actual API key\n", - "city_string=\"Pittsburgh, PA\"\n", - "city_code = get_citycode(city_string, api_key)\n", - "print(f\"City code: {city_code}\")" - ] - }, - { - "cell_type": "markdown", - "id": "c67d3a1e-cd18-43be-be6d-132c81c6f50a", - "metadata": {}, - "source": [ - "Q5. Explain the code in the following cell\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "id": "8a225ec2", - "metadata": {}, - "source": [ - "The code in the following cell prints the weather information of a specific city. It stores different information that is being retrieved from other functions and then prints it. " - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "f8e616f3-c37f-4865-a3e8-f60324763a9f", - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Headline': {'EffectiveDate': '2024-09-07T08:00:00-04:00', 'EffectiveEpochDate': 1725710400, 'Severity': 4, 'Text': 'Noticeably cooler Saturday', 'Category': 'cooler', 'EndDate': '2024-09-07T20:00:00-04:00', 'EndEpochDate': 1725753600, 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us'}, 'DailyForecasts': [{'Date': '2024-09-05T07:00:00-04:00', 'EpochDate': 1725534000, 'Sun': {'Rise': '2024-09-05T06:52:00-04:00', 'EpochRise': 1725533520, 'Set': '2024-09-05T19:44:00-04:00', 'EpochSet': 1725579840}, 'Moon': {'Rise': '2024-09-05T09:13:00-04:00', 'EpochRise': 1725541980, 'Set': '2024-09-05T20:50:00-04:00', 'EpochSet': 1725583800, 'Phase': 'WaxingCrescent', 'Age': 3}, 'Temperature': {'Minimum': {'Value': 14.3, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 28.3, 'Unit': 'C', 'UnitType': 17}}, 'RealFeelTemperature': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 30.4, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'RealFeelTemperatureShade': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 27.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'HoursOfSun': 12.7, 'DegreeDaySummary': {'Heating': {'Value': 0.0, 'Unit': 'C', 'UnitType': 17}, 'Cooling': {'Value': 3.0, 'Unit': 'C', 'UnitType': 17}}, 'AirAndPollen': [{'Name': 'AirQuality', 'Value': 46, 'Category': 'Good', 'CategoryValue': 1, 'Type': 'Nitrogen Dioxide'}, {'Name': 'Grass', 'Value': 12, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Mold', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'Ragweed', 'Value': 30, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Tree', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'UVIndex', 'Value': 6, 'Category': 'High', 'CategoryValue': 3}], 'Day': {'Icon': 1, 'IconPhrase': 'Sunny', 'HasPrecipitation': False, 'ShortPhrase': 'Plenty of sunshine', 'LongPhrase': 'Plenty of sunshine', 'PrecipitationProbability': 0, 'ThunderstormProbability': 0, 'RainProbability': 0, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 128, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 20.4, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 116, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 1, 'Evapotranspiration': {'Value': 4.6, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 7194.5, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 31, 'Maximum': 75, 'Average': 45}, 'WetBulbTemperature': {'Minimum': {'Value': 12.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 17.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 15.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 14.4, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 23.2, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 20.6, 'Unit': 'C', 'UnitType': 17}}}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False, 'ShortPhrase': 'Clear', 'LongPhrase': 'Clear', 'PrecipitationProbability': 2, 'ThunderstormProbability': 0, 'RainProbability': 2, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 129, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 14.8, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 113, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 0, 'Evapotranspiration': {'Value': 0.5, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 71.4, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 43, 'Maximum': 89, 'Average': 69}, 'WetBulbTemperature': {'Minimum': {'Value': 13.5, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 16.3, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 14.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 15.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 21.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 17.6, 'Unit': 'C', 'UnitType': 17}}}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us'}]}\n", - "{'Headline': {'EffectiveDate': '2024-09-07T08:00:00-04:00', 'EffectiveEpochDate': 1725710400, 'Severity': 4, 'Text': 'Noticeably cooler Saturday', 'Category': 'cooler', 'EndDate': '2024-09-07T20:00:00-04:00', 'EndEpochDate': 1725753600, 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us'}, 'DailyForecasts': [{'Date': '2024-09-05T07:00:00-04:00', 'EpochDate': 1725534000, 'Sun': {'Rise': '2024-09-05T06:52:00-04:00', 'EpochRise': 1725533520, 'Set': '2024-09-05T19:44:00-04:00', 'EpochSet': 1725579840}, 'Moon': {'Rise': '2024-09-05T09:13:00-04:00', 'EpochRise': 1725541980, 'Set': '2024-09-05T20:50:00-04:00', 'EpochSet': 1725583800, 'Phase': 'WaxingCrescent', 'Age': 3}, 'Temperature': {'Minimum': {'Value': 14.3, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 28.3, 'Unit': 'C', 'UnitType': 17}}, 'RealFeelTemperature': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 30.4, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'RealFeelTemperatureShade': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 27.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'HoursOfSun': 12.7, 'DegreeDaySummary': {'Heating': {'Value': 0.0, 'Unit': 'C', 'UnitType': 17}, 'Cooling': {'Value': 3.0, 'Unit': 'C', 'UnitType': 17}}, 'AirAndPollen': [{'Name': 'AirQuality', 'Value': 46, 'Category': 'Good', 'CategoryValue': 1, 'Type': 'Nitrogen Dioxide'}, {'Name': 'Grass', 'Value': 12, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Mold', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'Ragweed', 'Value': 30, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Tree', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'UVIndex', 'Value': 6, 'Category': 'High', 'CategoryValue': 3}], 'Day': {'Icon': 1, 'IconPhrase': 'Sunny', 'HasPrecipitation': False, 'ShortPhrase': 'Plenty of sunshine', 'LongPhrase': 'Plenty of sunshine', 'PrecipitationProbability': 0, 'ThunderstormProbability': 0, 'RainProbability': 0, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 128, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 20.4, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 116, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 1, 'Evapotranspiration': {'Value': 4.6, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 7194.5, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 31, 'Maximum': 75, 'Average': 45}, 'WetBulbTemperature': {'Minimum': {'Value': 12.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 17.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 15.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 14.4, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 23.2, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 20.6, 'Unit': 'C', 'UnitType': 17}}}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False, 'ShortPhrase': 'Clear', 'LongPhrase': 'Clear', 'PrecipitationProbability': 2, 'ThunderstormProbability': 0, 'RainProbability': 2, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 129, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 14.8, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 113, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 0, 'Evapotranspiration': {'Value': 0.5, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 71.4, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 43, 'Maximum': 89, 'Average': 69}, 'WetBulbTemperature': {'Minimum': {'Value': 13.5, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 16.3, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 14.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 15.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 21.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 17.6, 'Unit': 'C', 'UnitType': 17}}}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us'}]}\n", - "Pittsburgh, PA\n", - "Headline Text: Noticeably cooler Saturday\n", - "Temperature Minimum Value: 14.3\n" - ] - } - ], - "source": [ - "if (city_code!=0):\n", - " weather_data=get_weather(api_key, city_code)\n", - " print(weather_data)\n", - "# Extract 'Text' from 'Headline'\n", - " headline_text = weather_data['Headline']['Text']\n", - "# Extract 'Temperature Minimum' value\n", - " temperature_minimum = weather_data['DailyForecasts'][0]['Temperature']['Minimum']['Value']\n", - " print(city_string)\n", - " print(\"Headline Text:\", headline_text)\n", - " print(\"Temperature Minimum Value:\", temperature_minimum)\n", - "else: \n", - " print(\"Invalid search term\")" - ] - }, - { - "cell_type": "markdown", - "id": "29a8a8e9-102c-46e0-ad76-87c11f3002fd", - "metadata": {}, - "source": [ - "# Run all the cells and find the minimum temperature in Tucson, Arizona today \n", - "Write the minimum temperature here: \n", - "# Submit both .ipynb and .html files " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3f2cf97c", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.4" - }, - "widgets": { - "application/vnd.jupyter.widget-state+json": { - "state": {}, - "version_major": 2, - "version_minor": 0 - } - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} From ba1e222a8d031b409effc7894a45472e58b57c3f Mon Sep 17 00:00:00 2001 From: rkensouh <155992402+rkensouh@users.noreply.github.com> Date: Mon, 16 Sep 2024 09:40:56 +0300 Subject: [PATCH 09/13] Delete Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions.ipynb --- ...WebAPIs-AccuWeatherExample-Questions.ipynb | 275 ------------------ 1 file changed, 275 deletions(-) delete mode 100644 Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions.ipynb diff --git a/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions.ipynb b/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions.ipynb deleted file mode 100644 index bed2130656..0000000000 --- a/Lab2.3-UsingWebAPIs-AccuWeatherExample-Questions.ipynb +++ /dev/null @@ -1,275 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "13933ab3", - "metadata": {}, - "source": [ - "#Part 3 Demo of a RESTful access of the AccuWeather website with an API key\n", - "Step 1. Register on accuweather.com\n", - "Step2: Go to https://developer.accuweather.com/packages and get a free key (remember not to make too many calls - there is a limit)\n", - "Step 3: Use the key for working on Lab 2.3 (set the key in the file api_key.txt and read it into your code). " - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "f48627a3-1f6d-42c6-aa20-a317b2843f28", - "metadata": {}, - "outputs": [], - "source": [ - "import requests\n", - "from pathlib import Path" - ] - }, - { - "cell_type": "markdown", - "id": "68a4114c-0e63-4339-b259-e7baa6453299", - "metadata": {}, - "source": [ - "# Get a free key from AccuWeather site (has limited services and limited number of trys in a day)\n", - "\n", - "Insert the file named api_key.txt with the key value and place the file in the same folder as this notebook" - ] - }, - { - "cell_type": "markdown", - "id": "dae2a1c5-86fa-450f-9372-ba2c45af9e56", - "metadata": {}, - "source": [ - "Q1. What does the following function return?\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "id": "7cc88e49", - "metadata": {}, - "source": [ - "This function returns the contents of a file as a text without any trailing spaces or white spaces. It basically cleans the file." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "f46330f6-acd5-4d1c-84c5-9cb2f26bc424", - "metadata": {}, - "outputs": [], - "source": [ - "def read_api_key(filepath):\n", - " # Feel free to modify this function if you are storing the API Key differently\n", - " return Path(filepath).read_text().strip()" - ] - }, - { - "cell_type": "markdown", - "id": "2767380a-06de-4d96-b6ae-fa86d177185e", - "metadata": {}, - "source": [ - "Q2. What does the following function return?\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "id": "6de86387", - "metadata": {}, - "source": [ - "This function returns the city code for a specified city by querying the AccuWeather API. However, if the API does not work it prints an error message and returns 0." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "cf895b5c-42df-4aa9-a2c1-d9dd69c34ebf", - "metadata": {}, - "outputs": [], - "source": [ - "def get_citycode(citystring, api_key):\n", - " url=f\"http://dataservice.accuweather.com/locations/v1/cities/search?apikey={api_key}&q={citystring}\"\n", - " response = requests.get(url)\n", - " if response.status_code == 200:\n", - " data = response.json()\n", - " print(data)\n", - " return data[0]['Key']#271669\n", - " else:\n", - " print(\"Failed to retrieve data from: \"+ url+ \"\\n\"+ str(response))\n", - " return 0\n" - ] - }, - { - "cell_type": "markdown", - "id": "af302688-c771-46df-a303-ce92c7967701", - "metadata": {}, - "source": [ - "Q3. What does the following function return?\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "id": "44276518", - "metadata": {}, - "source": [ - "The following function returns the weather forecast for a specific city using the AccuWeather API. However, if the API does not work then it prints an error message." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "ebba52c6-752a-4e30-8c34-065f31f0e9d4", - "metadata": {}, - "outputs": [], - "source": [ - "def get_weather(api_key, city_code):\n", - " url = f\"http://dataservice.accuweather.com/forecasts/v1/daily/1day/{city_code}?apikey={api_key}&details=true&metric=true\"\n", - " response = requests.get(url)\n", - " if response.status_code == 200:\n", - " data = response.json()\n", - " print(data)\n", - " return data\n", - " else:\n", - " print(\"Failed to retrieve data\")\n", - " print(url)\n", - " print(response)" - ] - }, - { - "cell_type": "markdown", - "id": "ee7bc35b-cfa7-40a0-8782-2561a4a890a7", - "metadata": {}, - "source": [ - "Q4. Explain the code in the following cell\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "id": "d4e9dc8c", - "metadata": {}, - "source": [ - "The code in the cell uses the functions that we previously defined to read an API key from a file that we input. Then it uses it to get the city code of a specified city." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "57e14ff2-29bf-43fa-9dfd-a8f9622dca05", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[{'Version': 1, 'Key': '1310', 'Type': 'City', 'Rank': 35, 'LocalizedName': 'Pittsburgh', 'EnglishName': 'Pittsburgh', 'PrimaryPostalCode': '15219', 'Region': {'ID': 'NAM', 'LocalizedName': 'North America', 'EnglishName': 'North America'}, 'Country': {'ID': 'US', 'LocalizedName': 'United States', 'EnglishName': 'United States'}, 'AdministrativeArea': {'ID': 'PA', 'LocalizedName': 'Pennsylvania', 'EnglishName': 'Pennsylvania', 'Level': 1, 'LocalizedType': 'State', 'EnglishType': 'State', 'CountryID': 'US'}, 'TimeZone': {'Code': 'EDT', 'Name': 'America/New_York', 'GmtOffset': -4.0, 'IsDaylightSaving': True, 'NextOffsetChange': '2024-11-03T06:00:00Z'}, 'GeoPosition': {'Latitude': 40.441, 'Longitude': -79.996, 'Elevation': {'Metric': {'Value': 219.0, 'Unit': 'm', 'UnitType': 5}, 'Imperial': {'Value': 718.0, 'Unit': 'ft', 'UnitType': 0}}}, 'IsAlias': False, 'SupplementalAdminAreas': [{'Level': 2, 'LocalizedName': 'Allegheny', 'EnglishName': 'Allegheny'}], 'DataSets': ['AirQualityCurrentConditions', 'AirQualityForecasts', 'Alerts', 'DailyAirQualityForecast', 'DailyPollenForecast', 'ForecastConfidence', 'FutureRadar', 'MinuteCast', 'ProximityNotification-Lightning', 'Radar']}]\n", - "City code: 1310\n" - ] - } - ], - "source": [ - "api_key = read_api_key(filepath='''/Users/reemkonsowa/Desktop/api/api-key.txt''') # Replace the file with your actual API key\n", - "city_string=\"Pittsburgh, PA\"\n", - "city_code = get_citycode(city_string, api_key)\n", - "print(f\"City code: {city_code}\")" - ] - }, - { - "cell_type": "markdown", - "id": "c67d3a1e-cd18-43be-be6d-132c81c6f50a", - "metadata": {}, - "source": [ - "Q5. Explain the code in the following cell\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "id": "8a225ec2", - "metadata": {}, - "source": [ - "The code in the following cell prints the weather information of a specific city. It stores different information that is being retrieved from other functions and then prints it. " - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "f8e616f3-c37f-4865-a3e8-f60324763a9f", - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Headline': {'EffectiveDate': '2024-09-07T08:00:00-04:00', 'EffectiveEpochDate': 1725710400, 'Severity': 4, 'Text': 'Noticeably cooler Saturday', 'Category': 'cooler', 'EndDate': '2024-09-07T20:00:00-04:00', 'EndEpochDate': 1725753600, 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us'}, 'DailyForecasts': [{'Date': '2024-09-05T07:00:00-04:00', 'EpochDate': 1725534000, 'Sun': {'Rise': '2024-09-05T06:52:00-04:00', 'EpochRise': 1725533520, 'Set': '2024-09-05T19:44:00-04:00', 'EpochSet': 1725579840}, 'Moon': {'Rise': '2024-09-05T09:13:00-04:00', 'EpochRise': 1725541980, 'Set': '2024-09-05T20:50:00-04:00', 'EpochSet': 1725583800, 'Phase': 'WaxingCrescent', 'Age': 3}, 'Temperature': {'Minimum': {'Value': 14.3, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 28.3, 'Unit': 'C', 'UnitType': 17}}, 'RealFeelTemperature': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 30.4, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'RealFeelTemperatureShade': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 27.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'HoursOfSun': 12.7, 'DegreeDaySummary': {'Heating': {'Value': 0.0, 'Unit': 'C', 'UnitType': 17}, 'Cooling': {'Value': 3.0, 'Unit': 'C', 'UnitType': 17}}, 'AirAndPollen': [{'Name': 'AirQuality', 'Value': 46, 'Category': 'Good', 'CategoryValue': 1, 'Type': 'Nitrogen Dioxide'}, {'Name': 'Grass', 'Value': 12, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Mold', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'Ragweed', 'Value': 30, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Tree', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'UVIndex', 'Value': 6, 'Category': 'High', 'CategoryValue': 3}], 'Day': {'Icon': 1, 'IconPhrase': 'Sunny', 'HasPrecipitation': False, 'ShortPhrase': 'Plenty of sunshine', 'LongPhrase': 'Plenty of sunshine', 'PrecipitationProbability': 0, 'ThunderstormProbability': 0, 'RainProbability': 0, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 128, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 20.4, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 116, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 1, 'Evapotranspiration': {'Value': 4.6, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 7194.5, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 31, 'Maximum': 75, 'Average': 45}, 'WetBulbTemperature': {'Minimum': {'Value': 12.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 17.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 15.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 14.4, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 23.2, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 20.6, 'Unit': 'C', 'UnitType': 17}}}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False, 'ShortPhrase': 'Clear', 'LongPhrase': 'Clear', 'PrecipitationProbability': 2, 'ThunderstormProbability': 0, 'RainProbability': 2, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 129, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 14.8, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 113, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 0, 'Evapotranspiration': {'Value': 0.5, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 71.4, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 43, 'Maximum': 89, 'Average': 69}, 'WetBulbTemperature': {'Minimum': {'Value': 13.5, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 16.3, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 14.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 15.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 21.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 17.6, 'Unit': 'C', 'UnitType': 17}}}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us'}]}\n", - "{'Headline': {'EffectiveDate': '2024-09-07T08:00:00-04:00', 'EffectiveEpochDate': 1725710400, 'Severity': 4, 'Text': 'Noticeably cooler Saturday', 'Category': 'cooler', 'EndDate': '2024-09-07T20:00:00-04:00', 'EndEpochDate': 1725753600, 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?unit=c&lang=en-us'}, 'DailyForecasts': [{'Date': '2024-09-05T07:00:00-04:00', 'EpochDate': 1725534000, 'Sun': {'Rise': '2024-09-05T06:52:00-04:00', 'EpochRise': 1725533520, 'Set': '2024-09-05T19:44:00-04:00', 'EpochSet': 1725579840}, 'Moon': {'Rise': '2024-09-05T09:13:00-04:00', 'EpochRise': 1725541980, 'Set': '2024-09-05T20:50:00-04:00', 'EpochSet': 1725583800, 'Phase': 'WaxingCrescent', 'Age': 3}, 'Temperature': {'Minimum': {'Value': 14.3, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 28.3, 'Unit': 'C', 'UnitType': 17}}, 'RealFeelTemperature': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 30.4, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'RealFeelTemperatureShade': {'Minimum': {'Value': 14.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Cool'}, 'Maximum': {'Value': 27.0, 'Unit': 'C', 'UnitType': 17, 'Phrase': 'Very Warm'}}, 'HoursOfSun': 12.7, 'DegreeDaySummary': {'Heating': {'Value': 0.0, 'Unit': 'C', 'UnitType': 17}, 'Cooling': {'Value': 3.0, 'Unit': 'C', 'UnitType': 17}}, 'AirAndPollen': [{'Name': 'AirQuality', 'Value': 46, 'Category': 'Good', 'CategoryValue': 1, 'Type': 'Nitrogen Dioxide'}, {'Name': 'Grass', 'Value': 12, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Mold', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'Ragweed', 'Value': 30, 'Category': 'Moderate', 'CategoryValue': 2}, {'Name': 'Tree', 'Value': 0, 'Category': 'Low', 'CategoryValue': 1}, {'Name': 'UVIndex', 'Value': 6, 'Category': 'High', 'CategoryValue': 3}], 'Day': {'Icon': 1, 'IconPhrase': 'Sunny', 'HasPrecipitation': False, 'ShortPhrase': 'Plenty of sunshine', 'LongPhrase': 'Plenty of sunshine', 'PrecipitationProbability': 0, 'ThunderstormProbability': 0, 'RainProbability': 0, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 128, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 20.4, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 116, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 1, 'Evapotranspiration': {'Value': 4.6, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 7194.5, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 31, 'Maximum': 75, 'Average': 45}, 'WetBulbTemperature': {'Minimum': {'Value': 12.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 17.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 15.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 14.4, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 23.2, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 20.6, 'Unit': 'C', 'UnitType': 17}}}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False, 'ShortPhrase': 'Clear', 'LongPhrase': 'Clear', 'PrecipitationProbability': 2, 'ThunderstormProbability': 0, 'RainProbability': 2, 'SnowProbability': 0, 'IceProbability': 0, 'Wind': {'Speed': {'Value': 9.3, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 129, 'Localized': 'SE', 'English': 'SE'}}, 'WindGust': {'Speed': {'Value': 14.8, 'Unit': 'km/h', 'UnitType': 7}, 'Direction': {'Degrees': 113, 'Localized': 'ESE', 'English': 'ESE'}}, 'TotalLiquid': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Rain': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'Snow': {'Value': 0.0, 'Unit': 'cm', 'UnitType': 4}, 'Ice': {'Value': 0.0, 'Unit': 'mm', 'UnitType': 3}, 'HoursOfPrecipitation': 0.0, 'HoursOfRain': 0.0, 'HoursOfSnow': 0.0, 'HoursOfIce': 0.0, 'CloudCover': 0, 'Evapotranspiration': {'Value': 0.5, 'Unit': 'mm', 'UnitType': 3}, 'SolarIrradiance': {'Value': 71.4, 'Unit': 'W/m²', 'UnitType': 33}, 'RelativeHumidity': {'Minimum': 43, 'Maximum': 89, 'Average': 69}, 'WetBulbTemperature': {'Minimum': {'Value': 13.5, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 16.3, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 14.9, 'Unit': 'C', 'UnitType': 17}}, 'WetBulbGlobeTemperature': {'Minimum': {'Value': 15.2, 'Unit': 'C', 'UnitType': 17}, 'Maximum': {'Value': 21.0, 'Unit': 'C', 'UnitType': 17}, 'Average': {'Value': 17.6, 'Unit': 'C', 'UnitType': 17}}}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us', 'Link': 'http://www.accuweather.com/en/us/pittsburgh-pa/15219/daily-weather-forecast/1310?day=1&unit=c&lang=en-us'}]}\n", - "Pittsburgh, PA\n", - "Headline Text: Noticeably cooler Saturday\n", - "Temperature Minimum Value: 14.3\n" - ] - } - ], - "source": [ - "if (city_code!=0):\n", - " weather_data=get_weather(api_key, city_code)\n", - " print(weather_data)\n", - "# Extract 'Text' from 'Headline'\n", - " headline_text = weather_data['Headline']['Text']\n", - "# Extract 'Temperature Minimum' value\n", - " temperature_minimum = weather_data['DailyForecasts'][0]['Temperature']['Minimum']['Value']\n", - " print(city_string)\n", - " print(\"Headline Text:\", headline_text)\n", - " print(\"Temperature Minimum Value:\", temperature_minimum)\n", - "else: \n", - " print(\"Invalid search term\")" - ] - }, - { - "cell_type": "markdown", - "id": "29a8a8e9-102c-46e0-ad76-87c11f3002fd", - "metadata": {}, - "source": [ - "# Run all the cells and find the minimum temperature in Tucson, Arizona today \n", - "Write the minimum temperature here: \n", - "# Submit both .ipynb and .html files " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3f2cf97c", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.4" - }, - "widgets": { - "application/vnd.jupyter.widget-state+json": { - "state": {}, - "version_major": 2, - "version_minor": 0 - } - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} From 6470362f82bef6ee938d2e271ad2a27dd88382ef Mon Sep 17 00:00:00 2001 From: rkensouh <155992402+rkensouh@users.noreply.github.com> Date: Mon, 16 Sep 2024 09:49:37 +0300 Subject: [PATCH 10/13] Update configs.js --- src/meta/configs.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/meta/configs.js b/src/meta/configs.js index cd00fb0300..cfd99ac311 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -15,11 +15,6 @@ const Configs = module.exports; Meta.config = {}; -<<<<<<< HEAD -// called after data is loaded from db -function deserialize(config) { - const deserialized = {}; -======= // called after data is loaded from db function deserialize(config) { From 2998d1025b9eafa986dd4cb91c3a341a7b1afd80 Mon Sep 17 00:00:00 2001 From: rkensouh <155992402+rkensouh@users.noreply.github.com> Date: Mon, 16 Sep 2024 09:58:56 +0300 Subject: [PATCH 11/13] Update configs.js --- src/meta/configs.js | 39 +-------------------------------------- 1 file changed, 1 insertion(+), 38 deletions(-) diff --git a/src/meta/configs.js b/src/meta/configs.js index cfd99ac311..ae38be36f8 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -1,4 +1,3 @@ - 'use strict'; const nconf = require('nconf'); @@ -15,56 +14,22 @@ const Configs = module.exports; Meta.config = {}; - // called after data is loaded from db function deserialize(config) { const deserialized = {}; ->>>>>>> reem-feature/issue-meta-config.js Object.keys(config).forEach((key) => { const defaultType = typeof defaults[key]; const type = typeof config[key]; const number = parseFloat(config[key]); -<<<<<<< HEAD - if (defaultType === 'string' && type === 'number') { - deserialized[key] = String(config[key]); - } else if (defaultType === 'number' && type === 'string') { - if (!isNaN(number) && isFinite(config[key])) { - deserialized[key] = number; - } else { - deserialized[key] = defaults[key]; - } - } else if (config[key] === 'true') { - deserialized[key] = true; - } else if (config[key] === 'false') { - deserialized[key] = false; - } else if (config[key] === null) { - deserialized[key] = defaults[key]; - } else if (defaultType === 'undefined' && !isNaN(number) && isFinite(config[key])) { - deserialized[key] = number; - } else if (Array.isArray(defaults[key]) && !Array.isArray(config[key])) { - try { - deserialized[key] = JSON.parse(config[key] || '[]'); - } catch (err) { - winston.error(err.stack); - deserialized[key] = defaults[key]; - } - } else { - deserialized[key] = config[key]; - } - }); - return deserialized; -} - -======= deserialized[key] = handleTypeConversion(defaultType, type, config[key], number, key); }); return deserialized; } -// got this code from ChatGPT +// Function to handle type conversion during deserialization function handleTypeConversion(defaultType, type, value, number, key) { if (defaultType === 'string' && type === 'number') { return String(value); @@ -113,7 +78,6 @@ function handleArrayConversion(value, key) { } } ->>>>>>> reem-feature/issue-meta-config.js // called before data is saved to db function serialize(config) { const serialized = {}; @@ -274,7 +238,6 @@ async function getLogoSize(data) { size = await image.size(path.join(nconf.get('upload_path'), 'system', 'site-logo-x50.png')); } catch (err) { if (err.code === 'ENOENT') { - // For whatever reason the x50 logo wasn't generated, gracefully error out winston.warn('[logo] The email-safe logo doesn\'t seem to have been created, please re-upload your site logo.'); size = { height: 0, From d21c997a492da8a2d5b390d226a4caf6dfd4e337 Mon Sep 17 00:00:00 2001 From: rkensouh <155992402+rkensouh@users.noreply.github.com> Date: Mon, 16 Sep 2024 10:04:23 +0300 Subject: [PATCH 12/13] Update create.js --- src/user/create.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/user/create.js b/src/user/create.js index c68e7e7951..f5800aa74c 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -122,10 +122,6 @@ module.exports = function (User) { return userData.uid; } -<<<<<<< HEAD -======= - ->>>>>>> reem-feature/issue-meta-config.js async function storePassword(uid, password) { if (!password) { return; @@ -164,7 +160,6 @@ module.exports = function (User) { User.isPasswordValid = function (password, minStrength) { minStrength = (minStrength || minStrength === 0) ? minStrength : meta.config.minimumPasswordStrength; - // Sanity checks: Checks if defined and is string if (!password || !utils.isPasswordValid(password)) { throw new Error('[[error:invalid-password]]'); } @@ -187,7 +182,6 @@ module.exports = function (User) { let numTries = 0; let { username } = userData; while (true) { - /* eslint-disable no-await-in-loop */ const exists = await meta.userOrGroupExists(username); if (!exists) { return numTries ? username : null; From d613beef5e678b7086ce904958e514752d9ca4bc Mon Sep 17 00:00:00 2001 From: rkensouh <155992402+rkensouh@users.noreply.github.com> Date: Mon, 16 Sep 2024 10:09:31 +0300 Subject: [PATCH 13/13] Update create.js --- src/user/create.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/user/create.js b/src/user/create.js index f5800aa74c..70263f8704 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -122,6 +122,7 @@ module.exports = function (User) { return userData.uid; } + async function storePassword(uid, password) { if (!password) { return; @@ -160,6 +161,7 @@ module.exports = function (User) { User.isPasswordValid = function (password, minStrength) { minStrength = (minStrength || minStrength === 0) ? minStrength : meta.config.minimumPasswordStrength; + // Sanity checks: Checks if defined and is string if (!password || !utils.isPasswordValid(password)) { throw new Error('[[error:invalid-password]]'); } @@ -182,6 +184,7 @@ module.exports = function (User) { let numTries = 0; let { username } = userData; while (true) { + /* eslint-disable no-await-in-loop */ const exists = await meta.userOrGroupExists(username); if (!exists) { return numTries ? username : null;