Created
December 24, 2023 12:10
-
-
Save mrts/01f636c87c5a032046bd2a712b90964f to your computer and use it in GitHub Desktop.
Get customer emails from Merit using Merit API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hmac | |
import datetime | |
import json | |
from base64 import b64encode | |
from urllib.parse import urlencode | |
import requests | |
API_KEY = b'...' | |
API_ID = '...' | |
API_BASE_URL = 'https://aktiva.merit.ee/api/v1/' | |
API_GET_CUSTOMERS_URL = API_BASE_URL + 'getcustomers' | |
def calculate_signature(timestamp): | |
data_string = API_ID + timestamp | |
hash_obj = hmac.new(key=API_KEY, msg=data_string.encode('ascii'), digestmod='sha256') | |
return b64encode(hash_obj.digest()) | |
def get_query_params(): | |
timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S') | |
signature = calculate_signature(timestamp) | |
return { | |
'ApiId': API_ID, | |
'timestamp': timestamp, | |
'signature': signature, | |
} | |
def get_customer_emails_from_merit(): | |
query_params = get_query_params() | |
customers = requests.get(API_GET_CUSTOMERS_URL + '?' + urlencode(query_params)).text | |
return {c['Email'] for c in json.loads(customers) | |
if c['Contact'] != 'ARCHIVED' and c['Email'] is not None} | |
if __name__ == '__main__': | |
print(get_customer_emails_from_merit()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment