Skip to content

Commit

Permalink
Merge pull request #2 from nahuelhds/feature/custom-text-on-tweet
Browse files Browse the repository at this point in the history
Custom tweet on every tweet.
  • Loading branch information
nahuelhds committed Apr 21, 2020
2 parents 4b71e4e + 60683d5 commit 12462f2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
63 changes: 57 additions & 6 deletions diffengine/__init__.py
Expand Up @@ -278,6 +278,18 @@ class Diff(BaseModel):
tweeted = DateTimeField(null=True)
blogged = DateTimeField(null=True)

@property
def url_changed(self):
return self.old.url != self.new.url

@property
def title_changed(self):
return self.old.title != self.new.title

@property
def summary_changed(self):
return self.old.summary != self.new.summary

@property
def html_path(self):
# use prime number to spread across directories
Expand Down Expand Up @@ -340,9 +352,10 @@ def _generate_diff_images(self):


def setup_logging():
verbose = config.get('verbose', False)
path = config.get('log', home_path('diffengine.log'))
logging.basicConfig(
level=logging.INFO,
level=logging.DEBUG if verbose else logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
filename=path,
filemode="a"
Expand Down Expand Up @@ -476,11 +489,7 @@ def tweet_diff(diff, token):
auth.set_access_token(token['access_token'], token['access_token_secret'])
twitter = tweepy.API(auth)

text = diff.new.title
if len(text) >= 225:
text = text[0:225] + "…"

text += " " + diff.old.archive_url + " ➜ " + diff.new.archive_url
text = build_text(diff, config['lang'])

try:
status = twitter.update_with_media(diff.thumbnail_path, status=text, in_reply_to_status_id=diff.old.tweet_status_id)
Expand All @@ -495,6 +504,48 @@ def tweet_diff(diff, token):
logging.error("unable to tweet: %s", e)


def build_text(diff, lang):
logging.debug("Building text for diff %s" % diff.new.title)
text = None

# Try build the text from i18n
if all (k in lang for k in ("change_in", "the_url", "the_title", "the_summary")):
logging.debug("Found all required lang terms!")
try:
return '%s\n%s' % (build_text_from_changes(lang, diff.url_changed, diff.title_changed, diff.summary_changed), diff.new.archive_url)
except Exception as e:
logging.error("Could not build text from lang", e)

logging.debug("Building default text")
# otherwise, build it as usual
if text is None:
text = diff.new.title
if len(text) >= 225:
text = text[0:225] + "…"
text += " " + diff.old.archive_url + " ➜ " + diff.new.archive_url

return text


def build_text_from_changes(lang, url_changed, title_changed, summary_changed):
changes = []
if url_changed:
changes.append(lang['the_url'])
if title_changed:
changes.append(lang['the_title'])
if summary_changed:
changes.append(lang['the_summary'])

if len(changes) > 1:
and_change = ' %s ' % lang['and']
last_change = changes.pop(len(changes) - 1)
else:
and_change = ''
last_change = ''

return '%s %s%s%s' % (lang['change_in'], ', '.join(changes), and_change, last_change)


def init(new_home, prompt=True):
global home
home = new_home
Expand Down
18 changes: 17 additions & 1 deletion test_diffengine.py
Expand Up @@ -13,7 +13,6 @@
init("test", prompt=False)

# the sequence of these tests is significant

def test_version():
assert setup.version in UA

Expand Down Expand Up @@ -112,3 +111,20 @@ def test_fingerprint():
assert _fingerprint("foo<br>bar") == "foobar"
assert _fingerprint("foo'bar") == "foobar"
assert _fingerprint("foo’bar") == "foobar"

def test_build_text_from_changes():
lang = {
'change_in': "Changes in",
'the_url': "the URL",
'the_title': "the title",
'and': "and",
'the_summary': "the summary"
}

assert build_text_from_changes(lang, True, False, False) == "Changes in the URL"
assert build_text_from_changes(lang, False, True, False) == "Changes in the title"
assert build_text_from_changes(lang, False, False, True) == "Changes in the summary"
assert build_text_from_changes(lang, True, True, False) == "Changes in the URL and the title"
assert build_text_from_changes(lang, False, True, True) == "Changes in the title and the summary"
assert build_text_from_changes(lang, True, False, True) == "Changes in the URL and the summary"
assert build_text_from_changes(lang, True, True, True) == "Changes in the URL, the title and the summary"

0 comments on commit 12462f2

Please sign in to comment.