-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
108 lines (86 loc) · 3.07 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from flask import Flask, Response, send_file
from flask import send_from_directory
from flask import render_template
import pandas as pd
import os
import json
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
import warnings
from influxdb_client.client.warnings import MissingPivotFunction
warnings.simplefilter("ignore", MissingPivotFunction)
from tzlocal import get_localzone # $ pip install tzlocal
from birdnetapp.secrets import INFLUX_URL, INFLUX_TOKEN, INFLUX_ORG, INFLUX_BUCKET
from birdnetapp.config import APP_WINDOW, SAVEDIR
FLUTTER_WEB_APP = 'flutter/build/web'
influx_client = InfluxDBClient(url=INFLUX_URL, token=INFLUX_TOKEN, org=INFLUX_ORG)
write_api = influx_client.write_api(write_options=SYNCHRONOUS)
query_api = influx_client.query_api()
app = Flask(__name__, template_folder=FLUTTER_WEB_APP)
@app.route('/<path:name>')
def return_flutter_doc(name):
datalist = str(name).split('/')
DIR_NAME = FLUTTER_WEB_APP
if len(datalist) > 1:
for i in range(0, len(datalist) - 1):
DIR_NAME += '/' + datalist[i]
return send_from_directory(DIR_NAME, datalist[-1])
@app.route('/birds')
def birds():
# get local timezone
local_tz = str(get_localzone())
print('local tz', local_tz)
df = query_api.query_data_frame(f'''
m = from(bucket:"{INFLUX_BUCKET}")
|> range(start: -{APP_WINDOW})
|> filter(fn: (r) => r["_measurement"] == "birdnet")
c = m
|> count()
l = m
|> last()
join(tables: {{count:c, last:l}}, on: ["_field"])
|> group()
|>sort(columns: ["_time"], desc: true)
''')
#df.sort_values(["_time"], inplace=True, ascending=False)
#print(df)
df.set_index(df._time, inplace=True)
df.index = pd.to_datetime(df.index)
df.index = df.index.tz_convert(local_tz)
o = {v['_field'] : {'count' : v['_value_count'], 'time' : i.strftime('%m-%d %H:%M')} for i, v in df.iterrows()}
'''
files = []
folder = '/home/user/birdNet/'
for x in os.walk(folder):
files.append(x[2])
'''
return json.dumps(o)
@app.route('/')
def render_page():
return render_template('/index.html')
@app.route('/details/<name>')
def details(name):
files = []
folder = os.path.join(SAVEDIR, name)
for x in os.walk(folder):
contents = x[2]
for f in contents:
if f.endswith('.json'):
files.append(f.split('.')[0])
return json.dumps(sorted(files, reverse=True))
@app.route('/mp3/<name>/<date>')
def mp3(name, date):
fname = os.path.join(SAVEDIR, name, date + '.mp3')
def generate():
with open(fname, "rb") as f:
data = f.read(1024)
while data:
yield data
data = f.read(1024)
return Response(generate(), mimetype="audio/mp3")
@app.route('/png/<name>/<date>')
def png(name, date):
image = os.path.join(SAVEDIR, name, date + '.png')
return send_file(image, mimetype='image/png')
if __name__ == '__main__':
app.run(host='0.0.0.0')