Skip to content

Commit

Permalink
feat: translate to java
Browse files Browse the repository at this point in the history
  • Loading branch information
dsdanielpark committed May 18, 2023
1 parent 5ab388e commit aa548f4
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions translate_to/bardapi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
private String token;
private int timeout;
private Map<String, String> proxies;
private int reqId;
private String conversationId;
private String responseId;
private String choiceId;
private OkHttpClient client;
private String snim0e;

public Bard(String token, int timeout, Map<String, String> proxies) {
this.token = token;
this.timeout = timeout;
this.proxies = proxies;
this.reqId = new Random().nextInt(10000);
this.conversationId = "";
this.responseId = "";
this.choiceId = "";
this.client = new OkHttpClient.Builder().cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
}

@Override
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
if (token != null) {
return Collections.singletonList(new Cookie.Builder().name("__Secure-1PSID").value(token).build());
}
return Collections.emptyList();
}
}).build();
this.snim0e = getSNlM0e();
}

private String getSNlM0e() {
if (this.token == null || !this.token.endsWith(".")) {
throw new IllegalArgumentException("__Secure-1PSID value must end with a single dot. Enter correct __Secure-1PSID value.");
}

Request request = new Request.Builder()
.url("https://bard.google.com/")
.headers(Headers.of(HEADERS))
.build();

try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Response code not 200. Response Status is " + response.code());
}

String responseBody = response.body().string();
Matcher matcher = Pattern.compile("SNlM0e\":\"(.*?)\"").matcher(responseBody);
if (!matcher.find()) {
throw new RuntimeException("SNlM0e value not found in response. Check __Secure-1PSID value.");
}

return matcher.group(1);
} catch (IOException e) {
throw new RuntimeException("Error occurred while fetching SNlM0e value: " + e.getMessage());
}
}

public JSONObject getAnswer(String inputText) {
try {
String inputTextStruct = "[[\"" + inputText + "\"],null,[\"" + conversationId + "\",\"" + responseId + "\",\"" + choiceId + "\"]]";

JSONObject data = new JSONObject()
.put("f.req", new JSONArray().put(null).put(inputTextStruct))
.put("at", snim0e);

RequestBody requestBody = RequestBody.create(data.toString(), MEDIA_TYPE_JSON);

Request request = new Request.Builder()
.url(BARD_URL)
.headers(Headers.of(HEADERS))
.post(requestBody)
.build();

try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Response code not 200. Response Status is " + response.code());
}

String responseBody = response.body().string();
JSONArray parsedAnswer = new JSONArray(new JSONArray(responseBody).getJSONArray(0).getString(2));

if (parsedAnswer.isEmpty()) {
return new JSONObject().put("content", "Response Error: " + responseBody);
}

JSONObject bardAnswer = new JSONObject();
bardAnswer.put("content", parsedAnswer.getJSONArray(0).getString(0));
bardAnswer.put("conversation_id", parsedAnswer.getJSONArray(1).getString(0));
bardAnswer.put("response_id", parsedAnswer.getJSONArray(1).getString(1));
bardAnswer.put("factualityQueries", parsedAnswer.getJSONArray(3));
bardAnswer.put("textQuery", parsedAnswer.getJSONArray(2).optString(0, ""));

JSONArray choicesArray = new JSONArray();
JSONArray parsedChoices = parsedAnswer.getJSONArray(4);
for (int i = 0; i < parsedChoices.length(); i++) {
JSONArray choice = parsedChoices.getJSONArray(i);
JSONObject choiceObject = new JSONObject();
choiceObject.put("id", choice.getString(0));
choiceObject.put("content", choice.getString(1));
choicesArray.put(choiceObject);
}
bardAnswer.put("choices", choicesArray);

this.conversationId = bardAnswer.getString("conversation_id");
this.responseId = bardAnswer.getString("response_id");
this.choiceId = bardAnswer.getJSONArray("choices").getJSONObject(0).getString("id");
this.reqId += 100000;

return bardAnswer;
}
} catch (IOException e) {
throw new RuntimeException("Error occurred while fetching Bard answer: " + e.getMessage());
}
}

0 comments on commit aa548f4

Please sign in to comment.