app.js 12.6 KB

// 모듈 불러오기
var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');
var app = express(); // express 객체 저장
var async = require('async');

var crawlReol = require('./crawling/Reol');
var crawlYonezu = require('./crawling/Yonezu');
var crawlYorusika = require('./crawling/Yorusika');
var singer;

//body-parser 미들웨어 사용
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

// http://서버주소/keyboard
app.get('/keyboard', function(req,res){
    var data = {
        'type' : 'buttons', 
        'buttons' : ['일본', '한국']};
    res.json(data);
    
});

//8080포트로 서버 접속
http.createServer(app).listen(8080, () => {
    console.log('Server running..');
});

// http://서버주소/message
app.post('/message', function(req,res){
    //유저가 입력한 데이터
    var msg = req.body.content;
    console.log('전달받은 메시지 : ' + msg);
    var send = {};
    var task = [
        function(callback){
            switch(msg){
                case 'Reol-Discography' :
                    singer = 'Reol';
                    crawlReol.crawl_Reol('https://namu.wiki/w/%EB%A0%88%EC%98%A4%EB%A3%A8/%EC%9D%8C%EB%B0%98#toc',function(Reol){
                        callback(null,Reol);
                    });
                    break;
                case '米津玄師-Discography' :
                    singer = 'Yonezu';
                    crawlYonezu.crawl_yonezu('https://namu.wiki/w/%EC%9A%94%EB%84%A4%EC%A6%88%20%EC%BC%84%EC%8B%9C/%EB%94%94%EC%8A%A4%EC%BD%94%EA%B7%B8%EB%9E%98%ED%94%BC#s-3.2.9',function(yonezu){
                        callback(null,yonezu)
                    });
                    break;
                case 'ヨルシカ-Discography' :
                    singer = 'yorusika';
                    crawlYorusika.crawl_yorusika('http://yorushika.com/discography/', function(yorusika){
                        callback(null,yorusika);
                    });
                    break;
                default:
                    if(singer == 'Reol'){
                        crawlReol.crawl_Reol('https://namu.wiki/w/%EB%A0%88%EC%98%A4%EB%A3%A8/%EC%9D%8C%EB%B0%98#toc',function(Reol){
                            callback(null,Reol);
                        })
                    }
                    else if(singer == "Yonezu"){
                        crawlYonezu.crawl_yonezu('https://namu.wiki/w/%EC%9A%94%EB%84%A4%EC%A6%88%20%EC%BC%84%EC%8B%9C/%EB%94%94%EC%8A%A4%EC%BD%94%EA%B7%B8%EB%9E%98%ED%94%BC#s-3.2.9',function(yonezu){
                            callback(null,yonezu)
                        });
                    }
                    else if(singer=='yorusika'){
                        crawlYorusika.crawl_yorusika('http://yorushika.com/discography/', function(yorusika){
                            callback(null,yorusika);
                        });
                    }
                    else
                        callback(null,null);
                    break;
            }
            
        },
        function(discography, callback){
            var index = parseInt(msg[0])-1;
            switch (msg) {
                case '일본':
                    send = {
                        'message': {
                            'text': '등록된 가수 목록입니다.'
                        },
                        keyboard: {
                            'type': 'buttons',
                            'buttons': ['Reol', '米津玄師', 'ヨルシカ']
                        }

                    };
                    break;

                case '한국':
                    send = {
                        'message': {
                            'text': '등록된 가수 목록입니다.'
                        },
                        keyboard: {
                            'type': 'buttons',
                            'buttons': ['MOT', 'Gukkasten', 'ZICO']
                        }
                    };
                    break;
          // ---------- contents -------------- // 
                ///// Reol ////             
                case 'Reol' :
                    send = {
                        'message' : {
                            'text' : '이름 : Reol(れをる) \n성별 : 여성 \n생년월일 : 1993년 11월 9일 \n혈액형 : AB형',
                            'photo' : {
                                    'url' : 'https://www.reol.jp/images/profile/reol_Aphoto_2.png',
                                    'width' : 1000,
                                    'height' : 667
                            },
                            'message_button' : {
                                'label' : '공식 홈페이지',
                                'url' : "https://www.reol.jp/"
                            }
                        },
                        keyboard : {
                            'type' : 'buttons',
                            'buttons' : ['Reol-Discography']
                        }
                    };
                    break;
                case 'Reol-Discography' :
                    send = {
                        'message': {
                            'text': 'Album List',
                            'photo': {
                                'url': 'https://yt3.ggpht.com/a-/AN66SAydQt1STVdFCVvwEyZitr-v4dNGDKdyIt-oSA=s288-mo-c-c0xffffffff-rj-k-no',
                                'width': 300,
                                'height': 300
                            },
                            'message_button' : {
                                'label' : 'Youtube channel Link',
                                'url' : "https://www.youtube.com/channel/UCB6pJFaFByws3dQj4AdLdyA/"
                            }
                        },
                        keyboard: {
                            'type': 'buttons',  
                            'buttons': discography[0].json_album
                        }
                    }
                    break;
                /// 米津玄師 ///
                case '米津玄師':
                    send = {
                        'message': {
                            'text': '이름 : 米津玄師(Yonezu Kenshi) \n성별 : 남성 \n생년월일 : 1991년 3월 10일 \n혈액형 : O형',
                            'photo': {
                                'url': 'https://s3.namuwikiusercontent.com/s/5f35be0c28de612d20eccea8c795cee38eea58700af4723f6920fc00b48c491ae5e89572e7bcb5ab2e2eb37aa6c498dbc3f4896239d3f98e4794c9b1ec96031c43d77090d83f00ba5b7d65e566f415af055f79a8059dd2f6b1f36f0f8e742f9d',
                                'width': 1000,
                                'height': 667
                            },
                            'message_button': {
                                'label': '공식 홈페이지',
                                'url': "http://reissuerecords.net/"
                            }
                        },
                        keyboard: {
                            'type': 'buttons',
                            'buttons': ['米津玄師-Discography']
                        }
                    };
                    break;
                case '米津玄師-Discography':
                    send = {
                        'message': {
                            'text': 'Album List',
                            'photo': {
                                'url': 'https://yt3.ggpht.com/a-/AN66SAwKDZ2RTvreg5m1Ub6Wd6glpKCYMGrVG4mpqw=s288-mo-c-c0xffffffff-rj-k-no',
                                'width': 300,
                                'height': 300
                            },
                            'message_button' : {
                                'label' : 'Youtube channel Link',
                                'url' : "https://www.youtube.com/channel/UCUCeZaZeJbEYAAzvMgrKOPQ/"
                            }
                        },
                        keyboard: {
                            'type': 'buttons',
                            'buttons': discography[0].json_album
                        }

                    };
                    break;
                case 'ヨルシカ':
                    send = {
                        'message': {
                            'text': '개요 : 2인조 밴드\n멤버 : n-buna, suis \n역할 : 작사, 작곡, 편곡 - n-buna\n           보컬 - suis\n서포트 멤버 : \n  下鶴光康(Guitar)\n  キタニタツヤ(Bass)\n  Masack(Drums)',
                            'photo': {
                                'url': 'http://yorushika.com/wp-content/uploads/2018/05/biography_image2.jpg',
                                'width': 1000,
                                'height': 667
                            },
                            'message_button': {
                                'label': '공식 홈페이지',
                                'url': "http://yorushika.com/"
                            }
                        },
                        keyboard: {
                            'type': 'buttons',
                            'buttons': ['ヨルシカ-Discography']
                        }
                    };
                    break;
                case 'ヨルシカ-Discography':
                    send = {
                        'message': {
                            'text': 'Album List',
                            'photo': {
                                'url': 'https://yt3.ggpht.com/a-/AN66SAzKliDeZQJhb9JshzY4hJY_pJSbM1gfYoqY=s288-mo-c-c0xffffffff-rj-k-no',
                                'width': 300,
                                'height': 300
                            },
                            'message_button' : {
                                'label' : 'Youtube channel Link',
                                'url' : "https://www.youtube.com/channel/UCRIgIJQWuBJ0Cv_VlU3USNA"
                            }
                        },
                        keyboard: {
                            'type': 'buttons',
                            'buttons': discography[0].json_album
                        }

                    };
                    break; 
                // 앨범 정보 읽기 //
                case discography[0].json_album[index]:
                    var album = '앨범 : ' + discography[0].json_album[index].substr(3);
                    if (singer == 'Reol') {
                        send = {
                            'message': {
                                'text': album + '\n발매년도 : ' + discography[0].json_year[index] + '\n수록곡 : \n' + discography[0].json_track[index]
                            },
                            keyboard: {
                                'type': 'buttons',
                                'buttons': ['Reol-Discography', '초기화면']
                            }
                        }
                    }
                    else if (singer == 'Yonezu') {
                        send = {
                            'message': {
                                'text': album + '\n발매년도 : ' + discography[0].json_year[index] + '\n수록곡 : \n' + discography[0].json_track[index]
                            },
                            keyboard: {
                                'type': 'buttons',
                                'buttons': ['米津玄師-Discography', '초기화면']
                            }
                        }
                    }
                    else if (singer == 'yorusika'){
                        send = {
                            'message': {
                                'text': album + '\n발매년도 : ' + discography[0].json_year[index] + '\n수록곡 : \n' + discography[0].json_track[index]
                            },
                            keyboard: {
                                'type': 'buttons',
                                'buttons': ['ヨルシカ-Discography', '초기화면']
                            }
                        }
                    }
                    break;
                case '초기화면' : 
                    send = {
                        'message' : {
                            'text' : '초기화면으로 돌아갑니다.'
                        },                
                        keyboard : {
                            'type' : 'buttons',
                            'buttons' : ['일본', '한국']
                        }
                    }
                    break;
                default:
                    break;
            }
            callback(null ,send);
        }
    ];
    async.waterfall(task, function(err,result){
        console.log(send);
        res.json(send);
    });
    
});