Init project
This commit is contained in:
24
.gitignore
vendored
Normal file
24
.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
## IDEA ##
|
||||||
|
.idea/
|
||||||
|
out/
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
*.iws
|
||||||
|
*.ids
|
||||||
|
|
||||||
|
## ECLIPSE ##
|
||||||
|
.settings/
|
||||||
|
bin/
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
|
||||||
|
## GRADLE ##
|
||||||
|
.gradle/
|
||||||
|
build/
|
||||||
|
|
||||||
|
## MAVEN ##
|
||||||
|
target/
|
||||||
|
|
||||||
|
## OTHER ##
|
||||||
|
lib/
|
||||||
|
|
||||||
58
pom.xml
Normal file
58
pom.xml
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
||||||
|
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>VK Api Uni</name>
|
||||||
|
|
||||||
|
<groupId>ru.dmitriymx</groupId>
|
||||||
|
<artifactId>vkapiuni</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<slf4j.version>1.7.21</slf4j.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- LOGGER -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- COMPONENTS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.8.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.5.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${java.version}</source>
|
||||||
|
<target>${java.version}</target>
|
||||||
|
<encoding>${project.build.sourceEncoding}</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.15</version>
|
||||||
|
<configuration>
|
||||||
|
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
12
src/main/java/ru/dmitriymx/vkapi/Browser.java
Normal file
12
src/main/java/ru/dmitriymx/vkapi/Browser.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-04-10
|
||||||
|
*/
|
||||||
|
package ru.dmitriymx.vkapi;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface Browser {
|
||||||
|
Response get(String url) throws IOException;
|
||||||
|
Response post(String url, String data) throws IOException;
|
||||||
|
}
|
||||||
11
src/main/java/ru/dmitriymx/vkapi/Response.java
Normal file
11
src/main/java/ru/dmitriymx/vkapi/Response.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-04-10
|
||||||
|
*/
|
||||||
|
package ru.dmitriymx.vkapi;
|
||||||
|
|
||||||
|
public interface Response {
|
||||||
|
int getStatus();
|
||||||
|
String getContentType();
|
||||||
|
String getContent();
|
||||||
|
}
|
||||||
149
src/main/java/ru/dmitriymx/vkapi/VkApi.java
Normal file
149
src/main/java/ru/dmitriymx/vkapi/VkApi.java
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-04-10
|
||||||
|
*/
|
||||||
|
package ru.dmitriymx.vkapi;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class VkApi {
|
||||||
|
private static final String VKAPI_URL = "https://api.vk.com/method/";
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(VkApi.class);
|
||||||
|
private final String accessToken;
|
||||||
|
private final Browser browser;
|
||||||
|
private final Gson gson;
|
||||||
|
private String apiVersion = "5.62";
|
||||||
|
private long callPause = 1200L;
|
||||||
|
private long lastTime;
|
||||||
|
private int call = 0;
|
||||||
|
|
||||||
|
public VkApi(String accessToken, Browser browser) {
|
||||||
|
if (accessToken == null || accessToken.trim().isEmpty()) {
|
||||||
|
throw new RuntimeException("Access token don't be NULL or EMPTY!");
|
||||||
|
}
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
this.browser = browser;
|
||||||
|
this.gson = new Gson();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getApiVersion() {
|
||||||
|
return apiVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApiVersion(String apiVersion) {
|
||||||
|
this.apiVersion = apiVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCallPause() {
|
||||||
|
return callPause;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCallPause(long callPause) {
|
||||||
|
this.callPause = callPause;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObject execApi(String methodApi, Map<String, String> params) throws VkApiException {
|
||||||
|
checkCalls();
|
||||||
|
String url = VKAPI_URL + methodApi;
|
||||||
|
String postData = paramsToString(params);
|
||||||
|
|
||||||
|
Response response;
|
||||||
|
try {
|
||||||
|
response = browser.post(url, postData);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
chechResponse(response);
|
||||||
|
|
||||||
|
JsonObject jsonObject = gson.fromJson(response.getContent(), JsonObject.class);
|
||||||
|
if (jsonObject.has("error")) {
|
||||||
|
throw new VkApiException(jsonObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObject longExecApi(String server, String key, long ts, long wait) throws VkApiException {
|
||||||
|
String url = String.format("https://%s?act=a_check&key=%s&ts=%d&wait=%d&mode=2&version=1",
|
||||||
|
server, key, ts, wait
|
||||||
|
);
|
||||||
|
|
||||||
|
Response response;
|
||||||
|
try {
|
||||||
|
response = browser.get(url);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
chechResponse(response);
|
||||||
|
|
||||||
|
JsonObject jsonObject = gson.fromJson(response.getContent(), JsonObject.class);
|
||||||
|
if (jsonObject.has("error")) {
|
||||||
|
throw new VkApiException(jsonObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String paramsToString(Map<String, String> params) {
|
||||||
|
String collect = "";
|
||||||
|
if (params != null && params.size() > 0) {
|
||||||
|
collect = "&" + params.entrySet().stream()
|
||||||
|
.map(entry -> entry.getKey() + "=" + entry.getValue())
|
||||||
|
.collect(Collectors.joining("&"));
|
||||||
|
}
|
||||||
|
return "access_token=" + accessToken + "&v=" + apiVersion + collect;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkCalls() {
|
||||||
|
if (call >= 3) {
|
||||||
|
long currTime = System.currentTimeMillis();
|
||||||
|
long diff = currTime - lastTime;
|
||||||
|
|
||||||
|
if (diff <= callPause) {
|
||||||
|
safeSleep(callPause);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastTime = System.currentTimeMillis();
|
||||||
|
call = 0;
|
||||||
|
} else {
|
||||||
|
safeSleep(callPause/3);
|
||||||
|
call++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void chechResponse(Response response) {
|
||||||
|
if (response.getStatus() != 200) {
|
||||||
|
throw new IllegalStateException(String.format("code != 200 (%d)", response.getStatus()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.getContentType().equalsIgnoreCase("application/json") &&
|
||||||
|
!response.getContentType().equalsIgnoreCase("text/javascript")) {
|
||||||
|
throw new IllegalStateException(String.format("content type is not JSON/JavaScript (%s)", response.getContentType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.getContent().isEmpty()) {
|
||||||
|
throw new IllegalStateException("content is empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug(response.getContent());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void safeSleep(long ms) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(ms);
|
||||||
|
} catch (InterruptedException ignore) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/main/java/ru/dmitriymx/vkapi/VkApiException.java
Normal file
17
src/main/java/ru/dmitriymx/vkapi/VkApiException.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-04-10
|
||||||
|
*/
|
||||||
|
package ru.dmitriymx.vkapi;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
public class VkApiException extends Exception {
|
||||||
|
public VkApiException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public VkApiException(JsonObject jsonObject) {
|
||||||
|
this(jsonObject.getAsJsonObject("error").get("error_msg").getAsString());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user