Skip to content

Commit 19b7d32

Browse files
committedAug 18, 2019
Use correct URL for TWIC import
1 parent 5438daf commit 19b7d32

File tree

2 files changed

+27
-45
lines changed

2 files changed

+27
-45
lines changed
 

‎scidOnTheGo/src/main/java/org/scid/android/twic/ImportTwicActivity.java

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package org.scid.android.twic;
22

3-
import java.io.File;
4-
5-
import org.scid.android.IDownloadCallback;
6-
import org.scid.android.R;
7-
import org.scid.android.Tools;
8-
93
import android.app.ListActivity;
104
import android.app.ProgressDialog;
115
import android.content.Intent;
6+
import android.os.AsyncTask;
127
import android.os.Bundle;
138
import android.view.View;
149
import android.widget.AdapterView;
@@ -17,6 +12,12 @@
1712
import android.widget.ListView;
1813
import android.widget.Toast;
1914

15+
import org.scid.android.IDownloadCallback;
16+
import org.scid.android.R;
17+
import org.scid.android.Tools;
18+
19+
import java.io.File;
20+
2021
public class ImportTwicActivity extends ListActivity implements
2122
IDownloadCallback {
2223
private ProgressDialog progressDlg;
@@ -62,8 +63,8 @@ protected void showList() {
6263
if (progressDlg != null && progressDlg.isShowing()) {
6364
progressDlg.dismiss();
6465
}
65-
final ArrayAdapter<TwicItem> aa = new ArrayAdapter<TwicItem>(this,
66-
android.R.layout.simple_list_item_1, downloader.getLinkList());
66+
final ArrayAdapter<TwicItem> aa = new ArrayAdapter<>(this,
67+
android.R.layout.simple_list_item_1, downloader.getLinkList());
6768
setListAdapter(aa);
6869
ListView lv = getListView();
6970
lv.setOnItemClickListener(new OnItemClickListener() {
@@ -74,7 +75,7 @@ public void onItemClick(AdapterView<?> parent, View view, int pos,
7475
progressDlg = ProgressDialog.show(ImportTwicActivity.this,
7576
getString(R.string.twic_downloading),
7677
getString(R.string.downloading), true, false);
77-
new ImportZipTask().execute(ImportTwicActivity.this,
78+
new ImportZipTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ImportTwicActivity.this,
7879
progressDlg, item.getLink());
7980
}
8081
});
@@ -103,16 +104,13 @@ public void downloadFailure(String message) {
103104

104105
@Override
105106
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
106-
switch (requestCode) {
107-
case RESULT_PGN_IMPORT:
108-
// the result after importing the pgn file - delete pgn file
107+
if (requestCode == RESULT_PGN_IMPORT) {// the result after importing the pgn file - delete pgn file
109108
if (resultCode == RESULT_OK && data != null) {
110109
String pgnFileName = data.getAction();
111110
if (pgnFileName != null) {
112111
new File(pgnFileName).delete();
113112
}
114113
}
115-
break;
116114
}
117115
}
118116
}

‎scidOnTheGo/src/main/java/org/scid/android/twic/TwicDownloader.java

+16-32
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,13 @@
1717
import java.util.List;
1818
import java.util.Set;
1919

20-
public class TwicDownloader {
20+
class TwicDownloader {
21+
private final static String OLD_TWIC_SITE = "https://www.theweekinchess.com";
22+
private final static String NEW_TWIC_SITE = "https://theweekinchess.com";
23+
private final static String TWIC_SITE = NEW_TWIC_SITE + "/twic/";
24+
private Set<String> linkList = new HashSet<>();
2125

22-
private static String TWIC_SITE = "http://www.theweekinchess.com/twic/";
23-
private Set<String> linkList = new HashSet<String>();
24-
25-
public File getCurrentTwic(String directory) throws IOException {
26-
parseTwicSite();
27-
String currentZip = getCurrentTwicZipName();
28-
return getPgnFromZipUrl(directory, currentZip);
29-
}
30-
31-
public File getPgnFromZipUrl(String directory, String zipUrl) throws IOException {
26+
File getPgnFromZipUrl(String directory, String zipUrl) throws IOException {
3227
File f = Tools.downloadFile(zipUrl);
3328
if (f != null) {
3429
return Tools.unzip(directory, f, true);
@@ -37,38 +32,29 @@ public File getPgnFromZipUrl(String directory, String zipUrl) throws IOException
3732
}
3833
}
3934

40-
public List<TwicItem> getLinkList() {
41-
List<String> llist = new ArrayList<String>(linkList);
35+
List<TwicItem> getLinkList() {
36+
List<String> llist = new ArrayList<>(linkList);
4237
Collections.sort(llist, Collections.reverseOrder());
43-
List<TwicItem> result = new ArrayList<TwicItem>();
38+
List<TwicItem> result = new ArrayList<>();
4439
for (String link : llist) {
4540
result.add(new TwicItem(link));
4641
}
4742
return result;
4843
}
4944

50-
private String getCurrentTwicZipName() {
51-
String result = null;
52-
if (this.linkList.size() > 0) {
53-
result = this.getLinkList().get(0).getLink();
54-
}
55-
return result;
56-
}
57-
58-
public void parseTwicSite() {
59-
String data = "";
45+
void parseTwicSite() {
46+
String data;
6047
try {
6148
URL obj = new URL(TWIC_SITE);
6249
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
6350
con.setRequestMethod("GET");
64-
con.setRequestProperty("User-Agent", "Mozilla/5.0");
6551

6652
int status = con.getResponseCode();
6753
if (status != HttpURLConnection.HTTP_OK) {
6854
BufferedReader in = new BufferedReader(new InputStreamReader(
6955
con.getInputStream()));
7056
String inputLine;
71-
StringBuffer response = new StringBuffer();
57+
StringBuilder response = new StringBuilder();
7258
while ((inputLine = in.readLine()) != null) {
7359
response.append(inputLine);
7460
}
@@ -77,11 +63,11 @@ public void parseTwicSite() {
7763
} else {
7864
BufferedReader reader = new BufferedReader(new InputStreamReader(
7965
con.getInputStream()));
80-
StringBuffer stringBuffer = new StringBuffer();
81-
String line = null;
66+
StringBuilder stringBuffer = new StringBuilder();
67+
String line;
8268
int noLinks = 0;
8369
while (noLinks < 20 && (line = reader.readLine()) != null) {
84-
stringBuffer.append(line + "\n");
70+
stringBuffer.append(line).append("\n");
8571
if (line.contains("g.zip")) {
8672
noLinks++;
8773
}
@@ -92,13 +78,11 @@ public void parseTwicSite() {
9278
List<Link> links = Tools.getLinks(data);
9379
for (Link link : links) {
9480
if (link.getLink().endsWith("g.zip")) {
95-
linkList.add(link.getLink());
81+
linkList.add(link.getLink().replace(OLD_TWIC_SITE, NEW_TWIC_SITE));
9682
}
9783
}
9884
} catch (IOException e1) {
9985
Log.e("SCID", e1.getMessage(), e1);
100-
data = e1.getMessage();
10186
}
10287
}
103-
10488
}

0 commit comments

Comments
 (0)
Failed to load comments.