summaryrefslogtreecommitdiff
path: root/sublab_project/matekarte/tasks.py
blob: 5ba8fbd5f6f0fa67747e8cb53c58da3af2388d7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Celery tasks.
"""
from celery.task import PeriodicTask
from celery.schedules import crontab
from django.conf import settings
import os

from osmux import osmux

class OSMFetcher(object):
    """
    A mixin to get and store OpenStreetmap Data
    """

    @property
    def bbox(self):
        raise NotImplementedError

    @property
    def store_name(self):
        raise NotImplementedError

    @property
    def tag(self):
        raise NotImplementedError

    value = '*'

    def run(self, **kwargs):

        fn = os.path.join(settings.MEDIA_ROOT, self.store_name)
        fn_new = os.path.join(settings.MEDIA_ROOT, 'new-%s' % self.store_name)

        with open(fn_new, 'wb') as output:
            output.write(osmux(
                    self.tag,
                    self.bbox[0],
                    self.bbox[1],
                    self.bbox[2],
                    self.bbox[3],
                    self.value))
        os.rename(fn_new, fn)


class MateFetcher(OSMFetcher, PeriodicTask):
    run_every = crontab(minute=0, hour=3)
    bbox = (55.26, 46.52, 15.26, 5.22)
    store_name = 'mate.xml'
    tag = 'drink:club-mate'