Skip to content

Commit

Permalink
Add
Browse files Browse the repository at this point in the history
  • Loading branch information
aandrosov0 committed Dec 1, 2023
1 parent 98b5c7b commit 760053b
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package aandrosov.freegamesradar.app.social.media;

import aandrosov.freegamesradar.Game;

public interface SocialMediaClient {

void uploadGame(Game game) throws GameUploadException;

SocialMediaType getType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package aandrosov.freegamesradar.app.social.media;

public enum SocialMediaType {
VK
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package aandrosov.freegamesradar.app.social.media;

import aandrosov.freegamesradar.Game;
import aandrosov.freegamesradar.app.ApplicationUtils;
import aandrosov.freegamesradar.epic.games.store.EpicGamesGame;
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.photos.responses.GetWallUploadServerResponse;
import com.vk.api.sdk.objects.photos.responses.SaveWallPhotoResponse;
import com.vk.api.sdk.objects.photos.responses.WallUploadResponse;

import java.io.File;
import java.io.IOException;

public class VkGroupSocialMediaClient implements SocialMediaClient {

private final UserActor actor;
private final long groupId;

private final VkApiClient api;

public VkGroupSocialMediaClient(UserActor actor, long groupId) {
this.actor = actor;
this.groupId = groupId;
this.api = new VkApiClient(new HttpTransportClient());
}

@Override
public void uploadGame(Game game) throws GameUploadException {
try {
String message = game.title() + "\n" + game.status() + "\n" + game.url();

if (game instanceof EpicGamesGame) {
message = "\uD83D\uDD25Epic Games\n" + ((EpicGamesGame) game).freePeriod() + "\n" + message;
}

File gameCoverFile = ApplicationUtils.saveTempFileFromUrl(game.coverUrl(), null, ".jpg").toFile();
SaveWallPhotoResponse photo = uploadWallPhoto(gameCoverFile);
String attachment = "photo" + photo.getOwnerId() + "_" + photo.getId();
api.wall().post(actor).fromGroup(true).ownerId(-groupId).message(message).attachments(attachment).execute();
} catch (ClientException | IOException | ApiException exception) {
throw new GameUploadException("Cannot to upload a game \"" + game.title() + "\": " + exception.getMessage());
}
}

public SaveWallPhotoResponse uploadWallPhoto(File photo) throws ClientException, ApiException {
GetWallUploadServerResponse uploadServer = api.photos().getWallUploadServer(actor).groupId(groupId).execute();

String uploadUrl = uploadServer.getUploadUrl().toASCIIString();
WallUploadResponse wallUpload = api.upload().photoWall(uploadUrl, photo).execute();

return api.photos().saveWallPhoto(actor, wallUpload.getPhoto())
.server(wallUpload.getServer())
.hash(wallUpload.getHash())
.groupId(groupId)
.execute()
.get(0);
}

@Override
public SocialMediaType getType() {
return SocialMediaType.VK;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package aandrosov.freegamesradar.vk;

import com.vk.api.sdk.client.actors.Actor;

import java.io.*;

public class VkImplicitFlowAuthorizationData implements Actor<Long>, Serializable {

private final String accessToken;

private final Long expiresIn;
private final Long userId;

public VkImplicitFlowAuthorizationData(String accessToken, Long expiresIn, Long userId) {
this.accessToken = accessToken;
this.expiresIn = expiresIn;
this.userId = userId;
}

protected VkImplicitFlowAuthorizationData() {
this(null, null, null);
}

@Override
public String getAccessToken() {
return accessToken;
}

@Override
public Long getId() {
return userId;
}

public Long getExpiresIn() {
return expiresIn;
}

@Override
public String toString() {
return "VkImplicitFlowUserActor{" +
"accessToken='" + accessToken + '\'' +
", expiresIn=" + expiresIn +
", userId=" + userId +
'}';
}
}
12 changes: 12 additions & 0 deletions src/main/resources/help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
To run application you must set:
1. social media (only vk)
2. other parameters for your social media

Commands:
--help -> this message
--social_media -> social media to post (only vk)
--app_id -> your app id. Required for VK
--group_id -> your group id. Required for VK

Example:
./FreeGamesRadar --social_media vk --app_id 213123 --group_id 12352315312

0 comments on commit 760053b

Please sign in to comment.