-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98b5c7b
commit 760053b
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/main/java/aandrosov/freegamesradar/app/social/media/SocialMediaClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/aandrosov/freegamesradar/app/social/media/SocialMediaType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package aandrosov.freegamesradar.app.social.media; | ||
|
||
public enum SocialMediaType { | ||
VK | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/aandrosov/freegamesradar/app/social/media/VkGroupSocialMediaClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/aandrosov/freegamesradar/vk/VkImplicitFlowAuthorizationData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |