엄성진

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

......@@ -41,5 +41,5 @@ exports.all_ids[302] = 'PLfVO1GXBPp3ubihJVPi6HSltRIuvSqSI1'
exports.all_ids[303] = 'PLfVO1GXBPp3u0ckIfYZkADGlSQ6LQoAIQ'
exports.all_ids[304] = 'PLfVO1GXBPp3vo3sivJPfFzREBECRFepVK'
exports.all_ids[501] = ['p5iu1V30myk', 'i-SBnhaZSSU', 'Etfgb6A7hSI', 'IN2mml9xs_s',
'tp1uoFAfgHE', 'Qx22TnVVIfU', '3y1QIqR115A', 'VXBI1wt8XII', 'aWMBn2--E0Q', 'HyTbgBlnLCo'];
\ No newline at end of file
//exports.all_ids[501] = ['p5iu1V30myk', 'i-SBnhaZSSU', 'Etfgb6A7hSI', 'IN2mml9xs_s',
// 'tp1uoFAfgHE', 'Qx22TnVVIfU', '3y1QIqR115A', 'VXBI1wt8XII', 'aWMBn2--E0Q', 'HyTbgBlnLCo'];
\ No newline at end of file
......
......@@ -6,19 +6,164 @@ const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
var { google } = require('googleapis');
var service = google.youtube('v3');
//세팅값
var setting_userId = []; //유저아이디보관
var setting_Thumbnail = []; // 1:썸네일 출력(기본값) / 0:미출력
var setting_SendAmount = []; // 보낼 양 (1이상, 10이하의 정수) (기본값:1)
var setting_SendAddress = []; // 1:유튜브 주소 출력(기본값) / 0:미출력
exports.check = function (message, replyToken) {
function SEND(replyToken,title,url2,thumbnail,SendAmount,sendaddress) //검색해서 나온 내용 전송하는 함수
{
//sendMessage = JSON.stringify(response.data.items[0].snippet.title);
const rand_0to9 = Math.floor(Math.random() * 10); // 0~9까지 난수생성
sendMessage = "";
var temp_title = [], temp_url = [], temp_url2 = [], temp_thumpnail = [];
for (var i = 0; i < SendAmount; i++) {
temp_title[i] = title[i];
temp_url2[i] = url2[i];
temp_url[i] = 'https://www.youtube.com/watch?v=' + (temp_url2[i].replace(/\"/gi, ""));
sendMessage += temp_title[i] + "\n" +
temp_url[i] + "\n" + "\n";
}
console.log(sendMessage);
if (thumbnail==1) //썸네일을 활성화시켰을 경우, 맨 첫번째 작품 리스트 썸네일만 보여줌.
request.post(
{
url: TARGET_URL,
headers:
{
'Authorization': `Bearer ${Info.TOKEN}`
},
json:
{
"replyToken": replyToken,
"messages":
[
{
"type": "image",
"originalContentUrl": "https://img.youtube.com/vi/" + url2[0] + "/hqdefault.jpg",
"previewImageUrl": "https://img.youtube.com/vi/" + url2[0] + "/hqdefault.jpg"
},
{
"type": "text",
"text": sendMessage
}
]
}
}
);
}
function SEND_TEXT(replyToken,sendMessage) //단순 텍스트 전송하는 함수
{
console.log(sendMessage);
request.post(
{
url: TARGET_URL,
headers:
{
'Authorization': `Bearer ${Info.TOKEN}`
},
json:
{
"replyToken": replyToken,
"messages":
[
{
"type": "text",
"text": sendMessage
}
]
}
}
);
}
function SEND_LIST(replyToken,response,choice,thumbnail,SendAmount,sendaddress)//플레이리스트에서 나온 내용 전송하는 함수
{
//sendMessage = JSON.stringify(response.data.items[0].snippet.title);
sendMessage = "";
var temp_title = [], temp_url = [], temp_url2 = [], temp_thumpnail = [];
for (var i = 0; i < SendAmount; i++) {
temp_title[i] = JSON.stringify(response.data.items[i].snippet.title).replace(/\"/gi, "");
temp_url2[i] = JSON.stringify(response.data.items[i].snippet.resourceId.videoId);
//temp_url2[i] = choice;
temp_url[i] = 'https://www.youtube.com/watch?v=' + (temp_url2[i].replace(/\"/gi, ""));
//temp_thumpnail[i] = "https://img.youtube.com/vi/" + JSON.stringify(response.data.items[i].snippet.thumbnails.high.url).replace(/\"/gi, "") + "/hqdefault.jpg";
// 썸네일 : 사용자가 플레이리스트로 입력 시 리턴으로 너무 많은 정보들을 줘야함. 그래서 뺌.
sendMessage += temp_title[i] + "\n" +
temp_url[i] + "\n" + "\n";
// temp_thumpnail[i] + "\n" + "\n";
}
console.log(sendMessage);
if (thumbnail==1) //썸네일을 활성화시켰을 경우, 맨 첫번째 작품 리스트 썸네일만 보여줌.
request.post(
{
url: TARGET_URL,
headers:
{
'Authorization': `Bearer ${Info.TOKEN}`
},
json:
{
"replyToken": replyToken,
"messages":
[
{
"type": "image",
"originalContentUrl": "https://img.youtube.com/vi/" + (temp_url2[0].replace(/\"/gi, "")) + "/hqdefault.jpg",
"previewImageUrl": "https://img.youtube.com/vi/" + (temp_url2[0].replace(/\"/gi, "")) + "/hqdefault.jpg"
},
{
"type": "text",
"text": sendMessage
}
]
}
}
);
}
exports.check = function (message, replyToken, userId) {
if (setting_userId.indexOf(userId)==-1)
{
setting_userId.push(userId);
setting_Thumbnail.push(1);
setting_SendAmount.push(1);
setting_SendAddress.push(1);
console.log("없당");
}
else
{
console.log("있당");
}
//const rand_0to9 = Math.floor(Math.random() * 10); // 0~9까지 난수생성
var choice;
var choose;
switch (message) {
case "슬픈 노래":
choose = 501;
choice = ids.all_ids[choose][rand_0to9]
break;
case "힙합 플레이리스트":
choose = 101;
choice = ids.all_ids[choose]
......@@ -55,6 +200,15 @@ exports.check = function (message, replyToken) {
choose = 304;
choice = ids.all_ids[choose]
break;
case "도움말":
choose = 001;
choice = 0;
break;
case "설정":
choose = 002;
choice = 0;
break;
// 아래는 case 추가를 위한 템플릿
// case "":
......@@ -62,12 +216,44 @@ exports.check = function (message, replyToken) {
// choice = ids.all_ids[choose]
default:
console.log("잘못 입력했어요~ 다시 입력하세요");
choice = -1;
choose = 500;
choice = 0;
}
if (choose > 500) {
if (choose == 500) { //일반 검색
var optionParams={
q:message,
part:"snippet",
key:"AIzaSyBInggOtXxPFYIRee0Xs3vb5iZ9YE9_518",
maxResults:10
};
optionParams.q=encodeURI(optionParams.q);
var url="https://www.googleapis.com/youtube/v3/search?";
for(var option in optionParams){
url+=option+"="+optionParams[option]+"&";
}
//url의마지막에 붙어있는 & 정리
url=url.substr(0, url.length-1);
request(url, function(err, res, body){
var title=[]
var url2=[]
var data=JSON.parse(body).items;
for(var content in data)
{
title.push(data[content].snippet.title);
url2.push(data[content].id.videoId);
}
if (title.length==0)
console.log('검색된 동영상이 없습니다.');
else
SEND(replyToken,title,url2,1,3,1);
});
/*
service.videos.list
(
{
......@@ -88,125 +274,45 @@ exports.check = function (message, replyToken) {
console.log('검색된 동영상이 없습니다.');
}
else {
sendMessage = JSON.stringify(response.data.items[0].snippet.title);
console.log(sendMessage);
request.post(
{
url: TARGET_URL,
headers:
{
'Authorization': `Bearer ${Info.TOKEN}`
},
json:
{
"replyToken": replyToken,
"messages":
[
{
"type": "image",
"originalContentUrl": "https://img.youtube.com/vi/" + choice + "/hqdefault.jpg",
"previewImageUrl": "https://img.youtube.com/vi/" + choice + "/hqdefault.jpg"
},
{
"type": "text",
"text": sendMessage
}
]
}
}
);
SEND(replyToken,response,choice,1,1,1);
}
}
)
*/
}
else {
if (choice == -1) {
sendMessage = "잘못 입력했어요~ 다시 입력하세요";
request.post(
{
url: TARGET_URL,
headers:
{
'Authorization': `Bearer ${Info.TOKEN}`
},
json:
{
"replyToken": replyToken,
"messages":
[
{
"type": "text",
"text": sendMessage
}
]
}
}
);
}
else {
service.playlistItems.list({
key: 'AIzaSyBInggOtXxPFYIRee0Xs3vb5iZ9YE9_518',
part: 'snippet',
fields: 'items(snippet(title,resourceId,thumbnails(high(url))))', //제목, VideoId, Thumbnail 이미지 정보.
maxResults: 10,
playlistId: choice
}, function (err, response) {
else if (choose == 001){ // 도움말
sendMessage = "도움말 예시";
SEND_TEXT(replyToken,sendMessage);
}
else if (choose == 002) { // 설정
sendMessage = "설정 예시";
SEND_TEXT(replyToken,sendMessage);
}
else { //플레이리스트
service.playlistItems.list(
{
key: 'AIzaSyBInggOtXxPFYIRee0Xs3vb5iZ9YE9_518',
part: 'snippet',
fields: 'items(snippet(title,resourceId,thumbnails(high(url))))', //제목, VideoId, Thumbnail 이미지 정보.
maxResults: 10,
playlistId: choice
}, function (err, response){
if (err) {
console.log('The API returned an error: ', err);
return;
}
var video = response.data.items;
if (video.length == 0) {
if (video.length == 0)
console.log('검색된 동영상이 없습니다.');
} else {
sendMessage = "";
var temp_title = [], temp_url = [], temp_url2 = [], temp_thumpnail = [];
for (var i = 0; i < 10; i++) {
temp_title[i] = JSON.stringify(response.data.items[i].snippet.title).replace(/\"/gi, "");
temp_url2[i] = JSON.stringify(response.data.items[i].snippet.resourceId.videoId);
temp_url[i] = 'https://www.youtube.com/watch?v=' + (temp_url2[i].replace(/\"/gi, ""));
//temp_thumpnail[i] = "https://img.youtube.com/vi/" + JSON.stringify(response.data.items[i].snippet.thumbnails.high.url).replace(/\"/gi, "") + "/hqdefault.jpg";
// 썸네일 : 사용자가 플레이리스트로 입력 시 리턴으로 너무 많은 정보들을 줘야함. 그래서 뺌.
sendMessage += temp_title[i] + "\n" +
temp_url[i] + "\n" + "\n";
// temp_thumpnail[i] + "\n" + "\n";
} console.log(sendMessage);
request.post(
{
url: TARGET_URL,
headers:
{
'Authorization': `Bearer ${Info.TOKEN}`
},
json:
{
"replyToken": replyToken,
"messages":
[
// {
// "type": "image",
// "originalContentUrl": temp_thumpnail[0],
// "previewImageUrl": temp_thumpnail[0]
// },
{
"type": "text",
"text": sendMessage
}
]
}
}
);
}
})
}
else
SEND_LIST(replyToken,response,choice,1,10,1);
}
)
}
......
......@@ -6,17 +6,11 @@ const HTTPS = require('https');
const domain = "2020105631.oss2021.tk"
const sslport = 23023;
const bodyParser = require('body-parser');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply';
// var Info = require('./Info');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
var app = express();
app.use(bodyParser.json());
<<<<<<< HEAD
var sad = require('./video_sad');
=======
var songs = require('./check');
>>>>>>> feature/YouTube_API
var thumbnail = 1;// 썸네일 출력 여부 (1:출력/0:미출력/기본값:1)
app.post('/hook', function (req, res) {
......@@ -26,10 +20,11 @@ app.post('/hook', function (req, res) {
// request log
console.log('======================', new Date(), '======================');
send(eventObj.replyToken, message.text);
send(eventObj.replyToken, message.text,eventObj.source.userId);
res.sendStatus(200);
});
function send(replyToken, message) {
function send(replyToken, message,userId) {
request.post(
{
......@@ -37,8 +32,7 @@ function send(replyToken, message) {
},
function () {
songs.check(message, replyToken);
songs.check(message, replyToken,userId);
}
);
}
......