Skip to content

Commit

Permalink
Merge pull request #81 from andresfib/master
Browse files Browse the repository at this point in the history
Added documentation and better support for multiple mail addresses
  • Loading branch information
edsu authored Jun 25, 2020
2 parents 13a5ec1 + 1de4170 commit bffd2a6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 15 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ regular updates. And with the [readability] module, diffengine is able to
automatically extract the primary content of pages, without requiring special
parsing to remove boilerplate material. And like NYTDiff, instead of creating
another website for people to watch, diffengine pushes updates out to social
media where people are already, while also building a local database of diffs
media (via Twitter or email) where people are already, while also building a local database of diffs
that can be used for research purposes.

## Install
Expand All @@ -42,7 +42,9 @@ directory in my home directory, but you can use whatever location you want, you
just need to be able to write to it.

The first time you run diffengine it will prompt you to enter an RSS or Atom
feed URL to monitor and will authenticate with Twitter.
feed URL to monitor. You will the be asked to provide the credentials to
publish the diffs in social media.


```console
% diffengine /home/ed/.diffengine
Expand All @@ -65,6 +67,16 @@ What is your PIN: 8675309

Saved your configuration in /home/ed/.diffengine/config.yaml

Would you like to set up emailing edits with Sendgrid? [Y/n] y

Go to https://app.sendgrid.com/ and get an API key.

What is the API key? <API_KEY>

What email address is sending the email? <FROM_ADDRESS>

Who are the recipients of the emails? <RECEIVERS ADDRESSES_CSV>

Fetching initial set of entries.

Done!
Expand Down Expand Up @@ -140,11 +152,17 @@ If there are multiple feeds for an account, you can setup the `config.yml` like
twitter:
access_token: ACCESS_TOKEN
access_token_secret: ACCESS_TOKEN_SECRET
sendgrid:
sender: FROM_ADDRESS
recipients: TO_ADDRES1, TO_ADDRESS2
url: http://www.theglobeandmail.com/report-on-business/?service=rss
- name: The Globe and Mail - Opinion
twitter:
access_token: ACCESS_TOKEN
access_token_secret: ACCESS_TOKEN_SECRET
sendgrid:
sender: FROM_ADDRESS2
recipients: TO_ADDRES3, TO_ADDRESS4
url: http://www.theglobeandmail.com/opinion/?service=rss
- name: The Globe and Mail - News
twitter:
Expand All @@ -154,6 +172,8 @@ If there are multiple feeds for an account, you can setup the `config.yml` like
twitter:
consumer_key: CONSUMER_KEY
consumer_secret: CONSUMER_SECRET
sendgrid:
api_token: API_TOKEN
```

### Skip entry
Expand Down
6 changes: 3 additions & 3 deletions diffengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,16 +476,16 @@ def get_initial_config():
"access_token_secret": token[1],
}

answer = input("Would you like to set up emailing edits? [Y/n] ")
answer = input("Would you like to set up emailing edits with Sendgrid? [Y/n] ")
if answer.lower() == "y":
print("Go to https://app.sendgrid.com/ and get an API key.")
api_key = input("What is the API key? ")
sender = input("What email address is sending the email? ")
receivers = input("Who are receiving the emails? ")
recipients = input("Who are the recipients of the emails? ")

config["sendgrid"] = {"api_key": api_key}

config["feeds"][0]["sendgrid"] = {"sender": sender, "receivers": receivers}
config["feeds"][0]["sendgrid"] = {"sender": sender, "recipients": recipients}

print("Saved your configuration in %s/config.yaml" % home.rstrip("/"))
print("Fetching initial set of entries.")
Expand Down
26 changes: 18 additions & 8 deletions diffengine/sendgrid.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from datetime import datetime
from sendgrid import Mail, SendGridAPIClient
from sendgrid import Mail, Bcc, SendGridAPIClient

from diffengine.exceptions.sendgrid import (
AlreadyEmailedError,
Expand All @@ -13,22 +13,26 @@
class SendgridHandler:
api_token = None
sender = None
receivers = None
recipients = None

def __init__(self, config):

if not all(["api_token" in config, "sender" in config, "receivers" in config]):
if not all(["api_token" in config, "sender" in config, "recipients" in config]):
logging.warning(
"No global config found for sendgrid, expecting config set for each feed"
)

self.api_token = config.get("api_token")
self.sender = config.get("sender")
self.receivers = config.get("receivers")
self.recipients = self.build_recipients(config.get("recipients"))

def mailer(self, api_token):
return SendGridAPIClient(api_token)

def build_recipients(self, recipients):
if recipients:
return [x.strip() for x in recipients.split(",")]

def build_subject(self, diff):
return diff.old.title

Expand All @@ -47,18 +51,24 @@ def publish_diff(self, diff, feed_config):

api_token = feed_config.get("api_token", self.api_token)
sender = feed_config.get("sender", self.sender)
receivers = feed_config.get("receivers", self.receivers)
if not all([api_token, sender, receivers]):

recipients = None
if feed_config.get("recipients"):
recipients = self.build_recipients(feed_config.get("recipients"))
else:
recipients = self.recipients
if not all([api_token, sender, recipients]):
raise SendgridConfigNotFoundError

subject = self.build_subject(diff)
message = Mail(
from_email=sender,
to_emails=receivers,
subject=subject,
to_emails=recipients.pop(0),
html_content=self.build_html_body(diff),
)

if recipients:
message.bcc = recipients
try:
self.mailer(api_token).send(message)
diff.emailed = datetime.utcnow()
Expand Down
24 changes: 22 additions & 2 deletions test_diffengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def test_do_mail_if_entry_has_diff(self):
sendgrid_config = {
"api_token": "12345",
"sender": "[email protected]",
"receivers": "[email protected]",
"recipients": "[email protected]",
}
result = process_entry(entry, {"sendgrid": sendgrid_config}, None, sendgrid)

Expand Down Expand Up @@ -582,7 +582,7 @@ class SendgridHandlerTest(TestCase):
"sendgrid": {
"api_token": "12345",
"sender": "[email protected]",
"receivers": "[email protected]",
"recipients": "test@test.test, test2@test.test",
}
}

Expand Down Expand Up @@ -639,6 +639,26 @@ def test_raises_if_not_all_archive_urls_are_present(self):
"sendgrid.publish_diff raised AchiveUrlNotFoundError unexpectedly!"
)

def test_only_one_recipient(self):
config = {
"sendgrid": {
"api_token": "12345",
"sender": "[email protected]",
"recipients": "[email protected]",
}
}

sendgrid = SendgridHandler(config["sendgrid"])

diff = get_mocked_diff(False)
type(diff.old).archive_url = PropertyMock(return_value="http://test.url/old")
type(diff.new).archive_url = PropertyMock(return_value="http://test.url/new")

try:
sendgrid.publish_diff(diff, config["sendgrid"])
except Exception as e:
self.fail(e)


def get_mocked_diff(with_archive_urls=True):
old = MagicMock()
Expand Down

0 comments on commit bffd2a6

Please sign in to comment.