app.js 3.76 KB
// 네이버 검색 API예제는 블로그를 비롯 전문자료까지 호출방법이 동일하므로 blog검색만 대표로 예제를 올렸습니다.
// 네이버 검색 Open API 예제 - 블로그 검색
// -> 이걸 영화로 바꿀라고 blog를 다 movie로 바꿈
var express = require('express');
var app = express();
app.set('view engine','ejs');
var fs = require('fs');
var client_id = 'Lqxd8LWDGMYTe9YWJPBb';
var client_secret = '9HrBpH8oy1';

app.get('/', function(req, res){
    fs.readFile('fb_login.ejs', function(error, data){
    res.writeHead(200, {'Content-Type' : 'text/html '});
    res.end(data);
  });
});

app.get('/imgs/megabox',function (req,res) {
    fs.readFile('mega.png', function (error, data) {
      res.writeHead(200, {'Content-Type' : 'text/html'});
      res.end(data);
    });
})

app.get('/main',function (req,res) {
  fs.readFile('main.ejs', function(error, data){
  res.writeHead(200, {'Content-Type' : 'text/html '});
  res.end(data);
});
})

app.get('/serach',function (req,res) {
  var query=  req.query;
  var genre = query['genre'];
  if(genre != ""){
    var code = get_genre_code(genre);
    get_info_using_genre(code,res);
  }
})

function get_genre_code(genre_str) {
 var genre_num;
  switch (genre_str) {
    case "Action":
    genre_num = 19;
    break;
    case "Romance":
    genre_num = 5;
    break;
    case "Horror":
    genre_num = 4;
    break;
    case "Drama":
    genre_num = 1;
    break;
    case "Fantasy":
    genre_num = 2;
    break;
    case "Adventure":
    genre_num = 6;
    break;
    case "Thriller":
    genre_num = 7;
    break;
    case "Comedy":
    genre_num = 11;
    break;
    case "War":
    genre_num = 14;
    break;

    default:

  }

  return genre_num;
}

function get_info_using_genre(code,res) {
  var test_search = "다크";
  var en = encodeURI(test_search);
  var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+'&genre=' + code; // json 결과
  var request = require('request');
  var options = {
      url: api_url,
      //headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
      headers: {'X-Naver-Client-Id':'Lqxd8LWDGMYTe9YWJPBb', 'X-Naver-Client-Secret': '9HrBpH8oy1'}
   };
  request.get(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var data = JSON.parse(body);
      var total = data['total'];
      var items = data['items'];

      console.log(data);
      for(var i=0; i < total; i++){
          res.render('movie_info',{title:items[i]['title']});
      }
      //res.render('movie_info',{title:items[1]['title']});
    }
  });
}

app.get('/search/movie', function (req, res) {
   var api_url = 'https://openapi.naver.com/v1/search/movie.json?query=%EC%A3%BC%EC%8B%9D&display=10&start=1&genre=1' + encodeURI(req.query.query); // json 결과
   var request = require('request');
   var options = {
       url: api_url,
       //headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
       headers: {'X-Naver-Client-Id':'Lqxd8LWDGMYTe9YWJPBb', 'X-Naver-Client-Secret': '9HrBpH8oy1'}
    };
   request.get(options, function (error, response, body) {
     if (!error && response.statusCode == 200) {
       var data = JSON.parse(body);
       var total = data['total'];
       var items = data['items'];
       res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
      // res.end(JSON.stringify(items));
       res.end(body);
     } else {
       res.status(response.statusCode).end();
       console.log('error = ' + response.statusCode);
     }
   });
 });


 app.listen(3000, function () {
   //console.log('http://127.0.0.1:3000/search/blog?query=검색어 app listening on port 3000!');
   console.log('http://127.0.0.1:3000/search/movie?query=검색어 app listening on port 3000!');
 });