<commit_before>#!/usr/bin/env python
import httplib2
import os
import string
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage


service_dict = {
    'service': None
}

def _initializeService(initializing=None):
    SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
    APPLICATION_NAME = 'PYGS - Python for Google Sheets'
    
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    client_secret_dir = os.path.join(home_dir, 'clientsecrets')
    
    if not os.path.exists(client_secret_dir):
        os.makedirs(client_secret_dir)
        
    CLIENT_SECRET_FILE = os.path.join(client_secret_dir, 'client_secret.json')

    
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    
    credential_path = os.path.join(credential_dir,'sheets.googleapis.com-pygs-api-auth.json')

    store = Storage(credential_path)
    
    credentials = store.get()
    
    if not credentials or credentials.invalid:
        print "Credentials need to be created. Please allow access on the next screen and then re-run the command."
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        flags = tools.argparser.parse_args(args=[])
        credentials = tools.run_flow(flow, store, flags)
        print('Storing credentials to ' + credential_path)

    http = credentials.authorize(httplib2.Http())
    discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?version=v4')
    
    if initializing:
        service_dict['service'] = discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discoveryUrl)
    else:
        return discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discoveryUrl)
    

def _getService():
    if service_dict['service'] == None:
        service_dict['service'] = _initializeService()
        return service_dict['service']
    else:
        return service_dict['service']<commit_msg>Getting a connection reset by peer every once in a while. It may be because of a global variable declaration error or if the connection expires. I set it to rebuild the service every 30 minutes, but only if the request comes in. So if a request is made and the old service is from greater than 30 minutes ago, it re-initializes. Hoping this will fix it.<commit_after>#!/usr/bin/env python
import httplib2
import os
import string
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import datetime


service_dict = {
    'service': None,
    'last_updated': None
}

def _initializeService(initializing=None):
    global service_dict
    SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
    APPLICATION_NAME = 'PYGS - Python for Google Sheets'
    
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    client_secret_dir = os.path.join(home_dir, 'clientsecrets')
    
    if not os.path.exists(client_secret_dir):
        os.makedirs(client_secret_dir)
        
    CLIENT_SECRET_FILE = os.path.join(client_secret_dir, 'client_secret.json')

    
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    
    credential_path = os.path.join(credential_dir,'sheets.googleapis.com-pygs-api-auth.json')

    store = Storage(credential_path)
    
    credentials = store.get()
    
    if not credentials or credentials.invalid:
        print "Credentials need to be created. Please allow access on the next screen and then re-run the command."
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        flags = tools.argparser.parse_args(args=[])
        credentials = tools.run_flow(flow, store, flags)
        print('Storing credentials to ' + credential_path)

    http = credentials.authorize(httplib2.Http())
    discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?version=v4')
    
    if initializing:
        service_dict['service'] = discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discoveryUrl)
        service_dict['last_updated'] = datetime.datetime.now()
    else:
        return discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discoveryUrl)
    

def _getService():
    global service_dict
    #get a new service every 30 minutes
    outdated = False
    if datetime.datetime.now() > service_dict['last_updated'] +  datetime.timedelta(minutes=1):
        outdated = True

    if service_dict['service'] == None or outdated:
        service_dict['service'] = _initializeService()
        return service_dict['service']
    else:
        return service_dict['service']
{'fraction_non_alphanumeric': 0.09810456154967714, 'fraction_numerical': 0.0047906686107061025, 'mean_word_length': 3.626204238921002, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '784582', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1427, 'n_words': 350}