@angeligareta/tfapl
v1.0.1
Published
Extension of the Egg languaging program using the YouTube Api.
Downloads
3
Readme
Final Programming Language Project of Ángel Igareta
Description
The aim of this project was to extend the EGG language that we had previously made with anything we wanted. In this final project, I wanted to try something different.
I have been always a lover of social media, more specific in YouTube, so I decided to try the YouTube API and let it surprise me. After several hours of investigation (due there is not much information) I decided to start experimenting with it and do a npm module explaining what I have learnt.
For not repeating the description of the module, I ask you to follow this link and see what I made: YouTube-API-Features
The next step was to adapt it to egg. To make it clean I decided to use registry pattern and change the egg npm module in order to export the topEnv hash, that contains some functions that we can use in egg language. That's what I made, first I made an main program that receives an egg language program and run it, but before I require a new file: the youtube-egg.js file, that will extend the egg behavior.
YouTube EGG
In this file, first of all I require the YouTube features module that I explained previously and the topEnv map that allow us to extend the egg behaviour. Besides I assign to all the functions that offer the egg YouTube module to an error function, beacuse, in order to use the module, the user first have to 'require' it, by writing 'use("youtube")'.
// Necessary function for using youtube api features module.
const startFunctionOnMode = require('@angeligareta/youtube-api-features');
const fs = require('fs');
const colors = require('colors');
const printTitle = (string) => console.log(colors.yellow(string));
// We require topEnv for adding the necessary functions in order yo use youtube module.
let {topEnv} = require('@angeligareta/egg');
// In case the youtube function has not been required, show error.
topEnv['showYouTubeHelp'] = showError;
topEnv['showComments'] = showError;
topEnv['showChannelInfo'] = showError;
topEnv['showChannelVideos'] = showError;
topEnv['searchVideos'] = showError;
topEnv['searchChannels'] = showError;
function showError() {
throw new TypeError("In order to use youtube module functions, you must 'use' the youtube module as follow: \n" +
"\t use(\"youtube\")");
}
After that, I add the function 'use' to the topEnv and assign the correct functions to the functions that offer the egg YouTube module. However, before we must check if the user has the client_secret.json in the working directory, due it's necessary for using the youtube api. If you want to test it I recommend you to follow this tutorial for Node.JS: Node.js QuickStart
// If the user writes 'use("youtube")' we assign the correct functions to the topEnv. However, before we check if the user has
// the client_secret.json in the working directory, due it's necessary for using the youtube api.
topEnv['use'] = function(...argumentArray) {
if (argumentArray.length !== 1) {
throw new TypeError('Use function must receive only one argument.');
}
else {
if (argumentArray[0].toLowerCase() === 'youtube') { // Update libraries
if (!fs.existsSync('client_secret.json')) { // Check if it has the necessary api key.
throw new TypeError('In order to use youtube module functions, you must have a client_secret.json file!');
}
console.log("YouTube Module charged! To show help use: showYouTubeHelp");
topEnv['showYouTubeHelp'] = showYouTubeHelp;
topEnv['showComments'] = showComments;
topEnv['showChannelInfo'] = showChannelInfo;
topEnv['showChannelVideos'] = showChannelVideos;
topEnv['searchVideos'] = searchVideos;
topEnv['searchChannels'] = searchChannels;
}
}
};
How to use
For using it the first requisite is to have the client_secret.json file that offer the youtube API. In order to do that follow this tutorial for Node.JS: Node.js QuickStart
After that you have a set of examples in the examples folder to try. You can enter by command line:
npm start
Or
node main.js examples/<example>.egg
Have fun!