def parse_address(self, s): misc.update({ 'type': 'address_api_account' }, {'$inc': { 'remaining': -1 }}) query_string = { "auth-id": self.account['key_id'], "auth-token": self.account['token'] } try: result = requests.post( os.getenv('ADDRESS_EXTRACTION_API'), params=query_string, data=s.encode("utf8")).json() line = result['addresses'][0]['line'] surrounding = ' '.join(s.split("\n")[max(line - 2, 0):line + 2]) if any([ x in surrounding.lower() for x in ['unsubscribe', 'receive these emails'] ]): return None api_output = result['addresses'][0]['api_output'][0] return api_output.get('delivery_line_1', '') + " " + api_output.get('last_line', '') except (IndexError, KeyError, ValueError): return None
def __init__(self): account = misc.find_one({'type': 'address_api_account'}) if not account: account = self.get_new_account() account.update({'type': 'address_api_account'}) misc.insert(account) elif account.get('remaining') < 150: account = self.get_new_account() account.update({'type': 'address_api_account'}) misc.update({'_id': account['_id']}, account) self.account = account