Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing character and people fields #393

Merged
merged 6 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.katsute</groupId>
<artifactId>mal4j</artifactId>
<version>3.3.0</version>
<version>3.4.0-SNAPSHOT</version>

<profiles>
<profile>
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/dev/katsute/mal4j/Fields.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* The fields class holds all possible fields for a request. Usable in any methods that ask for fields.
*
* @since 1.1.0
* @version 3.3.0
* @version 3.4.0
* @author Katsute
*/
public abstract class Fields {
Expand Down Expand Up @@ -560,7 +560,7 @@ public static String serialization(final String... fields){
*
* @see #character
* @since 3.1.0
* @version 3.3.0
* @version 3.4.0
* @author Katsute
*/
public static class Character {
Expand All @@ -576,6 +576,9 @@ private Character(){}
public static final String alternative_name = "alternative_name";

public static final String main_picture = "main_picture";

public static final String num_favorites = "num_favorites";

public static final String pictures = "pictures";

public static final String biography = "biography";
Expand All @@ -597,6 +600,7 @@ private Character(){}
Character.last_name,
Character.alternative_name,
Character.main_picture,
Character.num_favorites,
Character.pictures,
Character.biography,
Character.animeography
Expand All @@ -609,6 +613,7 @@ private Character(){}
*
* @see #people
* @since 3.2.0
* @version 3.4.0
*/
public static class People {

Expand All @@ -620,8 +625,14 @@ private People(){}

public static final String birthday = "birthday";

public static final String alternative_names = "alternative_names";

public static final String main_picture = "main_picture";

public static final String num_favorites = "num_favorites";

public static final String more = "more";

}

/**
Expand All @@ -634,7 +645,10 @@ private People(){}
People.first_name,
People.last_name,
People.birthday,
People.main_picture
People.alternative_names,
People.main_picture,
People.num_favorites,
People.more
);

// user
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/dev/katsute/mal4j/MyAnimeListSchema_Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import dev.katsute.mal4j.property.Picture;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("SpellCheckingInspection")
abstract class MyAnimeListSchema_Character extends MyAnimeListSchema {
Expand All @@ -39,8 +41,10 @@ static Character asCharacter(final MyAnimeList mal, final JsonObject schema){
private String lastName;
private String alternativeNames;
private Picture mainPicture;
private Integer favorites;
private Picture[] pictures;
private String biography;
private Map<String,String> biographyDetails;
private Animeography[] animeography;

{
Expand All @@ -60,9 +64,10 @@ private void populate(final JsonObject schema){
lastName = schema.getString("last_name");
alternativeNames = schema.getString("alternative_name");
mainPicture = MyAnimeListSchema_Common.asPicture(mal, schema.getJsonObject("main_picture"));
favorites = schema.getInt("num_favorites");
pictures = adaptList(schema.getJsonArray("pictures"), s -> MyAnimeListSchema_Common.asPicture(mal, s), Picture.class);
biography = schema.getString("biography");

biographyDetails = biography == null ? null : MyAnimeListSchema_Common.asMap(biography);
animeography = adaptList(schema.getJsonArray("animeography"), s -> asAnimeography(mal, s), Animeography.class);
}

Expand Down Expand Up @@ -101,6 +106,13 @@ public final Picture getMainPicture(){
return mainPicture;
}

@Override
public final Integer getFavorites(){
if(favorites == null && draft)
populate();
return favorites;
}

@Override
public final Picture[] getPictures(){
if(pictures == null && draft)
Expand All @@ -115,6 +127,13 @@ public final String getBiography(){
return biography;
}

@Override
public final Map<String,String> getBiographyDetails(){
if(biographyDetails == null && draft)
populate();
return biographyDetails == null ? null : new HashMap<>(biographyDetails);
}

@Override
public final Animeography[] getAnimeography(){
if(animeography == null && draft)
Expand All @@ -132,8 +151,10 @@ public final String toString(){
", lastName='" + lastName + '\'' +
", alternativeNames='" + alternativeNames + '\'' +
", mainPicture=" + mainPicture +
", favorites=" + favorites +
", pictures=" + Arrays.toString(pictures) +
", biography='" + biography + '\'' +
", biographyDetails=" + biographyDetails +
", animeography=" + Arrays.toString(animeography) +
'}';
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/dev/katsute/mal4j/MyAnimeListSchema_Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import dev.katsute.mal4j.property.Picture;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

@SuppressWarnings("unused")
abstract class MyAnimeListSchema_Common extends MyAnimeListSchema {
Expand Down Expand Up @@ -153,4 +156,25 @@ public final int hashCode(){
};
}

// <br( ?\/?)?>
@SuppressWarnings("RegExpRedundantEscape")
private static final Pattern br = Pattern.compile("<br( ?\\/?)?>");

// (\r?\n)+
private static final Pattern rn = Pattern.compile("(\\r?\\n)+");

static Map<String,String> asMap(final String s){
final HashMap<String,String> map = new HashMap<>();
for(final String ln : rn.split(br.matcher(s).replaceAll(""))){
int cn = ln.indexOf(':');
if(cn > 0)
map.put(ln.substring(0, cn).trim(), ln.substring(cn + 1).trim());
else if(map.containsKey("*"))
map.put("*", (map.get("*") + '\n' + ln).trim());
else
map.put("*", ln.trim());
}
return map;
}

}
39 changes: 35 additions & 4 deletions src/main/java/dev/katsute/mal4j/MyAnimeListSchema_People.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import dev.katsute.mal4j.people.Person;
import dev.katsute.mal4j.property.Picture;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

abstract class MyAnimeListSchema_People extends MyAnimeListSchema {

Expand All @@ -30,10 +33,14 @@ static Person asPerson(final MyAnimeList mal, final JsonObject schema){

private final Long id = schema.getLong("id");

private final String firstName = schema.getString("first_name");
private final String lastName = schema.getString("last_name");
private final Long birthday = parseDate(schema.getString("birthday"));
private final Picture mainPicture = MyAnimeListSchema_Common.asPicture(mal, schema.getJsonObject("main_picture"));
private final String firstName = schema.getString("first_name");
private final String lastName = schema.getString("last_name");
private final Long birthday = parseDate(schema.getString("birthday"));
private final String[] alternative_names = schema.getStringArray("alternative_names");
private final Integer favorites = schema.getInt("num_favorites");
private final Picture mainPicture = MyAnimeListSchema_Common.asPicture(mal, schema.getJsonObject("main_picture"));
private final String more = schema.getString("more");
private final Map<String,String> moreDetails = more == null ? null : MyAnimeListSchema_Common.asMap(more);

@Override
public final Long getID(){
Expand All @@ -55,11 +62,31 @@ public final Date getBirthday(){
return birthday == null ? null : new Date(birthday);
}

@Override
public final String[] getAlternativeNames(){
return alternative_names == null ? null : Arrays.copyOf(alternative_names, alternative_names.length);
}

@Override
public final Picture getMainPicture(){
return mainPicture;
}

@Override
public final Integer getFavorites(){
return favorites;
}

@Override
public final String getMore(){
return more;
}

@Override
public final Map<String,String> getMoreDetails(){
return moreDetails == null ? null : new HashMap<>(moreDetails);
}

// additional methods

@Override
Expand All @@ -69,7 +96,11 @@ public final String toString(){
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", birthday=" + birthday +
", alternative_names=" + Arrays.toString(alternative_names) +
", favorites=" + favorites +
", mainPicture=" + mainPicture +
", more='" + more + '\'' +
", moreDetails=" + moreDetails +
'}';
}

Expand Down
24 changes: 23 additions & 1 deletion src/main/java/dev/katsute/mal4j/character/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
import dev.katsute.mal4j.property.ID;
import dev.katsute.mal4j.property.Picture;

import java.util.Map;

/**
* Represents a character.
*
* @see dev.katsute.mal4j.MyAnimeList#getAnimeCharacters(long)
* @see dev.katsute.mal4j.MyAnimeList#getCharacter(long)
* @see dev.katsute.mal4j.MyAnimeList#getCharacter(long, String...)
* @since 3.1.0
* @version 3.3.0
* @version 3.4.0
* @author Katsute
*/
public abstract class Character implements ID {
Expand Down Expand Up @@ -77,6 +79,15 @@ public abstract class Character implements ID {
*/
public abstract Picture getMainPicture();

/**
* Returns the number of favorites.
*
* @return favorites
*
* @since 3.4.0
*/
public abstract Integer getFavorites();

/**
* Returns the pictures for the character.
*
Expand All @@ -93,10 +104,21 @@ public abstract class Character implements ID {
*
* @return biography
*
* @see #getBiographyDetails()
* @since 3.1.0
*/
public abstract String getBiography();

/**
* Returns biography details as a map. Sorted in the same order as {@link #getBiography()}, biography content is saved under the <code>*</code> key.
*
* @return details
*
* @see #getBiography()
* @since 3.4.0
*/
public abstract Map<String,String> getBiographyDetails();

/**
* Returns a list of which Anime the character has appeared in.
*
Expand Down
Loading