17
17
import java .util .List ;
18
18
import java .util .Set ;
19
19
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 <>();
21
25
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 {
32
27
File f = Tools .downloadFile (zipUrl );
33
28
if (f != null ) {
34
29
return Tools .unzip (directory , f , true );
@@ -37,38 +32,29 @@ public File getPgnFromZipUrl(String directory, String zipUrl) throws IOException
37
32
}
38
33
}
39
34
40
- public List <TwicItem > getLinkList () {
41
- List <String > llist = new ArrayList <String >(linkList );
35
+ List <TwicItem > getLinkList () {
36
+ List <String > llist = new ArrayList <>(linkList );
42
37
Collections .sort (llist , Collections .reverseOrder ());
43
- List <TwicItem > result = new ArrayList <TwicItem >();
38
+ List <TwicItem > result = new ArrayList <>();
44
39
for (String link : llist ) {
45
40
result .add (new TwicItem (link ));
46
41
}
47
42
return result ;
48
43
}
49
44
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 ;
60
47
try {
61
48
URL obj = new URL (TWIC_SITE );
62
49
HttpURLConnection con = (HttpURLConnection ) obj .openConnection ();
63
50
con .setRequestMethod ("GET" );
64
- con .setRequestProperty ("User-Agent" , "Mozilla/5.0" );
65
51
66
52
int status = con .getResponseCode ();
67
53
if (status != HttpURLConnection .HTTP_OK ) {
68
54
BufferedReader in = new BufferedReader (new InputStreamReader (
69
55
con .getInputStream ()));
70
56
String inputLine ;
71
- StringBuffer response = new StringBuffer ();
57
+ StringBuilder response = new StringBuilder ();
72
58
while ((inputLine = in .readLine ()) != null ) {
73
59
response .append (inputLine );
74
60
}
@@ -77,11 +63,11 @@ public void parseTwicSite() {
77
63
} else {
78
64
BufferedReader reader = new BufferedReader (new InputStreamReader (
79
65
con .getInputStream ()));
80
- StringBuffer stringBuffer = new StringBuffer ();
81
- String line = null ;
66
+ StringBuilder stringBuffer = new StringBuilder ();
67
+ String line ;
82
68
int noLinks = 0 ;
83
69
while (noLinks < 20 && (line = reader .readLine ()) != null ) {
84
- stringBuffer .append (line + "\n " );
70
+ stringBuffer .append (line ). append ( "\n " );
85
71
if (line .contains ("g.zip" )) {
86
72
noLinks ++;
87
73
}
@@ -92,13 +78,11 @@ public void parseTwicSite() {
92
78
List <Link > links = Tools .getLinks (data );
93
79
for (Link link : links ) {
94
80
if (link .getLink ().endsWith ("g.zip" )) {
95
- linkList .add (link .getLink ());
81
+ linkList .add (link .getLink (). replace ( OLD_TWIC_SITE , NEW_TWIC_SITE ) );
96
82
}
97
83
}
98
84
} catch (IOException e1 ) {
99
85
Log .e ("SCID" , e1 .getMessage (), e1 );
100
- data = e1 .getMessage ();
101
86
}
102
87
}
103
-
104
88
}
0 commit comments