Skip to content

Use random user agent #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.time.Duration;
import java.util.*;
import java.util.function.BiConsumer;
Expand All @@ -58,7 +59,26 @@
*/
public class Insynsregistret {
private final boolean processTransactionInParallel;
private static final String HTTP_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11";
private static final String[] HTTP_USER_AGENTS = {
// Chrome
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
// Firefox
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14.7; rv:135.0) Gecko/20100101 Firefox/135.0",
"Mozilla/5.0 (X11; Linux i686; rv:135.0) Gecko/20100101 Firefox/135.0",
// Edge
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/131.0.2903.86",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/131.0.2903.86",
"Mozilla/5.0 (Linux; Android 10; HD1913) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.6943.49 Mobile Safari/537.36 EdgA/131.0.2903.87",
// Opera
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 OPR/116.0.0.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_7_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 OPR/116.0.0.0",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 OPR/116.0.0.0",
// Safari
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_7_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15"
};

public Insynsregistret() {
var parallelProperty = System.getProperty("insynsregistret.parallel", "false");
Expand Down Expand Up @@ -92,6 +112,10 @@ public Stream<Transaction> search(TransactionQuery query) throws IOException {
return parseTransactionResponse(response);
}

private static String getUserAgent() {
return HTTP_USER_AGENTS[new SecureRandom().nextInt(HTTP_USER_AGENTS.length)];
}

/**
* Internal method for sending the http request.
* @param url URL to send the request
Expand All @@ -103,7 +127,7 @@ protected BufferedReader sendRequest(String url, Charset encoding) throws IOExce
var req = HttpRequest.newBuilder(URI.create(url))
.timeout(Duration.ofSeconds(15))
.header("Accept-Encoding", "gzip")
.header("User-Agent", HTTP_USER_AGENT)
.header("User-Agent", getUserAgent())
.GET()
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.apptasticsoftware.insynsregistret;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
Expand Down Expand Up @@ -36,6 +37,7 @@ void checkColumnsLangSwedish() throws IOException {
assertEquals(22, headerColumns.length);
}

@Disabled("Due to too many requests")
@Test
void checkColumnsLangEnglish() throws IOException {
LocalDate date = LocalDate.of(2018, 3, 11);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ void transactionQueryBuilder() throws IOException {
}


@Disabled("Due to too many requests")
@Test
void liveSvQueryByTransactionDate() throws IOException {
Insynsregistret ir = new Insynsregistret();
Expand Down Expand Up @@ -362,21 +363,17 @@ void liveSvQueryByPublicationDate() throws IOException {
assertNotNull(transaction.getTransactionDate());
assertEquals(19, transaction.getTransactionDate().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME).length());
}
}

@Test
void noUnknownInstrumentType() throws IOException {
Insynsregistret ir = new Insynsregistret();

TransactionQuery transactionQuery = TransactionQueryBuilder.publicationsLastDays(15)
.build();
verifyUnknownInstrumentType(transactions);
}

List<Transaction> transactions = ir.search(transactionQuery)
private static void verifyUnknownInstrumentType(List<Transaction> transactions) {
var unknownInstrumentTypes = transactions.stream()
.filter(t -> t.getInstrumentType() != null && !t.getInstrumentType().isBlank())
.filter(t -> t.getInstrumentTypeDescription() == Transaction.InstrumentType.UNKNOWN)
.collect(Collectors.toList());

assertEquals(0, transactions.size());
assertEquals(0, unknownInstrumentTypes.size());
}

@Test
Expand All @@ -402,9 +399,12 @@ void liveEnQueryByTransactionDate() throws IOException {
assertNotNull(transaction.getTransactionDate());
assertEquals(19, transaction.getTransactionDate().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME).length());
}

verifyUnknownInstrumentType(transactions);
}


@Disabled("Due to too many requests")
@Test
void liveEnQueryByPublicationDate() throws IOException {
Insynsregistret ir = new Insynsregistret();
Expand Down Expand Up @@ -460,6 +460,7 @@ void transactionsFromAfterToDate() {
}


@Disabled("Due to too many requests")
@Test
void urlEncodingWithAmpersand() throws IOException {
Insynsregistret ir = new Insynsregistret();
Expand Down