Skip to content

Commit

Permalink
ARTE: fix collections with multiple children
Browse files Browse the repository at this point in the history
  • Loading branch information
alex1702 committed Jun 1, 2022
2 parents e230651 + 357c84d commit 8de2094
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sourceCompatibility = 1.8
targetCompatibility = 1.8
group = 'de.mediathekview'
archivesBaseName = "MServer"
version = '3.1.197'
version = '3.1.198'

def jarName = 'MServer.jar'
def mainClass = 'mServer.Main'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public ArteCategoryFilmsDTO deserialize(JsonElement aJsonElement, Type aType, Js
String programId = jsonElement.getAsJsonObject().get(JSON_ELEMENT_PROGRAMID).getAsString();
if (programId != null) {
if (programId.startsWith("RC-")) {
// add 1 to collection id to get list of episodes
try {
long collectionId = Long.parseLong(programId.replace("RC-", "")) + 1;
long collectionId = Long.parseLong(programId.replace("RC-", ""));
dto.addCollection(String.format("RC-%06d", collectionId));
} catch (NumberFormatException e) {
Log.errorLog(12834939, "Invalid collection id: " + programId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.lang.reflect.Type;

public class ArteCollectionDeserializer implements JsonDeserializer<ArteCategoryFilmsDTO> {
public class ArteCollectionChildDeserializer implements JsonDeserializer<ArteCategoryFilmsDTO> {
private static final String ATTRIBUTE_PROGRAM_ID = "programId";
private static final String ELEMENT_PROGRAMS = "programs";
private static final String ELEMENT_VIDEOS = "videos";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package mServer.crawler.sender.arte;


import com.google.gson.*;
import mServer.crawler.sender.base.JsonUtils;

import java.lang.reflect.Type;
import java.util.Optional;

public class ArteCollectionParentDeserializer implements JsonDeserializer<ArteCategoryFilmsDTO> {
private static final String ATTRIBUTE_KIND = "kind";
private static final String ATTRIBUTE_PROGRAM_ID = "programId";
private static final String ELEMENT_PROGRAMS = "programs";
private static final String ELEMENT_CHILDREN = "children";

public ArteCategoryFilmsDTO deserialize(final JsonElement aJsonElement, final Type aType, final JsonDeserializationContext aJsonDeserializationContext) throws JsonParseException {
final ArteCategoryFilmsDTO result = new ArteCategoryFilmsDTO();
if (aJsonElement.isJsonObject()) {
final JsonObject mainObj = aJsonElement.getAsJsonObject();

if (JsonUtils.checkTreePath(mainObj, ELEMENT_PROGRAMS)) {
final JsonArray programs = mainObj.get(ELEMENT_PROGRAMS).getAsJsonArray();
programs.forEach(program -> {
final JsonObject programObject = program.getAsJsonObject();
if (JsonUtils.checkTreePath(programObject, ELEMENT_CHILDREN)) {
programObject.get(ELEMENT_CHILDREN).getAsJsonArray().forEach(filmElement -> {
final JsonObject filmObject = filmElement.getAsJsonObject();
final Optional<String> kind = JsonUtils.getAttributeAsString(filmObject, ATTRIBUTE_KIND);
final Optional<String> programId = JsonUtils.getAttributeAsString(filmObject, ATTRIBUTE_PROGRAM_ID);

if (kind.isPresent() && kind.get().equalsIgnoreCase("TV_SERIES") && programId.isPresent()) {
result.addCollection(programId.get());
}
});
}
});
}
}
return result;
}
}
25 changes: 17 additions & 8 deletions src/main/java/mServer/crawler/sender/arte/MediathekArte.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,18 @@ private void loadSubCategory(String sender, String langCode, String aCategory, S
Gson gson = new GsonBuilder()
.registerTypeAdapter(ArteCategoryFilmsDTO.class, new ArteCategoryFilmListDeserializer())
.create();
Gson gsonCollection = new GsonBuilder()
.registerTypeAdapter(ArteCategoryFilmsDTO.class, new ArteCollectionDeserializer())
Gson gsonCollectionParent = new GsonBuilder()
.registerTypeAdapter(ArteCategoryFilmsDTO.class, new ArteCollectionParentDeserializer())
.create();
Gson gsonCollectionChild = new GsonBuilder()
.registerTypeAdapter(ArteCategoryFilmsDTO.class, new ArteCollectionChildDeserializer())
.create();

// erste Seite laden
int i = 2;
ArteCategoryFilmsDTO dto = loadSubCategoryPage(gson, sender, aUrl);
if (dto != null) {
loadCollections(sender, langCode, gsonCollection, dto);
loadCollections(sender, langCode, gsonCollectionParent, gsonCollectionChild, dto);

ArteCategoryFilmsDTO nextDto = dto;
while (PARSE_SUBCATEGORY_SUB_PAGES && nextDto != null && nextDto.hasNextPage()) {
Expand All @@ -242,7 +245,7 @@ private void loadSubCategory(String sender, String langCode, String aCategory, S
String url = String.format(URL_SUBCATEGORY, langCode.toLowerCase(), aCategory, i);
nextDto = loadSubCategoryPage(gson, sender, url);
if (nextDto != null) {
loadCollections(sender, langCode, gsonCollection, nextDto);
loadCollections(sender, langCode, gsonCollectionParent, gsonCollectionChild, nextDto);
nextDto.getProgramIds().forEach(programId -> dto.addProgramId(programId));
}

Expand All @@ -256,13 +259,19 @@ private void loadSubCategory(String sender, String langCode, String aCategory, S
}
}

private void loadCollections(String sender, String langCode, Gson gson, ArteCategoryFilmsDTO dto) {
private void loadCollections(String sender, String langCode, Gson gsonParent, Gson gsonChild, ArteCategoryFilmsDTO dto) {
dto.getCollectionIds().forEach(collectionId -> {
final String url = String.format(COLLECTION_URL, langCode, collectionId);
try {
final ArteCategoryFilmsDTO collectionDto = ArteHttpClient.executeRequest(sender, LOG, gson, url, ArteCategoryFilmsDTO.class);
if (collectionDto != null) {
collectionDto.getProgramIds().forEach(dto::addProgramId);
final ArteCategoryFilmsDTO parentDto = ArteHttpClient.executeRequest(sender, LOG, gsonParent, url, ArteCategoryFilmsDTO.class);
if (parentDto != null) {
parentDto.getCollectionIds().forEach(childCollectionId -> {
final String urlChild = String.format(COLLECTION_URL, langCode, childCollectionId);
final ArteCategoryFilmsDTO collectionDto = ArteHttpClient.executeRequest(sender, LOG, gsonChild, urlChild, ArteCategoryFilmsDTO.class);
if (collectionDto != null) {
collectionDto.getProgramIds().forEach(dto::addProgramId);
}
});
}
} catch (Exception e) {
Log.errorLog(894330855, e, url);
Expand Down

0 comments on commit 8de2094

Please sign in to comment.