-
Notifications
You must be signed in to change notification settings - Fork 4
/
restapi.py
148 lines (133 loc) · 4.79 KB
/
restapi.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from bottle import get, post, run
import os
import re
import time
server = "0.0.0.0"
port = 9081
file_server = "136.243.53.83"
port_file_server = 8001
def check_file_name(filename, start, end):
# check format
x = re.search("^\d\d\d\d-\d\d-\d\d_\d\d-\d\d-\d\d\.json$", filename)
y = re.search("^\d\d\d\d-\d\d-\d\d_\d\d-\d\d-\d\d$", start)
z = re.search("^\d\d\d\d-\d\d-\d\d_\d\d-\d\d-\d\d$", end)
# if format is ok
if x and y and z:
# create time from strings
file_time = int(time.mktime(time.strptime(filename.rsplit(".",1)[0], "%Y-%m-%d_%H-%M-%S")))
start_time = int(time.mktime(time.strptime(start, "%Y-%m-%d_%H-%M-%S")))
end_time = int(time.mktime(time.strptime(end, "%Y-%m-%d_%H-%M-%S")))
now = time.time()
print("Filetime:%s, now:%s, diff:%s" % (file_time, now, now-file_time))
if (int(time.time())-file_time) <= 3630:
return False
# check if file is in time interval
if file_time > (start_time-3600) and file_time <= end_time:
return True
print("Not in the interval")
return False
@get('/interval_all/<start>/<end>')
def do_intall(start, end):
paths = []
# go through files and directories
for (dirpath, dirnames, filenames) in os.walk(os.path.abspath(os.path.dirname(__file__))+"/data"):
# for all files
for filename in filenames:
print "Filename %s" % filename
if check_file_name(filename, start, end):
# adress of file
path = "http://" + file_server + ":" + str(port_file_server) + "/" + os.path.basename(os.path.dirname(dirpath))+ "/" + os.path.basename(dirpath) + "/" + filename
path = path.replace (" ", "_")
paths.append(path)
# return all adresses of files
return ("<a href='%s'>%s</a><br/>" % (path, path) for path in paths)
@get('/interval_keyword/<keyword>/<start>/<end>')
def do_intkey(keyword, start, end):
paths = []
# go through files and directories
for (dirpath, dirnames, filenames) in os.walk(os.path.abspath(os.path.dirname(__file__))+"/data/"+keyword):
# for all files
for filename in filenames:
if check_file_name(filename, start, end):
# adress of file
path = "http://" + file_server + ":" + str(port_file_server) + "/" + keyword + "/" + os.path.basename(dirpath) + "/" + filename
path = path.replace (" ", "_")
paths.append(path)
# return all adresses of files
return (path+"\n" for path in paths)
@get('/timestamp_all/<name>')
def do_timeall(name):
paths = []
# go through files and directories
for (dirpath, dirnames, filenames) in os.walk(os.path.abspath(os.path.dirname(__file__))+"/data"):
# for all files
for filename in filenames:
if check_file_name(filename, name, time.strftime("%Y-%m-%d_%H-%M-%S")):
# adress of file
path = "http://" + file_server + ":" + str(port_file_server) + "/" + os.path.basename(os.path.dirname(dirpath)) + "/" + os.path.basename(dirpath) + "/" + filename
path = path.replace (" ", "_")
paths.append(path)
# return all adresses of files
return (path+"\n" for path in paths)
@get('/timestamp_keyword/<keyword>/<name>')
def do_timekey(keyword, name):
paths = []
# go through files and directories
for (dirpath, dirnames, filenames) in os.walk(os.path.abspath(os.path.dirname(__file__))+"/data/"+keyword):
# for all files
for filename in filenames:
if check_file_name(filename, name, time.strftime("%Y-%m-%d_%H-%M-%S")):
# adress of file
path = "http://" + file_server + ":" + str(port_file_server) + "/" + keyword + "/" + os.path.basename(dirpath) + "/" + filename
path = path.replace (" ", "_")
paths.append(path)
# return all adresses of files
return (path+"\n" for path in paths)
# print keywords from file
@get('/print_keywords')
def do_print():
with open("keywords.txt", "r") as f:
file = f.read()
f.close()
return file
# add keyword to file
@get('/add_keyword/<keyword>')
def do_add(keyword):
keyword = keyword.replace("_", " ")
count = 0
with open("keywords.txt", "r+") as f:
file = f.readlines()
for line in file:
if line.strip():
count += 1
line = line.rstrip('\n\t ')
line = line.strip('\t ')
# checking if key is in file
if line == keyword:
return "Keyword is already in file.\n"
if count >= 400:
return "There is more than 400 keywords.\n"
f.write("\n%s" % keyword)
return "Keyword was added successfully.\n"
# delete keyword from file
@get('/delete_keyword/<keyword>')
def do_delete(keyword):
keyword = keyword.replace("_", " ")
new_file = ""
with open("keywords.txt", "r") as f:
file = f.readlines()
f.close()
for line in file:
line = line.rstrip('\n\t ')
line = line.strip('\t ')
line = line.strip('\r')
if line == keyword:
continue
else:
new_file += line +"\n"
new_file = new_file.rstrip('\n')
with open("keywords.txt", "w") as f:
f.write(new_file)
return "Keyword was deleted successfully.\n"
# run api
run(host=server, port=port)