Advertisement
Guest User

Untitled

a guest
Mar 14th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. package com.eagles13.updater;
  2.  
  3. import java.io.*;
  4. import java.net.URL;
  5. import java.nio.channels.Channels;
  6. import java.nio.channels.ReadableByteChannel;
  7. import java.nio.charset.Charset;
  8. import java.nio.file.Files;
  9. import java.nio.file.Paths;
  10. import java.util.Arrays;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13.  
  14. /**
  15.  * Created by Joshua on 09/06/2016.
  16.  */
  17. public class RuneMateUpdater {
  18.     public static void main(String[] args) {
  19.         try {
  20.             System.out.println("Updater: getting current version info...");
  21.             File versionFile = new File("rmversion");
  22.             if (!versionFile.isDirectory() && versionFile.exists() && versionFile.canRead()) {
  23.                 BufferedReader br = new BufferedReader(new FileReader(versionFile));
  24.                 checkUpdate(getNumbers(br.lines().findFirst().orElse("0")));
  25.             } else {
  26.                 System.out.println("Updater: no current version info found...");
  27.                 checkUpdate(0);
  28.             }
  29.         } catch (Exception ex) {
  30.             ex.printStackTrace();
  31.         }
  32.     }
  33.  
  34.  
  35.     private static void checkUpdate(int oldVersion) {
  36.         try {
  37.             System.out.println("Updater: checking for a new version");
  38.             String rmWebpage = webRequest("https://www.runemate.com/download/");
  39.             String downloadUrl = getBetween(rmWebpage, "var downloadLink = \"", "\";");
  40.             System.out.println("Updater: " + downloadUrl);
  41.             int newVersion = getNumbers(downloadUrl);
  42.             if (newVersion > oldVersion) {
  43.                 System.out.println("Updater: current version is " + oldVersion + " newer version available " + newVersion + " - downloading new version...");
  44.                 URL website = new URL(downloadUrl + "/standalone/RuneMate.jar");
  45.                 ReadableByteChannel rbc = Channels.newChannel(website.openStream());
  46.                 FileOutputStream fos = new FileOutputStream("RuneMate.jar");
  47.                 fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  48.                 Files.write(Paths.get("rmversion"), Arrays.<CharSequence>asList(String.valueOf(newVersion)), Charset.forName("UTF-8"));
  49.             } else {
  50.                 System.out.println("Updater: no update needed");
  51.             }
  52.         } catch (Exception ex) {
  53.             ex.printStackTrace();
  54.         }
  55.         System.out.println("Updater: complete");
  56.     }
  57.  
  58.     private static String webRequest(String url) {
  59.         System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36\n");
  60.         StringBuilder response = new StringBuilder();
  61.         try {
  62.             URL website = new URL(url);
  63.             BufferedReader in = new BufferedReader(new InputStreamReader(website.openStream()));
  64.             in.lines().forEach(response::append);
  65.             in.close();
  66.         } catch (IOException e) {
  67.             e.printStackTrace();
  68.         }
  69.         return response.toString();
  70.     }
  71.  
  72.     private static String getBetween(String haystack, String pre, String post) {
  73.         Pattern pattern = Pattern.compile( Pattern.quote(pre) + "(.+?)" + Pattern.quote(post));
  74.         Matcher matcher = pattern.matcher(haystack);
  75.         if (matcher.find()) {
  76.             return matcher.group(1);
  77.         }
  78.         return "No match could be found.";
  79.     }
  80.  
  81.     private static int getNumbers(String input) {
  82.         try {
  83.             return Integer.parseInt(input.replaceAll("\\D+", ""));
  84.         } catch (Exception ex) {
  85.             return 0;
  86.         }
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement