From eb8c9d95bfbd7e664c9de2767850ec4c465b520b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 9 Feb 2018 12:57:27 +0300 Subject: vk top --- .../src/main/java/com/juick/server/TopManager.java | 11 ++++- .../main/java/com/juick/server/VKWallManager.java | 57 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 juick-server/src/main/java/com/juick/server/VKWallManager.java (limited to 'juick-server/src/main/java/com') diff --git a/juick-server/src/main/java/com/juick/server/TopManager.java b/juick-server/src/main/java/com/juick/server/TopManager.java index 894dfda7..66e1d6d2 100644 --- a/juick-server/src/main/java/com/juick/server/TopManager.java +++ b/juick-server/src/main/java/com/juick/server/TopManager.java @@ -22,6 +22,8 @@ import com.juick.User; import com.juick.service.MessagesService; import com.juick.service.UserService; import com.juick.util.MessageUtils; +import com.vk.api.sdk.exceptions.ApiException; +import com.vk.api.sdk.exceptions.ClientException; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,6 +44,8 @@ public class TopManager { @Inject private FacebookPageManager facebookPageManager; @Inject + private VKWallManager vkWallManager; + @Inject private CrosspostManager crosspostManager; @Inject private UserService userService; @@ -63,15 +67,20 @@ public class TopManager { messagesService.setMessagePopular(m, 1); Message jmsg = messagesService.getMessage(m); String status = MessageUtils.getMessageHashTags(jmsg) + StringUtils.defaultString(jmsg.getText()); + String link = "https://juick.com/" + jmsg.getMid(); try { - facebookPageManager.post(status, "https://juick.com/" + jmsg.getMid()); + facebookPageManager.post(status, link); + vkWallManager.post(status, link); } catch (HttpClientErrorException ex) { HttpStatus statusCode = ex.getStatusCode(); String responseString = ex.getResponseBodyAsString(); logger.warn("facebook error {}: {}", statusCode.value(), responseString); + } catch (ClientException | ApiException e) { + logger.warn("vk error: {}", e); } jmsg.setUser(serviceUser); crosspostManager.twitterPost(jmsg); + }); } } diff --git a/juick-server/src/main/java/com/juick/server/VKWallManager.java b/juick-server/src/main/java/com/juick/server/VKWallManager.java new file mode 100644 index 00000000..364087eb --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/VKWallManager.java @@ -0,0 +1,57 @@ +package com.juick.server; + +import com.juick.User; +import com.juick.service.CrosspostService; +import com.juick.service.UserService; +import com.vk.api.sdk.client.TransportClient; +import com.vk.api.sdk.client.VkApiClient; +import com.vk.api.sdk.client.actors.UserActor; +import com.vk.api.sdk.exceptions.ApiException; +import com.vk.api.sdk.exceptions.ClientException; +import com.vk.api.sdk.httpclient.HttpTransportClient; +import com.vk.api.sdk.objects.apps.responses.GetResponse; +import com.vk.api.sdk.objects.wall.responses.PostResponse; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; + +@Component +public class VKWallManager { + @Value("${vk_appid}") + private String VK_APPID; + @Value("${vk_secret}") + private String VK_SECRET; + @Inject + private UserService userService; + @Inject + private CrosspostService crosspostService; + @Value("${vk_user:juick}") + private String serviceUsername; + @Value("${vk_group:-1}") + private String vkGroup; + + private User serviceUser; + private VkApiClient vk; + private UserActor actor; + + + @PostConstruct + public void init() throws ClientException, ApiException { + serviceUser = userService.getUserByName(serviceUsername); + TransportClient transportClient = HttpTransportClient.getInstance(); + vk = new VkApiClient(transportClient); + Pair tokens = crosspostService.getVkTokens(serviceUser.getUid()).get(); + actor = new UserActor(Integer.valueOf(tokens.getLeft()), tokens.getRight()); + } + public void post(String status, String link) throws ClientException, ApiException { + vk.wall().post(actor) + .ownerId(Integer.valueOf(vkGroup)) + .fromGroup(true) + .message(status) + .attachments(link) + .execute(); + } +} -- cgit v1.2.3