1
# -*- coding: utf-8 -*-
3
# This file is part of Pigeon Planner.
5
# Pigeon Planner is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
10
# Pigeon Planner is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with Pigeon Planner. If not, see <http://www.gnu.org/licenses/>
24
from yapsy.IPlugin import IPlugin
26
from pigeonplanner.core import const
27
from pigeonplanner.database.models import Pigeon
30
class KDBDParser(IPlugin):
37
def check(self, resultfile): # noqa
38
if not os.path.splitext(resultfile.name)[1] == ".html":
40
if "<title>KBDB/RFCB-Admin</title>" not in resultfile.read():
44
def scrape_race_data(self, html):
45
# Normally we'd use a proper HTML parser like BeautifulSoup to scrape the HTML looking
46
# for the data we need, but the website is made by someone with no understanding of how
47
# to use HTML so we're going with a dirty solution as well.
48
# The wanted data is within a table with just one row and cell, all including line breaks
49
# and formatting tags.
51
found = re.search(r"> (.*) - (.*) - (.*) - (.*?)<", html)
54
racepoint, date, _, sector = found.groups()
56
found = re.search(r"Aantal (\w*).* (\d*) <", html)
59
category, n_pigeons = found.groups()
61
dt = datetime.datetime.strptime(date, "%d-%m-%y")
62
date = dt.strftime(const.DATE_FORMAT)
67
"n_pigeons": n_pigeons,
69
"racepoint": racepoint
72
def scrape_results(self, html):
73
found = re.search(r"var data = (.*);", html)
75
raise ValueError("No results found.")
76
raw_results = json.loads(found.group(1))
77
for result in raw_results:
78
full_ring = result["Ringnummer"]
80
year = "20%s" % full_ring[-2:]
81
place = result["Plaats"]
82
speed = result["Snelheid"]
84
pigeon = Pigeon.get_for_band(("", "", ring, year))
85
self.results[pigeon] = [ring, year, str(place), speed]
86
except Pigeon.DoesNotExist:
89
def parse_file(self, resultfile):
90
html = resultfile.read()
91
self.scrape_race_data(html)
92
self.scrape_results(html)
93
return self.race_data, self.results