In: Computer Science
In Java please, question is: Find a website that publishes the current temperature in your area, and write a screen-scraper program Weather so that typing java weather followed by your zip code will give you a weather forecast
Iam Sharing Both the codes in python and java it might be helpful you
package realtimeweb.weatherservice.domain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Forecast {
private String longDescription;
private String description;
private String imageUrl;
private String temperatureLabel;
private String periodName;
private Integer probabilityOfPrecipitation;
private String periodTime;
private Integer temperature
public String getLongDescription() {
return this.longDescription;
}
public void setLongDescription(String longDescription) {
this.longDescription = longDescription;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImageUrl() {
return this.imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getTemperatureLabel() {
return this.temperatureLabel;
}
public void setTemperatureLabel(String temperatureLabel) {
this.temperatureLabel = temperatureLabel;
}
public String getPeriodName() {
return this.periodName;
}
public void setPeriodName(String periodName) {
this.periodName = periodName;
}
public Integer getProbabilityOfPrecipitation() {
return this.probabilityOfPrecipitation;
}
public void setProbabilityOfPrecipitation(Integer
probabilityOfPrecipitation) {
this.probabilityOfPrecipitation = probabilityOfPrecipitation;
}
public String getPeriodTime() {
return this.periodTime;
}
public void setPeriodTime(String periodTime) {
this.periodTime = periodTime;
}
public Integer getTemperature() {
return this.temperature;
}
public void setTemperature(Integer temperature) {
this.temperature = temperature;
}
public String toString() {
return "Forecast["
+longDescription+", "+description+", "+imageUrl+",
"+temperatureLabel+", "+periodName+",
"+probabilityOfPrecipitation+", "+periodTime+",
"+temperature+"]";
}
@SuppressWarnings("unchecked")
public Forecast(Map<String, Object> raw) {
try {
this.longDescription = ((Map<String, Object>)
raw.get("data")).get("text").toString();
this.description = ((Map<String, Object>)
raw.get("data")).get("weather").toString();
this.imageUrl = ((Map<String, Object>)
raw.get("data")).get("iconLink").toString();
this.temperatureLabel = ((Map<String, Object>)
raw.get("time")).get("tempLabel").toString();
this.periodName = ((Map<String, Object>)
raw.get("time")).get("startPeriodName").toString();
this.probabilityOfPrecipitation = Integer.parseInt(((Map<String,
Object>) raw.get("data")).get("pop").toString());
this.periodTime = ((Map<String, Object>)
raw.get("time")).get("startValidTime").toString();
this.temperature = Integer.parseInt(((Map<String, Object>)
raw.get("data")).get("temperature").toString());
} catch (NullPointerException e) {
System.err.println("Could not
convert the response to a Forecast; a field was missing.");
e.printStackTrace();
} catch (ClassCastException e) {
System.err.println("Could not
convert the response to a Forecast; a field had the wrong
structure.");
e.printStackTrace();
}
}
public Forecast(String periodName, String periodTime, String
temperatureLabel, int temperature, int probabilityOfPrecipitation,
String description, String imageUrl, String longDescription)
{
this.periodName = periodName;
this.periodTime = periodTime;
this.temperatureLabel =
temperatureLabel;
this.temperature =
temperature;
this.probabilityOfPrecipitation =
probabilityOfPrecipitation;
this.description =
description;
this.imageUrl = imageUrl;
this.longDescription =
longDescription;
}
@SuppressWarnings("unchecked")
public static ArrayList<Forecast>
parse(Map<String, Object> raw) {
HashMap<String, Object>
rawTime = (HashMap<String, Object>) raw.get("time");
HashMap<String, Object>
rawData = (HashMap<String, Object>) raw.get("data");
ArrayList<String> periodNames
= ((ArrayList<String>)rawTime.get("startPeriodName"));
ArrayList<String> periodTimes
= ((ArrayList<String>)rawTime.get("startValidTime"));
ArrayList<String> tempLabels
= ((ArrayList<String>)rawTime.get("tempLabel"));
ArrayList<String>
temperatures =
((ArrayList<String>)rawData.get("temperature"));
ArrayList<String> pops =
((ArrayList<String>)rawData.get("pop"));
for (int i = 0; i < pops.size();
i+= 1) {
if (pops.get(i)
== null) {
pops.set(i, "0");
}
}
ArrayList<String> weather =
((ArrayList<String>)rawData.get("weather"));
ArrayList<String> icons =
((ArrayList<String>)rawData.get("iconLink"));
ArrayList<String> text =
((ArrayList<String>)rawData.get("text"));
ArrayList<Forecast> forecasts = new ArrayList<Forecast>();
python
import requests
from pprint import pprint
def weather_data(query):
res=requests.get('http://api.openweathermap.org/data/2.5/weather?'+query+'&APPID=****************************8&units=metric');
return res.json();
def print_weather(result,city
print("{}'s temperature: {}°C
".format(city,result['main']['temp']))
print("Wind speed: {}
m/s".format(result['wind']['speed']))
print("Description:
{}".format(result['weather'][0]['description']))
print("Weather:
{}".format(result['weather'][0]['main']))
def main():
city=input('Enter the city:'
print()
try:
query='q='+city
w_data=weather_data(query);
print_weather(w_data, city)
print()
except:
print('City name not found...')
if __name__=='__main__':
main()