-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fddff4e
commit a3a4e02
Showing
7 changed files
with
1,542 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,5 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,24 @@ | ||
# youtube-music-rest-api-microservice | ||
Wrapper around the YouTube Music API package to create a REST API microservice | ||
Wrapper around the YouTube Music API package to create a REST API microservice using Express. | ||
|
||
|
||
## Using | ||
|
||
Clone the repo locally do the following steps: | ||
|
||
Copy the env.example into another file called .env along with the contents. | ||
|
||
At the root of the project in a command line window run these two commands | ||
|
||
* npm install | ||
* npm run serve | ||
|
||
That should start the server to the port chose in the .env file. | ||
|
||
Assuming your port is 3000, if you go to `localhost:3000` you should see it say "Hello World". | ||
|
||
That's it! You can then use the other endpoints to get YouTube Music API data from the unofficial API. | ||
|
||
You can run this locally or deploy to your favorite hosting provider. | ||
|
||
Highly suggest using `pm2` to run this service on a server. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
APP_PORT=3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const YoutubeMusicApi = require('youtube-music-api') | ||
|
||
async function initialize(){ | ||
const api = new YoutubeMusicApi() | ||
const info = await api.initalize() // Retrieves Innertube Config | ||
console.log('YT Info', info); | ||
return api; | ||
} | ||
|
||
class YTMusicAPI{ | ||
constructor(){} | ||
async init(){ | ||
this.api = await initialize(); | ||
} | ||
async getSearchSuggestions(request){ | ||
try{ | ||
return await this.api.getSearchSuggestions(request.query.query); | ||
}catch(e){ | ||
console.log(e); | ||
return {error: e.message}; | ||
} | ||
} | ||
async search(request){ | ||
try{ | ||
|
||
let category = null; | ||
if(request.query.category){ | ||
category = request.query.category; | ||
} | ||
|
||
return await this.api.search(request.query.query, category); | ||
}catch(e){ | ||
console.log(e); | ||
return {error: e.message}; | ||
} | ||
} | ||
async getSong(request){ | ||
try{ | ||
return await this.api.getSong(request.params.id); | ||
}catch(e){ | ||
console.log(e); | ||
return {error: e.message, e}; | ||
} | ||
} | ||
async getArtist(request){ | ||
try{ | ||
return await this.api.getArtist(request.params.id); | ||
}catch(e){ | ||
console.log(e); | ||
return {error: e.message}; | ||
} | ||
} | ||
async getAlbum(request){ | ||
try{ | ||
return await this.api.getAlbum(request.params.id); | ||
}catch(e){ | ||
console.log(e); | ||
return {error: e.message}; | ||
} | ||
} | ||
async getPlaylist(request){ | ||
try{ | ||
return await this.api.getPlaylist(request.params.id); | ||
}catch(e){ | ||
return {error: e.message}; | ||
} | ||
} | ||
} | ||
|
||
module.exports = YTMusicAPI; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// importing the dependencies | ||
require('dotenv').config(); | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const cors = require('cors'); | ||
const helmet = require('helmet'); | ||
const morgan = require('morgan'); | ||
const root = require('rootrequire'); | ||
const YTMusicAPI = require(root+'/helpers/yt_api'); | ||
const yt_api = new YTMusicAPI(); | ||
|
||
// function to initialize the yt_api on every api request | ||
async function init_api(req, res, next){ | ||
await yt_api.init(); | ||
next(); | ||
}; | ||
|
||
// defining the Express app | ||
const app = express(); | ||
|
||
// call on every request to keep the yt_api fresh | ||
app.use(init_api); | ||
|
||
// adding Helmet to enhance your API's security | ||
app.use(helmet()); | ||
|
||
// using bodyParser to parse JSON bodies into JS objects | ||
app.use(bodyParser.json()); | ||
|
||
// enabling CORS for all requests | ||
app.use(cors()); | ||
|
||
// adding morgan to log HTTP requests | ||
app.use(morgan('combined')); | ||
|
||
|
||
// Routes | ||
app.get('/', async (req, res) => { | ||
res.send('Hello World!'); | ||
}); | ||
|
||
app.get('/search', async (req, res) => { | ||
const results = await yt_api.search(req); | ||
res.send(results); | ||
}); | ||
|
||
app.get('/song/:id', async (req, res) => { | ||
const results = await yt_api.getSong(req); | ||
res.send(results); | ||
}); | ||
|
||
app.get('/album/:id', async (req, res) => { | ||
const results = await yt_api.getAlbum(req); | ||
res.send(results); | ||
}); | ||
|
||
app.get('/playlist/:id', async (req, res) => { | ||
const results = await yt_api.getPlaylist(req); | ||
res.send(results); | ||
}); | ||
|
||
app.get('/artist/:id', async (req, res) => { | ||
const results = await yt_api.getArtist(req); | ||
res.send(results); | ||
}); | ||
|
||
// starting the server | ||
app.listen(process.env.APP_PORT, () => { | ||
console.log('listening on port ' + process.env.APP_PORT); | ||
}); |
Oops, something went wrong.