conditions.py 1.2 KB
Newer Older
1
2
3
#! /usr/bin/env python
# -*- coding: utf-8 -*-

Cédric Bonhomme's avatar
Cédric Bonhomme committed
4
5
6
7
import datetime
import requests
import json

8
9
10
11
12
13
14
15
16
17
18
19
def default():
    """
    Default function, called if no 'condition' function is defined for the restaurant (in restaurants.json).
    """
    return True

def quick():
    """
    Quick only on fridays.
    """
    return datetime.datetime.today().weekday() == 4

20
21
def not_on_fridays():
    """
Cédric Bonhomme's avatar
Cédric Bonhomme committed
22
    Test if today is not friday.
23
24
    """
    return not quick()
Cédric Bonhomme's avatar
Cédric Bonhomme committed
25
26
27
28
29
30
31

def before_11h45():
    """
    Return True if it is not later than 11:45 a.m..
    """
    return datetime.datetime.strptime("11:45", "%H:%M").time() > datetime.datetime.now().time()

32
33
def rain():
    """
Cédric Bonhomme's avatar
Cédric Bonhomme committed
34
    Return True if it is not raining.
35
36
37
38
    This test can be used for the 'Chambre de Commerce' because it is far from us.
    """
    try:
        result = requests.get("http://api.openweathermap.org/data/2.5/weather?q=Luxembourg,Lux")
39
    except Exception as e:
Cédric's avatar
Cédric committed
40
        print(str(e))
41
        return True
42

43
    if result.status_code != 200:
Cédric's avatar
Cédric committed
44
        print(result.status_code)
45
        return True
46

47
    json_result = json.loads(result.text)
Cédric's avatar
Cédric committed
48
49
    try:
        return json_result['weather'][0]['main'] != 'Rain'
50
    except Exception as e:
Cédric's avatar
Cédric committed
51
        print(str(e))
52
        return False