Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
NodyHub committed Jul 2, 2017
1 parent c21b417 commit d88f11c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion flathunter.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def get_formatted_durations(config, address):
name = duration.get('name')
for mode in duration.get('modes', list()):
if 'gm_id' in mode and 'title' in mode:
duration = util.getDistance(config, address, dest, mode['gm_id'])
duration = util.get_distance(config, address, dest, mode['gm_id'])
out += "> %s (%s): %s\n" % (name, mode['title'], duration)

return out.strip()
Expand Down
22 changes: 11 additions & 11 deletions flathunter/idmaintainer.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
__author__ = "Jan Harrie"
__version__ = "0.1"
__maintainer__ = "Jan Harrie"
__email__ = "[email protected]"
__status__ = "Prodction"

import sqlite3 as lite
import sys

# ~ Logging KungFoo
import logging

log = logging.getLogger()
__author__ = "Jan Harrie"
__version__ = "0.1"
__maintainer__ = "Jan Harrie"
__email__ = "[email protected]"
__status__ = "Prodction"


class IdMaintainer:
__log__ = logging.getLogger()

def __init__(self, db_name):
self.CON = None
try:
Expand All @@ -22,11 +22,11 @@ def __init__(self, db_name):
cur.execute('CREATE TABLE IF NOT EXISTS processed (ID INTEGER)')

except lite.Error as e:
log.error("Error %s:" % e.args[0])
self.__log__.error("Error %s:" % e.args[0])
sys.exit(1)

def add(self, expose_id):
log.debug('add(' + str(expose_id) + ')')
self.__log__.debug('add(' + str(expose_id) + ')')
cur = self.CON.cursor()
cur.execute('INSERT INTO processed VALUES(' + str(expose_id) + ')')
self.CON.commit()
Expand All @@ -41,8 +41,8 @@ def get(self):
break
res.append(row[0])

log.info('already processed: ' + str(len(res)))
log.debug(str(res))
self.__log__.info('already processed: ' + str(len(res)))
self.__log__.debug(str(res))
return res

def foo(self):
Expand Down
13 changes: 6 additions & 7 deletions flathunter/immosearch.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import logging, requests, re
from time import sleep
from bs4 import BeautifulSoup


class ImmoSearcher:
URL_PATTERN = re.compile(r'https://www\.immobilienscout24\.de')
__log__ = logging.getLogger(__name__)

def __init__(self):
self.logger = logging.getLogger(self.__class__.__name__)
logging.getLogger("requests").setLevel(logging.WARNING)

def get_results(self, search_url):
Expand All @@ -16,22 +15,22 @@ def get_results(self, search_url):
search_url = re.sub(r"/Suche/(.+?)/P-\d+", "/Suche/\1/P-%i", search_url)
else:
search_url = re.sub(r"/Suche/(.+?)/", r"/Suche/\1/P-%i/", search_url)
self.logger.debug("Got search URL %s" % search_url)
self.__log__.debug("Got search URL %s" % search_url)

# load first page to get number of entries
page_no = 1
soup = self.get_page(search_url, page_no)
no_of_results = int(
soup.find_all(lambda e: e.has_attr('data-is24-qa') and e['data-is24-qa'] == 'resultlist-resultCount')[
0].text)
self.logger.info('Number of results: ' + str(no_of_results))
self.__log__.info('Number of results: ' + str(no_of_results))

# get data from first page
entries = self.extract_data(soup)

# iterate over all remaining pages
while len(entries) < no_of_results:
self.logger.debug('Next Page')
self.__log__.debug('Next Page')
page_no += 1
soup = self.get_page(search_url, page_no)
entries.extend(self.extract_data(soup))
Expand All @@ -41,7 +40,7 @@ def get_results(self, search_url):
def get_page(self, search_url, page_no):
resp = requests.get(search_url % page_no)
if resp.status_code != 200:
self.logger.error("Got response (%i): %s" % (resp.status_code, resp.content))
self.__log__.error("Got response (%i): %s" % (resp.status_code, resp.content))
return BeautifulSoup(resp.content, 'html.parser')

def extract_data(self, soup):
Expand All @@ -67,5 +66,5 @@ def extract_data(self, soup):
}
entries.append(details)

self.logger.debug('extracted: ' + str(entries))
self.__log__.debug('extracted: ' + str(entries))
return entries
6 changes: 3 additions & 3 deletions flathunter/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import logging
from datetime import datetime, timedelta


GM_MODE_TRANSIT = 'transit'
GM_MODE_BICYCLE = 'bicycling'
GM_MODE_DRIVING = 'driving'

__logger__ = logging.getLogger(__name__)


def getDistance(config, address, dest, mode):
def get_distance(config, address, dest, mode):
# get timestamp for next monday at 9:00:00 o'clock
now = datetime.today().replace(hour=9,minute=0,second=0)
nextMonday = now + timedelta(days=(7-now.weekday()))
Expand All @@ -28,7 +27,8 @@ def getDistance(config, address, dest, mode):
gm_key = config.get('google_maps_api',dict()).get('key')

if not gm_key and mode != GM_MODE_DRIVING:
__logger__.warning("No Google Maps API key configured and without using a mode different from 'driving' is not allowed. Downgrading to mode 'drinving' thus. ")
__logger__.warning("No Google Maps API key configured and without using a mode different from 'driving' is not "
"allowed. Downgrading to mode 'drinving' thus. ")
mode = 'driving'
base_url = base_url.replace('&key={key}', '')

Expand Down
21 changes: 11 additions & 10 deletions flathunter/wgsearch.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
import logging, requests, re
import logging
import requests
import re
from bs4 import BeautifulSoup


class WGSearcher:
URL_PATTERN = re.compile(r'https://www\.wg-gesucht\.de')
__log__ = logging.getLogger(__name__)

def __init__(self):
self.logger = logging.getLogger(self.__class__.__name__)
logging.getLogger("requests").setLevel(logging.WARNING)

def get_results(self, search_url):
self.logger.debug("Got search URL %s" % search_url)
self.__log__.debug("Got search URL %s" % search_url)

# load first page
page_no = 0
soup = self.get_page(search_url, page_no)
no_of_pages = 0 # TODO get it from soup
self.logger.info('Found pages: ' + str(no_of_pages))
self.__log__.info('Found pages: ' + str(no_of_pages))

# get data from first page
entries = self.extract_data(soup)
self.logger.debug('Number of found entries: ' + str(len(entries)))
self.__log__.debug('Number of found entries: ' + str(len(entries)))

# iterate over all remaining pages
while (page_no + 1) < no_of_pages: # page_no starts with 0, no_of_pages with 1
page_no += 1
self.logger.debug('Checking page %i' % page_no)
self.__log__.debug('Checking page %i' % page_no)
soup = self.get_page(search_url, page_no)
entries.extend(self.extract_data(soup))
self.logger.debug('Number of found entries: ' + str(len(entries)))
self.__log__.debug('Number of found entries: ' + str(len(entries)))

return entries

def get_page(self, search_url, page_no):
resp = requests.get(search_url) # TODO add page_no in url
if resp.status_code != 200:
self.logger.error("Got response (%i): %s" % (resp.status_code, resp.content))
self.__log__.error("Got response (%i): %s" % (resp.status_code, resp.content))
return BeautifulSoup(resp.content, 'html.parser')

def extract_data(self, soup):
Expand Down Expand Up @@ -70,7 +71,7 @@ def extract_data(self, soup):
}
entries.append(details)

self.logger.debug('extracted: ' + str(entries))
self.__log__.debug('extracted: ' + str(entries))

return entries

Expand Down

0 comments on commit d88f11c

Please sign in to comment.