Jump to content

rWeather - Weather API


Reid

Recommended Posts

Currently working on a weather application for fun and experience. Feel free to use as well as give me some feedback smile.png

package com.reiddacosta.rweather;

import java.io.IOException;

import com.reiddacosta.rweather.api.Weather;

/**
 * rWeather v1.0
 * 
 * @author Reid DaCosta
 *
 */
public class rWeather {

	public static void main(String[] args) throws IOException {
		System.out.println(new Weather().getTemperature());
	}
}

package com.reiddacosta.rweather.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * Weather Class
 * 
 * @author Reid DaCosta
 *
 */
public class Weather {

	private static final String BASE = "https://api.forecast.io/forecast/477268b98a0bc386f1e6e624777a6077/";
	private String userLatitude;
	private String userLongitude;
	private int userTemperature;
	private Location location;

	/**
	 * Default Constructor
	 * 
	 * @throws IOException
	 */
	public Weather() throws IOException {
		location = new Location();
		this.userLatitude = location.getLatitude();
		this.userLongitude = location.getLongitude();
		userTemperature = (int) toCelsius(getUserTeperature());
	}
	
	/**
	 * Returns the temperature for the user based on their location.
	 * 
	 * @return userTemperature
	 */
	public int getTemperature() {
		return userTemperature;
	}
	
	/**
	 * Sets the temperature for the user.
	 * 
	 * @param userTemperature
	 */
	public void setTemperature(int userTemperature){
		this.userTemperature = userTemperature;
	}

	/**
	 * Returns the temperature based on the users location.
	 * 
	 * @return userTeperature
	 * @throws IOException
	 */
	private String getUserTeperature() throws IOException {
		final URL url = new URL(BASE + userLatitude + "," + userLongitude);
		BufferedReader file = new BufferedReader(new InputStreamReader(url.openStream()));
		String line, almost;
		String userTeperature = null;
		while ((line = file.readLine()) != null) {
			if (line.contains("apparentTemperature")) {
				userTeperature = (line);
			}
		}
		file.close();
		almost = userTeperature.substring(userTeperature.indexOf("apparentTemperature"),userTeperature.indexOf("dewPoint"));
		return almost.substring(almost.indexOf(":") + 1, almost.indexOf(","));
	}

	/**
	 * Returns the temperature in Celsius.
	 * 
	 * @param fahTemp
	 * @return tempCelsius
	 */
	private double toCelsius(String fahTemp) {
		return (int) (Double.parseDouble(fahTemp) - 32) * 5 / 9;
	}
}

package com.reiddacosta.rweather.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * Location Class
 * 
 * @author Reid DaCosta
 *
 */
public class Location {

	private static final String BASE = "http://whatsmylatlng.com";
	private String userLocation;
	private String userLatitude;
	private String userLongitude;

	/**
	 * Default Constructor
	 * 
	 * @throws IOException
	 */
	public Location() throws IOException {
		this.userLocation = getuserLocation();
		this.userLatitude = userLocation.substring(0, userLocation.indexOf(","));
		this.userLongitude = userLocation.substring(userLocation.indexOf(",") + 1, userLocation.length());
	}

	/**
	 * Returns the Latitude of the user.
	 * 
	 * @return userLatitude
	 */
	public String getLatitude() {
		return userLatitude.trim();
	}
	
	/**
	 * Sets the Latitude of the user.
	 * 
	 * @param userLatitude
	 */
	public void setLatitude(String userLatitude) {
		this.userLatitude = userLatitude;
	}

	/**
	 * Returns the Longitude of the user.
	 * 
	 * @return userLongitude
	 */
	public String getLongitude() {
		return userLongitude.trim();
	}
	
	/**
	 * Sets the Longitude of the user.
	 * 
	 * @param userLongitude
	 */
	public void setLongitude(String userLongitude){
		this.userLongitude = userLongitude;
	}

	/**
	 * Returns the Latitude & Longitude of the user.
	 * 
	 * @return userLocation
	 * @throws IOException
	 */
	private String getuserLocation() throws IOException {
		final URL url = new URL(BASE);
		BufferedReader file = new BufferedReader(new InputStreamReader(url.openStream()));
		String line;
		String userLocation = null;
		while ((line = file.readLine()) != null) {
			if (line.contains("geo.initialize")) {
				userLocation = (line);
			}
		}
		file.close();
		return userLocation.substring(userLocation.indexOf("(") + 1,userLocation.indexOf(")"));
	}
}

Edited by Reid
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...