Skip to content

Commit

Permalink
Make it live
Browse files Browse the repository at this point in the history
  • Loading branch information
abetwothree committed Aug 5, 2022
1 parent fddff4e commit a3a4e02
Show file tree
Hide file tree
Showing 7 changed files with 1,542 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ dist

# TernJS port file
.tern-port

.env
24 changes: 23 additions & 1 deletion README.md
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.
1 change: 1 addition & 0 deletions env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APP_PORT=3000
70 changes: 70 additions & 0 deletions helpers/yt_api.js
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;
70 changes: 70 additions & 0 deletions index.js
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);
});
Loading

0 comments on commit a3a4e02

Please sign in to comment.