1
1
# -*- coding: utf-8 -*-
2
2
import re
3
+ from datetime import timedelta , datetime
3
4
4
5
import sqlalchemy .exc
5
6
from pipobot .lib .known_users import KnownUser
18
19
19
20
20
21
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 ))
22
26
23
27
def __init__ (self , bot ):
24
28
desc = "Extracting title of page from URL"
@@ -31,41 +35,56 @@ def answer(self, sender, message):
31
35
else :
32
36
urls = set (URLS_RE .findall (message ))
33
37
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
+
34
43
try :
35
- repost_msg = self .check_repost (sender , urls )
44
+ repost_msg = self .check_repost (sender , urls , title_page )
36
45
except sqlalchemy .exc .OperationalError :
37
46
self .bot .session .rollback ()
38
47
repost_msg = []
39
48
except sqlalchemy .exc .InvalidRequestError :
40
49
repost_msg = []
41
50
42
- title_page = self .get_title (urls )
43
51
send = repost_msg + title_page
44
52
return None if send == [] else "\n " .join (send )
45
53
46
- def check_repost (self , sender , urls ):
54
+ def check_repost (self , sender , urls , titles ):
55
+ if not self .repost :
56
+ return []
47
57
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 :
53
68
send .append ('OLD! ' )
54
69
first = KnownUser .get_antihl (res .jid , self .bot )
55
70
first_date = 'le ' + res .date .strftime ('%x' ) + ' à ' + res .date .strftime ('%X' )
56
71
first_date = first_date .decode ("utf-8" )
57
72
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 ))
59
74
else :
60
75
ret = u'Ce lien a déjà été posté %s fois depuis que %s l’a découvert, %s, sur %s…'
61
76
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 ()
69
88
return send
70
89
71
90
def get_title (self , urls ):
0 commit comments