I’m trying to make a little app for personal use intended to get weather data from a website. To make this, I have to make some workaround to bypass a 403 error and I’m able to create three files (one for every day for which the weather forecast are intended to be analyzed). Then I create a function to analyze these files and putting data into three tables. Here is the code I wrote:
//-----------------------Importing libraries and other stuff-----------------------
// importing libraries to htmlWorkaround() work
import java.net.*;
import java.io.*;
//creating Tables
Table table0;
Table table1;
Table table2;
Table[] table = {table0, table1, table2};
//defining variables to be used in the tables
int time;
String weatherCondition;
int speedMin;
int speedMax;
String direction;
//-----------------------SETUP-----------------------
void setup() {
//setting up the Tables
for (int i = 0; i < 3; i++) {
table[i] = new Table();
table[i].addColumn("time");
table[i].addColumn("weatherCondition");
table[i].addColumn("speedMin");
table[i].addColumn("speedMax");
table[i].addColumn("direction");
}
//calling htmlWorkaround() to generate files to be read
htmlWorkaround();
//calling loadData() to analyze the files
loadData();
}
//-----------------------DRAW-----------------------
void draw() {
}
//-----------------------htmlWorkaround() >> avoids 403 error-----------------------
void htmlWorkaround() {
String[] weatherURLs = {"WEBSITE-URL-0", "WEBSITE-URL-1", "WEBSITE-URL-2"};
for (int i = 0; i < weatherURLs.length; i++) {
URL url;
//Create a writer to print the console out
PrintWriter output = createWriter("weatherText" + i +".txt");
//
try {
// Create a URL object
url = new URL(weatherURLs[i]);
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
// Read all of the text returned by the HTTP server
BufferedReader in = new BufferedReader
(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String htmlText;
while ( (htmlText = in.readLine ()) != null) {
output.println(htmlText);
}
in.close();
}
//Should be called once the website throws 403 back, case 1
catch (MalformedURLException e) {
e.printStackTrace(output);
System.out.println(output.toString());
}
//Should be called once the website throws 4}03 back, case 2
catch (IOException e) {
e.printStackTrace(output);
System.out.println(output.toString());
}
//Clearing and closing after the job is done
finally {
output.flush();
output.close();
exit();
}
}
}
//-----------------------loadData()-----------------------
void loadData() {
for (int j = 0; j < 3; j++) {
//loading htmlWorkaround() output as an array of string
String[] testoDaAnalizzare = loadStrings("weatherText" + j + ".txt");
//joining the array to get a String
String testoDaAnalizzareUnito = join(testoDaAnalizzare, "");
//cleaning
String testoDaAnalizzareUnitoRipulito = testoCompreso(testoDaAnalizzareUnito, "<ul class=\"mb-24\">", "</ul>");
//making another array to get infos related to each hour
String[] slotOrari = split(testoDaAnalizzareUnitoRipulito, "</li>");
//hour (time), weather condition (weatherCondition),
//wind speed min and max (
for (int i = 0; i < slotOrari.length - 2; i++) {
//finding the hour
String delimitatoreInizio = "<time>";
String delimitatoreFine = "</time>";
//converting to int
time = int(testoCompreso(slotOrari[i], delimitatoreInizio, delimitatoreFine));
//adding a row to the table
TableRow newRow = table[j].addRow();
//setting the data found
newRow.setInt("time", time);
/*
repeating the process for other data needed
*/
}
saveTable(table[j], "tabella" + j + ".csv");
}
}
//-----------------------testoCompreso >> feeds loadString()-----------------------
String testoCompreso(String s, String inizio, String fine) {
int start = s.indexOf(inizio);
if (start == -1) {
return "";
};
start += inizio.length();
int end = s.indexOf(fine, start);
if (end == -1) {
return "";
}
return s.substring(start, end);
}
This process successfully creates the files I need to be analyzed. The point is:
how can I delete the text files generated by htmlWorkaround()?
I tried to create new files named “weatherText” + j + “.txt” after saveTable and then deleting them, but they stand still in the sketch folder. How can I fix this problem?