-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
74 lines (57 loc) · 1.78 KB
/
app.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
from flask import Flask, jsonify, request, render_template
from jsonschema import validate
from twitter_api import TwitterApi
from weather import Weather
from thingspeak import ThingSpeak
import env
import json
import requests
app = Flask(__name__)
@app.route('/')
def dashboard():
return render_template('dashboard.html', name='David')
@app.route('/api')
def publish_api():
schema = {
"type" : "object",
"properties" : {
"temperature" : {"type" : "string"},
"humidity" : {"type" : "string"},
"rain" : {"type" : "string"}
}
}
try:
params = {
'temperature': request.args.get('temperature'),
'humidity': request.args.get('humidity'),
'rain': request.args.get('rain')
}
print(params)
validate(params, schema)
temperature = params['temperature']
humidity = params['humidity']
rain = params['rain']
# weather = Weather(temperature, humidity, rain)
# weather.store()
ts = ThingSpeak(temperature, humidity, rain)
ts.send()
rain_msg = {
0: 'não está chovendo',
1: 'está caindo uma chuva fraca',
2: 'chove muito'
}[rain]
# import random
# rand = random.randint(10,99)
tweet = 'No momento fazem %.1fº, a humidade relativa do ar é de %d%% e %s' % (temperature, humidity, rain_msg)
twitter = TwitterApi()
twitter.publish(tweet)
return jsonify({'message': 'success'})
except Exception as e:
log = open('error.log', 'a')
log.write('%s\n' % str(e))
log.close()
return jsonify({"error": str(e)})
if __name__ == '__main__':
# app.debug = True
app.run()