New improvements

This commit is contained in:
Robert Vokac 2024-01-27 21:09:21 +00:00
parent 7b13aefa44
commit 65bc0d9fd6
No known key found for this signature in database
GPG Key ID: 693D30BEE3329055
6 changed files with 116 additions and 97 deletions

2
.gitignore vendored
View File

@ -10,3 +10,5 @@ starttime.txt
overtime.txt
highlighted
proxy.txt
out.txt
pocasi.txt

3
aaa Normal file
View File

@ -0,0 +1,3 @@
<html><head><meta charset="UTF-8"></head><body>/* !AMP */display:none}.ribbon-search-desktop .szn-input-with-suggest-list::-moz-placeholder{color:#757575;color:var(--ribbon-search-placeholder-color,#757575)}.ribbon-search-desktop .szn-input-with-suggest-list::placeholder{color:#757575;color:var(--ribbon-search-placeholder-color,#757575)}.ribbon-search-desktop .szn-suggest-list{font-family:Trivia Seznam,Helvetica,Arial,sans-serif}.szn-mobile-input-with-suggest-list--light{--color-button-primary:var(--ribbon-search-button-color,#c00)}.szn-mobile-input-with-suggest-list__button--icon-back{box-sizing:content-box;padding:0 10px;width:20px}.szn-mobile-input-with-suggest-list__button--icon-clear{box-sizing:content-box;padding:0 22px;width:12px}.szn-mobile-input-with-suggest-list__button--icon-search{box-sizing:content-box;padding:0 12px;width:20px}.szn-mobile-input-with-suggest-list__button--icon-back .ribbon-icon,.szn-mobile-input-with-suggest-list__button--icon-clear .ribbon-icon,.szn-mobile-input-with-suggest-list__button--icon-search .ribbon-icon{height:auto;width:100%}.szn-mobile-input-with-suggest-list__controlPanel__input-2,.szn-mobile-input-with-suggest-list__controlPanel__input-3{flex-grow:1;font-family:Arial,Helvetica,sans-serif;font-size:18px;width:auto}.ribbon-mobile-search__input{display:none}.ribbon-service{align-items:center;color:#000;color:var(--ribbon-service-color,#000);display:flex;flex-shrink:1;font-size:16px;height:32px}.ribbon-service,.ribbon-service:hover{text-decoration:none}.ribbon-service:hover,.ribbon-service:visited{color:#000;color:var(--ribbon-service-color,#000)}.ribbon-service__icon{display:none;height:32px;margin-right:8px;width:32px}.ribbon-service__icon-label{display:none;white-space:nowrap}.ribbon-service__logo{height:24px;max-height:32px;max-width:100%;min-height:24px;width:auto}.ribbon-service__logo--amp{max-width:160px}@media (min-width:600px){.ribbon-service__icon,.ribbon-service__icon-label{display:block}.ribbon-service__icon~.ribbon-service__logo{display:none}}.ribbon-seznam{align-items:center;color:#c00;color:var(--ribbon-seznam-color,#c00);display:flex;flex-shrink:0}.ribbon-seznam__link,.ribbon-seznam__link:hover,.ribbon-seznam__link:visited{color:#c00;color:var(--ribbon-seznam-color,#c00);text-decoration:none}.ribbon{background:#fff;background:var(--ribbon-background-color,#fff);box-shadow:0 0 2px 0 rgba(0,0,0,.1),0 8px 30px -12px rgba(0,0,0,.12);box-sizing:border-box;display:flex;height:56px;left:0;padding:0 16px;position:fixed;top:0;width:100%;z-index:9000}.ribbon--base{font-family:Trivia Seznam,Helvetica,Arial,sans-serif;font-size:14px}.ribbon--amp{box-shadow:none;overflow:hidden}.ribbon--blind{clip:rect(1px 1px 1px 1px);clip:rect(1px,1px,1px,1px);height:1px;overflow:hidden;width:1px}.ribbon--blind,.ribbon--unfixed{position:absolute}.ribbon--show-search:not(.ribbon--amp) .ribbon-seznam{visibility:hidden}@media (min-width:600px){.ribbon--show-search:not(.ribbon--amp) .ribbon-seznam{visibility:visible}}.ribbon__amp-search{background:#fff;background:var(--ribbon-background-color,#fff);box-shadow:0 0 2px 0 rgba(0,0,0,.1),0 8px 30px -12px rgba(0,0,0,.12);box-sizing:border-box;height:56px;left:0;padding:0 16px;position:fixed;top:0;width:100%;z-index:8999}.ribbon__menu-button{color:#909090;color:var(--ribbon-menu-button-color,#909090);margin-left:-8px;margin-right:8px}.ribbon__menu-button .ribbon-button__button{border-radius:4px;box-sizing:content-box;height:16px;padding:8px;width:16px}.ribbon__menu-button .ribbon-button__button:focus-visible{outline:2px solid #000}.ribbon__center,.ribbon__end,.ribbon__start{align-items:center;display:flex}.ribbon__center{flex-grow:1;min-width:0}.ribbon__end{flex-shrink:0;margin-left:16px}.ribbon__end--reversed{display:flex;flex-shrink:0}@media (min-width:600px){.ribbon:not(.ribbon--unfixed){padding-right:31px;width:100vw}.ribbon__end{margin-left:0}.ribbon__end--reversed{flex-direction:row-reverse}.ribbon__center{margin:0 16px}}.ribbon__end .ribbon-seznam{margin-right:16px}.ribbon__menu-button .ribbon-icon{height:12px}@media (min-width:600px){.ribbon .ribbon-seznam{margin-left:16px;margin-right:0}}</style><div <br>
</body></html>

View File

@ -12,7 +12,7 @@ public class Main {
public static void main(String[] args) throws IOException {
while (true) {
boolean test = false;
boolean test = true;
File starttimeTxt = new File("starttime.txt");
File overtimeTxt = new File("overtime.txt");
String lastStartTime = Utils.readTextFromFile(starttimeTxt);

View File

@ -78,6 +78,7 @@ public class TimeCalcWindow {
JButton exitButton = new JButton("Exit");
//window.add(weatherButton);
window.add(jokeButton);
window.add(restartButton);
window.add(exitButton);

View File

@ -5,6 +5,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@ -55,7 +56,7 @@ public class Utils {
//nothing to do
return null;
}
return new String(Files.readAllBytes(file.toPath()));
return new String(Files.readAllBytes(file.toPath()), "UTF-8");
}
public static Color[] getRandomColors() {
Color[] result = new Color[12];

View File

@ -1,19 +1,23 @@
package rvc.timecalc;
import javax.net.ssl.HttpsURLConnection;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
@ -24,7 +28,7 @@ import java.net.URL;
*/
public class WeatherWindow extends JFrame {
public WeatherWindow() {
this.setSize(800, 600);
this.setSize(400, 300);
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
@ -32,107 +36,115 @@ public class WeatherWindow extends JFrame {
scrollPane.setBounds(10, 10, 750, 550);
getContentPane().add(scrollPane);
File proxyTxt = new File("proxy.txt");
if(!proxyTxt.exists()) {
jep.setText("Sorry, file proxy.txt was not found.");
return;
}
final HttpProxy httpProxy;
// File proxyTxt = new File("proxy.txt");
// if (!proxyTxt.exists()) {
// jep.setText("Sorry, file proxy.txt was not found.");
// return;
// }
// final HttpProxy httpProxy;
// try {
// httpProxy = new HttpProxy(proxyTxt);
// } catch (RuntimeException e) {
// jep.setContentType("text/html");
// jep.setText(e.getMessage());
// return;
// }
try {
httpProxy = new HttpProxy(proxyTxt);
} catch(RuntimeException e) {
String pocasiHtml = null;
File pocasiHtmlFile = new File("pocasi.html");
try {
pocasiHtml = downloadFile("https://pocasi.seznam.cz/praha");
pocasiHtml = prettyFormatXml(pocasiHtml, 4);
Utils.writeTextToFile(pocasiHtmlFile, pocasiHtml);
} catch (Exception e) {
e.printStackTrace();
pocasiHtml = pocasiHtmlFile.exists() ? Utils.readTextFromFile(pocasiHtmlFile) : "Sorry, pocasi.html was not found.";
}
{
StringBuilder sb = new StringBuilder();
boolean ogm_detailed_forecast_Started = false;
for(String line:pocasiHtml.split("\\r?\\n|\\r")) {
if(line.contains("ogm-detailed-forecast")) {
ogm_detailed_forecast_Started = true;
}
if(ogm_detailed_forecast_Started && line.contains("<button aria-label=")) {
String l = line
.trim()
.replace("<button aria-label=\"","").split("class")[0];
sb.append(l);
sb.append("<br>\n\n");
System.out.println("Found another line");
}
}
pocasiHtml = sb.toString();
}
jep.setContentType("text/html");
jep.setText(e.getMessage());
return;
}
try {
jep.setText(downloadFile3("https://pocasi.seznam.cz/praha",
httpProxy));
}catch (Exception e) {
jep.setText("<html><head><meta charset=\"UTF-8\"></head><body>" + pocasiHtml + "</body></html>");
Utils.writeTextToFile(new File("aaa"),"<html><head><meta charset=\"UTF-8\"></head><body>" + pocasiHtml + "</body></html>");
} catch (Exception e) {
e.printStackTrace();
jep.setContentType("text/html");
jep.setText("<html>Could not load " + e.getMessage() + "</html>");
}
}
public String downloadFile2(String s, HttpProxy httpProxy) throws IOException {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(httpProxy.getUser(),
httpProxy.getPassword().toCharArray());
}
});
Proxy proxy = new Proxy(
Proxy.Type.HTTP, new InetSocketAddress(httpProxy.getUrl(), Integer.valueOf(httpProxy.getPort())));
URL url = new URL(s);
System.getProperties().put( "proxySet", "true" );
System.setProperty("http.proxyHost", httpProxy.getUrl());
System.setProperty("http.proxyPort", httpProxy.getPort());
System.setProperty("http.proxyUser", httpProxy.getUser());
System.setProperty("http.proxyPassword", httpProxy.getPassword());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
StringBuilder responseBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
responseBuilder.append(line);
}
return responseBuilder.toString();
}
}
public String downloadFile3(String s, HttpProxy httpProxy)
public String downloadFile(String urlString)
throws IOException {
System.getProperties().put( "proxySet", "true" );
System.setProperty("http.proxyHost", httpProxy.getUrl());
System.setProperty("http.proxyPort", httpProxy.getPort());
System.setProperty("http.proxyUser", httpProxy.getUser());
System.setProperty("http.proxyPassword", httpProxy.getPassword());
String uri = s;
URL url = new URL(uri);
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(httpProxy.getUser(),
httpProxy.getPassword().toCharArray());
}});
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
//connection.setDoOutput(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "text/html");
connection.setRequestProperty("Accept", "text/html");
connection.setRequestProperty("Method", "GET");
connection.setRequestProperty("Encoding", "UTF-8");
connection.setReadTimeout(60000);
InputStream stream = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(stream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder content = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
if (conn.getResponseCode() == 200) {
br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String strCurrentLine;
while ((strCurrentLine = br.readLine()) != null) {
sb.append(strCurrentLine).append("\n");
}
} else {
br = new BufferedReader(
new InputStreamReader(conn.getErrorStream()));
String strCurrentLine;
while ((strCurrentLine = br.readLine()) != null) {
sb.append(strCurrentLine).append("\n");
}
}
return sb.toString();
}
bufferedReader.close();
return content.toString();
private static String prettyFormatXml(final String input, final int indent) {
try {
Source xmlInput = new StreamSource(new StringReader(input));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", indent);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
} catch (Throwable e) {
try {
Source xmlInput = new StreamSource(new StringReader(input));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
} catch (Throwable t) {
return input;
}
}
}
}