<commit_before>var goog = require('googleapis')
var demons = goog.auth.OAuth2
var memoize = require('memoizee')
require('polyfill-promise')
var goog = require('googleapis')
var OAuth2 = goog.auth.OAuth2

var oauthClient = new OAuth2(process.env.GA_CLIENT_ID, process.env.GA_CLIENT_SECRET)
oauthClient.setCredentials({
  access_token: process.env.GA_ACCESS_TOKEN,
  refresh_token: process.env.GA_REFRESH_TOKEN
})

function getMonthlyUniques() {
    var d = new Date
    var firstOfMonth = d.getFullYear() +'-'+ pad(d.getMonth()+1, 2, 0) + '-01'

  return new Promise(function (resolve, reject) {
    goog.analytics({version: 'v3', auth: oauthClient}).data.ga.get({
      'start-date':firstOfMonth,
      'end-date':'today',
      'ids': process.env.GA_PROFILE_ID, 
      'metrics': 'ga:users'
    }, function (err, res) {
      if (err) {
        return reject(err)
      }
      return resolve(res.rows[0][0])
    })
  })
}
<commit_msg>Add manual token refresh logic<commit_after>var goog = require('googleapis')
var demons = goog.auth.OAuth2
var memoize = require('memoizee')
require('polyfill-promise')
var goog = require('googleapis')
var OAuth2 = goog.auth.OAuth2
goog.options({proxy: 'http://localhost:8888', strictSSL: false})
var oauthClient = new OAuth2(process.env.GA_CLIENT_ID, process.env.GA_CLIENT_SECRET)
oauthClient.setCredentials({
  access_token: process.env.GA_ACCESS_TOKEN,
  refresh_token: process.env.GA_REFRESH_TOKEN
})

function refreshAuth() {
  return new Promise(function (resolve, reject) {
    oauthClient.refreshAccessToken(function(err, tokens) {
      if (err) { return reject(err) }
      return resolve()
    });
  })
}

function getMonthlyUniques(retries) {
    var d = new Date
    var firstOfMonth = d.getFullYear() +'-'+ pad(d.getMonth()+1, 2, 0) + '-01'

  return new Promise(function (resolve, reject) {
    goog.analytics({version: 'v3', auth: oauthClient}).data.ga.get({
      'start-date':firstOfMonth,
      'end-date':'today',
      'ids': process.env.GA_PROFILE_ID, 
      'metrics': 'ga:users'
    }, function (err, res) {
      if (err) {
        if (err.code == 401 && !retries) {
          return refreshAuth().then(function () {
            return resolve(getMonthlyUniques(1))
          })
        }
        return reject(err)
      }
      return resolve(res.rows[0][0])
    })
  })
}

{'fraction_non_alphanumeric': 0.14940068493150685, 'fraction_numerical': 0.0136986301369863, 'mean_word_length': 3.5913555992141455, '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': '4895956', 'n_tokens_mistral': 821, 'n_tokens_neox': 806, 'n_words': 157}