Skip to content

Commit

Permalink
Merge pull request #13 from Muhin007/wp-6
Browse files Browse the repository at this point in the history
Wp 6 CompletableFuture
  • Loading branch information
Muhin007 authored Jun 24, 2019
2 parents 97fd8ff + b45a49d commit 8ace926
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 328 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/github/muhin007/iWeatherProvider/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class Main {

public static void main(String[] args) {
WeatherAggregator wA = WeatherAggregator.getInstance();
wA.start();
WeatherAggregator weatherAggregator= WeatherAggregator.getInstance();
weatherAggregator.AVGTemp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,25 @@ public static String performRequest(String urlStr) {

HttpURLConnection http = null;
try {
assert url != null;
http = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
try {
assert http != null;
BufferedReader br = new BufferedReader(new InputStreamReader(http.getInputStream()));
char[] buf = new char[1000000];

int r = 0;
int r;
do {
if ((r = br.read(buf)) > 0)
sb.append(new String(buf, 0, r));
} while (r > 0);
} catch (IOException e) {
e.printStackTrace();
} finally {
assert http != null;
http.disconnect();
}
return sb.toString();
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
package com.github.muhin007.iWeatherProvider.weatherAdaptor.aerisapi;

import com.github.muhin007.iWeatherProvider.weatherAdaptor.WeatherAdaptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.muhin007.iWeatherProvider.weatherAdaptor.JSONReadProcess;
import com.github.muhin007.iWeatherProvider.weatherAdaptor.aerisapi.JSONClass.Period;
import com.github.muhin007.iWeatherProvider.weatherAdaptor.aerisapi.JSONClass.Response;
import com.github.muhin007.iWeatherProvider.weatherAdaptor.aerisapi.JSONClass.Temp;

public class WeatherAdaptorAerisapi extends WeatherAdaptor {
TempFromAerisapi tempFromAerisapi = new TempFromAerisapi();
import java.io.IOException;

private int tempAerisapi;
public class WeatherAdaptorAerisapi{
private int tempFromAerisapi;

@Override
public void requestTempAPI() {
tempAerisapi = tempFromAerisapi.getTempFromAerisapi();
}
public int getTempFromAerisapi(String cityName) {

public double getTempAerisapi() {
requestTempAPI();
return tempAerisapi;
String request = "https://api.aerisapi.com/forecasts/" + cityName + ",?format=json&filter=day&limit=1&client_id=a04yWLdF5v83ZGGqanosb&client_secret=2moGBj8LBRIlJdhaufg9qCxN7IPLCokJ3OjamuKK";
String result = JSONReadProcess.performRequest(request);
ObjectMapper mapper = new ObjectMapper();
Temp temp = null;
try {
temp = mapper.readValue(result, Temp.class);
} catch (IOException e) {
e.printStackTrace();
}
assert temp != null;
for (Response response : temp.getResponse()) {
for (Period period : response.getPeriods()) {
tempFromAerisapi = (int) period.getMaxTempC();
}
}
return tempFromAerisapi;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
package com.github.muhin007.iWeatherProvider.weatherAdaptor.apixu;

import com.github.muhin007.iWeatherProvider.weatherAdaptor.WeatherAdaptor;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class WeatherAdaptorApixu extends WeatherAdaptor {
TempFromApixu tempFromApixu = new TempFromApixu();
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;

private int tempApixu;
@Override
public void requestTempAPI() {
tempApixu = tempFromApixu.getTempFromApixu();
public class WeatherAdaptorApixu {

public int getTempFromApixu(String cityName) {
String uri = "http://api.apixu.com/v1/current.xml?key=d85bbc64aab34428894192757192306&q=" + cityName;

String output = getString(uri);
return Integer.parseInt(output.split("<temp_c>")[1].split("</temp_c>")[0]);
}
public double getTempApixu() {
requestTempAPI();
return tempApixu;

public String getString(String uri) {
URL url = null;
try {
url = new URL(uri);
} catch (MalformedURLException e) {
e.printStackTrace();
}

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null;
try {
assert db != null;
assert url != null;
doc = db.parse(url.openStream());
} catch (SAXException | IOException e) {
e.printStackTrace();
}

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = tf.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
assert transformer != null;
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
try {
transformer.transform(new DOMSource(doc), new StreamResult(writer));
} catch (TransformerException e) {
e.printStackTrace();
}
return writer.getBuffer().toString().replaceAll("[\n\r]", "");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package com.github.muhin007.iWeatherProvider.weatherAdaptor.worldweatheronline;

import com.github.muhin007.iWeatherProvider.weatherAdaptor.WeatherAdaptor;
import com.github.muhin007.iWeatherProvider.weatherAdaptor.apixu.WeatherAdaptorApixu;

public class WeatherAdaptorWorldweatheronline extends WeatherAdaptor {
public class WeatherAdaptorWorldweatheronline{
private WeatherAdaptorApixu wAA = new WeatherAdaptorApixu();

TempFromWorldweatheronline tempFromWorldweatheronline = new TempFromWorldweatheronline();
private int tempWorldweatheronline;

@Override
public void requestTempAPI() {
tempWorldweatheronline = tempFromWorldweatheronline.getTempWorldweatheronline();
}

public double getTempFromWorldweatheronline() {
requestTempAPI();
return tempWorldweatheronline;
public int getTempFromWorldweatheronline(String cityName) {
String uri = "http://api.worldweatheronline.com/premium/v1/weather.ashx?key=bc27cfec9d314543bdf70006191806&q=" + cityName + "&format=xml&num_of_days=1&lang=ru";
String output = wAA.getString(uri);
return Integer.parseInt(output.split("<temp_C>")[1].split("</temp_C>")[0]);
}
}
Loading

0 comments on commit 8ace926

Please sign in to comment.