2022-06-07 14:42:24 +00:00
|
|
|
from flask import g
|
|
|
|
import itertools
|
|
|
|
import requests
|
2022-06-10 15:13:32 +00:00
|
|
|
from sqlalchemy import or_
|
2022-06-07 14:42:24 +00:00
|
|
|
import files.helpers.const as const
|
|
|
|
from files.classes.user import User
|
|
|
|
from files.classes.comment import Comment
|
|
|
|
from files.classes.notifications import Notification
|
2022-06-20 21:01:42 +00:00
|
|
|
from files.helpers.sanitize import sanitize
|
2022-06-07 14:42:24 +00:00
|
|
|
|
|
|
|
# https://api.pushshift.io/meta provides key server_ratelimit_per_minute
|
|
|
|
# At time of writing, the ratelimit is 120 req/min. We get nowhere near this
|
|
|
|
# with current keyword quantities. If this ever changes, consider reading the
|
|
|
|
# value from /meta and doing a random selection of keywords.
|
|
|
|
|
|
|
|
def offsite_mentions_task():
|
|
|
|
if const.REDDIT_NOTIFS_SITE:
|
2022-06-10 15:13:32 +00:00
|
|
|
row_send_to = g.db.query(User.id) \
|
|
|
|
.filter(or_(User.admin_level >= const.REDDIT_NOTIFS_JL_MIN,
|
2022-06-10 11:12:19 +00:00
|
|
|
User.offsitementions == True)).all()
|
2022-06-07 14:42:24 +00:00
|
|
|
send_to = [x[0] for x in row_send_to]
|
|
|
|
|
|
|
|
site_mentions = get_mentions(const.REDDIT_NOTIFS_SITE)
|
|
|
|
notify_mentions(send_to, site_mentions)
|
|
|
|
|
|
|
|
if const.REDDIT_NOTIFS_USERS:
|
|
|
|
for query, send_user in const.REDDIT_NOTIFS_USERS.items():
|
|
|
|
user_mentions = get_mentions([query])
|
|
|
|
notify_mentions([send_user], user_mentions, mention_str='mention of you')
|
|
|
|
|
|
|
|
def get_mentions(queries):
|
|
|
|
kinds = ['submission', 'comment']
|
|
|
|
mentions = []
|
|
|
|
for kind, query in itertools.product(kinds, queries):
|
|
|
|
try:
|
|
|
|
data = requests.get(f'https://api.pushshift.io/reddit/{kind}/search'
|
2022-09-25 03:23:50 +00:00
|
|
|
+ f'?html_decode=true&q={query}&size=1', timeout=5, proxies=proxies).json()['data']
|
2022-06-07 14:42:24 +00:00
|
|
|
except: break
|
|
|
|
|
2022-06-20 21:01:42 +00:00
|
|
|
for i in data:
|
2022-06-07 14:42:24 +00:00
|
|
|
# Special case: PokemonGoRaids says 'Marsey' a lot unrelated to us.
|
|
|
|
if i['subreddit'] == 'PokemonGoRaids': continue
|
|
|
|
|
2022-06-22 22:40:53 +00:00
|
|
|
if kind == 'comment':
|
2022-07-03 02:13:25 +00:00
|
|
|
body = i["body"].replace('>', '> ')
|
|
|
|
text = f'<blockquote><p>{body}</p></blockquote>'
|
2022-06-22 22:40:53 +00:00
|
|
|
else:
|
2022-07-03 02:13:25 +00:00
|
|
|
title = i["title"].replace('>', '> ')
|
2022-07-17 17:14:01 +00:00
|
|
|
|
|
|
|
# Special case: a spambot says 'WPD' a lot unrelated to us.
|
|
|
|
if 'Kathrine Mclaurin' in title: continue
|
|
|
|
|
2022-07-03 02:13:25 +00:00
|
|
|
text = f'<blockquote><p>{title}</p></blockquote>'
|
|
|
|
|
|
|
|
if i["selftext"]:
|
|
|
|
selftext = i["selftext"].replace('>', '> ')[:5000]
|
|
|
|
text += f'<br><blockquote><p>{selftext}</p></blockquote>'
|
|
|
|
|
2022-06-22 22:40:53 +00:00
|
|
|
|
2022-06-20 21:01:42 +00:00
|
|
|
mentions.append({
|
|
|
|
'permalink': i['permalink'],
|
2022-07-10 13:02:24 +00:00
|
|
|
'author': i['author'],
|
2022-06-22 22:40:53 +00:00
|
|
|
'text': text,
|
2022-06-20 21:01:42 +00:00
|
|
|
})
|
2022-06-07 14:42:24 +00:00
|
|
|
|
|
|
|
return mentions
|
|
|
|
|
|
|
|
def notify_mentions(send_to, mentions, mention_str='site mention'):
|
|
|
|
for m in mentions:
|
2022-07-10 13:02:24 +00:00
|
|
|
author = m['author']
|
2022-06-20 21:01:42 +00:00
|
|
|
permalink = m['permalink']
|
2022-09-16 16:30:34 +00:00
|
|
|
text = sanitize(m['text'], golden=False)
|
2022-06-20 21:01:42 +00:00
|
|
|
notif_text = \
|
2022-07-10 13:02:24 +00:00
|
|
|
f"""<p>New {mention_str} by <a href="https://old.reddit.com/u/{author}" rel="nofollow noopener noreferrer" target="_blank">/u/{author}</a></p><p><a href="https://old.reddit.com{permalink}?context=89" rel="nofollow noopener noreferrer" target="_blank">https://old.reddit.com{permalink}?context=89</a></p>{text}"""
|
2022-06-07 14:42:24 +00:00
|
|
|
|
|
|
|
existing_comment = g.db.query(Comment.id).filter_by(
|
2022-07-08 19:03:04 +00:00
|
|
|
author_id=const.AUTOJANNY_ID,
|
2022-06-20 21:01:42 +00:00
|
|
|
parent_submission=None,
|
2022-06-07 14:42:24 +00:00
|
|
|
body_html=notif_text).one_or_none()
|
2022-07-10 13:02:24 +00:00
|
|
|
if existing_comment: break
|
2022-06-07 14:42:24 +00:00
|
|
|
|
|
|
|
new_comment = Comment(
|
2022-07-08 19:03:04 +00:00
|
|
|
author_id=const.AUTOJANNY_ID,
|
2022-06-07 14:42:24 +00:00
|
|
|
parent_submission=None,
|
|
|
|
body_html=notif_text,
|
|
|
|
distinguish_level=6)
|
|
|
|
g.db.add(new_comment)
|
|
|
|
g.db.flush()
|
|
|
|
new_comment.top_comment_id = new_comment.id
|
|
|
|
|
|
|
|
for user_id in send_to:
|
|
|
|
notif = Notification(comment_id=new_comment.id, user_id=user_id)
|
2022-07-10 13:02:24 +00:00
|
|
|
g.db.add(notif)
|