android_id/app/src/main/java/com/psmreborn/pkgj/NoPayStationParser.java

119 lines
3.3 KiB
Java

package com.psmreborn.pkgj;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
public class NoPayStationParser extends AsyncTask<Void, Void, Void> {
private boolean wasError = false;
private String errorMsg = "";
private Context ctx = null;
private ProgressDialog dialog = null;
private File psmGamesFile = null;
private boolean forceRefresh = false;
public NoPayStationParser(Context context, boolean refresh){
this.ctx = context;
this.psmGamesFile = new File(ctx.getFilesDir(), "PSM_GAMES.tsv");
this.forceRefresh = true;
}
private void download() throws IOException {
if(psmGamesFile.exists() && !forceRefresh) {
return;
}
URL url = new URL("http://nopaystation.com/tsv/PSM_GAMES.tsv");
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream(this.psmGamesFile);
StrUtil.copyTo(input, output);
output.close();
input.close();
}
private void parse() throws IOException {
BufferedReader tsvReader = new BufferedReader(new FileReader(this.psmGamesFile));
tsvReader.readLine(); // skip the first line
String line = tsvReader.readLine();
while(line != null) {
String[] values = line.split("\t");
if(values.length >= 4){
String titleId = values[0];
String title = values[2];
String downloadUrl = values[3];
String zRif = values[4];
if(!title.contains("[Unity]") && !zRif.equals("MISSING")){
((MainActivity)ctx).addGame(new Game(titleId, title, downloadUrl, zRif));
}
}
line = tsvReader.readLine();
};
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(ctx);
dialog.setTitle("Refreshing Items...");
dialog.setMessage("Downloading index ...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
try {
download();
parse();
}
catch(Exception e){
this.wasError = true;
this.errorMsg = e.toString();
return null;
}
return null;
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
((MainActivity)ctx).updateGames();
if(wasError) {
new AlertDialog.Builder((Activity)ctx)
.setTitle("Error Obtaining PSM_GAMES.TSV..")
.setMessage(this.errorMsg)
.setCancelable(false)
.setPositiveButton("OK",null).show();
}
}
}