Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
- Added a fix for Reddit 403'ing the bot, by including a user-agent h…
Browse files Browse the repository at this point in the history
…eader impersonating a Firefox client
  • Loading branch information
Eukon05 committed Mar 3, 2024
1 parent c8aac40 commit 4a45407
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/eukon05/dilanbot/reddit/RedditService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class RedditService {
private final Gson gson;
private final HttpClient client = HttpClient.newHttpClient();
private static final String REDDIT_API_URL = "https://www.reddit.com/r/%s/randomrising.json?limit=1";
private static final String MOZILLA_USER_AGENT = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0";
private static final String USER_AGENT_HEADER = "User-Agent";

public RedditService(Gson gson) {
this.gson = gson;
Expand All @@ -26,6 +28,7 @@ public RedditService(Gson gson) {
RedditSubmission getRandomSubmission(String subredditName) throws IOException, InterruptedException {
HttpResponse<String> response = client.send(HttpRequest
.newBuilder()
.setHeader(USER_AGENT_HEADER, MOZILLA_USER_AGENT)
.uri(URI.create(String.format(REDDIT_API_URL, URLEncoder.encode(subredditName, StandardCharsets.UTF_8))))
.build(), HttpResponse.BodyHandlers.ofString());

Expand Down

0 comments on commit 4a45407

Please sign in to comment.