~timovwb/pigeonplanner/devel

« back to all changes in this revision

Viewing changes to pigeonplanner/resultparsers/kbdb.py

  • Committer: Timo Vanwynsberghe
  • Date: 2020-08-02 12:06:45 UTC
  • Revision ID: timovwb@gmail.com-20200802120645-w2b9x3m5hm9fdcv4
Added KBDB resultparser

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# This file is part of Pigeon Planner.
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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/>
 
17
 
 
18
 
 
19
import os
 
20
import re
 
21
import json
 
22
import datetime
 
23
 
 
24
from yapsy.IPlugin import IPlugin
 
25
 
 
26
from pigeonplanner.core import const
 
27
from pigeonplanner.database.models import Pigeon
 
28
 
 
29
 
 
30
class KDBDParser(IPlugin):
 
31
    def __init__(self):
 
32
        super().__init__()
 
33
 
 
34
        self.results = {}
 
35
        self.race_data = {}
 
36
 
 
37
    def check(self, resultfile):  # noqa
 
38
        if not os.path.splitext(resultfile.name)[1] == ".html":
 
39
            return False
 
40
        if "<title>KBDB/RFCB-Admin</title>" not in resultfile.read():
 
41
            return False
 
42
        return True
 
43
 
 
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.
 
50
 
 
51
        found = re.search(r"> (.*) - (.*) - (.*) - (.*?)<", html)
 
52
        if found is None:
 
53
            raise ValueError
 
54
        racepoint, date, _, sector = found.groups()
 
55
 
 
56
        found = re.search(r"Aantal (\w*).* (\d*) <", html)
 
57
        if found is None:
 
58
            raise ValueError
 
59
        category, n_pigeons = found.groups()
 
60
 
 
61
        dt = datetime.datetime.strptime(date, "%d-%m-%y")
 
62
        date = dt.strftime(const.DATE_FORMAT)
 
63
 
 
64
        self.race_data = {
 
65
            "sector": sector,
 
66
            "category": category,
 
67
            "n_pigeons": n_pigeons,
 
68
            "date": date,
 
69
            "racepoint": racepoint
 
70
        }
 
71
 
 
72
    def scrape_results(self, html):
 
73
        found = re.search(r"var data = (.*);", html)
 
74
        if found is None:
 
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"]
 
79
            ring = full_ring[:-2]
 
80
            year = "20%s" % full_ring[-2:]
 
81
            place = result["Plaats"]
 
82
            speed = result["Snelheid"]
 
83
            try:
 
84
                pigeon = Pigeon.get_for_band(("", "", ring, year))
 
85
                self.results[pigeon] = [ring, year, str(place), speed]
 
86
            except Pigeon.DoesNotExist:
 
87
                pass
 
88
 
 
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