Created
November 8, 2012 06:32
-
-
Save atdt/4037228 to your computer and use it in GitHub Desktop.
Wikipedia Telnet 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
// Wikipedia telnet server | |
// | |
// To install depenedncies: | |
// npm install request | |
// npm install cheerio | |
// | |
var net = require( 'net' ); | |
var cheerio = require('cheerio'); | |
var request = require('request'); | |
var fs = require('fs'); | |
var port = 1081; | |
var logo = fs.readFileSync( 'wiki-logo.txt' ); | |
var baseUrl = 'http://en.wikipedia.org/wiki/'; | |
var ps1 = '\n\n>>> '; | |
function recv( socket, data ) { | |
data = data.toString().replace( /(\r\n|\n|\r)/gm, '' ); | |
data = data.replace( /\s/g, '_' ); | |
if ( data === 'quit' ) { | |
socket.end( 'Bye!\n' ); | |
return; | |
} | |
request( { uri: baseUrl + data }, function ( error, response, body ) { | |
if ( body && body.length ) { | |
$ = cheerio.load( body ); | |
socket.write( $( '#mw-content-text p' ).first().text() + '\n' ); | |
} else { | |
socket.write( 'Error: ' + response.statusCode ); | |
} | |
socket.write( ps1 ); | |
} ); | |
} | |
net.createServer( function ( socket ) { | |
socket.write( logo ); | |
socket.write( ps1 ); | |
socket.on( 'data', recv.bind( null, socket ) ); | |
} ).listen( port ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment