forked from CSC330Calendar/Calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSupport - Get Temp
111 lines (86 loc) · 4.09 KB
/
Support - Get Temp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cal;
/* returns the current temperature at your location. If the user's zipcode
* passed in (as a string) it does the same thing but about 2 seconds quicker
* this is a heavily modified version of Professor Iacona's WebPageGetter
*/
/* Here is an ideal point to indicate that we absolutely made portions of
* this with intent of greater funcionality. In this case, we would only
* get a user's location the first time or maybe store it in the database
* but the point is we have two getTemp() methods and we only really need
* to use the slower version one time, when a user creates their account
*/
import java.io.*;
import java.net.*;
public class getTemp{
public static void main(String[] args){
System.out.println(getTemperature());
}
public static String getTemperature(String zip){
StringBuffer buffer = new StringBuffer();
String theUrl = "https://www.bing.com/search?q=" + zip + "+weather&form=EDGEAR&qs=WA&cvid=8921203ba63844f99e376494cc5fe5f6&pq=" + zip + "%20weather";
try {
URL gotoUrl = new URL(theUrl);
InputStreamReader isr = new InputStreamReader(gotoUrl.openStream());
BufferedReader inStream = new BufferedReader(isr);
String inputLine;
while ((inputLine = inStream.readLine()) != null && !buffer.toString().contains("on Bing")){
buffer.append(inputLine + "\n");
}
StringBuilder loc = new StringBuilder();
loc.append(buffer.charAt(buffer.indexOf("on Bing") + 12));
loc.append(buffer.charAt(buffer.indexOf("on Bing") + 13));
return new String(loc + "\u00B0 F");
} catch (IOException e) {
e.printStackTrace();
}
return "error";
}
private static String getUserCity(){
System.setProperty("http.agent", "Chrome");
StringBuffer buffer = new StringBuffer();
String theUrl = "https://www.iplocation.net";
try {
URL gotoUrl = new URL(theUrl);
InputStreamReader isr = new InputStreamReader(gotoUrl.openStream());
BufferedReader inStream = new BufferedReader(isr);
String inputLine;
while ((inputLine = inStream.readLine()) != null && !buffer.toString().contains("</td></tr></tbody><thead><tr><th>ISP")){
buffer.append(inputLine + "\n");
}
StringBuilder loc = new StringBuilder();
for(int i = 94; buffer.charAt(buffer.indexOf("</td><td>United States") + i) != '<'; i++)
loc.append(buffer.charAt(buffer.indexOf("</td><td>United States") + i));
return new String(loc);
} catch (IOException e) {
e.printStackTrace();
}
return "error";
}
public static String getTemperature(){
System.setProperty("http.agent", "Chrome");
StringBuffer buffer = new StringBuffer();
String theUrl;
if (getUserCity().contains(" "))
theUrl = "https://www.wunderground.com/cgi-bin/findweather/getForecast?query=" + getUserCity().substring(0, getUserCity().indexOf(' ')) + "+" + getUserCity().substring(getUserCity().indexOf(' ') + 1, getUserCity().length());
else
theUrl = "https://www.wunderground.com/cgi-bin/findweather/getForecast?query=" + getUserCity();
System.out.println("theurl = " + theUrl);
try {
URL gotoUrl = new URL(theUrl);
InputStreamReader isr = new InputStreamReader(gotoUrl.openStream());
BufferedReader inStream = new BufferedReader(isr);
String inputLine;
while ((inputLine = inStream.readLine()) != null && !buffer.toString().contains("°")){
buffer.append(inputLine + "\n");
}
StringBuilder cur = new StringBuilder();
cur.append(buffer.charAt(buffer.indexOf("°") - 4));
cur.append(buffer.charAt(buffer.indexOf("°") - 3));
System.out.println(cur);
return new String(cur + "\u00B0 F");
} catch (IOException e) {
e.printStackTrace();
}
return "error";
}
}