Skip to content

Commit

Permalink
Add HTTP proxy for Youtube Client
Browse files Browse the repository at this point in the history
  • Loading branch information
mithandir committed Aug 31, 2024
1 parent fb36d92 commit 1506e47
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package ch.climbd.newsfeed.controller.scheduler;

import io.github.thoroldvix.api.TranscriptRetrievalException;
import io.github.thoroldvix.api.YoutubeClient;
import io.github.thoroldvix.api.YtApiV3Endpoint;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Iterator;
import java.util.Map;

/**
* Added HTTP proxy configuration for Youtube
*/
final class DefaultYoutubeClientCopy implements YoutubeClient {
private final HttpClient httpClient;

DefaultYoutubeClientCopy() {
this.httpClient = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("192.168.2.5", 3128)))
.build();
}

DefaultYoutubeClientCopy(HttpClient httpClient) {
this.httpClient = httpClient;
}

public String get(String url, Map<String, String> headers) throws TranscriptRetrievalException {
String videoId = url.split("=")[1];
String errorMessage = "Request to YouTube failed.";
String[] headersArray = this.createHeaders(headers);
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).headers(headersArray).build();

HttpResponse response;
try {
response = this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException var9) {
IOException e = var9;
throw new TranscriptRetrievalException(videoId, errorMessage, e);
} catch (InterruptedException var10) {
InterruptedException e = var10;
Thread.currentThread().interrupt();
throw new TranscriptRetrievalException(videoId, errorMessage, e);
}

if (response.statusCode() != 200) {
throw new TranscriptRetrievalException(videoId, errorMessage + " Status code: " + response.statusCode());
} else {
return (String) response.body();
}
}

public String get(YtApiV3Endpoint endpoint, Map<String, String> params) throws TranscriptRetrievalException {
String paramsString = this.createParamsString(params);
String errorMessage = String.format("Request to YouTube '%s' endpoint failed.", endpoint);
HttpRequest.Builder var10000 = HttpRequest.newBuilder();
String var10001 = endpoint.url();
HttpRequest request = var10000.uri(URI.create(var10001 + "?" + paramsString)).build();

HttpResponse response;
try {
response = this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException var8) {
IOException e = var8;
throw new TranscriptRetrievalException(errorMessage, e);
} catch (InterruptedException var9) {
InterruptedException e = var9;
Thread.currentThread().interrupt();
throw new TranscriptRetrievalException(errorMessage, e);
}

if (response.statusCode() != 200) {
throw new TranscriptRetrievalException(errorMessage + " Status code: " + response.statusCode());
} else {
return (String) response.body();
}
}

private String createParamsString(Map<String, String> params) {
StringBuilder paramString = new StringBuilder();
Iterator var3 = params.entrySet().iterator();

while (var3.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry) var3.next();
String value = this.formatValue(entry.getValue());
paramString.append(entry.getKey()).append("=").append(value).append("&");
}

paramString.deleteCharAt(paramString.length() - 1);
return paramString.toString();
}

private String formatValue(String value) {
return value.replaceAll(" ", "%20");
}

private String[] createHeaders(Map<String, String> headers) {
String[] headersArray = new String[headers.size() * 2];
int i = 0;

Map.Entry entry;
for (Iterator var4 = headers.entrySet().iterator(); var4.hasNext(); headersArray[i++] = (String) entry.getValue()) {
entry = (Map.Entry) var4.next();
headersArray[i++] = (String) entry.getKey();
}

return headersArray;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class RssProcessor {
private static final Logger LOG = LoggerFactory.getLogger(RssProcessor.class);
private final ZoneId zoneId = ZoneId.of("Europe/Berlin");
private final YoutubeTranscriptApi youtubeTranscriptApi = TranscriptApiFactory.createDefault();
private final YoutubeTranscriptApi youtubeTranscriptApi = TranscriptApiFactory.createWithClient(new DefaultYoutubeClientCopy());

@Autowired
private MongoController mongo;
Expand Down Expand Up @@ -82,13 +82,14 @@ public void processRss(String url, String language) {
}

private void processYoutubeTranscription(NewsEntry item) {

if (item.getLink().startsWith("https://www.youtube.com/watch?v=")) {
var videoId = item.getLink().substring(32);
try {
TranscriptList transcriptList = youtubeTranscriptApi.listTranscripts(videoId);
var fragments = transcriptList.findTranscript("en").fetch();
var content = TranscriptFormatters.textFormatter().format(fragments);
LOG.info("Transcript found for video: {}", content);
LOG.info("Transcript found for video: {}", item.getTitle());
item.setContent(content);
} catch (TranscriptRetrievalException e) {
LOG.warn("No transcript found for video: {}", videoId);
Expand Down

0 comments on commit 1506e47

Please sign in to comment.