-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardapio
executable file
·413 lines (339 loc) · 12.3 KB
/
cardapio
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/bin/env python
import os
from html.parser import HTMLParser
from dateutil.parser import parse
import subprocess
import sys
###### GLOBAL VARIABLES ######################
_T_TABSIZE = 9
TAB_SIZE = 35
SND_TAB_SIZE = 20
CARDAPIO="/tmp/cardapio-ru/ru.html"
leiaCardapio = False
dias = -1
conjDias = []
qualRef = 0
whatItemAmI = 0
##############################################
####### CLASSES ##############################
class refeicao:
'classe referente a almoços e jantares, refeicoes'
def __init__(self):
self.periodo = ""
self.pratoPrincipalCarne = ""
self.pratoPrincipalVegetariano = ""
self.guarnicao = ""
self.arroz = ""
self.feijao = ""
self.saladas = ""
self.sobremesa = ""
def imprime(self):
print("Periodo:", self.periodo)
print("Prato Principal Carne :", self.pratoPrincipalCarne)
print("Prato Principal Vegetariano ♡:", self.pratoPrincipalVegetariano)
print("Guarnicao:", self.guarnicao)
print("Arroz:", self.arroz)
print("Feijao:", self.feijao)
print("Saladas:", self.saladas)
print("Sobremesa:", self.sobremesa)
class diaDaSemana:
'classe referente a um dia no ru'
def __init__(self):
self.data = ""
self.diaDaSemana = ""
self.almoco = refeicao()
self.jantar = refeicao()
def imprime(self):
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("%d/%d/%d - %s" % (self.data.day, self.data.month, self.data.year, self.diaDaSemana))
self.almoco.imprime()
self.jantar.imprime()
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
####### LOOSE GLOBAL VARIABLES ###############
semana = []
for dia in range(7):
semana.append(diaDaSemana())
refeicaoAtual = refeicao()
#############################################
####### PROGRAM CORE ########################
class HTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag == 'div':
if(len(attrs) != 0):
if attrs[0][0] == 'id' and attrs[0][1] =='cardapio':
global leiaCardapio
leiaCardapio = True
def handle_data(self, data):
global leiaCardapio
global dias
global conjDias
global qualRef
global whatItemAmI
global refeicaoAtual
global semana
reset = False
if(data == "Acompanhe as notícias da UFSCar também pelas redes sociais oficiais da Universidade "):
leiaCardapio = False
if(leiaCardapio):
tam = data.split()
if len(tam) > 0:
#print("Encontrei alguns dados:", data)
try :
d = parse(data, dayfirst=True)
#print("%d/%d/%d" % (d.day, d.month, d.year))
if d not in conjDias:
dias += 1
setattr(semana[dias], 'data', d)
conjDias.append(d)
qualRef = 0
#print("-------->Data não está no conjunto de dias")
else:
#print("-------->Data está no conjunto de dias")
if qualRef == 0:
qualRef = 1
else:
print("qualRef está com valor estranho")
except:
if whatItemAmI == 7 and data != "Sobremesa: ":
setattr(refeicaoAtual, 'sobremesa', data)
#print("Li oitavo item, sobremesa")
#print(data)
whatItemAmI += 1
#ultimo item, incrementando refeicao a dia
if qualRef == 0:
setattr(semana[dias], 'almoco', refeicaoAtual)
elif qualRef == 1:
setattr(semana[dias], 'jantar', refeicaoAtual)
else:
print("Erro ao atribuir refeicao a dia. handle_data:except:whatItemAmI == 7")
#limpando refeicao atual
refeicaoAtual = refeicao()
#recomecando leitura de nova refeicao
whatItemAmI = 0
reset = True
if whatItemAmI == 6 and data != "Saladas: ":
setattr(refeicaoAtual, 'saladas', data)
#print("Li setimo item, salada")
#print(data)
whatItemAmI += 1
if whatItemAmI == 5 and data != "Feijão: ":
setattr(refeicaoAtual, 'feijao', data)
#print("Li sexto item, feijao")
#print(data)
whatItemAmI += 1
if whatItemAmI == 4 and data != "Arroz: ":
setattr(refeicaoAtual, 'arroz', data)
#print("Li quinto item, arroz")
#print(data)
whatItemAmI += 1
if whatItemAmI == 3 and data != "Guarnição: ":
setattr(refeicaoAtual, 'guarnicao', data)
#print("Li quarto item, guarnicao")
#print(data)
whatItemAmI += 1
if whatItemAmI == 2 and data != "Prato Principal: ":
try:
carne, veg = data.split("/ ")
setattr(refeicaoAtual, 'pratoPrincipalCarne', carne)
setattr(refeicaoAtual, 'pratoPrincipalVegetariano', veg)
#print("Li terceiro item, prato principal")
#print("Carnista:",carne)
#print("Vegetariano:", veg)
except:
try: #sdds padroes
carne, veg = data.split("/")
setattr(refeicaoAtual, 'pratoPrincipalCarne', carne)
setattr(refeicaoAtual, 'pratoPrincipalVegetariano', veg)
except:
#print("Valor indefinido")
setattr(refeicaoAtual, 'pratoPrincipalCarne', data)
whatItemAmI += 1
if whatItemAmI == 1 and data != " - ":
setattr(refeicaoAtual, 'periodo', data.capitalize())
#print("Li segundo item, periodo")
#print(data.capitalize())
whatItemAmI += 1
if whatItemAmI == 0 and data != ": " and not reset:
if data != "Bebida: " and data != "Não Definido.":
setattr(semana[dias], 'diaDaSemana', data)
#print("Li primeiro item, dia da semana")
#print(data)
whatItemAmI += 1
print("",end='')
#############################################
#############################################
###### FUNCTIONS ##################################################################################
def buildLine(no_meals, line_no):
line = ""
if line_no > 9:
ref = "jantar"
else:
ref = "almoço"
for i in range(no_meals):
if line_no % 9 == 0:
idx = line_no // 9
if idx == 0:
line += getHeader(i)
elif idx == 1:
line += getBlankLine()
elif idx == 2:
line += getFooter()
if line_no % 9 == 1:
line += getTitleLine(ref)
if line_no % 9 == 2:
line += getMealLine(i, ref,'pratoPrincipalCarne')
if line_no % 9 == 3:
line += getMealLine(i, ref,'pratoPrincipalVegetariano')
if line_no % 9 == 4:
line += getMealLine(i, ref,'arroz')
if line_no % 9 == 5:
line += getMealLine(i, ref,'feijao')
if line_no % 9 == 6:
line += getMealLine(i, ref,'guarnicao')
if line_no % 9 == 7:
line += getMealLine(i, ref,'saladas')
if line_no % 9 == 8:
line += getMealLine(i, ref,'sobremesa')
if i == no_meals - 1:
line += "\n"
if line_no == 18:
line += "\n"
else:
line += " "
return line
def getRes():
x_output = subprocess.check_output(["xrandr"])
res_ant = ""
for it in x_output.split():
curr_it = str(it)
if "*" in curr_it:
return res_ant
res_ant = it
def getTerRes():
h, w = map(int, subprocess.check_output(["stty", "size"]).split())
return h, w
def getDisp(h, w):
if w < 38:
print("Are you kidding me?")
print("Enlarge your terminal")
sys.exit(1)
else:
meal_space = 38
no_meals = 0
while w >= meal_space:
w -= meal_space
no_meals += 1
if meal_space == 38:
meal_space += 1
return no_meals
def getDataEDia(index):
global semana
dataEDia = ""
dataEDia += printDate(getattr(semana[index], 'data'))
dataEDia += " - "
dataEDia += getattr(semana[index], 'diaDaSemana')
dataEDia += " "
return dataEDia
def getMenuHeader(w):
if w >= 18:
center = w // 2
center -= 9
no_tabs = 0
while center > 9:
center -= 9
no_tabs += 1
return printTabs(no_tabs) + printAlignment(center) + "CARDAPIO DA SEMANA"
else:
return ""
def getHeader(day):
header = "~~ " + getDataEDia(day) + "~" * (TAB_SIZE - 3 - len(getDataEDia(day)))
return header
def getFooter():
return "~" * TAB_SIZE
def getMealLine(index):
global semana
refeicao = getattr(semana[index], 'almoco')
carne = getattr(refeicao, 'pratoPrincipalCarne')
mealLine = "* "
if len(carne) > TAB_SIZE - 4:
carne = carne[0:TAB_SIZE - 7]
carne += "..."
mealLine += carne
else:
mealLine += carne
mealLine += " " * (TAB_SIZE - len(carne) - 4)
mealLine += " *"
return mealLine
def getMealLine(index, meal, attr):
global semana
if meal == "almoço":
meal = "almoco"
refeicao = getattr(semana[index], meal)
food = getattr(refeicao, attr)
mealLine = "* "
if attr == "arroz":
mealLine += "Arroz "
elif attr == "feijao":
mealLine += "Feijão "
elif attr == "saladas":
mealLine += "Salada "
elif attr == "guarnicao":
mealLine += "Guarnição "
elif attr == "sobremesa":
mealLine += "Sobremesa "
if len(food) > TAB_SIZE - 4:
food = food[0:TAB_SIZE - 5 - len(mealLine)] #3 from "..." + 2 from " *"
food += "..."
mealLine += food
else:
mealLine += food
mealLine += " " * (TAB_SIZE - 2 - len(mealLine)) #2 from " *"
mealLine += " *"
return mealLine
def getBlankLine():
blankLine = "*" + " " * (TAB_SIZE - 2) + "*"
return blankLine
def newGetBlankLine():
return "*"+ ""
def getTitleLine(title):
titleline = "* >>>"
titleline += title.capitalize()
titleline += " " * (TAB_SIZE - 12)
titleline += "*"
return titleline
def printTabs(no):
return '\t' * no
def printAlignment(leftover):
spaces = 0
while leftover >= 2:
leftover -= 2
spaces += 1
return " " * spaces
def printDate(date):
return '{}/{}/{}'.format(date.day, date.month, date.year)
def main():
#html downloader and updater
os.system("./download.sh")
global TAB_SIZE
global SND_TAB_SIZE
f = open(CARDAPIO, "r")
html = f.read()
parser = HTMLParser()
parser.feed(html)
f.close()
#for dia in semana:
# dia.imprime()
r = getRes().decode("utf-8")
h, w = getTerRes()
no_meals_per_line = getDisp(h, w)
meals_left = 7
print(getMenuHeader(w))
while meals_left > 0:
if meals_left < no_meals_per_line:
no_meals_per_line = meals_left
for i in range(19):
print(buildLine(no_meals_per_line, i),end='')
meals_left -= no_meals_per_line
###################################################################################################
if __name__ == "__main__":
main()