-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroadcast_api.py
More file actions
233 lines (195 loc) · 6.85 KB
/
broadcast_api.py
File metadata and controls
233 lines (195 loc) · 6.85 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from mysql_class import mysql
from random import sample
from datetime import date
from datetime import datetime
from datetime import timedelta
import os.path
#The API connect mysql and arrange the schedule and write it to the schedule.txt
def arrange_schedule(json_obj):
schedule_dir = json_obj["schedule_dir"]
arrange_mode = json_obj["arrange_mode"]
return_msg = {}
update_fail = False
find_fail = False
deal_result = []
#connect to mysql
db = mysql()
if db.connect() == -1:
return_msg["num"] = 0
return return_msg
#update expire data berfore query
sql = ("UPDATE image_data " \
+"SET img_is_expire=1 " \
+"WHERE TO_DAYS(NOW())-TO_DAYS(img_end_date)>0 " \
+"or (TO_DAYS(NOW())-TO_DAYS(img_end_date)=0 and TIME_TO_SEC(DATE_FORMAT(NOW(), '%H:%i:%s'))-TIME_TO_SEC(img_end_time)>0)")
if db.cmd(sql) == -1:
update_fail = True
#find images that may be schedule
sql = "SELECT a0.img_id, a0.img_system_name, a0.img_display_time, a1.type_dir, a0.img_end_time FROM " \
+" (SELECT img_id, type_id, img_system_name, img_display_time, img_end_time " \
+" FROM image_data " \
+" WHERE img_is_expire=0 and TO_DAYS(NOW())-TO_DAYS(img_start_date)>=0 " \
+" and TIME_TO_SEC(DATE_FORMAT(NOW(), '%H:%i:%s')) between TIME_TO_SEC(img_start_time) and TIME_TO_SEC(img_end_time)) a0 " \
+" LEFT JOIN (SELECT type_id, type_dir FROM image_type) a1 on a0.type_id=a1.type_id "
#print(sql)
pure_result = db.query(sql)
if pure_result == -1:
find_fail = True
else:
#restruct results of query
for result_row in pure_result:
deal_result.append([result_row[0], (result_row[3] + result_row[1]), result_row[2], result_row[4]])
# img_id, dir and file name, display time, end time
#display in loop or random display
if arrange_mode == 1:
"DO NOTHING"
elif arrange_mode == 2:
if len(deal_result)>20:
deal_result = sample(deal_result, 20)
#update img display count and write to schedule
date_now = date.today()
schedule_file = ("broad_" + str(date_now.year) + "_" + str(date_now.month) + "_" + str(date_now.day) + ".txt")
schedule_file = os.path.join(schedule_dir,("static/log/"+schedule_file))
add_num = 0
try:
if not os.path.isfile(schedule_file) :
file_pointer = open(schedule_file, "w")
else :
file_pointer = open(schedule_file, "a")
if len(deal_result)>0:
for tt in range(len(deal_result)):
sql = "UPDATE image_data SET img_display_count=img_display_count+1 WHERE img_id='"+deal_result[tt][0]+"'"
if db.cmd(sql) == -1:
update_fail = True
else :
int_str1 = str(deal_result[tt][2])
int_str2 = str(deal_result[tt][3])
write_str = ("0 " + deal_result[tt][0] + " " + deal_result[tt][1] + " " + int_str1 + " " + int_str2 + " \n")
# image_id, img_dir, display time, expire_time
file_pointer.write(write_str)
add_num+=1
file_pointer.close()
except:
"Do noting"
db.close()
return_msg["num"] = add_num
return return_msg
#The API load schedule.txt and find out the first image which has not print and the time limit still allow
def load_schedule(json_obj):
schedule_dir = json_obj["schedule_dir"]
return_msg = {}
date_now = date.today()
schedule_file = ("broad_" + str(date_now.year) + "_" + str(date_now.month) + "_" + str(date_now.day) + ".txt")
new_file = ("new_" + schedule_file)
old_file = ("old_" + schedule_file)
schedule_file = os.path.join(schedule_dir,("static/log/"+schedule_file))
new_file = os.path.join(schedule_dir,("static/log/"+new_file))
old_file = os.path.join(schedule_dir,("static/log/"+old_file))
if os.path.isfile(new_file) :
try:
if os.path.isfile(old_file) :
os.remove(old_file)
os.rename(schedule_file, old_file)
os.rename(new_file,schedule_file)
except:
"Do noting"
try:
file_pointer = open(schedule_file, "r+")
except:
return_msg["next_img"] = "img/0.jpg"
return_msg["limit_time"] = 5
return_msg["enough_schedule"] = 0
return return_msg
pure_data = ""
deal_data = ""
char_count = 0
lock_count = []
already_get_target = 0
check_less_line = 0
enough_less_line = 0
for line in file_pointer:
pure_data = ""
pure_data = line.rstrip('\n').split(' ')
if pure_data[0] is '0':
if already_get_target == 0:
limit_time = pure_data[4].split(":")
now_time = datetime.now()
time1 = timedelta(hours=int(limit_time[0]), minutes=int(limit_time[1]), seconds=int(limit_time[2]))
time2 = timedelta(hours=now_time.hour, minutes=now_time.minute, seconds=now_time.second)
time3 = timedelta(hours=0, minutes=0, seconds=0)
if (time1 - time2) > time3:
lock_count.append(char_count)
already_get_target = 1
deal_data = pure_data
elif (time1 - time2) <= time3:
lock_count.append(char_count)
else :
check_less_line = check_less_line + 1
if check_less_line > 10:
enough_less_line = 1
break
char_count+=(len(line))
if len(lock_count) > 0 :
for seek_num in lock_count :
file_pointer.seek(seek_num, 0)
file_pointer.write('1')
file_pointer.close()
try:
return_msg["next_img"] = deal_data[2]
return_msg["limit_time"] = int(deal_data[3])
return_msg["enough_schedule"] = int(enough_less_line)
except:
return_msg["next_img"] = "img/0.jpg"
return_msg["limit_time"] = 5
return_msg["enough_schedule"] = 0
return return_msg
return return_msg
#the api can only edit furtre schedule.
#It can edit schedule not to print image by setting img_check=0 or edit furtre schedule to print new image.
def edit_schedule(json_obj):
schedule_dir = json_obj["schedule_dir"]
next_img = json_obj["next_img"]
img_check = json_obj["img_check"]
img_id = json_obj["img_id"]
img_dir = json_obj["img_dir"]
img_time = json_obj["img_time"]
img_end_time = json_obj["img_end_time"]
return_msg = {}
#can not edit scheduling next item
if next_img <= 1:
return_msg["result"] = "error next_img <= 1"
return return_msg
date_now = date.today()
schedule_file = ("broad_" + str(date_now.year) + "_" + str(date_now.month) + "_" + str(date_now.day) + ".txt")
new_file = ("new_" + schedule_file)
schedule_file = os.path.join(schedule_dir,("static/log/"+schedule_file))
new_file = os.path.join(schedule_dir,("static/log/"+new_file))
try:
file1 = open(schedule_file, "r")
file2 = open(new_file, "w")
pure_data = ""
deal_data = (str(img_check) + ' ' + img_id + ' ' + img_dir + ' ' + str(img_time) + ' ' + img_end_time + '\n')
pass_count = 0
for line in file1:
pure_data = ""
pure_data = line.rstrip('\n').split(' ')
if pure_data[0] is '0':
pass_count+=1
if pass_count == next_img:
file2.write(deal_data)
else :
file2.write(line)
else :
file2.write(line)
file1.close()
file2.close()
except:
try:
if os.path.isfile(new_file) :
os.remove(new_file)
except:
"Do nothing"
return_msg["result"] = "error"
return return_msg
return_msg["result"] = "scuess"
return return_msg