-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1013 from mediathekview/feature/1005
ard: new day page url
- Loading branch information
Showing
6 changed files
with
1,251 additions
and
561 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 40 additions & 10 deletions
50
src/main/java/de/mediathekview/mserver/crawler/ard/json/ArdDayPageDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,63 @@ | ||
package de.mediathekview.mserver.crawler.ard.json; | ||
|
||
import com.google.gson.*; | ||
import de.mediathekview.mserver.base.utils.JsonUtils; | ||
import de.mediathekview.mserver.crawler.ard.ArdConstants; | ||
import de.mediathekview.mserver.crawler.ard.ArdFilmInfoDto; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.HashSet; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
public class ArdDayPageDeserializer extends ArdTeasersDeserializer | ||
implements JsonDeserializer<Set<ArdFilmInfoDto>> { | ||
public class ArdDayPageDeserializer implements JsonDeserializer<Set<ArdFilmInfoDto>> { | ||
|
||
private static final String ELEMENT_TEASERS = "teasers"; | ||
private static final String ELEMENT_CHANNELS = "channels"; | ||
private static final String ELEMENT_LINKS = "links"; | ||
private static final String ELEMENT_TARGET = "target"; | ||
private static final String ELEMENT_TIMESLOTS = "timeSlots"; | ||
private static final String ATTRIBUTE_URL_ID = "urlId"; | ||
|
||
@Override | ||
public Set<ArdFilmInfoDto> deserialize( | ||
final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) { | ||
final Set<ArdFilmInfoDto> results = new HashSet<>(); | ||
|
||
if (!jsonElement.isJsonArray()) { | ||
return results; | ||
final JsonObject jsonObject = jsonElement.getAsJsonObject(); | ||
if (jsonObject.has(ELEMENT_CHANNELS)) { | ||
final JsonArray channels = jsonObject.get(ELEMENT_CHANNELS).getAsJsonArray(); | ||
results.addAll(parseChannels(channels)); | ||
} | ||
|
||
final JsonObject firstElement = jsonElement.getAsJsonArray().get(0).getAsJsonObject(); | ||
return results; | ||
} | ||
|
||
if (firstElement.has(ELEMENT_TEASERS)) { | ||
final JsonArray teasers = firstElement.get(ELEMENT_TEASERS).getAsJsonArray(); | ||
results.addAll(parseTeasers(teasers)); | ||
private Set<ArdFilmInfoDto> parseChannels(JsonArray channels) { | ||
Set<ArdFilmInfoDto> entries = new HashSet<>(); | ||
for (JsonElement channel : channels) { | ||
final JsonArray timeSlots = channel.getAsJsonObject().get(ELEMENT_TIMESLOTS).getAsJsonArray(); | ||
for (JsonElement timeSlot : timeSlots) { | ||
for (JsonElement entry : timeSlot.getAsJsonArray()) { | ||
final JsonObject entryObject = entry.getAsJsonObject(); | ||
final Optional<String> id = toId(entryObject); | ||
id.ifPresent(s -> entries.add(createFilmInfo(s, 1))); | ||
} | ||
} | ||
} | ||
return entries; | ||
} | ||
|
||
return results; | ||
private ArdFilmInfoDto createFilmInfo(final String id, final int numberOfClips) { | ||
final String url = String.format(ArdConstants.ITEM_URL, id); | ||
return new ArdFilmInfoDto(id, url, numberOfClips); | ||
} | ||
|
||
private Optional<String> toId(final JsonObject teaserObject) { | ||
if (JsonUtils.checkTreePath(teaserObject, null, ELEMENT_LINKS, ELEMENT_TARGET)) { | ||
final JsonObject targetObject = | ||
teaserObject.get(ELEMENT_LINKS).getAsJsonObject().get(ELEMENT_TARGET).getAsJsonObject(); | ||
return JsonUtils.getAttributeAsString(targetObject, ATTRIBUTE_URL_ID); | ||
} | ||
return JsonUtils.getAttributeAsString(teaserObject, ATTRIBUTE_URL_ID); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.