def test__get_matches(self):
     res = {
         'current_matches': [{
             'id': 1,
             'distance': .234
         }],
         'historical_matches': [{
             'id': 1,
             'distance': .234
         }],
         'index_search_time': 1.234,
         'total_searched': 100,
         'used_current_index': True,
         'used_historical_index': True,
         'target_result': {}
     }
     with mock.patch(
             'redditrepostsleuth.core.services.duplicateimageservice.requests.get'
     ) as mock_get:
         dup_svc = DuplicateImageService(
             Mock(),
             Mock(),
             Mock(),
             config=MagicMock(index_api='http://good.com'))
         mock_get.return_value = SimpleNamespace(**{
             'text': json.dumps(res),
             'status_code': 200
         })
         res = dup_svc._get_matches('111', 1, 1)
         self.assertIsInstance(res, ImageIndexApiResult)
         self.assertTrue(len(res.current_matches) == 1)
         self.assertTrue(len(res.historical_matches) == 1)
 def test__remove_duplicates_one_dup_remove(self):
     matches = [
         ImageSearchMatch('test.com', 123, Post(id=1), 10, 10, 32),
         ImageSearchMatch('test.com', 123, Post(id=1), 10, 10, 32),
         ImageSearchMatch('test.com', 123, Post(id=2), 10, 10, 32)
     ]
     dup_svc = DuplicateImageService(Mock(),
                                     Mock(),
                                     Mock(),
                                     config=MagicMock())
     r = dup_svc._remove_duplicates(matches)
     self.assertEqual(2, len(r))
 def test__get_image_search_match_from_index_result_no_post(self):
     with mock.patch.object(DuplicateImageService,
                            '_get_post_from_index_id') as dup:
         dup.return_value = None
         dup_svc = DuplicateImageService(Mock(),
                                         Mock(),
                                         Mock(),
                                         config=MagicMock())
         r = dup_svc._get_image_search_match_from_index_result(
             {
                 'id': 123,
                 'distance': .123
             }, 'test.com',
             '40bec6703e3f3c2b0fc491a1c0c16cff273f00c00c020ff91b6807cc060c0014'
         )
         self.assertIsNone(r)
    def test__get_post_from_index_id_no_index_post_found_return_none(self):

        historical_repo = MagicMock()
        uow = MagicMock()
        uowm = MagicMock()
        historical_repo.get_by_id.return_value = None
        type(uow).image_post = mock.PropertyMock(return_value=historical_repo)
        uow.__enter__.return_value = uow
        uowm.start.return_value = uow
        dup_svc = DuplicateImageService(uowm,
                                        Mock(),
                                        Mock(),
                                        config=MagicMock())
        r = dup_svc._get_post_from_index_id(123)
        historical_repo.get_by_id.assert_called()
        self.assertIsNone(r)
 def __init__(self):
     self.config = Config()
     self.redlock = get_redlock_factory(self.config)
     self.reddit = get_reddit_instance(self.config)
     self.reddit_manager = RedditManager(self.reddit)
     self.uowm = SqlAlchemyUnitOfWorkManager(get_db_engine(self.config))
     self.event_logger = EventLogging(config=self.config)
     notification_svc = NotificationService(self.config)
     self.response_handler = ResponseHandler(
         self.reddit_manager,
         self.uowm,
         self.event_logger,
         source='summons',
         live_response=self.config.live_responses,
         notification_svc=notification_svc)
     dup_image_svc = DuplicateImageService(self.uowm,
                                           self.event_logger,
                                           self.reddit,
                                           config=self.config)
     response_builder = ResponseBuilder(self.uowm)
     self.summons_handler = SummonsHandler(
         self.uowm,
         dup_image_svc,
         self.reddit_manager,
         response_builder,
         self.response_handler,
         event_logger=self.event_logger,
         summons_disabled=False,
         notification_svc=notification_svc)
Exemplo n.º 6
0
 def __init__(self):
     self.config = Config()
     from redditrepostsleuth.core.services.duplicateimageservice import DuplicateImageService
     self.uowm = SqlAlchemyUnitOfWorkManager(get_db_engine(self.config))
     self.notification_svc = NotificationService(self.config)
     self.event_logger = EventLogging()
     self.reddit = get_reddit_instance(self.config)
     self.dup_service = DuplicateImageService(self.uowm, self.event_logger,
                                              self.reddit)
 def test__get_matches_unknown_exception(self):
     with mock.patch(
             'redditrepostsleuth.core.services.duplicateimageservice.requests.get'
     ) as mock_get:
         dup_svc = DuplicateImageService(
             Mock(),
             Mock(),
             Mock(),
             config=MagicMock(index_api='http://test.com'))
         mock_get.side_effect = Exception('Ouch')
         self.assertRaises(Exception, dup_svc._get_matches, '111', 1, 1)
 def test__get_matches_bad_status_code(self):
     with mock.patch(
             'redditrepostsleuth.core.services.duplicateimageservice.requests.get'
     ) as mock_get:
         dup_svc = DuplicateImageService(
             Mock(),
             Mock(),
             Mock(),
             config=MagicMock(index_api='http://test.com'))
         mock_get.return_value = SimpleNamespace(**{'status_code': 500})
         self.assertRaises(NoIndexException, dup_svc._get_matches, '111', 1,
                           1)
 def test__get_image_search_match_from_index_result_valid_post(self):
     with mock.patch.object(DuplicateImageService,
                            '_get_post_from_index_id') as dup:
         dup.return_value = Post(
             id=456,
             dhash_h=
             '40bec6703e3f3c2b0fc491a1c0c16cff273f00c00c020ff91b6807cc060c0014'
         )
         dup_svc = DuplicateImageService(Mock(),
                                         Mock(),
                                         Mock(),
                                         config=MagicMock())
         r = dup_svc._get_image_search_match_from_index_result(
             {
                 'id': 123,
                 'distance': .123
             }, 'test.com',
             '40bec6703e3f3c2b0fc491a1c0c16cff273f00c00c020ff91b6807cc060c0014'
         )
         self.assertIsInstance(r, ImageSearchMatch)
         self.assertEqual(123, r.index_match_id)
         self.assertEqual(456, r.post.id)
    def test__get_post_from_index_id_valid_match_historical(self):
        def return_post_with_id(id):
            return Post(id=id)

        historical_repo = MagicMock()
        post_repo = MagicMock(get_by_post_id=MagicMock(
            side_effect=return_post_with_id))
        uow = MagicMock()
        uowm = MagicMock()
        historical_repo.get_by_id.return_value = MagicMock(post_id=123)
        type(uow).image_post = mock.PropertyMock(return_value=historical_repo)
        type(uow).posts = mock.PropertyMock(return_value=post_repo)
        uow.__enter__.return_value = uow
        uowm.start.return_value = uow
        dup_svc = DuplicateImageService(uowm,
                                        Mock(),
                                        Mock(),
                                        config=MagicMock())
        r = dup_svc._get_post_from_index_id(123)
        historical_repo.get_by_id.assert_called()
        self.assertIsInstance(r, Post)
        self.assertEqual(123, r.id)
 def test__get_matches_invalid_response_data(self):
     with mock.patch(
             'redditrepostsleuth.core.services.duplicateimageservice.requests.get'
     ) as mock_get:
         dup_svc = DuplicateImageService(
             Mock(),
             Mock(),
             Mock(),
             config=MagicMock(index_api='http://test.com'))
         mock_get.return_value = SimpleNamespace(
             **{
                 'text': json.dumps({'junk': 'data'}),
                 'status_code': 200
             })
         self.assertRaises(NoIndexException, dup_svc._get_matches, '111', 1,
                           1)
 def __init__(self):
     self.config = Config()
     self.reddit = get_reddit_instance(self.config)
     self.reddit_manager = RedditManager(self.reddit)
     self.uowm = SqlAlchemyUnitOfWorkManager(get_db_engine(self.config))
     event_logger = EventLogging(config=self.config)
     response_handler = ResponseHandler(
         self.reddit_manager,
         self.uowm,
         event_logger,
         source='submonitor',
         live_response=self.config.live_responses)
     dup_image_svc = DuplicateImageService(self.uowm,
                                           event_logger,
                                           self.reddit,
                                           config=self.config)
     response_builder = ResponseBuilder(self.uowm)
     self.sub_monitor = SubMonitor(dup_image_svc,
                                   self.uowm,
                                   self.reddit_manager,
                                   response_builder,
                                   response_handler,
                                   event_logger=event_logger,
                                   config=self.config)
Exemplo n.º 13
0
from redditrepostsleuth.core.db.db_utils import get_db_engine
from redditrepostsleuth.core.db.uow.sqlalchemyunitofworkmanager import SqlAlchemyUnitOfWorkManager
from redditrepostsleuth.core.services.responsebuilder import ResponseBuilder

from redditrepostsleuth.core.util.helpers import get_reddit_instance, get_redis_client
from redditrepostsleuth.core.services.duplicateimageservice import DuplicateImageService
from redditrepostsleuth.submonitorsvc.submonitor import SubMonitor

if __name__ == '__main__':
    config = Config()
    event_logger = EventLogging(config=config)
    uowm = SqlAlchemyUnitOfWorkManager(get_db_engine(config))
    response_builder = ResponseBuilder(uowm)
    reddit = get_reddit_instance(config)
    reddit_manager = RedditManager(reddit)
    dup = DuplicateImageService(uowm, event_logger, reddit, config=config)
    monitor = SubMonitor(dup,
                         uowm,
                         reddit_manager,
                         response_builder,
                         ResponseHandler(reddit_manager,
                                         uowm,
                                         event_logger,
                                         source='submonitor',
                                         live_response=config.live_responses),
                         event_logger=event_logger,
                         config=config)
    redis = get_redis_client(config)
    while True:
        while True:
            queued_items = redis.lrange('submonitor', 0, 20000)
from redditrepostsleuth.core.services.responsebuilder import ResponseBuilder
from redditrepostsleuth.core.util.imagehashing import get_image_hashes
from redditrepostsleuth.core.util.reddithelpers import get_reddit_instance
from redditrepostsleuth.submonitorsvc.submonitor import SubMonitor

config = Config(
    r'/home/barry/PycharmProjects/RedditRepostSleuth/sleuth_config.json')
reddit = get_reddit_instance(config)
uowm = SqlAlchemyUnitOfWorkManager(get_db_engine(config))
reddit_manager = RedditManager(reddit)
event_logger = EventLogging(config=config)
response_handler = ResponseHandler(reddit_manager,
                                   uowm,
                                   event_logger,
                                   source='submonitor')
dup_image_svc = DuplicateImageService(uowm, event_logger, config=config)
response_builder = ResponseBuilder(uowm)
sub_monitor = SubMonitor(dup_image_svc,
                         uowm,
                         reddit_manager,
                         response_builder,
                         response_handler,
                         event_logger=event_logger,
                         config=config)

with uowm.start() as uow:
    post = uow.posts.get_by_post_id('iirpkm')
    target_hashes = get_image_hashes(post.searched_url, hash_size=32)

with uowm.start() as uow:
    monitored_subs = uow.monitored_sub.get_all()