phantasmobile/app/listmonk/utils.py

104 lines
3.2 KiB
Python
Raw Normal View History

2023-09-23 12:38:28 +00:00
import logging
import requests
from django.conf import settings
2023-09-23 15:33:42 +00:00
from django.utils.timezone import datetime, timedelta
2023-09-23 12:38:28 +00:00
logger = logging.getLogger(__name__)
headers = {
'Content-Type': 'application/json;charset=utf-8'
}
2023-09-23 12:38:28 +00:00
2023-11-19 13:29:39 +00:00
def send_week(date):
2023-09-23 12:38:28 +00:00
from . import views
2023-09-23 12:58:41 +00:00
date = datetime.strptime(date, '%Y-%m-%d')
2023-11-26 14:04:56 +00:00
year, month, day = date.split('-')
2023-09-23 12:58:41 +00:00
monday = views.get_monday(date)
2023-09-23 12:38:28 +00:00
body = views.week(year, month, day)
2023-09-23 12:58:41 +00:00
name = 'weekly-digest-%s' % monday
subject = 'Phantas.ma weekly update %s' % views.format_week(monday)
2023-09-23 12:38:28 +00:00
return send_email(name, subject, body)
def send_email(name, subject, body):
'''
send_meail('weekly digest 2023-09-22', 'Phantas.ma weekly update Sep 23', body)
'''
if not settings.LISTMONK_LISTS:
return False
data = {
'name': name,
'subject': subject,
'lists': settings.LISTMONK_LISTS,
'content_type': 'html',
'type': 'regular',
'body': body
}
url = settings.LISTMONK_API + 'campaigns'
auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD)
r = requests.post(url, json=data, headers=headers, auth=auth).json()
list_id = r.get('data', {}).get('id')
if list_id and settings.LISTMONK_SEND:
url = settings.LISTMONK_API + 'campaigns/%s/status' % list_id
2023-09-23 12:38:28 +00:00
data = {"status": "running"}
r = requests.put(url, json=data, headers=headers, auth=auth).json()
return True
else:
return False
def email2name(email):
name = email.split("@")[0]
name = name.replace(".", " ")
name = " ".join([part.capitalize() for part in name.split(" ")])
return name
def add_email(email):
url = settings.LISTMONK_API + 'subscribers'
auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD)
exists = url + '?' + "list_id=&query=email='%s'&page=1&order_by=id&order=desc" % email
if not len(requests.get(exists, auth=auth).json()['data']['results']):
data = {
"email": email,
"name": email2name(email),
"status": "enabled",
"lists": settings.LISTMONK_LISTS,
}
r = requests.post(url, json=data, headers=headers, auth=auth).json()
if r.get("data", {}).get("id"):
return True
else:
2023-09-28 09:49:37 +00:00
logger.error("failed to add email to list %s => %s", email, r)
return False
return False
2023-09-29 11:20:09 +00:00
def delete_bounced_emails():
url = settings.LISTMONK_API + 'bounces'
auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD)
2023-10-01 07:07:48 +00:00
r = requests.get(url + '?per_page=10000', auth=auth).json()
2023-09-29 11:20:09 +00:00
ids = [sub['subscriber_id'] for sub in r['data']['results']]
url = settings.LISTMONK_API + 'subscribers'
for id in ids:
r = requests.delete(url + '?id=%s' % id, auth=auth).json()
if not r.get("data"):
print(id)
2023-10-01 07:07:48 +00:00
def delete_blacklisted_emails():
url = settings.LISTMONK_API + 'subscribers'
auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD)
r = requests.get(url + '?per_page=10000', auth=auth).json()
ids = [u['id'] for u in r['data']['results'] if u['status'] == 'blocklisted']
for id in ids:
r = requests.delete(url + '?id=%s' % id, auth=auth).json()
if not r.get("data"):
print(id)