Merge branch 'devel'

This commit is contained in:
Sebastien 2018-03-07 15:36:13 +01:00
commit e8c18eafc3
7 changed files with 23 additions and 206 deletions

6
.gitignore vendored
View File

@ -20,3 +20,9 @@ src/main/resources/templates/css
src/main/resources/templates/js
\.directory
\.credentials/
src/main/resources/static/error/css
src/main/resources/static/error/js

View File

@ -66,8 +66,6 @@ dependencies {
compile 'org.apache.commons:commons-lang3:3.7'
compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.apis:google-api-services-youtube:v3-rev192-1.23.0'
compile 'com.google.oauth-client:google-oauth-client-java6:1.23.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('com.jayway.jsonpath:json-path')

View File

@ -81,12 +81,6 @@ public class MainBot {
}));
}
try {
YoutubeTools.getInstance(jda.getGuilds().get(0)).getYouTubeService();
} catch (IOException e) {
e.printStackTrace();
}
Init.polish(jda);
ready = true;

View File

@ -1,41 +0,0 @@
package net.Broken.audio.Youtube;
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver;
import com.google.api.client.util.Preconditions;
import net.Broken.Tools.PrivateMessage;
import net.dv8tion.jda.core.entities.Guild;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
public class Authorization extends AuthorizationCodeInstalledApp {
private Guild guild;
private Logger logger = LogManager.getLogger();
/**
* @param flow authorization code flow
* @param receiver verification code receiver
*/
public Authorization(AuthorizationCodeFlow flow, VerificationCodeReceiver receiver, Guild guild) {
super(flow, receiver);
this.guild = guild;
}
@Override
protected void onAuthorization(AuthorizationCodeRequestUrl authorizationUrl) throws IOException {
notify(authorizationUrl.build());
}
protected void notify(String url){
Preconditions.checkNotNull(url);
logger.fatal("Please open this URL: "+url);
PrivateMessage.send(guild.getOwner().getUser(),"Please open this url to confirm google api account acces : " + url,null);
}
}

View File

@ -1,54 +0,0 @@
package net.Broken.audio.Youtube;
import com.google.api.client.extensions.java6.auth.oauth2.AbstractPromptReceiver;
import net.dv8tion.jda.core.entities.Guild;
import org.apache.logging.log4j.LogManager;
import java.io.IOException;
public class Receiver extends AbstractPromptReceiver {
private static Receiver INSTANCE;
private Guild guild;
private String code;
private Receiver(Guild guild){
this.guild = guild;
}
public static Receiver getInstance(Guild guild){
if(INSTANCE == null)
INSTANCE = new Receiver(guild);
return INSTANCE;
}
@Override
public String getRedirectUri() throws IOException {
return System.getenv("SITE_URL") + "/youtube/callback/";
}
@Override
public String waitForCode() {
if(System.getenv("SITE_URL").isEmpty()){
LogManager.getLogger().fatal("Please set \"SITE_URL\" environment variable and restart the bot!");
}
while(code == null){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return code;
}
@Override
public void stop() {
}
public void setCode(String code) {
this.code = code;
}
}

View File

@ -1,63 +1,31 @@
package net.Broken.audio.Youtube;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.YouTubeScopes;
import com.google.api.services.youtube.model.SearchListResponse;
import com.google.api.services.youtube.model.SearchResult;
import net.dv8tion.jda.core.entities.Guild;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.util.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class YoutubeTools {
/** Application name. */
private final String APPLICATION_NAME = "Discord Bot";
/** Directory to store user credentials for this application. */
private final File DATA_STORE_DIR = new File(".credentials/java-youtube-api");
private final File CLIENT_SECRET_DIR = new File(".credentials/client_secret.json");
/** Global instance of the {@link FileDataStoreFactory}. */
private FileDataStoreFactory DATA_STORE_FACTORY;
/** Global instance of the JSON factory. */
private final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/** Global instance of the HTTP transport. */
private HttpTransport HTTP_TRANSPORT;
private Logger logger = LogManager.getLogger();
private Guild guild;
/** Global instance of the scopes required by this quickstart.
*
* If modifying these scopes, delete your previously saved credentials
* at ~/.credentials/drive-java-quickstart
*/
private final Collection<String> SCOPES = Arrays.asList(YouTubeScopes.YOUTUBEPARTNER, YouTubeScopes.YOUTUBE_FORCE_SSL);
private static YoutubeTools INSTANCE ;
private YoutubeTools(Guild guild){
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
logger.catching(t);
}
this.guild = guild;
}
@ -68,49 +36,17 @@ public class YoutubeTools {
}
/**
* Creates an authorized Credential object.
* @return an authorized Credential object.
* @throws IOException
*/
private Credential authorize() throws IOException {
// Load client secrets.
InputStream in = new FileInputStream(CLIENT_SECRET_DIR);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader( in ));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("online")
.build();
Credential credential = new Authorization(flow, Receiver.getInstance(null), guild).authorize("user");
logger.debug("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
/**
* Build and return an authorized API client service, such as a YouTube
* Data API client service.
* @return an authorized API client service
* @throws IOException
*/
public YouTube getYouTubeService() throws IOException {
Credential credential = authorize();
return new YouTube.Builder(
HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
public String getRelatedVideo(String videoId, ArrayList<String> history) throws IOException, GoogleJsonResponseException, Throwable {
YouTube youtube = getYouTubeService();
// YouTube youtube = getYouTubeService();
YouTube.Builder builder = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
});
builder.setApplicationName("youtube-cmdline-search-sample");
YouTube youtube = builder.build();
HashMap<String, String> parameters = new HashMap<>();
parameters.put("part", "snippet");
@ -126,6 +62,8 @@ public class YoutubeTools {
searchListRelatedVideosRequest.setType(parameters.get("type"));
}
searchListRelatedVideosRequest.setKey(System.getenv("GOOGLE_API_KEY"));
SearchListResponse response = searchListRelatedVideosRequest.execute();
for(SearchResult item : response.getItems()){

View File

@ -1,24 +0,0 @@
package net.Broken.webView;
import net.Broken.audio.Youtube.Receiver;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class YoutubeCallBack {
@RequestMapping("/youtube/callback")
public String callback(@RequestParam(value="error", required = false, defaultValue = "") String error,
@RequestParam(value = "code", required = false, defaultValue = "") String code,
Model model){
model.addAttribute("error", error);
model.addAttribute("code", code);
if(!code.equals("")){
Receiver.getInstance(null).setCode(code);
}
return "youtubeCallBack";
}
}