Browse Source

Handles errors that occur when the song is not found

Dotos 8 years ago
parent
commit
a73db6786f
3 changed files with 95 additions and 23 deletions
  1. 1 1
      app.js
  2. 32 19
      routes/netease.js
  3. 62 3
      routes/v1.js

+ 1 - 1
app.js

@@ -46,7 +46,7 @@ app.use(function(req, res, next) {
      * 处理OPTIONS请求
      */
     if (req.method === 'OPTIONS') {
-        res.send(200);
+        res.sendStatus(200);
     } else next();
 });
 app.use(bodyParser.json());

+ 32 - 19
routes/netease.js

@@ -52,36 +52,49 @@ router.get('/:channel', function(req, res, next) {
     }
     util.requestServer(config).then(ret => {
         if (channel == 'song') {
-            config['path'] = links.song_url
-            config['params'] = {
-                "ids": [id],
-                "br": br,
-                "csrf_token": ""
-            }
-            util.requestServer(config).then(rt => {
-                let song = ret.songs[0]
-                song['mp3'] = rt.data[0]
-                res.send({
-                    data: song,
-                    status: {
-                        code: 200,
-                        msg: ''
-                    }
+            let songs = ret.songs
+            if (songs.length) {
+                config['path'] = links.song_url
+                config['params'] = {
+                    "ids": [id],
+                    "br": br,
+                    "csrf_token": ""
+                }
+                util.requestServer(config).then(rt => {
+                    let song = songs[0]
+                    song['mp3'] = rt.data[0]
+                    res.send({
+                        data: song,
+                        status: {
+                            code: 200,
+                            msg: ''
+                        }
+                    })
+                }).catch(ex => {
+                    console.log(ex)
+                    res.send({
+                        data: {},
+                        status: {
+                            code: -1,
+                            msg: 'something happend. Please checked your id or url'
+                        }
+                    })
                 })
-            }).catch(ex => {
-                console.log(ex)
+            } else {
                 res.send({
                     data: {},
                     status: {
                         code: -1,
-                        msg: 'something happend. Please checked your id or url'
+                        msg: 'sorry, no result, please changed song id.'
                     }
                 })
-            })
+            }
         } else {
             res.send(ret)
         }
+
     }).catch(err => {
+        console.log(err)
         res.send({
             data: {},
             status: {

+ 62 - 3
routes/v1.js

@@ -1,17 +1,76 @@
 var express = require('express');
-var request = require('superagent');
+var request = require('request');
 var router = express.Router();
 var disabledIP = require('../utils/disabledIP').list;
 var cookie = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36' };
 
+
 router.get('/*', function(req, res, next) {
-    getJSON(req, res, next);
+    // let link = req.query.url || '';
+    // let cb = req.query.cb || '';
+    // getJSON(req, res, next);
+    convert(req, res, next)
 });
 
 router.post('/*', function(req, res, next) {
-    getJSON(req, res, next);
+    // getJSON(req, res, next);
+    convert(req, res, next)
 });
 
+const convert = (req, res, next) => {
+    let method = req.method.toUpperCase();
+    let link = req.query.url || req.body.url;
+    let cb = req.query.callback || req.body.callback;
+    let params = req.body;
+    let config = {
+        method: method,
+        uri: link,
+        json: params,
+        headers: req.headers
+    }
+
+    var options = {
+        uri: 'https://www.googleapis.com/urlshortener/v1/url',
+        method: 'get',
+        json: { "longUrl": "http://www.google.com/" }
+    };
+    createServer(options).then(ret => {
+        console.log(ret)
+    }).catch(ex => {
+        console.log(ex)
+    })
+
+    // switch(method){
+    //     case 'GET':
+    //         let originalUrl = req.originalUrl;
+    //         originalUrl = originalUrl.replace(/\/v1\?/,'').replace('?','&');
+
+    //         break;
+    // }
+    // if (link) {
+    //     let
+    //     switch (method) {
+    //         case 'GET':
+    //             link +=
+    //     }
+
+    // }
+    //console.log(req.headers)
+
+}
+
+const createServer = (config) => {
+    return new Promise((resolve, reject) => {
+        request(config, (err, ret, body) => {
+            if (!err && ret.statusCode === 200) {
+                resolve(JSON.parse(body))
+            } else {
+                reject(err)
+            }
+        })
+    })
+}
+
 function getJSON(req, res, next) {
     var ip = req.headers['x-real-ip'] ? req.headers['x-real-ip'] : req.ip.replace(/::ffff:/, '');
     var host = req.hostname;