-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collect Email Activity from Mailersend
- Loading branch information
Showing
12 changed files
with
332 additions
and
128 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,6 +299,9 @@ def b64_json_env(key: str): | |
EMAIL_SUPPORT_ADDRESS = "[email protected]" | ||
EMAIL_SHOW_ENV_BANNER = env.bool("EMAIL_SHOW_ENV_BANNER", default=False) # type: ignore | ||
MAILERLITE_API_KEY = env("MAILERLITE_API_KEY", default="") # type: ignore | ||
MAILERSEND_API_TOKEN = env("MAILERSEND_API_TOKEN", default="") # type: ignore | ||
MAILERSEND_COLLECT_ACTIVITY = env.bool("MAILERSEND_COLLECT_ACTIVITY", default=False) # type: ignore | ||
MAILERSEND_DOMAIN_ID = env("MAILERSEND_DOMAIN_ID", default="") # type: ignore | ||
BREVO_API_KEY = env("BREVO_API_KEY", default="") # type: ignore | ||
SEND_BREVO_EMAILS = env.bool("SEND_BREVO_EMAILS", default=False) # type: ignore | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,63 @@ | ||
import asyncio | ||
from io import BytesIO | ||
# import asyncio | ||
# from io import BytesIO | ||
|
||
from PIL import Image | ||
from playwright.async_api import async_playwright | ||
# from PIL import Image | ||
# from playwright.async_api import async_playwright | ||
|
||
|
||
class ScreenshotRequestBody: | ||
def __init__(self, url: str, selector: str): | ||
self.url = url | ||
self.selector = selector | ||
# class ScreenshotRequestBody: | ||
# def __init__(self, url: str, selector: str): | ||
# self.url = url | ||
# self.selector = selector | ||
|
||
|
||
async def take_screenshot(request_body: ScreenshotRequestBody): | ||
if not request_body.url or not request_body.selector: | ||
raise Exception("Missing required fields: url or selector") | ||
# async def take_screenshot(request_body: ScreenshotRequestBody): | ||
# if not request_body.url or not request_body.selector: | ||
# raise Exception("Missing required fields: url or selector") | ||
|
||
browser = None | ||
page = None | ||
# browser = None | ||
# page = None | ||
|
||
try: | ||
async with async_playwright() as p: | ||
browser = await p.chromium.launch() | ||
page = await browser.new_page(device_scale_factor=2) | ||
# try: | ||
# async with async_playwright() as p: | ||
# browser = await p.chromium.launch() | ||
# page = await browser.new_page(device_scale_factor=2) | ||
|
||
await page.set_viewport_size({"width": 1280, "height": 800}) | ||
await page.goto(request_body.url, wait_until="networkidle") | ||
# await page.set_viewport_size({"width": 1280, "height": 800}) | ||
# await page.goto(request_body.url, wait_until="networkidle") | ||
|
||
element = await page.query_selector(request_body.selector) | ||
if not element: | ||
raise Exception("Element not found") | ||
# element = await page.query_selector(request_body.selector) | ||
# if not element: | ||
# raise Exception("Element not found") | ||
|
||
screenshot_buffer = await element.screenshot(type="png") | ||
image = Image.open(BytesIO(screenshot_buffer)) | ||
with BytesIO() as output_buffer: | ||
image.save(output_buffer, format="WEBP", quality=80) | ||
optimized_image_buffer = output_buffer.getvalue() | ||
# screenshot_buffer = await element.screenshot(type="png") | ||
# image = Image.open(BytesIO(screenshot_buffer)) | ||
# with BytesIO() as output_buffer: | ||
# image.save(output_buffer, format="WEBP", quality=80) | ||
# optimized_image_buffer = output_buffer.getvalue() | ||
|
||
return optimized_image_buffer | ||
# return optimized_image_buffer | ||
|
||
finally: | ||
if page: | ||
await page.close() | ||
if browser: | ||
await browser.close() | ||
# finally: | ||
# if page: | ||
# await page.close() | ||
# if browser: | ||
# await browser.close() | ||
|
||
|
||
async def main(): | ||
try: | ||
request_body = ScreenshotRequestBody( | ||
url="https://www.totem.org/circles/event/zdx624ofr/social", | ||
selector="[data-img]", | ||
) | ||
screenshot = await take_screenshot(request_body) | ||
with open("screenshot.webp", "wb") as f: | ||
f.write(screenshot) | ||
print("Screenshot saved as screenshot.webp") | ||
except Exception as e: | ||
print("Error:", str(e)) | ||
# async def main(): | ||
# try: | ||
# request_body = ScreenshotRequestBody( | ||
# url="https://www.totem.org/circles/event/zdx624ofr/social", | ||
# selector="[data-img]", | ||
# ) | ||
# screenshot = await take_screenshot(request_body) | ||
# with open("screenshot.webp", "wb") as f: | ||
# f.write(screenshot) | ||
# print("Screenshot saved as screenshot.webp") | ||
# except Exception as e: | ||
# print("Error:", str(e)) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) | ||
# if __name__ == "__main__": | ||
# asyncio.run(main()) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 5.1.4 on 2024-12-15 17:00 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('email', '0006_remove_emaillog_body_remove_emaillog_subscribed_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='EmailActivity', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('event', models.CharField(max_length=255)), | ||
('timestamp', models.DateTimeField()), | ||
('email', models.EmailField(max_length=254)), | ||
('status', models.CharField(blank=True, max_length=255, null=True)), | ||
('message_id', models.CharField(max_length=255, unique=True)), | ||
('metadata', models.JSONField(blank=True, null=True)), | ||
], | ||
), | ||
] |
52 changes: 52 additions & 0 deletions
52
totem/email/migrations/0008_rename_message_id_emailactivity_activity_id_and_more.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Generated by Django 5.1.4 on 2024-12-15 20:39 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('email', '0007_emailactivity'), | ||
] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name='emailactivity', | ||
old_name='message_id', | ||
new_name='activity_id', | ||
), | ||
migrations.RenameField( | ||
model_name='emailactivity', | ||
old_name='metadata', | ||
new_name='data', | ||
), | ||
migrations.RemoveField( | ||
model_name='emailactivity', | ||
name='event', | ||
), | ||
migrations.AddField( | ||
model_name='emailactivity', | ||
name='event_type', | ||
field=models.CharField(max_length=50, null=True), | ||
), | ||
migrations.AddField( | ||
model_name='emailactivity', | ||
name='subject', | ||
field=models.CharField(blank=True, max_length=255, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name='emailactivity', | ||
name='email', | ||
field=models.EmailField(blank=True, max_length=254, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name='emailactivity', | ||
name='id', | ||
field=models.AutoField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='emailactivity', | ||
name='status', | ||
field=models.CharField(blank=True, max_length=50, null=True), | ||
), | ||
] |
25 changes: 25 additions & 0 deletions
25
totem/email/migrations/0009_emailactivity_date_created_and_more.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 5.1.4 on 2024-12-15 21:04 | ||
|
||
import django.utils.timezone | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('email', '0008_rename_message_id_emailactivity_activity_id_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='emailactivity', | ||
name='date_created', | ||
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), | ||
preserve_default=False, | ||
), | ||
migrations.AddField( | ||
model_name='emailactivity', | ||
name='date_modified', | ||
field=models.DateTimeField(auto_now=True), | ||
), | ||
] |
Oops, something went wrong.