Skip to content

Commit

Permalink
Sentry error fixes. Also added mod mail for OF detection
Browse files Browse the repository at this point in the history
  • Loading branch information
barrycarey committed Sep 19, 2023
1 parent 932e897 commit c40f5a4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def image_links_from_gallery_meta_data(meta_data: dict[str, dict]) -> list[str]:
log.info('Gallery image still processing')
raise GalleryNotProcessed(f'Gallery image {k} is still processing')

if v['status'] == 'failed':
log.info('Gallery failed, skipping')
continue

if v['status'] != 'valid':
raise ValueError(f'Unexpected status in Gallery meta data {v["status"]}')

Expand Down
4 changes: 4 additions & 0 deletions redditrepostsleuth/core/services/response_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ def send_private_message(
self.reddit.auth.limits['remaining']
)
log.info('Sent PM to %s. ', user.name)
except RedditAPIException as e:
if e.error_type == 'NOT_WHITELISTED_BY_USER_MESSAGE':
log.warning('Cannot send PM to %s. Not whitelisted', user.name)
except Exception as e:
log.exception('Failed to send PM to %s', user.name, exc_info=True)
raise
Expand All @@ -159,6 +162,7 @@ def send_private_message(
recipient=user.name
)
self._save_private_message(bot_message)

return bot_message

def reply_to_private_message(self, message: Message, body: str) -> NoReturn:
Expand Down
13 changes: 6 additions & 7 deletions redditrepostsleuth/core/util/imagehashing.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import json
import logging
from typing import Dict, Text, Optional

import requests
from io import BytesIO
from typing import Text, Optional
from urllib import request
from urllib.error import HTTPError

import imagehash
import requests
from PIL import Image, UnidentifiedImageError
from PIL.Image import DecompressionBombError
from requests.exceptions import ConnectionError

from redditrepostsleuth.core.exception import ImageConversionException, ImageRemovedException, InvalidImageUrlException
from redditrepostsleuth.core.db.databasemodels import Post
from redditrepostsleuth.core.exception import ImageConversionException, ImageRemovedException, InvalidImageUrlException

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -78,12 +77,12 @@ def generate_img_by_url_requests(url: str) -> Optional[Image]:
raise ImageRemovedException('Image removed')
elif res.status_code == 403:
log.warning('Unauthorized: %s', url)
raise InvalidImageUrlException
raise InvalidImageUrlException(f'Unauthorized on {url}')
raise ImageConversionException(f'Status {res.status_code}')

try:
return Image.open(BytesIO(res.content))
except UnidentifiedImageError as e:
except (UnidentifiedImageError, DecompressionBombError) as e:
log.warning('Failed to hash image %s: %s', url, e)
raise ImageConversionException(e)

Expand Down
5 changes: 4 additions & 1 deletion redditrepostsleuth/core/util/replytemplates.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,7 @@

HIGH_VOLUME_REPOSTER_FOUND = 'User [u/{username}](https://reddit.com/u/{username}) has been flagged as a high volume reposter.\n\n' \
'They just created [this submission](https://redd.it/{post_id}) on [r/{subreddit}](https://reddit.com/r/{subreddit}) \n\n' \
'In the last 7 days I have detected {repost_count} reposts created by them'
'In the last 7 days I have detected {repost_count} reposts created by them'

ADULT_PROMOTER_SUBMISSION_FOUND = 'User [u/{username}](https://reddit.com/u/{username}) has been flagged as an adult content promoter.\n\n' \
'They just submitted [this post](https://redd.it/{post_id}) in r/{subreddit}'
53 changes: 46 additions & 7 deletions redditrepostsleuth/submonitorsvc/submonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
from prawcore import Forbidden

from redditrepostsleuth.core.config import Config
from redditrepostsleuth.core.db.databasemodels import Post, MonitoredSub, MonitoredSubChecks, UserReview
from redditrepostsleuth.core.db.databasemodels import Post, MonitoredSub, MonitoredSubChecks
from redditrepostsleuth.core.db.uow.unitofwork import UnitOfWork
from redditrepostsleuth.core.db.uow.unitofworkmanager import UnitOfWorkManager
from redditrepostsleuth.core.model.search.image_search_results import ImageSearchResults
from redditrepostsleuth.core.model.search.search_results import SearchResults
from redditrepostsleuth.core.notification.notification_service import NotificationService
from redditrepostsleuth.core.services.duplicateimageservice import DuplicateImageService
from redditrepostsleuth.core.services.eventlogging import EventLogging
from redditrepostsleuth.core.services.reddit_manager import RedditManager
from redditrepostsleuth.core.services.response_handler import ResponseHandler
from redditrepostsleuth.core.services.responsebuilder import ResponseBuilder
from redditrepostsleuth.core.util.helpers import build_msg_values_from_search, build_image_msg_values_from_search, \
get_image_search_settings_for_monitored_sub, get_link_search_settings_for_monitored_sub, \
get_text_search_settings_for_monitored_sub
from redditrepostsleuth.core.util.replytemplates import REPOST_MODMAIL, NO_BAN_PERMISSIONS, HIGH_VOLUME_REPOSTER_FOUND
from redditrepostsleuth.core.util.repost.repost_helpers import filter_search_results, log_search
from redditrepostsleuth.core.util.replytemplates import REPOST_MODMAIL, NO_BAN_PERMISSIONS, HIGH_VOLUME_REPOSTER_FOUND, \
ADULT_PROMOTER_SUBMISSION_FOUND
from redditrepostsleuth.core.util.repost.repost_helpers import filter_search_results
from redditrepostsleuth.core.util.repost.repost_search import image_search_by_post, link_search, text_search_by_post

log = logging.getLogger(__name__)
Expand All @@ -45,6 +46,7 @@ def __init__(
self.response_builder = response_builder
self.resposne_handler = response_handler
self.event_logger = event_logger
self.notification_svc = None
if config:
self.config = config
else:
Expand Down Expand Up @@ -100,11 +102,34 @@ def handle_only_fans_check(self, post: Post, uow: UnitOfWork, monitored_sub: Mon

log.info('User %s is flagged as an adult promoter, taking action', user.username)
if monitored_sub.adult_promoter_remove_post:
self._remove_post(monitored_sub, self.reddit.submission(post.post_id))
if self.notification_svc:
self.notification_svc.send_notification(
f'Post by [{post.author}](https://reddit.com/u/{post.author}) removed from [r/{post.subreddit}](https://reddit.com/r/{post.subreddit})',
subject='Onlyfans Removal'
)
self._remove_post(monitored_sub, self.reddit.submission(post.post_id), mod_note=f'Adult Content Promoter Remova. Notes: {user.notes}')

if monitored_sub.adult_promoter_ban_user:
if self.notification_svc:
self.notification_svc.send_notification(
f'User [{post.author}](https://reddit.com/u/{post.author}) banned from [r/{post.subreddit}](https://reddit.com/r/{post.subreddit})',
subject='Onlyfans Ban Issued'
)
self._ban_user(post.author, monitored_sub.name, user.notes)

if monitored_sub.adult_promoter_notify_mod_mail:
message_body = ADULT_PROMOTER_SUBMISSION_FOUND.format(
username=post.author,
subreddit=monitored_sub.name,
post_id=post.post_id,
)
self.resposne_handler.send_mod_mail(
monitored_sub.name,
message_body,
f'New Submission From Adult Content Promoter',
source='sub_monitor'
)


def handle_high_volume_reposter_check(self, post: Post, uow: UnitOfWork, monitored_sub: MonitoredSub) -> None:
"""
Expand All @@ -126,6 +151,10 @@ def handle_high_volume_reposter_check(self, post: Post, uow: UnitOfWork, monitor

repost_count = uow.stat_top_reposter.get_total_reposts_by_author_and_day_range(post.author, 7)

if not repost_count:
log.debug('User %s has no reposts, skipping high volume check', post.author)
return

if monitored_sub.high_volume_reposter_threshold < 10:
log.info('High volume threshold failsafe. Skipping check')
return
Expand All @@ -136,9 +165,19 @@ def handle_high_volume_reposter_check(self, post: Post, uow: UnitOfWork, monitor
return

if monitored_sub.high_volume_reposter_remove_post:
if self.notification_svc:
self.notification_svc.send_notification(
f'Post by [{post.author}](https://reddit.com/u/{post.author}) removed from [r/{post.subreddit}](https://reddit.com/r/{post.subreddit})',
subject='High Volume Removal'
)
self._remove_post(monitored_sub, self.reddit.submission(post.post_id))

if monitored_sub.high_volume_reposter_ban_user:
if self.notification_svc:
self.notification_svc.send_notification(
f'User [{post.author}](https://reddit.com/u/{post.author}) banned from [r/{post.subreddit}](https://reddit.com/r/{post.subreddit})',
subject='High Volume Reposter Ban Issued'
)
self._ban_user(post.author, monitored_sub.name, 'High volume of reposts detected by Repost Sleuth')

if monitored_sub.high_volume_reposter_notify_mod_mail:
Expand Down Expand Up @@ -337,7 +376,7 @@ def _lock_comment(self, monitored_sub: MonitoredSub, comment: Comment) -> None:
except Exception as e:
log.exception('Failed to lock comment', exc_info=True)

def _remove_post(self, monitored_sub: MonitoredSub, submission: Submission) -> None:
def _remove_post(self, monitored_sub: MonitoredSub, submission: Submission, mod_note: str = None) -> None:
"""
Check if given sub wants posts removed. Remove is enabled
@param monitored_sub: Monitored sub
Expand All @@ -347,7 +386,7 @@ def _remove_post(self, monitored_sub: MonitoredSub, submission: Submission) -> N
try:
removal_reason_id = self._get_removal_reason_id(monitored_sub.removal_reason, submission.subreddit)
log.info('Attempting to remove post https://redd.it/%s with removal ID %s', submission.id, removal_reason_id)
submission.mod.remove(reason_id=removal_reason_id)
submission.mod.remove(reason_id=removal_reason_id, mod_note=mod_note)
except Forbidden:
log.error('Failed to remove post https://redd.it/%s, no permission', submission.id)
except Exception as e:
Expand Down

0 comments on commit c40f5a4

Please sign in to comment.