def __init__(self,myDir = None,files = None): if myDir != None: self.myDir = myDir if files != None: self.files = [f for f in files if fexist(self.myDir+f)] self.output = {} self.failed = [] self.status = '' self.resettime()
def get_access_token(path_to_tok='./translate.tok'): if fexist(path_to_tok): ftok = open(path_to_tok, 'r+') tokdata = pload(ftok) expiretime = tokdata['expires_in'] if (datetime.now() - expiretime) > timedelta(10,0): return tokdata['token'] else: ftok = open(path_to_tok, 'w') args = {'client_id':clientid,'client_secret':clientse,'scope':'http://api.microsofttranslator.com/','grant_type':'client_credentials'} enc_args = urllib.urlencode(args) req = urllib2.Request(tok_url,enc_args) response = urllib2.urlopen(req) data = json.load(response) timeandten = datetime.now() + timedelta(minutes = 10) pdump({'token':data['access_token'],'expires_in':timeandten}, ftok) return data['access_token']