Skip to content

Commit

Permalink
Merge pull request #4 from codders/bugfix/fix-empty-filter
Browse files Browse the repository at this point in the history
Fixed bug in exclusion filtering
  • Loading branch information
codders authored May 21, 2020
2 parents bcfe966 + 5a2581d commit 2292ef3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
4 changes: 2 additions & 2 deletions flathunter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ def launch_flat_hunt(config):
id_watch = IdMaintainer('%s/processed_ids.db' % os.path.dirname(os.path.abspath(__file__)))

hunter = Hunter(config)
hunter.hunt_flats(config, searchers, id_watch)
hunter.hunt_flats(searchers, id_watch)

while config.get('loop', dict()).get('active', False):
time.sleep(config.get('loop', dict()).get('sleeping_time', 60 * 10))
hunter.hunt_flats(config, searchers, id_watch)
hunter.hunt_flats(searchers, id_watch)


def main():
Expand Down
25 changes: 13 additions & 12 deletions flathunter/hunter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class Hunter:
GM_MODE_BICYCLE = 'bicycling'
GM_MODE_DRIVING = 'driving'

def __init__(self, cfg):
self.cfg = cfg
self.excluded_titles = self.cfg.get('excluded_titles', list())
def __init__(self, config):
self.config = config
self.excluded_titles = self.config.get('excluded_titles', list())

def hunt_flats(self, config, searchers, id_watch):
sender = SenderTelegram(config)
new_links = 0
def hunt_flats(self, searchers, id_watch):
sender = SenderTelegram(self.config)
new_exposes = []
processed = id_watch.get()

for url in config.get('urls', list()):
for url in self.config.get('urls', list()):
self.__log__.debug('Processing URL: ' + url)

try:
Expand Down Expand Up @@ -59,7 +59,7 @@ def hunt_flats(self, config, searchers, id_watch):
break

# calculdate durations
message = config.get('message', "").format(
message = self.config.get('message', "").format(
title=expose['title'],
rooms=expose['rooms'],
size=expose['size'],
Expand All @@ -71,10 +71,10 @@ def hunt_flats(self, config, searchers, id_watch):
# durations=self.get_formatted_durations(config, address)).strip()

# if no excludes, send messages
if self.excluded_titles is None:
if len(self.excluded_titles) == 0:
# send message to all receivers
sender.send_msg(message)
new_links += 1
new_exposes.append(expose)
id_watch.add(expose['id'])
continue

Expand All @@ -85,10 +85,11 @@ def hunt_flats(self, config, searchers, id_watch):
if not found_objects:
# send message to all receivers
sender.send_msg(message)
new_links += 1
new_exposes.append(expose)
id_watch.add(expose['id'])

self.__log__.info(str(new_links) + ' new offer found')
self.__log__.info(str(len(new_exposes)) + ' new offers found')
return new_exposes

def get_formatted_durations(self, config, address):
out = ""
Expand Down
21 changes: 21 additions & 0 deletions test/test_hunter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
import yaml
from flathunter.crawl_immowelt import CrawlImmowelt
from flathunter.hunter import Hunter
from flathunter.idmaintainer import IdMaintainer

class HunterTest(unittest.TestCase):

DUMMY_CONFIG = """
urls:
- https://www.immowelt.de/liste/berlin/wohnungen/mieten?roomi=2&prima=1500&wflmi=70&sort=createdate%2Bdesc
"""

def setUp(self):
self.hunter = Hunter(yaml.safe_load(self.DUMMY_CONFIG))
self.searchers = [CrawlImmowelt()]
self.id_watch = IdMaintainer(":memory:")

def test_hunt_flats(self):
exposes = self.hunter.hunt_flats(self.searchers, self.id_watch)
self.assertTrue(len(exposes) > 0, "Expected to find exposes")

0 comments on commit 2292ef3

Please sign in to comment.