Skip to content

Instantly share code, notes, and snippets.

@ceberous
Last active May 31, 2018 22:11
Show Gist options
  • Select an option

  • Save ceberous/a808323df9021c45c0d989b081e074f8 to your computer and use it in GitHub Desktop.

Select an option

Save ceberous/a808323df9021c45c0d989b081e074f8 to your computer and use it in GitHub Desktop.
Downloads Twitch and BTTV Emote 1x Set
const cp = require( "child_process" );
function get_node_global_path() {
try {
const output = cp.spawnSync( "npm" , [ "root" , "-g" ] , { encoding : "utf8" } );
return output.stdout.trim();
}
catch( error ) { console.log( error ); process.exit( 1 ); }
}
const NodeGlobalPath = get_node_global_path();
const path = require( "path" );
const process = require( "process" );
const fs = require( "fs" );
const request = require( path.join( NodeGlobalPath , "request" ) );
const cheerio = require( path.join( NodeGlobalPath , "cheerio" ) );
const DownloadProgress = require( path.join( NodeGlobalPath , "download-progress" ) );
const CurrentScriptPath = process.cwd();
var DefaultEmoteFolderPath = path.join( CurrentScriptPath , "default" );
!fs.existsSync( DefaultEmoteFolderPath ) && fs.mkdirSync( DefaultEmoteFolderPath );
DefaultEmoteFolderPath = path.join( DefaultEmoteFolderPath , "1" );
!fs.existsSync( DefaultEmoteFolderPath ) && fs.mkdirSync( DefaultEmoteFolderPath );
var BttvEmoteFolderPath = path.join( CurrentScriptPath , "bttv" );
!fs.existsSync( BttvEmoteFolderPath ) && fs.mkdirSync( BttvEmoteFolderPath );
BttvEmoteFolderPath = path.join( BttvEmoteFolderPath , "1" );
!fs.existsSync( BttvEmoteFolderPath ) && fs.mkdirSync( BttvEmoteFolderPath );
const default_emotes_url = "https://www.twitchemotes.com";
function get_default_links() {
return new Promise( function( resolve , reject ) {
try {
var wResults = [];
request( default_emotes_url , function ( err , response , body ) {
if ( err ) { console.log( err ); reject( err ); return; }
try { var $ = cheerio.load( body ); }
catch(err) { reject( "cheerio load failed" ); return; }
$( "a.emote-name" ).each( function () {
var child = $( this ).children( "img" )[ 0 ];
var alt_txt = $( child ).attr( "data-regex" );
var src_url = $( child ).attr( "src" );
wResults.push( { url: src_url , dest: path.join( DefaultEmoteFolderPath , alt_txt + ".png" ) } );
});
resolve( wResults );
});
}
catch( error ) { console.log( error ); reject( error ); }
});
}
const bttv_cdn_b1 = "https://cdn.betterttv.net/emote/";
const bttv_cdn_b2 = "/1x";
const bttv_emotes_url = "https://api.betterttv.net/2/emotes";
function get_bttv_links() {
return new Promise( function( resolve , reject ) {
try {
request( bttv_emotes_url , function ( err , response , body ) {
if ( err ) { console.log( err ); reject( err ); return; }
var json = null;
try { json = JSON.parse( body ); }
catch(err) { reject( "not json ??" ); return; }
var final_results = [];
for ( var i = 0; i < json.emotes.length; ++i ) {
final_results.push({
url: bttv_cdn_b1 + json.emotes[ i ][ "id" ] + bttv_cdn_b2 ,
dest: path.join( BttvEmoteFolderPath , json.emotes[ i ][ "code" ] + "." + json.emotes[ i ][ "imageType" ] )
});
}
resolve( final_results );
});
}
catch( error ) { console.log( error ); reject( error ); }
});
}
function download_all( wItems ) {
return new Promise( async function( resolve , reject ) {
try {
var download = DownloadProgress( wItems , {} );
download.get( function ( err ) {
if ( err ) { reject( err ); return; }
console.log( "DONE" );
resolve();
});
}
catch( error ) { console.log( error ); reject( error ); }
});
}
( async ()=> {
const default_emotes = await get_default_links();
const bttv_emotes = await get_bttv_links();
const all_emotes = default_emotes.concat( bttv_emotes );
await download_all( all_emotes );
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment