postmark-unsub-listmonk/postmark-unsub-python.py

37 lines
882 B
Python
Raw Normal View History

2023-12-04 19:29:00 +00:00
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import os
import pandas as pd
import requests
import json
import dotenv
# In[ ]:
dotenv.load_dotenv()
# In[ ]:
suppr_data = pd.read_json(os.getenv("POSTMARK_SUPPRESSION_JSON"))
# In[ ]:
username = os.getenv("LISTMONK_USERNAME")
password = os.getenv("LISTMONK_PASSWORD")
listmonk_api_url = os.getenv("LISTMONK_API_URL")
# In[ ]:
subscribers = requests.get(f"{listmonk_api_url}/subscribers?page=1&per_page=all", auth=(username, password))
# In[ ]:
subsc_data = pd.DataFrame(json.loads(subscribers.content.decode("utf-8"))['data']['results'])
# In[ ]:
id_a_supprimer = list(suppr_data.merge(subsc_data, how="inner", left_on="email_address", right_on="email")["id"])
# In[ ]:
for id in id_a_supprimer:
res = requests.put(f"{listmonk_api_url}/subscribers/{id}/blocklist", auth=(username, password))
print(res.status_code)