Finally finished giant ES6 refactor
This commit is contained in:
parent
c6d48cc424
commit
03e2666958
39 changed files with 553 additions and 635 deletions
|
@ -1,20 +1,15 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var Promise = require('bluebird');
|
||||
var request = require('superagent');
|
||||
require('superagent-bluebird-promise');
|
||||
import { parse } from 'url';
|
||||
import querystring from 'querystring';
|
||||
import request from 'superagent';
|
||||
import 'superagent-bluebird-promise';
|
||||
|
||||
var credentials = {
|
||||
const credentials = {
|
||||
key: process.env.YOUTUBE_KEY,
|
||||
};
|
||||
|
||||
var apiRoot = "https://www.googleapis.com/freebase/v1/topic";
|
||||
const apiRoot = "https://www.googleapis.com/freebase/v1/topic";
|
||||
|
||||
module.exports.get = function(topic) {
|
||||
return request.get(apiRoot + topic + "?key=" + credentials.key).promise().then(function(res) {
|
||||
return res.body;
|
||||
})
|
||||
export function* get(topic) {
|
||||
const result = yield request.get(apiRoot + topic + "?key=" + credentials.key).promise();
|
||||
return result.body;
|
||||
}
|
||||
|
||||
|
||||
module.exports.get("/m/0dwcrm_");
|
|
@ -1,139 +1,124 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var freebase = require('./freebase');
|
||||
var querystring = require('querystring');
|
||||
var moment = require('moment');
|
||||
var Promise = require('bluebird');
|
||||
var request = require('superagent');
|
||||
require('superagent-bluebird-promise');
|
||||
import { parse } from 'url';
|
||||
import querystring from 'querystring';
|
||||
import moment from 'moment';
|
||||
import request from 'superagent';
|
||||
import 'superagent-bluebird-promise';
|
||||
import { match as urlMatch } from './url';
|
||||
import freebase from './freebase';
|
||||
|
||||
module.exports.id = "youtube";
|
||||
module.exports.id = 'youtube';
|
||||
|
||||
if (!process.env.YOUTUBE_KEY) {
|
||||
console.warn("YOUTUBE_KEY environment variable not found, deactivating Youtube.");
|
||||
} else {
|
||||
console.warn('YOUTUBE_KEY environment variable not found, deactivating Youtube.');
|
||||
}
|
||||
|
||||
var credentials = {
|
||||
const credentials = {
|
||||
key: process.env.YOUTUBE_KEY,
|
||||
};
|
||||
|
||||
var apiRoot = "https://www.googleapis.com/youtube/v3";
|
||||
const apiRoot = 'https://www.googleapis.com/youtube/v3';
|
||||
|
||||
module.exports.match = require('./url').match;
|
||||
export const match = urlMatch;
|
||||
|
||||
export function parseUrl(url) {
|
||||
const parsed = parse(url);
|
||||
const query = querystring.parse(parsed.query);
|
||||
let id = query.v;
|
||||
|
||||
module.exports.parseUrl = function(url) {
|
||||
var parsed = parse(url);
|
||||
var query = querystring.parse(parsed.query);
|
||||
var id = query.v;
|
||||
|
||||
if (!id) {
|
||||
id = parsed.path.substr(1);
|
||||
if (!id) {
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
return module.exports.lookupId(id, "track");
|
||||
return lookupId(id, 'track');
|
||||
}
|
||||
|
||||
module.exports.lookupId = function(id, type) {
|
||||
|
||||
var path = "/videos?part=snippet%2CtopicDetails%2CcontentDetails&id=" + id + "&key=" + credentials.key;
|
||||
|
||||
return request.get(apiRoot + path).promise().then(function(res) {
|
||||
var item = res.body.items[0];
|
||||
if (item.topicDetails.topicIds) {
|
||||
var promises = [];
|
||||
var match = {
|
||||
id: id,
|
||||
service: "youtube",
|
||||
name: item.snippet.title,
|
||||
type: "track",
|
||||
album: {name: ""},
|
||||
streamUrl: "https://youtu.be/" + id,
|
||||
purchaseUrl: null,
|
||||
artwork: {
|
||||
small: item.snippet.thumbnails.medium.url,
|
||||
large: item.snippet.thumbnails.high.url,
|
||||
}
|
||||
};
|
||||
item.topicDetails.topicIds.forEach(function(topicId) {
|
||||
promises.push(freebase.get(topicId).then(function(topic) {
|
||||
if (topic.property["/type/object/type"].values.some(function(value) {
|
||||
return value.text == "Musical Artist";
|
||||
})) {
|
||||
match.artist = {name: topic.property["/type/object/name"].values[0].text};
|
||||
} else if (topic.property["/type/object/type"].values.some(function(value) {
|
||||
return value.text == "Musical Recording";
|
||||
})) {
|
||||
//if (moment.duration(item.contentDetails.duration).asSeconds() < 900) {
|
||||
match.name = topic.property["/type/object/name"].values[0].text;
|
||||
if (topic.property["/music/recording/releases"]) {
|
||||
match.type = "album";
|
||||
match.album.name = topic.property["/music/recording/releases"].values[0].text;
|
||||
}
|
||||
//}
|
||||
} else if (topic.property["/type/object/type"].values.some(function(value) {
|
||||
return value.text == "Musical Album";
|
||||
})) {
|
||||
match.name = topic.property["/type/object/name"].values[0].text;
|
||||
match.type = "album";
|
||||
}
|
||||
}, function(err) {
|
||||
console.log(err)
|
||||
}));
|
||||
});
|
||||
return Promise.all(promises).then(function() {
|
||||
return match;
|
||||
}, function(err) {
|
||||
console.log(err)
|
||||
return {service: "youtube"};
|
||||
});
|
||||
} else {
|
||||
return {service: "youtube"};
|
||||
export function* lookupId(id, type) {
|
||||
|
||||
const path = '/videos?part=snippet%2CtopicDetails%2CcontentDetails&id=' + id + '&key=' + credentials.key;
|
||||
|
||||
const result = yield request.get(apiRoot + path).promise();
|
||||
const item = res.body.items[0];
|
||||
if (!item.topicDetails.topicIds) {
|
||||
return {service: 'youtube'};
|
||||
}
|
||||
|
||||
const promises = [];
|
||||
const match = {
|
||||
id: id,
|
||||
service: 'youtube',
|
||||
name: item.snippet.title,
|
||||
type: 'track',
|
||||
album: {name: ''},
|
||||
streamUrl: 'https://youtu.be/' + id,
|
||||
purchaseUrl: null,
|
||||
artwork: {
|
||||
small: item.snippet.thumbnails.medium.url,
|
||||
large: item.snippet.thumbnails.high.url,
|
||||
}
|
||||
}, function(err) {
|
||||
console.log(err)
|
||||
return {service: "youtube"};
|
||||
});
|
||||
};
|
||||
|
||||
for (let topic of yield freebase.get(topicId)) {
|
||||
const musicalArtist = topic.property['/type/object/type'].values.some((value) => {
|
||||
return value.text == 'Musical Artist';
|
||||
});
|
||||
|
||||
const musicalRecording = topic.property['/type/object/type'].values.some(function(value) {
|
||||
return value.text == 'Musical Recording';
|
||||
});
|
||||
|
||||
const musicalAlbum = topic.property['/type/object/type'].values.some(function(value) {
|
||||
return value.text == 'Musical Album';
|
||||
})
|
||||
|
||||
if (musicalArtist) {
|
||||
match.artist = {name: topic.property['/type/object/name'].values[0].text};
|
||||
} else if (musicalRecording) {
|
||||
match.name = topic.property['/type/object/name'].values[0].text;
|
||||
if (topic.property['/music/recording/releases']) {
|
||||
match.type = 'album';
|
||||
match.album.name = topic.property['/music/recording/releases'].values[0].text;
|
||||
}
|
||||
} else if (musicalAlbum) {
|
||||
match.name = topic.property['/type/object/name'].values[0].text;
|
||||
match.type = 'album';
|
||||
}
|
||||
}
|
||||
return match;
|
||||
};
|
||||
|
||||
module.exports.search = function(data) {
|
||||
var query, album;
|
||||
var type = data.type;
|
||||
export function* search(data) {
|
||||
let query, album;
|
||||
const type = data.type;
|
||||
|
||||
if (type == "album") {
|
||||
query = data.artist.name + " " + data.name;
|
||||
if (type == 'album') {
|
||||
query = data.artist.name + ' ' + data.name;
|
||||
album = data.name;
|
||||
} else if (type == "track") {
|
||||
query = data.artist.name + " " + data.name;
|
||||
} else if (type == 'track') {
|
||||
query = data.artist.name + ' ' + data.name;
|
||||
album = data.album.name
|
||||
}
|
||||
|
||||
var path = "/search?part=snippet&q=" + encodeURIComponent(query) + "&type=video&videoCaption=any&videoCategoryId=10&key=" + credentials.key;
|
||||
const path = '/search?part=snippet&q=' + encodeURIComponent(query) + '&type=video&videoCaption=any&videoCategoryId=10&key=' + credentials.key;
|
||||
|
||||
return request.get(apiRoot + path).promise().then(function(res) {
|
||||
var result = res.body.items[0];
|
||||
const result = yield request.get(apiRoot + path).promise();
|
||||
const item = result.body.items[0];
|
||||
|
||||
if (!result) {
|
||||
return {service:"youtube", type: "video"};
|
||||
} else {
|
||||
return {
|
||||
service: "youtube",
|
||||
type: "video",
|
||||
id: result.id.videoId,
|
||||
name: result.snippet.title,
|
||||
streamUrl: "https://www.youtube.com/watch?v=" + result.id.videoId,
|
||||
purchaseUrl: null,
|
||||
artwork: {
|
||||
small: result.snippet.thumbnails.medium.url,
|
||||
large: result.snippet.thumbnails.high.url,
|
||||
}
|
||||
};
|
||||
}
|
||||
}, function(err) {
|
||||
console.log(err)
|
||||
return {service: "youtube"};
|
||||
});
|
||||
if (!item) {
|
||||
return {service:'youtube', type: 'video'};
|
||||
} else {
|
||||
return {
|
||||
service: 'youtube',
|
||||
type: 'video',
|
||||
id: item.id.videoId,
|
||||
name: item.snippet.title,
|
||||
streamUrl: 'https://www.youtube.com/watch?v=' + item.id.videoId,
|
||||
purchaseUrl: null,
|
||||
artwork: {
|
||||
small: item.snippet.thumbnails.medium.url,
|
||||
large: item.snippet.thumbnails.high.url,
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
"use strict";
|
||||
var parse = require("url").parse;
|
||||
var querystring = require('querystring');
|
||||
import { parse } from 'url';
|
||||
import querystring from 'querystring';
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
var parsed = parse(url);
|
||||
|
||||
export function* match(url, type) {
|
||||
const parsed = parse(url);
|
||||
if (parsed.host.match(/youtu\.be$/)) {
|
||||
return true;
|
||||
} else if (parsed.host.match(/youtube\.com$/)) {
|
||||
var query = querystring.parse(parsed.query);
|
||||
const query = querystring.parse(parsed.query);
|
||||
return !!query.v;
|
||||
}
|
||||
return false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue