Commit 1c694cc7 authored by Jerome Chauveau's avatar Jerome Chauveau
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

pom.xml

0 → 100644
+30 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>fr.unicaen.pdn-certic</groupId>
    <artifactId>zotero-java-client</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>

    </properties>

    <dependencies>
        <dependency>
            <groupId>com.konghq</groupId>
            <artifactId>unirest-java</artifactId>
            <version>3.6.00</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.0</version>
        </dependency>
    </dependencies>
</project>
 No newline at end of file
+78 −0
Original line number Diff line number Diff line
package fr.unicaen.pdncertic.zotero;

import com.fasterxml.jackson.core.type.TypeReference;
import kong.unirest.*;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONException;
import kong.unirest.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ZoteroClient {

    private static String API_URL = "https://api.zotero.org/";

    public static List<ZoteroItem> search(ZoteroLibrary library, String query) throws ZoteroClientException {
        try {
            List<ZoteroItem> items = new ArrayList<>();
            String url = API_URL + library.getType().toString() + "/" + library.getId() + "/items?q=" + query;
            GetRequest getRequest = Unirest.get(url);
            if (library.getApiKey() != null)
                getRequest = getRequest.header("Authorization", "Bearer " + library.getApiKey());
            HttpResponse<JsonNode> response = getRequest.asJson();
            System.err.println(response.getBody().toString());
            if (response.getStatus() == 200) {
                JSONArray jsonResponseArray = response.getBody().getArray();
                for (int i = 0; i < jsonResponseArray.length(); i++) {
                    String href = jsonResponseArray.getJSONObject(i).getJSONObject("links").getJSONObject("self").getString("href");
                    JSONObject dataObject = jsonResponseArray.getJSONObject(i).getJSONObject("data");
                    ObjectMapper mapper = new ObjectMapper();
                    Map<String, Object> map = mapper.readValue(dataObject.toString(), new TypeReference<Map<String, Object>>(){});
                    String key = readDataValue("key", dataObject);
                    ZoteroItem currentItem = new ZoteroItem(key, href, map, library);
                    items.add(currentItem);
                }
                return items;
            } else {
                throw new ZoteroClientException("");
            }
        }
        catch(Exception e){
            throw new ZoteroClientException("bad json response format ?");
        }

    }

    public static String readDataValue(String key, JSONObject dataObject){
        try {
            return dataObject.get(key).toString();
        }
        catch(JSONException e){
            return null;
        }
    }

    /** tests **/
    public static void main(String[] args){
        fr.unicaen.pdncertic.zotero.ZoteroLibrary[] libs = {
                new fr.unicaen.pdncertic.zotero.ZoteroLibrary(fr.unicaen.pdncertic.zotero.ZoteroLibrary.LibraryType.users, "475425"),
                new fr.unicaen.pdncertic.zotero.ZoteroLibrary(fr.unicaen.pdncertic.zotero.ZoteroLibrary.LibraryType.groups, "447270"),
                new fr.unicaen.pdncertic.zotero.ZoteroLibrary(fr.unicaen.pdncertic.zotero.ZoteroLibrary.LibraryType.groups, "427575")
        };
        List<fr.unicaen.pdncertic.zotero.ZoteroItem> items = null;
        for(int i = 0; i < libs.length; i++) {
            fr.unicaen.pdncertic.zotero.ZoteroLibrary lib = libs[i];
            try {
                items = fr.unicaen.pdncertic.zotero.ZoteroClient.search(lib, "e");
            } catch (fr.unicaen.pdncertic.zotero.ZoteroClientException e) {
                e.printStackTrace();
            }
            for (fr.unicaen.pdncertic.zotero.ZoteroItem it : items) {
                System.err.println(it.getKey() + " -> " + it.getDataValue("publisher")+"/"+ it.getDataValue("date"));
            }
        }
    }

}
+8 −0
Original line number Diff line number Diff line
package fr.unicaen.pdncertic.zotero;

public class ZoteroClientException extends Exception {

    public ZoteroClientException(String message){
        super(message);
    }
}
+57 −0
Original line number Diff line number Diff line
package fr.unicaen.pdncertic.zotero;

public class ZoteroCreator {


    private String creatorType;
    private String localized;
    private String firstName;
    private String lastName;
    private String name;

    public ZoteroCreator(String type, String firstName, String lastName){
        this.creatorType = type;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getCreatorType() {
        return creatorType;
    }

    public String getLocalized() {
        return localized;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getName() {
        return name;
    }

    public void setCreatorType(String creatorType) {
        this.creatorType = creatorType;
    }

    public void setLocalized(String localized) {
        this.localized = localized;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setName(String name) {
        this.name = name;
    }
}
 No newline at end of file
+43 −0
Original line number Diff line number Diff line
package fr.unicaen.pdncertic.zotero;

import java.util.Map;

public class ZoteroItem {

    private String key;
    private ZoteroLibrary library;
    private Map<String, Object> dataMap;
    private String href;

    public ZoteroItem(String key, String href, Map<String, Object> dataMap, ZoteroLibrary library){
        this.key = key;
        this.href = href;
        this.dataMap = dataMap;
        this.library = library;
    }

    public String getKey() {
        return this.key;
    }

    public String getHref() {
        return this.href;
    }

    public ZoteroLibrary getLibrary() {
        return library;
    }

    public void setLibrary(ZoteroLibrary library) {
        this.library = library;
    }


    public Map<String, Object> getDataMap(){
        return this.dataMap;
    }

    public Object getDataValue(String key){
        return this.dataMap.get(key);
    }
}
Loading