tobby48 5 gadus atpakaļ
vecāks
revīzija
37b9ea0bc4

+ 19
- 0
pom.xml Parādīt failu

@@ -45,6 +45,12 @@
45 45
 			<id>projectlombok.org</id>
46 46
 			<url>http://projectlombok.org/mavenrepo</url>
47 47
 		</repository>
48
+		
49
+		<repository>
50
+		    <id>jcenter</id>
51
+		    <name>jcenter-bintray</name>
52
+		    <url>https://jcenter.bintray.com</url>
53
+		</repository>
48 54
 	</repositories>
49 55
 
50 56
 
@@ -225,12 +231,25 @@
225 231
 		    <artifactId>sbt-pgp</artifactId>
226 232
 		    <version>1.1.0</version>
227 233
 		</dependency> -->
234
+		
235
+		<dependency>
236
+		    <groupId>net.dv8tion</groupId>
237
+		    <artifactId>JDA</artifactId>
238
+		    <version>LATEST</version>
239
+		</dependency>
228 240
 	    
229 241
 		<dependency>
230 242
 			<groupId>com.sparkjava</groupId>
231 243
 			<artifactId>spark-core</artifactId>
232 244
 			<version>2.9.0</version>
233 245
 		</dependency>
246
+		<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
247
+		<dependency>
248
+			<groupId>org.freemarker</groupId>
249
+			<artifactId>freemarker</artifactId>
250
+			<version>2.3.29</version>
251
+		</dependency>
252
+				
234 253
 		
235 254
 		<!-- https://mvnrepository.com/artifact/org.twitter4j/twitter4j-core -->
236 255
 		<dependency>

+ 29
- 0
src/main/java/kr/co/swh/lecture/opensource/discord/TExample.java Parādīt failu

@@ -0,0 +1,29 @@
1
+package kr.co.swh.lecture.opensource.discord;
2
+
3
+import javax.security.auth.login.LoginException;
4
+
5
+import net.dv8tion.jda.api.AccountType;
6
+import net.dv8tion.jda.api.JDA;
7
+import net.dv8tion.jda.api.JDABuilder;
8
+import net.dv8tion.jda.api.OnlineStatus;
9
+
10
+public class TExample {
11
+
12
+	public static JDA jda;
13
+	public static void main(String[] args) {
14
+		// TODO Auto-generated method stub
15
+		JDABuilder jb = new JDABuilder(AccountType.BOT);
16
+		jb.setAutoReconnect(true);
17
+		jb.setStatus(OnlineStatus.DO_NOT_DISTURB);
18
+		jb.setToken("NjYzMjgzODYxMTUyNTk1OTkz.XhGR3g.GubjRZ-9njci3CUCoasIFj02mmU");
19
+		jb.addEventListeners(new TListener());
20
+		
21
+		try {
22
+			jda = jb.build();
23
+		} catch (LoginException e) {
24
+			// TODO Auto-generated catch block
25
+			e.printStackTrace();
26
+		}
27
+	}
28
+
29
+}

+ 33
- 0
src/main/java/kr/co/swh/lecture/opensource/discord/TListener.java Parādīt failu

@@ -0,0 +1,33 @@
1
+package kr.co.swh.lecture.opensource.discord;
2
+
3
+import java.util.List;
4
+
5
+import net.dv8tion.jda.api.entities.Message;
6
+import net.dv8tion.jda.api.entities.TextChannel;
7
+import net.dv8tion.jda.api.entities.User;
8
+import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
9
+import net.dv8tion.jda.api.hooks.ListenerAdapter;
10
+import twitter4j.Status;
11
+
12
+public class TListener extends ListenerAdapter{
13
+	
14
+	@Override
15
+	public void onMessageReceived(MessageReceivedEvent event) {
16
+		// TODO Auto-generated method stub
17
+		User user = event.getAuthor();
18
+		TextChannel tc = event.getTextChannel();
19
+		Message msg = event.getMessage();
20
+		if(user.isBot()) return;
21
+		if(msg.getContentRaw().charAt(0) == '!') {
22
+			String[] args = msg.getContentRaw().substring(1).split(" ");
23
+			if(args.length < 0) return;
24
+			if(args.length == 2 && args[0].equalsIgnoreCase("twitter")) {
25
+				List<Status> status = new TwitterContents().getContents(args[1]);
26
+				for(Status s : status) {
27
+					tc.sendMessage(s.getText()).queue();
28
+				}
29
+			}
30
+		}
31
+	}
32
+
33
+}

+ 69
- 0
src/main/java/kr/co/swh/lecture/opensource/discord/TwitterContents.java Parādīt failu

@@ -0,0 +1,69 @@
1
+package kr.co.swh.lecture.opensource.discord; 
2
+
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+
6
+import twitter4j.Query;
7
+import twitter4j.QueryResult;
8
+import twitter4j.Status;
9
+import twitter4j.Twitter;
10
+import twitter4j.TwitterException;
11
+import twitter4j.TwitterFactory;
12
+import twitter4j.conf.ConfigurationBuilder;
13
+
14
+/**
15
+ * <pre>
16
+ * kr.co.swh.lecture.opensource.discord 
17
+ * TwitterContents.java
18
+ *
19
+ * 설명 :	https://github.com/Twitter4J/Twitter4J
20
+ * </pre>
21
+ * 
22
+ * @since : 2019. 12. 13.
23
+ * @author : tobby48
24
+ * @version : v1.0
25
+ */
26
+public class TwitterContents {
27
+
28
+	String consumerKey;
29
+	String consumerSecret;
30
+	String acessToken;
31
+	String acessTokenSecret;
32
+	
33
+	public TwitterContents() {
34
+		consumerKey = "M3ioqfTgFkX2x04cYj8zoxoqB";
35
+        consumerSecret = "W52Zj3NL4CCwN1ROFsJY2M6qP6AZtNtWu2Ppkvf21x7PkFnEcz";
36
+        acessToken = "728767904-rvWxQuaQ7kIpOibulxvoHipDT8pWuEoukp4GaFYu";
37
+        acessTokenSecret = "DrIV41WcnXKCTW1rDUAzmcD4alzq3cHYW90pfrOvvhuvL";
38
+	}
39
+	
40
+	public List<Status> getContents(String key){
41
+        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
42
+        configurationBuilder.setDebugEnabled(true)
43
+                .setOAuthConsumerKey(consumerKey)
44
+                .setOAuthConsumerSecret(consumerSecret)
45
+                .setOAuthAccessToken(acessToken)
46
+                .setOAuthAccessTokenSecret(acessTokenSecret);
47
+        TwitterFactory tf = new TwitterFactory(configurationBuilder.build());
48
+        Twitter twitter = tf.getInstance();
49
+        
50
+        Query query = new Query();
51
+        query.setLang("ko");
52
+        query.setQuery(key);
53
+        query.setSince("2019-09-28");
54
+        
55
+        QueryResult queryResult = null;
56
+		try {
57
+			queryResult = twitter.search(query);
58
+		} catch (TwitterException e) {
59
+			// TODO Auto-generated catch block
60
+			e.printStackTrace();
61
+		}
62
+        
63
+        ArrayList<Status> result = new ArrayList<>();
64
+        for (Status status : queryResult.getTweets()) {
65
+            result.add(status);
66
+        }
67
+        return result;
68
+	}
69
+}

+ 23
- 21
src/main/java/kr/co/swh/lecture/opensource/sparkjava/FreeMarkerExample.java Parādīt failu

@@ -1,21 +1,23 @@
1
-//package kr.co.swh.lecture.opensource.sparkjava;
2
-//
3
-//import static spark.Spark.get;
4
-//import static spark.Spark.modelAndView;
5
-//import java.util.HashMap;
6
-//import java.util.Map;
7
-//
8
-//public class FreeMarkerExample {
9
-//
10
-//    public static void main(String args[]) {
11
-//
12
-//        get("/hello", (request, response) -> {
13
-//            Map<String, Object> attributes = new HashMap<>();
14
-//            attributes.put("message", "Hello FreeMarker World");
15
-//
16
-//            // The hello.ftl file is located in directory:
17
-//            // src/test/resources/spark/examples/templateview/freemarker
18
-//            return modelAndView(attributes, "hello.ftl");
19
-//        }, new FreeMarkerTemplateEngine());
20
-//    }
21
-//}
1
+package kr.co.swh.lecture.opensource.sparkjava;
2
+
3
+import static spark.Spark.get;
4
+import static spark.Spark.modelAndView;
5
+
6
+import java.io.IOException;
7
+import java.util.HashMap;
8
+import java.util.Map;
9
+
10
+public class FreeMarkerExample {
11
+
12
+    public static void main(String args[]) throws IOException {
13
+
14
+        get("/hello", (request, response) -> {
15
+            Map<String, Object> attributes = new HashMap<>();
16
+            attributes.put("message", "Hello FreeMarker World");
17
+
18
+            // The hello.ftl file is located in directory:
19
+            // src/test/resources/spark/examples/templateview/freemarker
20
+            return modelAndView(attributes, "hello.ftl");
21
+        }, new FreeMarkerTemplateEngine());
22
+    }
23
+}

+ 44
- 30
src/main/java/kr/co/swh/lecture/opensource/sparkjava/FreeMarkerTemplateEngine.java Parādīt failu

@@ -1,36 +1,50 @@
1
-//package kr.co.swh.lecture.opensource.sparkjava;
2
-//
3
-//import static spark.Spark.*;
4
-//
5
-//import spark.TemplateEngine;
6
-//
7
-//public class FreeMarkerTemplateEngine extends TemplateEngine {
8
-//
9
-//    private Configuration configuration;
10
-//
11
-//    protected FreeMarkerTemplateEngine() {
1
+package kr.co.swh.lecture.opensource.sparkjava;
2
+
3
+import java.io.File;
4
+import java.io.IOException;
5
+import java.io.StringWriter;
6
+
7
+import freemarker.cache.FileTemplateLoader;
8
+import freemarker.template.Configuration;
9
+import freemarker.template.Template;
10
+import freemarker.template.TemplateException;
11
+import spark.ModelAndView;
12
+import spark.TemplateEngine;
13
+
14
+public class FreeMarkerTemplateEngine extends TemplateEngine {
15
+
16
+    private Configuration configuration;
17
+    private final String PATH = "src/main/resources/freemaker";
18
+    		
19
+    public FreeMarkerTemplateEngine() throws IOException {
12 20
 //        this.configuration = createFreemarkerConfiguration();
13
-//    }
14
-//
15
-//    @Override
16
-//    public String render(ModelAndView modelAndView) {
17
-//        try {
18
-//            StringWriter stringWriter = new StringWriter();
19
-//
20
-//            Template template = configuration.getTemplate(modelAndView.getViewName());
21
-//            template.process(modelAndView.getModel(), stringWriter);
22
-//
23
-//            return stringWriter.toString();
24
-//        } catch (IOException e) {
25
-//            throw new IllegalArgumentException(e);
26
-//        } catch (TemplateException e) {
27
-//            throw new IllegalArgumentException(e);
28
-//        }
29
-//    }
30
-//
21
+    	configuration.setClassForTemplateLoading(FreeMarkerTemplateEngine.class, "/");
22
+        FileTemplateLoader templateLoader = new FileTemplateLoader(new File("resources"));
23
+        configuration.setTemplateLoader(templateLoader);
24
+
25
+//        Template helloTemp= configuration.getTemplate("hello.ftl");
26
+    }
27
+
31 28
 //    private Configuration createFreemarkerConfiguration() {
32 29
 //        Configuration retVal = new Configuration();
33 30
 //        retVal.setClassForTemplateLoading(FreeMarkerTemplateEngine.class, "freemarker");
34 31
 //        return retVal;
35 32
 //    }
36
-//}
33
+
34
+	@Override
35
+	public String render(ModelAndView modelAndView) {
36
+		// TODO Auto-generated method stub
37
+		try {
38
+            StringWriter stringWriter = new StringWriter();
39
+
40
+            Template template = configuration.getTemplate(modelAndView.getViewName());
41
+            template.process(modelAndView.getModel(), stringWriter);
42
+
43
+            return stringWriter.toString();
44
+        } catch (IOException e) {
45
+            throw new IllegalArgumentException(e);
46
+        } catch (TemplateException e) {
47
+            throw new IllegalArgumentException(e);
48
+        }
49
+	}
50
+}

+ 1
- 0
src/main/resources/hello.ftl Parādīt failu

@@ -0,0 +1 @@
1
+<h1>${message}</h1>

+ 25
- 0
src/main/resources/public/index.html Parādīt failu

@@ -0,0 +1,25 @@
1
+<!DOCTYPE html>
2
+<html lang="en">
3
+<head>
4
+<meta charset="utf-8">
5
+<title>Welcome to SWH Academy</title>
6
+<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+<meta name="description" content="Lectures in SWH Academy" />
8
+<meta name="author" content="tobby48@gmail.com" />
9
+</head>
10
+<body bgcolor=white>
11
+	<table border="5" cellpadding="10">
12
+		<tr>
13
+			<td>
14
+				<h1>sparkjava Static Resource 입니다.</h1>
15
+			</td>
16
+			<td>
17
+				<h3>22.</h3>
18
+			</td>
19
+		</tr>
20
+	</table>
21
+	<ul>
22
+		<li><a href="swhacademy.ga">SWH Homepage</a>.
23
+	</ul>
24
+</body>
25
+</html>