added searchPackage and getPackageTree to arch impl
This commit is contained in:
parent
5c0b0e115b
commit
513ba715b6
@ -1,85 +1,96 @@
|
|||||||
package fr.packageviewer.ArchParser;
|
package fr.packageviewer.ArchParser;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import java.net.http.*;
|
||||||
|
import org.json.*;
|
||||||
|
|
||||||
public class ArchParser {
|
public class ArchParser {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will return the String json of the package from the Arch Linux API
|
||||||
|
* @param packageName the package name to get the json from
|
||||||
|
* @return json of the package
|
||||||
|
*/
|
||||||
public String getPackageFromAPI(String packageName){
|
public String getPackageFromAPI(String packageName){
|
||||||
|
// create a new http client
|
||||||
String jsonStr = """
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
{
|
// and create its url
|
||||||
"version": 2,
|
HttpRequest request = HttpRequest.newBuilder(URI.create("https://archlinux.org/packages/search/json/?name="+packageName)).build();
|
||||||
"limit": 250,
|
// send its url and return the string given
|
||||||
"valid": true,
|
try {
|
||||||
"results": [
|
return client.send(request, HttpResponse.BodyHandlers.ofString()).body();
|
||||||
{
|
} catch (IOException|InterruptedException e) {
|
||||||
"pkgname": "bash",
|
e.printStackTrace();
|
||||||
"pkgbase": "bash",
|
}
|
||||||
"repo": "core",
|
// if there's an error, return an empty string
|
||||||
"arch": "x86_64",
|
return "";
|
||||||
"pkgver": "5.1.016",
|
|
||||||
"pkgrel": "1",
|
|
||||||
"epoch": 0,
|
|
||||||
"pkgdesc": "The GNU Bourne Again shell",
|
|
||||||
"url": "https://www.gnu.org/software/bash/bash.html",
|
|
||||||
"filename": "bash-5.1.016-1-x86_64.pkg.tar.zst",
|
|
||||||
"compressed_size": 1707705,
|
|
||||||
"installed_size": 8592907,
|
|
||||||
"build_date": "2022-01-08T18:31:11Z",
|
|
||||||
"last_update": "2022-01-09T19:38:32.491Z",
|
|
||||||
"flag_date": "2022-09-26T16:50:03.401Z",
|
|
||||||
"maintainers": [
|
|
||||||
"felixonmars",
|
|
||||||
"anthraxx",
|
|
||||||
"grazzolini"
|
|
||||||
],
|
|
||||||
"packager": "felixonmars",
|
|
||||||
"groups": [],
|
|
||||||
"licenses": [
|
|
||||||
"GPL"
|
|
||||||
],
|
|
||||||
"conflicts": [],
|
|
||||||
"provides": [
|
|
||||||
"sh"
|
|
||||||
],
|
|
||||||
"replaces": [],
|
|
||||||
"depends": [
|
|
||||||
"glibc",
|
|
||||||
"libreadline.so=8-64",
|
|
||||||
"ncurses",
|
|
||||||
"readline"
|
|
||||||
],
|
|
||||||
"optdepends": [
|
|
||||||
"bash-completion: for tab completion"
|
|
||||||
],
|
|
||||||
"makedepends": [],
|
|
||||||
"checkdepends": []
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"num_pages": 1,
|
|
||||||
"page": 1
|
|
||||||
}""";
|
|
||||||
return jsonStr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for a package and return a list of packages
|
||||||
|
* @param packageName the package name to search
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<SearchedPackage> searchPackage(String packageName){
|
||||||
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create("https://archlinux.org/packages/search/json/?q="+packageName))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response;
|
||||||
|
try{
|
||||||
|
response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
}catch(IOException|InterruptedException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONObject json = new JSONObject(response.body());
|
||||||
|
|
||||||
|
List<SearchedPackage> searchedPackagesList = new ArrayList<>();
|
||||||
|
|
||||||
|
// iterate for every package in the list
|
||||||
|
for (Object searchResultObj : json.getJSONArray("results")) {
|
||||||
|
// convert object into String
|
||||||
|
JSONObject searchResultJson = (JSONObject) searchResultObj;
|
||||||
|
// add package into to list
|
||||||
|
searchedPackagesList.add(new SearchedPackage(
|
||||||
|
searchResultJson.getString("pkgname"),
|
||||||
|
searchResultJson.getString("pkgver"),
|
||||||
|
searchResultJson.getString("repo"),
|
||||||
|
searchResultJson.getString("pkgdesc")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return searchedPackagesList;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will parse the given json and store data in a Package
|
* Will generate a dependencies tree of depth 'depth' given the package name
|
||||||
* @param packageName the packag name to search
|
* @param packageName the package name to search
|
||||||
* @param depth depth to search dependencies
|
* @param depth depth to search dependencies
|
||||||
* @return new Package
|
* @return new Package
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public Package getPackageTree(String packageName, int depth){
|
public Package getPackageTree(String packageName, int depth){
|
||||||
String name, version, repo, description;
|
String name, version, repo, description;
|
||||||
List<Package> deps = new ArrayList<>();
|
List<Package> deps = new ArrayList<>();
|
||||||
|
|
||||||
// parse the json
|
// parse the json
|
||||||
JSONObject json = new JSONObject(getPackageFromAPI(packageName));
|
JSONObject json = new JSONObject(getPackageFromAPI(packageName));
|
||||||
//
|
|
||||||
|
JSONArray resultsArrayJson = json.getJSONArray("results");
|
||||||
|
if(resultsArrayJson.length()==0){
|
||||||
|
// unknown package, probably an abstract dependency
|
||||||
|
return null;
|
||||||
|
}
|
||||||
JSONObject resultJson = json.getJSONArray("results").getJSONObject(0);
|
JSONObject resultJson = json.getJSONArray("results").getJSONObject(0);
|
||||||
|
|
||||||
// get infos except dependencies
|
// get infos except dependencies
|
||||||
@ -92,7 +103,8 @@ public class ArchParser {
|
|||||||
if(depth==0){
|
if(depth==0){
|
||||||
return new Package(name, version, repo, description, Collections.emptyList());
|
return new Package(name, version, repo, description, Collections.emptyList());
|
||||||
} else {
|
} else {
|
||||||
for (Object depPackageNameObj : json.getJSONArray("depends")) {
|
// iterate for every package in the list
|
||||||
|
for (Object depPackageNameObj : resultJson.getJSONArray("depends")) {
|
||||||
// convert object into String
|
// convert object into String
|
||||||
String depPackageName = (String) depPackageNameObj;
|
String depPackageName = (String) depPackageNameObj;
|
||||||
// add package into Package List
|
// add package into Package List
|
||||||
|
|||||||
@ -2,19 +2,12 @@ package fr.packageviewer.ArchParser;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Package {
|
public class Package extends SearchedPackage {
|
||||||
String name;
|
|
||||||
String version;
|
|
||||||
String repo;
|
|
||||||
String description;
|
|
||||||
List<Package> deps;
|
List<Package> deps;
|
||||||
|
|
||||||
public Package(String name, String version, String repo, String desciption, List<Package> deps) {
|
public Package(String name, String version, String repo, String description, List<Package> deps) {
|
||||||
this.name = name;
|
super(name, version, repo, description);
|
||||||
this.version = version;
|
|
||||||
this.repo = repo;
|
|
||||||
this.description = desciption;
|
|
||||||
this.deps = deps;
|
this.deps = deps;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,16 @@
|
|||||||
|
package fr.packageviewer.ArchParser;
|
||||||
|
|
||||||
|
public class SearchedPackage {
|
||||||
|
String name;
|
||||||
|
String version;
|
||||||
|
String repo;
|
||||||
|
String description;
|
||||||
|
|
||||||
|
public SearchedPackage(String name, String version, String repo, String desciption) {
|
||||||
|
this.name = name;
|
||||||
|
this.version = version;
|
||||||
|
this.repo = repo;
|
||||||
|
this.description = desciption;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user