엄성진

Simply Function, Add Searching Function, Add User Setting(part)

...@@ -41,5 +41,5 @@ exports.all_ids[302] = 'PLfVO1GXBPp3ubihJVPi6HSltRIuvSqSI1' ...@@ -41,5 +41,5 @@ exports.all_ids[302] = 'PLfVO1GXBPp3ubihJVPi6HSltRIuvSqSI1'
41 exports.all_ids[303] = 'PLfVO1GXBPp3u0ckIfYZkADGlSQ6LQoAIQ' 41 exports.all_ids[303] = 'PLfVO1GXBPp3u0ckIfYZkADGlSQ6LQoAIQ'
42 exports.all_ids[304] = 'PLfVO1GXBPp3vo3sivJPfFzREBECRFepVK' 42 exports.all_ids[304] = 'PLfVO1GXBPp3vo3sivJPfFzREBECRFepVK'
43 43
44 -exports.all_ids[501] = ['p5iu1V30myk', 'i-SBnhaZSSU', 'Etfgb6A7hSI', 'IN2mml9xs_s',
45 - 'tp1uoFAfgHE', 'Qx22TnVVIfU', '3y1QIqR115A', 'VXBI1wt8XII', 'aWMBn2--E0Q', 'HyTbgBlnLCo'];
...\ No newline at end of file ...\ No newline at end of file
44 +//exports.all_ids[501] = ['p5iu1V30myk', 'i-SBnhaZSSU', 'Etfgb6A7hSI', 'IN2mml9xs_s',
45 +// 'tp1uoFAfgHE', 'Qx22TnVVIfU', '3y1QIqR115A', 'VXBI1wt8XII', 'aWMBn2--E0Q', 'HyTbgBlnLCo'];
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -6,19 +6,164 @@ const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' ...@@ -6,19 +6,164 @@ const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
6 var { google } = require('googleapis'); 6 var { google } = require('googleapis');
7 var service = google.youtube('v3'); 7 var service = google.youtube('v3');
8 8
9 +//세팅값
10 +var setting_userId = []; //유저아이디보관
11 +var setting_Thumbnail = []; // 1:썸네일 출력(기본값) / 0:미출력
12 +var setting_SendAmount = []; // 보낼 양 (1이상, 10이하의 정수) (기본값:1)
13 +var setting_SendAddress = []; // 1:유튜브 주소 출력(기본값) / 0:미출력
9 14
10 -exports.check = function (message, replyToken) { 15 +function SEND(replyToken,title,url2,thumbnail,SendAmount,sendaddress) //검색해서 나온 내용 전송하는 함수
16 +{
17 + //sendMessage = JSON.stringify(response.data.items[0].snippet.title);
11 18
12 - const rand_0to9 = Math.floor(Math.random() * 10); // 0~9까지 난수생성 19 + sendMessage = "";
20 + var temp_title = [], temp_url = [], temp_url2 = [], temp_thumpnail = [];
21 + for (var i = 0; i < SendAmount; i++) {
13 22
23 + temp_title[i] = title[i];
24 +
25 + temp_url2[i] = url2[i];
26 + temp_url[i] = 'https://www.youtube.com/watch?v=' + (temp_url2[i].replace(/\"/gi, ""));
27 +
28 + sendMessage += temp_title[i] + "\n" +
29 + temp_url[i] + "\n" + "\n";
30 + }
31 +
32 +
33 + console.log(sendMessage);
34 + if (thumbnail==1) //썸네일을 활성화시켰을 경우, 맨 첫번째 작품 리스트 썸네일만 보여줌.
35 + request.post(
36 + {
37 + url: TARGET_URL,
38 + headers:
39 + {
40 + 'Authorization': `Bearer ${Info.TOKEN}`
41 + },
42 + json:
43 + {
44 + "replyToken": replyToken,
45 + "messages":
46 + [
47 + {
48 + "type": "image",
49 + "originalContentUrl": "https://img.youtube.com/vi/" + url2[0] + "/hqdefault.jpg",
50 + "previewImageUrl": "https://img.youtube.com/vi/" + url2[0] + "/hqdefault.jpg"
51 + },
52 + {
53 + "type": "text",
54 + "text": sendMessage
55 + }
56 + ]
57 + }
58 + }
59 + );
60 +}
61 +
62 +function SEND_TEXT(replyToken,sendMessage) //단순 텍스트 전송하는 함수
63 +{
64 + console.log(sendMessage);
65 + request.post(
66 + {
67 + url: TARGET_URL,
68 + headers:
69 + {
70 + 'Authorization': `Bearer ${Info.TOKEN}`
71 + },
72 + json:
73 + {
74 + "replyToken": replyToken,
75 + "messages":
76 + [
77 + {
78 + "type": "text",
79 + "text": sendMessage
80 + }
81 + ]
82 + }
83 + }
84 + );
85 +}
86 +
87 +function SEND_LIST(replyToken,response,choice,thumbnail,SendAmount,sendaddress)//플레이리스트에서 나온 내용 전송하는 함수
88 +{
89 + //sendMessage = JSON.stringify(response.data.items[0].snippet.title);
90 +
91 + sendMessage = "";
92 + var temp_title = [], temp_url = [], temp_url2 = [], temp_thumpnail = [];
93 + for (var i = 0; i < SendAmount; i++) {
94 +
95 + temp_title[i] = JSON.stringify(response.data.items[i].snippet.title).replace(/\"/gi, "");
96 +
97 + temp_url2[i] = JSON.stringify(response.data.items[i].snippet.resourceId.videoId);
98 + //temp_url2[i] = choice;
99 + temp_url[i] = 'https://www.youtube.com/watch?v=' + (temp_url2[i].replace(/\"/gi, ""));
100 +
101 + //temp_thumpnail[i] = "https://img.youtube.com/vi/" + JSON.stringify(response.data.items[i].snippet.thumbnails.high.url).replace(/\"/gi, "") + "/hqdefault.jpg";
102 + // 썸네일 : 사용자가 플레이리스트로 입력 시 리턴으로 너무 많은 정보들을 줘야함. 그래서 뺌.
103 +
104 + sendMessage += temp_title[i] + "\n" +
105 + temp_url[i] + "\n" + "\n";
106 + // temp_thumpnail[i] + "\n" + "\n";
107 + }
108 +
109 +
110 + console.log(sendMessage);
111 + if (thumbnail==1) //썸네일을 활성화시켰을 경우, 맨 첫번째 작품 리스트 썸네일만 보여줌.
112 + request.post(
113 + {
114 + url: TARGET_URL,
115 + headers:
116 + {
117 + 'Authorization': `Bearer ${Info.TOKEN}`
118 + },
119 + json:
120 + {
121 + "replyToken": replyToken,
122 + "messages":
123 + [
124 + {
125 + "type": "image",
126 + "originalContentUrl": "https://img.youtube.com/vi/" + (temp_url2[0].replace(/\"/gi, "")) + "/hqdefault.jpg",
127 + "previewImageUrl": "https://img.youtube.com/vi/" + (temp_url2[0].replace(/\"/gi, "")) + "/hqdefault.jpg"
128 + },
129 + {
130 + "type": "text",
131 + "text": sendMessage
132 + }
133 + ]
134 + }
135 + }
136 + );
137 +}
138 +
139 +
140 +
141 +
142 +
143 +
144 +exports.check = function (message, replyToken, userId) {
145 +
146 +
147 + if (setting_userId.indexOf(userId)==-1)
148 + {
149 + setting_userId.push(userId);
150 + setting_Thumbnail.push(1);
151 + setting_SendAmount.push(1);
152 + setting_SendAddress.push(1);
153 + console.log("없당");
154 + }
155 + else
156 + {
157 + console.log("있당");
158 + }
159 +
160 +
161 +
162 + //const rand_0to9 = Math.floor(Math.random() * 10); // 0~9까지 난수생성
14 var choice; 163 var choice;
15 var choose; 164 var choose;
16 165
17 switch (message) { 166 switch (message) {
18 - case "슬픈 노래":
19 - choose = 501;
20 - choice = ids.all_ids[choose][rand_0to9]
21 - break;
22 case "힙합 플레이리스트": 167 case "힙합 플레이리스트":
23 choose = 101; 168 choose = 101;
24 choice = ids.all_ids[choose] 169 choice = ids.all_ids[choose]
...@@ -55,6 +200,15 @@ exports.check = function (message, replyToken) { ...@@ -55,6 +200,15 @@ exports.check = function (message, replyToken) {
55 choose = 304; 200 choose = 304;
56 choice = ids.all_ids[choose] 201 choice = ids.all_ids[choose]
57 break; 202 break;
203 + case "도움말":
204 + choose = 001;
205 + choice = 0;
206 + break;
207 + case "설정":
208 + choose = 002;
209 + choice = 0;
210 + break;
211 +
58 212
59 // 아래는 case 추가를 위한 템플릿 213 // 아래는 case 추가를 위한 템플릿
60 // case "": 214 // case "":
...@@ -62,12 +216,44 @@ exports.check = function (message, replyToken) { ...@@ -62,12 +216,44 @@ exports.check = function (message, replyToken) {
62 // choice = ids.all_ids[choose] 216 // choice = ids.all_ids[choose]
63 217
64 default: 218 default:
65 - console.log("잘못 입력했어요~ 다시 입력하세요"); 219 + choose = 500;
66 - choice = -1; 220 + choice = 0;
67 } 221 }
68 222
69 - if (choose > 500) { 223 + if (choose == 500) { //일반 검색
70 - 224 +
225 + var optionParams={
226 + q:message,
227 + part:"snippet",
228 + key:"AIzaSyBInggOtXxPFYIRee0Xs3vb5iZ9YE9_518",
229 + maxResults:10
230 + };
231 + optionParams.q=encodeURI(optionParams.q);
232 + var url="https://www.googleapis.com/youtube/v3/search?";
233 + for(var option in optionParams){
234 + url+=option+"="+optionParams[option]+"&";
235 + }
236 +
237 + //url의마지막에 붙어있는 & 정리
238 + url=url.substr(0, url.length-1);
239 +
240 + request(url, function(err, res, body){
241 + var title=[]
242 + var url2=[]
243 + var data=JSON.parse(body).items;
244 + for(var content in data)
245 + {
246 + title.push(data[content].snippet.title);
247 + url2.push(data[content].id.videoId);
248 + }
249 + if (title.length==0)
250 + console.log('검색된 동영상이 없습니다.');
251 + else
252 + SEND(replyToken,title,url2,1,3,1);
253 + });
254 +
255 +
256 + /*
71 service.videos.list 257 service.videos.list
72 ( 258 (
73 { 259 {
...@@ -88,125 +274,45 @@ exports.check = function (message, replyToken) { ...@@ -88,125 +274,45 @@ exports.check = function (message, replyToken) {
88 console.log('검색된 동영상이 없습니다.'); 274 console.log('검색된 동영상이 없습니다.');
89 } 275 }
90 else { 276 else {
91 - sendMessage = JSON.stringify(response.data.items[0].snippet.title); 277 + SEND(replyToken,response,choice,1,1,1);
92 - console.log(sendMessage);
93 - request.post(
94 - {
95 - url: TARGET_URL,
96 - headers:
97 - {
98 - 'Authorization': `Bearer ${Info.TOKEN}`
99 - },
100 - json:
101 - {
102 - "replyToken": replyToken,
103 - "messages":
104 - [
105 - {
106 - "type": "image",
107 - "originalContentUrl": "https://img.youtube.com/vi/" + choice + "/hqdefault.jpg",
108 - "previewImageUrl": "https://img.youtube.com/vi/" + choice + "/hqdefault.jpg"
109 - },
110 - {
111 - "type": "text",
112 - "text": sendMessage
113 - }
114 - ]
115 - }
116 - }
117 - );
118 } 278 }
119 } 279 }
120 280
121 ) 281 )
282 + */
122 } 283 }
123 284
124 - else { 285 + else if (choose == 001){ // 도움말
125 - if (choice == -1) { 286 + sendMessage = "도움말 예시";
126 - sendMessage = "잘못 입력했어요~ 다시 입력하세요"; 287 + SEND_TEXT(replyToken,sendMessage);
127 - request.post( 288 + }
128 - { 289 + else if (choose == 002) { // 설정
129 - url: TARGET_URL, 290 + sendMessage = "설정 예시";
130 - headers: 291 + SEND_TEXT(replyToken,sendMessage);
131 - { 292 + }
132 - 'Authorization': `Bearer ${Info.TOKEN}` 293 +
133 - }, 294 + else { //플레이리스트
134 - json: 295 + service.playlistItems.list(
135 - { 296 + {
136 - "replyToken": replyToken, 297 + key: 'AIzaSyBInggOtXxPFYIRee0Xs3vb5iZ9YE9_518',
137 - "messages": 298 + part: 'snippet',
138 - [ 299 + fields: 'items(snippet(title,resourceId,thumbnails(high(url))))', //제목, VideoId, Thumbnail 이미지 정보.
139 - { 300 + maxResults: 10,
140 - "type": "text", 301 + playlistId: choice
141 - "text": sendMessage 302 + }, function (err, response){
142 - }
143 - ]
144 - }
145 - }
146 - );
147 - }
148 - else {
149 - service.playlistItems.list({
150 - key: 'AIzaSyBInggOtXxPFYIRee0Xs3vb5iZ9YE9_518',
151 - part: 'snippet',
152 - fields: 'items(snippet(title,resourceId,thumbnails(high(url))))', //제목, VideoId, Thumbnail 이미지 정보.
153 - maxResults: 10,
154 - playlistId: choice
155 - }, function (err, response) {
156 if (err) { 303 if (err) {
157 console.log('The API returned an error: ', err); 304 console.log('The API returned an error: ', err);
158 return; 305 return;
159 } 306 }
160 -
161 var video = response.data.items; 307 var video = response.data.items;
162 - if (video.length == 0) { 308 + if (video.length == 0)
163 console.log('검색된 동영상이 없습니다.'); 309 console.log('검색된 동영상이 없습니다.');
164 - } else { 310 + else
165 - sendMessage = ""; 311 + SEND_LIST(replyToken,response,choice,1,10,1);
166 - var temp_title = [], temp_url = [], temp_url2 = [], temp_thumpnail = []; 312 + }
167 - for (var i = 0; i < 10; i++) { 313 + )
168 -
169 - temp_title[i] = JSON.stringify(response.data.items[i].snippet.title).replace(/\"/gi, "");
170 -
171 - temp_url2[i] = JSON.stringify(response.data.items[i].snippet.resourceId.videoId);
172 - temp_url[i] = 'https://www.youtube.com/watch?v=' + (temp_url2[i].replace(/\"/gi, ""));
173 -
174 - //temp_thumpnail[i] = "https://img.youtube.com/vi/" + JSON.stringify(response.data.items[i].snippet.thumbnails.high.url).replace(/\"/gi, "") + "/hqdefault.jpg";
175 - // 썸네일 : 사용자가 플레이리스트로 입력 시 리턴으로 너무 많은 정보들을 줘야함. 그래서 뺌.
176 -
177 - sendMessage += temp_title[i] + "\n" +
178 - temp_url[i] + "\n" + "\n";
179 - // temp_thumpnail[i] + "\n" + "\n";
180 - } console.log(sendMessage);
181 - request.post(
182 - {
183 - url: TARGET_URL,
184 - headers:
185 - {
186 - 'Authorization': `Bearer ${Info.TOKEN}`
187 - },
188 - json:
189 - {
190 - "replyToken": replyToken,
191 - "messages":
192 - [
193 - // {
194 - // "type": "image",
195 - // "originalContentUrl": temp_thumpnail[0],
196 - // "previewImageUrl": temp_thumpnail[0]
197 - // },
198 - {
199 - "type": "text",
200 - "text": sendMessage
201 - }
202 - ]
203 - }
204 - }
205 - );
206 - }
207 - })
208 - }
209 } 314 }
315 +
210 316
211 317
212 318
......
...@@ -6,17 +6,11 @@ const HTTPS = require('https'); ...@@ -6,17 +6,11 @@ const HTTPS = require('https');
6 const domain = "2020105631.oss2021.tk" 6 const domain = "2020105631.oss2021.tk"
7 const sslport = 23023; 7 const sslport = 23023;
8 const bodyParser = require('body-parser'); 8 const bodyParser = require('body-parser');
9 -const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'; 9 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
10 -// var Info = require('./Info');
11 var app = express(); 10 var app = express();
12 app.use(bodyParser.json()); 11 app.use(bodyParser.json());
13 -<<<<<<< HEAD
14 -
15 -var sad = require('./video_sad');
16 -
17 -=======
18 var songs = require('./check'); 12 var songs = require('./check');
19 ->>>>>>> feature/YouTube_API 13 +var thumbnail = 1;// 썸네일 출력 여부 (1:출력/0:미출력/기본값:1)
20 14
21 15
22 app.post('/hook', function (req, res) { 16 app.post('/hook', function (req, res) {
...@@ -26,10 +20,11 @@ app.post('/hook', function (req, res) { ...@@ -26,10 +20,11 @@ app.post('/hook', function (req, res) {
26 20
27 // request log 21 // request log
28 console.log('======================', new Date(), '======================'); 22 console.log('======================', new Date(), '======================');
29 - send(eventObj.replyToken, message.text); 23 +
24 + send(eventObj.replyToken, message.text,eventObj.source.userId);
30 res.sendStatus(200); 25 res.sendStatus(200);
31 }); 26 });
32 -function send(replyToken, message) { 27 +function send(replyToken, message,userId) {
33 28
34 request.post( 29 request.post(
35 { 30 {
...@@ -37,8 +32,7 @@ function send(replyToken, message) { ...@@ -37,8 +32,7 @@ function send(replyToken, message) {
37 }, 32 },
38 33
39 function () { 34 function () {
40 - songs.check(message, replyToken); 35 + songs.check(message, replyToken,userId);
41 -
42 } 36 }
43 ); 37 );
44 } 38 }
......