11package  com .eternalcode .commons .updater .impl ;
22
3+ import  com .eternalcode .commons .Lazy ;
34import  com .eternalcode .commons .updater .UpdateChecker ;
45import  com .eternalcode .commons .updater .UpdateResult ;
56import  com .eternalcode .commons .updater .Version ;
89import  java .net .http .HttpRequest ;
910import  java .net .http .HttpResponse ;
1011import  java .time .Duration ;
11- import  java .util .Optional ;
12+ import  org .json .JSONArray ;
13+ import  org .json .JSONException ;
14+ import  org .json .JSONObject ;
1215
1316public  final  class  ModrinthUpdateChecker  implements  UpdateChecker  {
1417
@@ -17,11 +20,7 @@ public final class ModrinthUpdateChecker implements UpdateChecker {
1720    private  static  final  String  USER_AGENT  = "UpdateChecker/1.0" ;
1821    private  static  final  Duration  TIMEOUT  = Duration .ofSeconds (10 );
1922
20-     private  final  HttpClient  client ;
21- 
22-     public  ModrinthUpdateChecker () {
23-         this .client  = HttpClient .newBuilder ().connectTimeout (TIMEOUT ).build ();
24-     }
23+     private  final  Lazy <HttpClient > client  = new  Lazy <>(() -> HttpClient .newBuilder ().connectTimeout (TIMEOUT ).build ());
2524
2625    @ Override 
2726    public  UpdateResult  check (String  projectId , Version  currentVersion ) {
@@ -35,7 +34,7 @@ public UpdateResult check(String projectId, Version currentVersion) {
3534            HttpRequest  request  =
3635                HttpRequest .newBuilder ().uri (URI .create (url )).header ("User-Agent" , USER_AGENT ).timeout (TIMEOUT ).build ();
3736
38-             HttpResponse <String > response  = this .client .send (request , HttpResponse .BodyHandlers .ofString ());
37+             HttpResponse <String > response  = this .client .get (). send (request , HttpResponse .BodyHandlers .ofString ());
3938
4039            if  (response .statusCode () != 200 ) {
4140                return  createEmptyResult (currentVersion );
@@ -46,47 +45,48 @@ public UpdateResult check(String projectId, Version currentVersion) {
4645                return  createEmptyResult (currentVersion );
4746            }
4847
49-             Optional <String > versionNumber  = extractJsonValue (json , "version_number" );
50-             Optional <String > downloadUrl  = extractJsonValue (json , "url" );
51- 
52-             if  (versionNumber .isEmpty ()) {
53-                 return  createEmptyResult (currentVersion );
54-             }
55- 
56-             String  releaseUrl  = MODRINTH_BASE_URL  + "/"  + projectId  + "/version/"  + versionNumber .get ();
57-             Version  latestVersion  = new  Version (versionNumber .get ());
58- 
59-             return  new  UpdateResult (currentVersion , latestVersion , downloadUrl .orElse (null ), releaseUrl );
48+             return  parseVersionResponse (json , currentVersion , projectId );
6049        }
6150        catch  (Exception  exception ) {
6251            throw  new  RuntimeException ("Failed to check Modrinth updates for project: "  + projectId , exception );
6352        }
6453    }
6554
66-     private  UpdateResult  createEmptyResult ( Version  currentVersion ) {
67-         return   new   UpdateResult ( currentVersion ,  currentVersion ,  null ,  null ); 
68-     } 
55+     private  UpdateResult  parseVersionResponse ( String   json ,  Version  currentVersion ,  String   projectId ) {
56+         try  { 
57+              JSONArray   versions  =  new   JSONArray ( json ); 
6958
70-     private  Optional <String > extractJsonValue (String  json , String  key ) {
71-         if  (json  == null  || key  == null ) {
72-             return  Optional .empty ();
73-         }
59+             if  (versions .isEmpty ()) {
60+                 return  createEmptyResult (currentVersion );
61+             }
7462
75-         String  pattern  = "\" "  + key  + "\" :\" " ;
76-         int  start  = json .indexOf (pattern );
63+             JSONObject  latestVersionObj  = versions .getJSONObject (0 );
7764
78-         if  (start  == -1 ) {
79-             return  Optional .empty ();
80-         }
65+             String  versionNumber  = latestVersionObj .optString ("version_number" , null );
66+             if  (versionNumber  == null  || versionNumber .trim ().isEmpty ()) {
67+                 return  createEmptyResult (currentVersion );
68+             }
8169
82-         start  += pattern .length ();
83-         int  end  = json .indexOf ("\" " , start );
70+             String  downloadUrl  = null ;
71+             if  (latestVersionObj .has ("files" )) {
72+                 JSONArray  files  = latestVersionObj .getJSONArray ("files" );
73+                 if  (!files .isEmpty ()) {
74+                     JSONObject  firstFile  = files .getJSONObject (0 );
75+                     downloadUrl  = firstFile .optString ("url" , null );
76+                 }
77+             }
8478
85-         if  (end  == -1 ) {
86-             return  Optional .empty ();
79+             String  releaseUrl  = MODRINTH_BASE_URL  + "/"  + projectId  + "/version/"  + versionNumber ;
80+             Version  latestVersion  = new  Version (versionNumber );
81+ 
82+             return  new  UpdateResult (currentVersion , latestVersion , downloadUrl , releaseUrl );
83+         }
84+         catch  (JSONException  exception ) {
85+             return  createEmptyResult (currentVersion );
8786        }
87+     }
8888
89-          String   value  =  json . substring ( start ,  end ); 
90-         return  value . isEmpty () ?  Optional . empty () :  Optional . of ( value );
89+     private   UpdateResult   createEmptyResult ( Version   currentVersion ) { 
90+         return  new   UpdateResult ( currentVersion ,  currentVersion ,  null ,  null );
9191    }
9292}
0 commit comments