added searchPackage and getPackageTree to arch impl
This commit is contained in:
parent
5c0b0e115b
commit
513ba715b6
@ -1,85 +1,96 @@
|
||||
package fr.packageviewer.ArchParser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import java.net.http.*;
|
||||
import org.json.*;
|
||||
|
||||
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){
|
||||
// create a new http client
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
// and create its url
|
||||
HttpRequest request = HttpRequest.newBuilder(URI.create("https://archlinux.org/packages/search/json/?name="+packageName)).build();
|
||||
// send its url and return the string given
|
||||
try {
|
||||
return client.send(request, HttpResponse.BodyHandlers.ofString()).body();
|
||||
} catch (IOException|InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// if there's an error, return an empty string
|
||||
return "";
|
||||
}
|
||||
|
||||
String jsonStr = """
|
||||
{
|
||||
"version": 2,
|
||||
"limit": 250,
|
||||
"valid": true,
|
||||
"results": [
|
||||
{
|
||||
"pkgname": "bash",
|
||||
"pkgbase": "bash",
|
||||
"repo": "core",
|
||||
"arch": "x86_64",
|
||||
"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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will parse the given json and store data in a Package
|
||||
* @param packageName the packag name to search
|
||||
* 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 generate a dependencies tree of depth 'depth' given the package name
|
||||
* @param packageName the package name to search
|
||||
* @param depth depth to search dependencies
|
||||
* @return new Package
|
||||
*/
|
||||
|
||||
public Package getPackageTree(String packageName, int depth){
|
||||
String name, version, repo, description;
|
||||
List<Package> deps = new ArrayList<>();
|
||||
|
||||
// parse the json
|
||||
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);
|
||||
|
||||
// get infos except dependencies
|
||||
@ -92,7 +103,8 @@ public class ArchParser {
|
||||
if(depth==0){
|
||||
return new Package(name, version, repo, description, Collections.emptyList());
|
||||
} else {
|
||||
for (Object depPackageNameObj : json.getJSONArray("depends")) {
|
||||
// iterate for every package in the list
|
||||
for (Object depPackageNameObj : resultJson.getJSONArray("depends")) {
|
||||
// convert object into String
|
||||
String depPackageName = (String) depPackageNameObj;
|
||||
// add package into Package List
|
||||
|
||||
@ -2,18 +2,11 @@ package fr.packageviewer.ArchParser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Package {
|
||||
String name;
|
||||
String version;
|
||||
String repo;
|
||||
String description;
|
||||
public class Package extends SearchedPackage {
|
||||
List<Package> deps;
|
||||
|
||||
public Package(String name, String version, String repo, String desciption, List<Package> deps) {
|
||||
this.name = name;
|
||||
this.version = version;
|
||||
this.repo = repo;
|
||||
this.description = desciption;
|
||||
public Package(String name, String version, String repo, String description, List<Package> deps) {
|
||||
super(name, version, repo, description);
|
||||
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