<commit_before>// External dependencies
var fs = require( 'fs' ),
	openurl = require( 'openurl' ),
	url = require( 'url' ),
	path = require( 'path' ),
	express = require( 'express' ),
	wpcomOAuth = require( 'wpcom-oauth' ),
	debug = require( 'debug' )( 'wpgs:auth' ),
	q = require( 'q' );

// Internal dependencies
var Site = require( './site' );

// Promisify functions
var readFile = q.nbind( fs.readFile, fs );

var authFilename = '.wpcomauth',
	token = '';

function loadTokenFromFile() {
	var promise = q.defer();
	authFilename = authFilename + '-' + Site.getUrl();
	readFile( authFilename, { encoding: 'utf8' } )
	.then( function( content ) {
		if ( ! content || ! content.replace ) {
			debug( 'invalid data found in auth file', content );
			throw 'Invalid data found in auth file';
		}
		token = content.replace( /\s/, '' );
		debug( 'auth token loaded' );
		promise.resolve();
	} )
	.catch( function() {
		debug( 'error reading wpcomauth token' );
		console.error( 'No WordPress.com auth token found in `' + authFilename + '`' );
		promise.reject();
	} );
	return promise.promise;
}

var server;

function getAuthFromServer() {
	var promise = q.defer();
	var settings = require( './settings.json' );
	var wpoauth = wpcomOAuth( settings );
	var pub = path.join( __dirname, '/public' );
	var app = express();
	app.use( express.static( pub ) );

	app.set( 'views', path.join( __dirname, 'views' ) );
	app.set( 'view engine', 'jade' );

	// Home
	app.get( '/', function( req, res ) {
		res.redirect( wpoauth.urlToConnect() + '&blog=' + Site.getUrl() );
	} );

	// OAuth response with code
	var redirectPath = url.parse( wpoauth.opts.url.redirect ).pathname;
	app.get( redirectPath, function( req, res ) {
		var code = req.query.code;
		res.redirect( '/get-token/' + code );
	} );

	// Access token fetch
	app.get( '/get-token/:code', function( req, res ) {
		wpoauth.code( req.params.code );
		wpoauth.requestAccessToken( function( err, data ) {
			if ( err ) {
				return res.render( 'error', err );
			}
			res.render( 'ok', data );
			debug( 'token received from server' );<commit_msg>Adjust how blog is included in requests<commit_after>// External dependencies
var fs = require( 'fs' ),
	openurl = require( 'openurl' ),
	url = require( 'url' ),
	path = require( 'path' ),
	express = require( 'express' ),
	wpcomOAuth = require( 'wpcom-oauth' ),
	debug = require( 'debug' )( 'wpgs:auth' ),
	q = require( 'q' );

// Internal dependencies
var Site = require( './site' );

// Promisify functions
var readFile = q.nbind( fs.readFile, fs );

var authFilename = '.wpcomauth',
	token = '';

function loadTokenFromFile() {
	var promise = q.defer();
	authFilename = authFilename + '-' + Site.getUrl();
	readFile( authFilename, { encoding: 'utf8' } )
	.then( function( content ) {
		if ( ! content || ! content.replace ) {
			debug( 'invalid data found in auth file', content );
			throw 'Invalid data found in auth file';
		}
		token = content.replace( /\s/, '' );
		debug( 'auth token loaded' );
		promise.resolve();
	} )
	.catch( function() {
		debug( 'error reading wpcomauth token' );
		console.error( 'No WordPress.com auth token found in `' + authFilename + '`' );
		promise.reject();
	} );
	return promise.promise;
}

var server;

function getAuthFromServer() {
	var promise = q.defer();
	var settings = require( './settings.json' );
	var wpoauth = wpcomOAuth( settings );
	var pub = path.join( __dirname, '/public' );
	var app = express();
	app.use( express.static( pub ) );

	app.set( 'views', path.join( __dirname, 'views' ) );
	app.set( 'view engine', 'jade' );

	// Home
	app.get( '/', function( req, res ) {
		res.redirect( wpoauth.urlToConnect( { blog: Site.getUrl() } ) );
	} );

	// OAuth response with code
	var redirectPath = url.parse( wpoauth.opts.url.redirect ).pathname;
	app.get( redirectPath, function( req, res ) {
		var code = req.query.code;
		res.redirect( '/get-token/' + code );
	} );

	// Access token fetch
	app.get( '/get-token/:code', function( req, res ) {
		wpoauth.code( req.params.code );
		wpoauth.requestAccessToken( function( err, data ) {
			if ( err ) {
				debug( 'error while reading response code', err, data );
				res.render( 'error', err );
				return;
			}
			res.render( 'ok', data );
			debug( 'token received from server' );
{'fraction_non_alphanumeric': 0.17461430575035064, 'fraction_numerical': 0.0004675081813931744, 'mean_word_length': 3.4900314795383003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5742244', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1453, 'n_words': 408}