Skip to content

Commit 17ce33d

Browse files
committed
URL: Do not send "OLD!" messages whenever the link is quite fresh
1 parent bcfb9c7 commit 17ce33d

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

url/__init__.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import re
3+
from datetime import timedelta, datetime
34

45
import sqlalchemy.exc
56
from pipobot.lib.known_users import KnownUser
@@ -18,7 +19,10 @@
1819

1920

2021
class CmdUrl(ListenModule):
21-
_config = (("repost", bool, False), ("repost_ignore", list, []))
22+
# repost_ignore_delay is the number of seconds to wait between two
23+
# submissions of the same url before prompting that this is an old message.
24+
_config = (("repost", bool, False), ("repost_ignore", list, []),
25+
("repost_ignore_delay", int, 86400))
2226

2327
def __init__(self, bot):
2428
desc = "Extracting title of page from URL"
@@ -31,41 +35,56 @@ def answer(self, sender, message):
3135
else:
3236
urls = set(URLS_RE.findall(message))
3337

38+
# We cannot iter by number on sets, because of their intrinsic structure
39+
urls = list(urls)
40+
41+
title_page = self.get_title(urls)
42+
3443
try:
35-
repost_msg = self.check_repost(sender, urls)
44+
repost_msg = self.check_repost(sender, urls, title_page)
3645
except sqlalchemy.exc.OperationalError:
3746
self.bot.session.rollback()
3847
repost_msg = []
3948
except sqlalchemy.exc.InvalidRequestError:
4049
repost_msg = []
4150

42-
title_page = self.get_title(urls)
4351
send = repost_msg + title_page
4452
return None if send == [] else "\n".join(send)
4553

46-
def check_repost(self, sender, urls):
54+
def check_repost(self, sender, urls, titles):
55+
if not self.repost:
56+
return []
4757
send = []
48-
if self.repost:
49-
for url in urls:
50-
if not any(i in url for i in self.repost_ignore):
51-
res = self.bot.session.query(RepostUrl).filter(RepostUrl.url == url).first()
52-
if res:
58+
for i in range(0, len(urls)):
59+
url = urls[i]
60+
# the conversion to unicode is quite important to prevent sqlite conversion errors between 8-bytestrings and UTF-8 sqlite3 values
61+
title_page = unicode(titles[i])
62+
if not any(k in url for k in self.repost_ignore):
63+
res = self.bot.session.query(RepostUrl).filter(RepostUrl.url == url).first()
64+
if res:
65+
# Do not send a message if the link was shared less than repost_ignore_delay
66+
# seconds ago or the page title changed since its submission
67+
if (datetime.now() - res.last_date) > timedelta(seconds=self.repost_ignore_delay) and title_page == res.title:
5368
send.append('OLD! ')
5469
first = KnownUser.get_antihl(res.jid, self.bot)
5570
first_date = 'le ' + res.date.strftime('%x') + ' à ' + res.date.strftime('%X')
5671
first_date = first_date.decode("utf-8")
5772
if res.count == 1:
58-
send.append(u'Ce lien a déjà été posté %s par %s sur %s…' % (first_date, first, res.chan))
73+
send.append(u'Ce lien a déjà été posté %s par %s sur %s…' % (first_date, first, first.chan))
5974
else:
6075
ret = u'Ce lien a déjà été posté %s fois depuis que %s l’a découvert, %s, sur %s…'
6176
send.append(ret % (res.count, first, first_date, res.chan))
62-
res.count += 1
63-
else:
64-
u = RepostUrl(url,
65-
self.bot.occupants.pseudo_to_jid(sender),
66-
self.bot.chatname)
67-
self.bot.session.add(u)
68-
self.bot.session.commit()
77+
res.title = title_page
78+
res.count += 1
79+
# Update the time someone posted the link
80+
res.last_date = datetime.now()
81+
else:
82+
u = RepostUrl(url,
83+
self.bot.occupants.pseudo_to_jid(sender),
84+
self.bot.chatname,
85+
title_page)
86+
self.bot.session.add(u)
87+
self.bot.session.commit()
6988
return send
7089

7190
def get_title(self, urls):

url/model.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ class RepostUrl(Base):
99
__tablename__ = "url"
1010
url = Column(String(250), primary_key=True)
1111
count = Column(Integer)
12+
title = Column(String(250))
1213
date = Column(DateTime)
14+
last_date = Column(DateTime)
1315
jid = Column(String(250))
1416
chan = Column(String(250))
1517

16-
def __init__(self, url, jid, chan):
18+
def __init__(self, url, jid, chan, title=""):
1719
self.url = url
1820
self.jid = jid
1921
self.count = 1
22+
self.title = title
2023
self.date = datetime.datetime.now()
24+
self.last_date = self.date
2125
self.chan = chan

0 commit comments

Comments
 (0)