From 7b36e8bf0c9b2e53c862fea90e9a6ba711f3ea3b Mon Sep 17 00:00:00 2001
From: Jonathan Cremin <jonathan@crem.in>
Date: Mon, 23 Oct 2017 22:47:14 +0100
Subject: [PATCH] Fix itunes matching

---
 docker-compose.yml           | 15 ++++++++-
 lib/services/itunes/index.js | 20 ++++++-----
 package-lock.json            | 65 ++++++++++++++++++++++++++----------
 package.json                 |  6 ++--
 routes/search.js             |  5 ++-
 yarn.lock                    | 18 ++++++----
 6 files changed, 91 insertions(+), 38 deletions(-)

diff --git a/docker-compose.yml b/docker-compose.yml
index 97ece12..e9735f3 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -21,8 +21,21 @@ services:
       - "3000:3000"
     command: yarn run watch-server
   worker:
-    extends: app
     build: ./
+    environment:
+      DEBUG: "combine.fm*"
+      VUE_ENV: server
+      DATABASE_URL:
+      REDIS_URL:
+      GOOGLE_EMAIL:
+      GOOGLE_PASSWORD:
+      XBOX_CLIENT_ID:
+      XBOX_CLIENT_SECRET:
+      YOUTUBE_KEY:
+      SPOTIFY_CLIENT_ID:
+      SPOTIFY_CLIENT_SECRET:
+    volumes:
+      - ./:/app:cached
     command: yarn run worker
     ports:
       - "3001:3000"
diff --git a/lib/services/itunes/index.js b/lib/services/itunes/index.js
index d2ab3e4..b983675 100644
--- a/lib/services/itunes/index.js
+++ b/lib/services/itunes/index.js
@@ -11,14 +11,19 @@ export function* parseUrl(url) {
   const matches = parsed.path.match(/[/]?([/]?[a-z]{2}?)?[/]+album[/]+([^/]+)[/]+([^?]+)/);
   const query = querystring.parse(parsed.query);
 
+  let itunesId = matches[3];
+
   if (matches) {
     let type = 'album';
-    let id = matches[3].substr(2);
-    if (query.i) {
-      type = 'track';
-      id = query.i;
+    if (matches[3].match(/^id/)) {
+      itunesId = matches[3].substr(2);
+      if (query.i) {
+        type = 'track';
+        itunesId = query.i;
+      }
     }
-    return yield module.exports.lookupId(id, type, matches[1] || 'us');
+
+    return yield module.exports.lookupId(itunesId, type, matches[1] || 'us');
   }
   throw new Error();
 }
@@ -30,15 +35,14 @@ export function* lookupId(possibleId, type, countrycode) {
     cc = possibleId.substr(0, 2);
     id = possibleId.substr(2);
   }
-
+  console.log(id)
   let path = `/lookup?id=${id}`;
   if (cc) {
     path = `/${cc}${path}`;
   }
-
   const response = yield request.get(apiRoot + path);
   let result = JSON.parse(response.text);
-
+  console.log(result);
   if (!result.results || result.resultCount === 0 || !result.results[0].collectionId) {
     const error = new Error('Not Found');
     error.status = 404;
diff --git a/package-lock.json b/package-lock.json
index b7c1b86..de1745a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -413,6 +413,11 @@
       "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-1.10.0.tgz",
       "integrity": "sha1-muuabF6IY4qtFx4Wf1kAq+JINdA="
     },
+    "bluebird": {
+      "version": "3.5.1",
+      "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz",
+      "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA=="
+    },
     "boxen": {
       "version": "1.2.1",
       "resolved": "https://registry.npmjs.org/boxen/-/boxen-1.2.1.tgz",
@@ -572,6 +577,24 @@
       "integrity": "sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk=",
       "dev": true
     },
+    "co-mocha": {
+      "version": "1.2.1",
+      "resolved": "https://registry.npmjs.org/co-mocha/-/co-mocha-1.2.1.tgz",
+      "integrity": "sha512-hlgDSWGXG1PuXiBTWUi+9ewhy0gII2hbNpS4lE0Esyr/eJlYx2xuIVV8ufEYxcBCzYOqiwcEZ7ck1CidWGNWkA==",
+      "dev": true,
+      "requires": {
+        "co": "4.6.0",
+        "is-generator": "1.0.3"
+      },
+      "dependencies": {
+        "co": {
+          "version": "4.6.0",
+          "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz",
+          "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=",
+          "dev": true
+        }
+      }
+    },
     "code-point-at": {
       "version": "1.1.0",
       "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz",
@@ -1919,13 +1942,6 @@
             }
           }
         },
-        "string_decoder": {
-          "version": "1.0.1",
-          "bundled": true,
-          "requires": {
-            "safe-buffer": "5.0.1"
-          }
-        },
         "string-width": {
           "version": "1.0.2",
           "bundled": true,
@@ -1935,6 +1951,13 @@
             "strip-ansi": "3.0.1"
           }
         },
+        "string_decoder": {
+          "version": "1.0.1",
+          "bundled": true,
+          "requires": {
+            "safe-buffer": "5.0.1"
+          }
+        },
         "stringstream": {
           "version": "0.0.5",
           "bundled": true,
@@ -2371,6 +2394,12 @@
       "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=",
       "dev": true
     },
+    "is-generator": {
+      "version": "1.0.3",
+      "resolved": "https://registry.npmjs.org/is-generator/-/is-generator-1.0.3.tgz",
+      "integrity": "sha1-wUwhBX7TbjKNuANHlmxpP4hjifM=",
+      "dev": true
+    },
     "is-glob": {
       "version": "2.0.1",
       "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-2.0.1.tgz",
@@ -3080,9 +3109,9 @@
       }
     },
     "parallelshell": {
-      "version": "3.0.1",
-      "resolved": "https://registry.npmjs.org/parallelshell/-/parallelshell-3.0.1.tgz",
-      "integrity": "sha512-xsSQGhJTFjbSW3WGsnSFTg1G17dcDU+gMH7bWxjAiy/3H579KPFbSp3fhkZ+jx2wcZuoav46nq+QVX0I0xPcyA==",
+      "version": "3.0.2",
+      "resolved": "https://registry.npmjs.org/parallelshell/-/parallelshell-3.0.2.tgz",
+      "integrity": "sha512-aW73W8tmYiFZtQi41pweV3WWT6o/EvSxAVQHbumOhN53H47OuWQwrRc11xQ2i44GFvR5AjtzhD92r8Kv9X+7Iw==",
       "dev": true
     },
     "parse-glob": {
@@ -3646,14 +3675,6 @@
         "duplexer": "0.1.1"
       }
     },
-    "string_decoder": {
-      "version": "1.0.3",
-      "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz",
-      "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==",
-      "requires": {
-        "safe-buffer": "5.1.1"
-      }
-    },
     "string-width": {
       "version": "2.1.1",
       "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz",
@@ -3681,6 +3702,14 @@
         }
       }
     },
+    "string_decoder": {
+      "version": "1.0.3",
+      "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz",
+      "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==",
+      "requires": {
+        "safe-buffer": "5.1.1"
+      }
+    },
     "strip-ansi": {
       "version": "3.0.1",
       "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz",
diff --git a/package.json b/package.json
index 5720052..09aedc1 100644
--- a/package.json
+++ b/package.json
@@ -50,7 +50,7 @@
     "babel-preset-es2015": "^6.0.0",
     "babel-preset-latest-minimal": "^1.1.2",
     "babel-register": "^6.26.0",
-    "bluebird": "^3.4.1",
+    "bluebird": "^3.5.1",
     "bulma": "^0.6.0",
     "co": "~4.6.0",
     "css-loader": "^0.28.1",
@@ -95,14 +95,14 @@
   "devDependencies": {
     "babel-plugin-transform-runtime": "^6.15.0",
     "babel-runtime": "^6.26.0",
-    "co-mocha": "^1.2.0",
+    "co-mocha": "^1.2.1",
     "eslint": "^4.7.0",
     "eslint-config-airbnb": "^15.1.0",
     "eslint-plugin-import": "^2.7.0",
     "istanbul": "^0.4.0",
     "mocha": "^3.5.3",
     "nodemon": "^1.12.1",
-    "parallelshell": "^3.0.1",
+    "parallelshell": "^3.0.2",
     "should": "^13.0.1"
   }
 }
diff --git a/routes/search.js b/routes/search.js
index 7400a01..7621c29 100644
--- a/routes/search.js
+++ b/routes/search.js
@@ -1,17 +1,20 @@
 import { parse } from 'url';
 import kue from 'kue';
+import debuglog from 'debug';
 
 import lookup from '../lib/lookup';
 import services from '../lib/services';
 import { find, create } from '../lib/share';
 
+const debug = debuglog('combine.fm:search');
+
 const queue = kue.createQueue({
   redis: process.env.REDIS_URL,
 });
 
 export default function* () {
   const url = parse(this.request.body.url);
-
+  debug(`URL ${url}`);
   this.assert(url.host, 400, { error: { message: 'You need to submit a url.' } });
 
   const music = yield lookup(this.request.body.url);
diff --git a/yarn.lock b/yarn.lock
index 57bfa47..f3d73fe 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -993,10 +993,14 @@ bluebird@^2.3.2:
   version "2.11.0"
   resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-2.11.0.tgz#534b9033c022c9579c56ba3b3e5a5caafbb650e1"
 
-bluebird@^3.0.5, bluebird@^3.1.1, bluebird@^3.3.4, bluebird@^3.4.1, bluebird@^3.4.6:
+bluebird@^3.0.5, bluebird@^3.1.1, bluebird@^3.3.4, bluebird@^3.4.6:
   version "3.5.0"
   resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c"
 
+bluebird@^3.5.1:
+  version "3.5.1"
+  resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.1.tgz#d9551f9de98f1fcda1e683d17ee91a0602ee2eb9"
+
 bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
   version "4.11.6"
   resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.6.tgz#53344adb14617a13f6e8dd2ce28905d1c0ba3215"
@@ -1344,9 +1348,9 @@ co-body@^5.1.0:
     raw-body "^2.2.0"
     type-is "^1.6.14"
 
-co-mocha@^1.2.0:
-  version "1.2.0"
-  resolved "https://registry.yarnpkg.com/co-mocha/-/co-mocha-1.2.0.tgz#d9be35a2a2d16f4b1b0e83f6973401ca4b6660af"
+co-mocha@^1.2.1:
+  version "1.2.1"
+  resolved "https://registry.yarnpkg.com/co-mocha/-/co-mocha-1.2.1.tgz#145b997d58acd56616b3de557ff24df3d7cd63ba"
   dependencies:
     co "^4.0.0"
     is-generator "^1.0.1"
@@ -4134,9 +4138,9 @@ pako@~0.2.0:
   version "0.2.9"
   resolved "https://registry.yarnpkg.com/pako/-/pako-0.2.9.tgz#f3f7522f4ef782348da8161bad9ecfd51bf83a75"
 
-parallelshell@^3.0.1:
-  version "3.0.1"
-  resolved "https://registry.yarnpkg.com/parallelshell/-/parallelshell-3.0.1.tgz#0587ce1535350543916d8f3d329fbfa4a0355f22"
+parallelshell@^3.0.2:
+  version "3.0.2"
+  resolved "https://registry.yarnpkg.com/parallelshell/-/parallelshell-3.0.2.tgz#fffc55aaa145bdd44b5381cf7fd5e521fc21aa7b"
 
 parse-asn1@^5.0.0:
   version "5.1.0"