private String titleFrom(WsProgramme programme, Iterable<WsAudioItem> audioItems) { if (!Strings.isNullOrEmpty(programme.getEpisodeTitle())) { return programme.getEpisodeTitle(); } for (WsAudioItem audioItem : audioItems) { if (!Strings.isNullOrEmpty(audioItem.getTitle())) { return audioItem.getTitle(); } } return null; }
private Policy policyFor(WsAudioItem audioItem) { Policy policy = new Policy(); for (String date : ImmutableList.of( audioItem.getAllowDownloadFrom(), audioItem.getInputDatetime(), audioItem.getLastAmendTimestamp())) { DateTime parsed = parse(date); if (parsed != null) { policy.setAvailabilityStart(parsed); break; } } policy.setAvailableCountries(ImmutableSet.of(Countries.ALL)); policy.setRevenueContract(PRIVATE); return policy; }
@Override public void handle(WsProgramme programme, Iterable<WsAudioItem> audioItems) { checkNotNull(programme.getProgId()); checkNotNull(programme.getSeriesId()); String episodeUri = uriFor(programme); Maybe<Identified> possibleEpisode = resolver.findByCanonicalUris(ImmutableSet.of(episodeUri)).get(episodeUri); Episode episode = null; if (possibleEpisode.hasValue()) { Identified resolved = possibleEpisode.requireValue(); if (resolved instanceof Episode) { episode = (Episode) resolved; } else { log.record( errorEntry() .withDescription( "Resolved %s for episode %s", resolved.getClass().getSimpleName(), episodeUri)); return; } } else { episode = new Episode(episodeUri, curieFor(programme), WORLD_SERVICE); } episode.setParentRef(new ParentRef(uriForBrand(programme.getSeriesId()))); episode.setTitle(titleFrom(programme, audioItems)); episode.setDescription(programme.getSynopsis()); if (!Strings.isNullOrEmpty(programme.getEpisodeNo()) && programme.getEpisodeNo().matches("\\d+")) { episode.setEpisodeNumber(Integer.parseInt(programme.getEpisodeNo())); } episode.setGenres(WsGenre.genresForCode(programme.getGenreCode())); episode.setMediaType(AUDIO); episode.setSpecialization(RADIO); if (!Iterables.isEmpty(audioItems)) { for (WsAudioItem audioItem : audioItems) { Version version = new Version(); if (!Strings.isNullOrEmpty(audioItem.getDuration()) && audioItem.getDuration().matches("\\d+")) { version.setDuration(new Duration(Long.parseLong(audioItem.getDuration()))); } Policy policy = policyFor(audioItem); String broadcastUri = audioItem.getLinkAudioBroadcastQuality(); if (!Strings.isNullOrEmpty(broadcastUri)) { version.addManifestedAs(encodingFrom(policy, broadcastUri, MimeType.AUDIO_WAV)); } String thumbnailUri = audioItem.getLinkAudioThumbnail(); if (!Strings.isNullOrEmpty(thumbnailUri)) { version.addManifestedAs(encodingFrom(policy, thumbnailUri, MimeType.AUDIO_MP3)); } if (!version.getManifestedAs().isEmpty()) { episode.addVersion(version); } } } Broadcast broadcast = broadcastFrom(programme); if (broadcast != null) { Version version = Iterables.getFirst(episode.getVersions(), new Version()); if (version.getDuration() == null) { version.setDuration(Duration.standardSeconds(broadcast.getBroadcastDuration())); } version.addBroadcast(broadcast); } writer.createOrUpdate(episode); }