private void changeMetadata(final String content, final Upload upload) { final Map<String, Object> params = new HashMap<>(METADATA_PARAMS_SIZE); params.putAll(getMetadataSocial(upload)); params.putAll(getMetadataMonetization(content, upload)); params.putAll(getMetadataMetadata(upload)); params.putAll(getMetadataPermissions(upload)); System.out.println(Joiner.on(MODIFIED_SEPERATOR).skipNulls().join(params.keySet())); params.put("modified_fields", Joiner.on(MODIFIED_SEPERATOR).skipNulls().join(params.keySet())); params.put("creator_share_feeds", "yes"); final String token = extractor(content, "var session_token = \"", "\""); params.put("session_token", token); params.put("action_edit_video", "1"); try { final HttpResponse<String> response = Unirest.post( String.format( "https://www.youtube.com/metadata_ajax?video_id=%s", upload.getVideoid())) .fields(params) .asString(); LOGGER.info(response.getBody()); } catch (final Exception e) { LOGGER.warn("Metadata not set", e); } }
@Override public void activateBrowserfeatures(final Upload upload) throws UnirestException { // Create a local instance of cookie store // Populate cookies if needed final CookieStore cookieStore = new BasicCookieStore(); for (final PersistentCookieStore.SerializableCookie serializableCookie : upload.getAccount().getSerializeableCookies()) { final BasicClientCookie cookie = new BasicClientCookie( serializableCookie.getCookie().getName(), serializableCookie.getCookie().getValue()); cookie.setDomain(serializableCookie.getCookie().getDomain()); cookieStore.addCookie(cookie); } final HttpClient client = HttpClientBuilder.create().useSystemProperties().setDefaultCookieStore(cookieStore).build(); Unirest.setHttpClient(client); final HttpResponse<String> response = Unirest.get(String.format(VIDEO_EDIT_URL, upload.getVideoid())).asString(); changeMetadata(response.getBody(), upload); final RequestConfig clientConfig = RequestConfig.custom().setConnectTimeout(600000).setSocketTimeout(600000).build(); Unirest.setHttpClient(HttpClientBuilder.create().setDefaultRequestConfig(clientConfig).build()); }
private Map<String, Object> getMetadataMonetization(final String content, final Upload upload) { final Map<String, Object> params = Maps.newHashMapWithExpectedSize(MONETIZE_PARAMS_SIZE); final Metadata metadata = upload.getMetadata(); final Monetization monetization = upload.getMonetization(); if (monetization.isClaim() && License.YOUTUBE == upload.getMetadata().getLicense()) { params.put("video_monetization_style", "ads"); if (!monetization.isPartner() || ClaimOption.MONETIZE == monetization.getClaimoption()) { params.put("claim_style", "ads"); params.put("enable_overlay_ads", boolConverter(monetization.isOverlay())); params.put("trueview_instream", boolConverter(monetization.isTrueview())); params.put("instream", boolConverter(monetization.isInstream())); params.put("long_ads_checkbox", boolConverter(monetization.isInstreamDefaults())); params.put("paid_product", boolConverter(monetization.isProduct())); params.put( "allow_syndication", boolConverter(Syndication.GLOBAL == monetization.getSyndication())); } if (monetization.isPartner()) { params.put( "claim_type", ClaimType.AUDIO_VISUAL == monetization.getClaimtype() ? "B" : ClaimType.VISUAL == monetization.getClaimtype() ? "V" : "A"); final String toFind = ClaimOption.MONETIZE == monetization.getClaimoption() ? "Monetize in all countries" : ClaimOption.TRACK == monetization.getClaimoption() ? "Track in all countries" : "Block in all countries"; final Pattern pattern = Pattern.compile( "<option\\s*value=\"([^\"]+?)\"\\s*(selected(=\"\")?)?\\sdata-is-monetized-policy=\"(true|false)\"\\s*>\\s*([^<]+?)\\s*</option>"); final Matcher matcher = pattern.matcher(content); String usagePolicy = null; int position = 0; while (matcher.find(position)) { position = matcher.end(); if (matcher.group(5).trim().equals(toFind)) { usagePolicy = matcher.group(1); } } params.put("usage_policy", usagePolicy); final String assetName = monetization.getAsset().name().toLowerCase(Locale.getDefault()); params.put("asset_type", assetName); params.put( assetName + "_custom_id", monetization.getCustomId().isEmpty() ? upload.getVideoid() : monetization.getCustomId()); params.put(assetName + "_notes", monetization.getNotes()); params.put(assetName + "_tms_id", monetization.getTmsid()); params.put(assetName + "_isan", monetization.getIsan()); params.put(assetName + "_eidr", monetization.getEidr()); if (Asset.TV != monetization.getAsset()) { // WEB + MOVIE ONLY params.put( assetName + "_title", !monetization.getTitle().isEmpty() ? monetization.getTitle() : metadata.getTitle()); params.put(assetName + "_description", monetization.getDescription()); } else { // TV ONLY params.put("show_title", monetization.getTitle()); params.put("episode_title", monetization.getTitleepisode()); params.put("season_nb", monetization.getSeasonNb()); params.put("episode_nb", monetization.getEpisodeNb()); } } } return params; }