-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
236 lines (181 loc) · 7.54 KB
/
main.py
File metadata and controls
236 lines (181 loc) · 7.54 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
226
227
228
229
230
231
232
233
234
235
236
import kivy
kivy.require("1.10.1")
from kivy.app import App
from kivy.uix.pagelayout import PageLayout
from kivy.properties import NumericProperty, ObjectProperty, ListProperty, StringProperty, BooleanProperty
from kivy.clock import Clock
from kivy.uix.label import Label
from kivy.config import Config
import datetime
import winsound
from yr.libyr import Yr
import ghostscript
import locale
import pygame
from pyo365 import Account, Connection
from bs4 import BeautifulSoup
import re
from credentials import credentails
scopes = ['https://graph.microsoft.com/Mail.ReadWrite', "offline_access"]
account = Account(credentials, scopes=scopes)
con = Connection(credentials, scopes=scopes)
class PagesClass(PageLayout):
time = ObjectProperty(datetime.datetime.now(), True)
waited = ObjectProperty(datetime.datetime.now())
alarmHour = ObjectProperty(None)
alarmMinute = ObjectProperty(None)
alarmText = ObjectProperty(None)
alarm = BooleanProperty(False)
currentDay = NumericProperty(7)
schedulePlan = ObjectProperty(None)
schedulePlans = ListProperty([["Musikk", "Naturfag", "Engelsk"],
["Matematikk", "Matematikk", "Norsk", "Valgfag", "Valgfag"],
["Matematikk", "Fremmedspråk", "Naturfag", "Norsk", "Norsk"],
["Engelsk", "Fremmedspråk", "Samfunnsfag", "Kroppsøving", "Kroppsøving"],
["Kunst Og Håndverk", "Kunst Og Håndverk", "Utdanningsvalg", "Samfunnsfag"]])
currentHour = NumericProperty(None)
weatherSymbol = StringProperty("1")
degrees = NumericProperty(0)
days = ListProperty(["Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag", "Lørdag", "Søndag", ""])
months = ListProperty(["Januar", "Februar", "Mars", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Desember"])
mailContainers = ListProperty(None)
def playSound(self):
winsound.PlaySound("God_morgen.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
def update(self, dt):
self.time = datetime.datetime.now()
if (self.time - self.waited).seconds >= 60:
self.page = 0
weekday = self.time.weekday()
if weekday != self.currentDay:
self.currentDay = weekday
#self.updateSchedule()
#ukeplan = convert_from_path("ukeplan.pdf")
self.updateImages()
if self.time.hour != self.currentHour:
self.currentHour = self.time.hour
self.updateWeather()
self.update_mails()
def updateSchedule(self):
if self.currentDay < 5:
current = self.schedulePlans[self.currentDay]
else:
current = self.schedulePlans[0]
self.schedulePlan.clear_widgets()
self.schedulePlan.add_widget(Label(text="Timeplan", font_size=60))
for i in current:
self.schedulePlan.add_widget(Label(text=i, font_size=30))
def updateWeather(self):
weather = Yr(location_name="Norge/Oslo/Oslo/Kringsjå")
now = weather.now()
self.weatherSymbol = now["symbol"]["@number"]
self.degrees = int(now["temperature"]["@value"])
def updateImages(self):
mailbox = account.mailbox()
query = mailbox.new_query().on_attribute('subject').contains('Melding fra portalen: Informasjon uke').order_by("subject", ascending=False)
message = mailbox.get_message(query=query, download_attachments=True)
message.attachments[0].save("./", "ukeplan.pdf")
inbox = mailbox.inbox_folder()
messages = inbox.get_messages()
pdf2jpeg("./ukeplan.pdf", "./ukeplan")
page1 = pygame.image.load("ukeplan1.png")
page2 = pygame.image.load("ukeplan2.png")
w = 965
h = 555
x = 141
y = 984
timeplan = pygame.surface.Surface((w, h))
timeplan.blit(page1, (0, 0), (x, y, w, h))
pygame.image.save(timeplan, "timeplan.png")
w = 907
h = 1400
x = 141
y = 220
lekser = pygame.surface.Surface((w, h))
lekser.blit(page2, (0, 0), (x, y, w, h))
w2 = lekser.get_height() - 1
while lekser.get_at((500, w2))[0] > 100 and w2 > 0:
w2 -= 1
y2 = 0
while lekser.get_at((500, y2))[0] > 100 and y2 < lekser.get_height() - 1:
y2 += 1
lekser2 = pygame.surface.Surface((w, w2 - y2))
lekser2.blit(lekser, (0, 0), (0, y2, w, w2))
pygame.image.save(lekser2, "lekser.png")
self.timeplanImage.reload()
self.lekserImage.reload()
def on_touch_down(self, touch):
self.waited = datetime.datetime.now()
if self.page == 0 and touch.is_double_tap:
self.alarm = not self.alarm
if self.alarm:
self.alarmText.color = (0, 1, 0, 1)
else:
self.alarmText.color = (1, 0, 0, 1)
return super().on_touch_down(touch)
def update_mails(self):
mailbox = account.mailbox()
messages = mailbox.get_messages()
#[x.extract() for x in soup.findAll('a')]
#print(soup.get_text())
#print(soup.prettify())
self.mails.clear_widgets()
j = 0
for i in messages:
if i.sender.address == "noreply@portal.skoleplattform.no" and j < 5:
self.mails.add_widget(Label(text=i.subject[22:]))
soup = BeautifulSoup(i.body, "html.parser")
divs = soup.findAll("div")
divs[len(divs) - 1].extract()
text = soup.get_text()
print(text)
text = addLines(text, 100)
#print(text)
self.mailContainers[j].add_widget(Label(text=text))
j += 1
def pdf2jpeg(pdf_input_path, jpeg_output_path):
args = ["pdf2jpeg", # actual value doesn't matter
"-dNOPAUSE",
"-sDEVICE=jpeg",
"-r144",
"-sOutputFile=" + jpeg_output_path + "%d.png",
pdf_input_path]
encoding = locale.getpreferredencoding()
ghostscript.Ghostscript(*[a.encode(encoding) for a in args])
def addLines(string, max_characters):
string += " trash"
lines = []
last_space = 0
total_index = 0
line_index = 0
while total_index + line_index < len(string):
#print(line_index, total_index, last_space, string[total_index + line_index], lines, len(string))
if string[total_index + line_index] == " ":
last_space = total_index + line_index
if string[total_index + line_index:total_index + line_index + 1] == "\n":
lines.append(string[total_index:total_index + line_index])
total_index += line_index + 1
line_index = 0
last_space = 0
if line_index >= max_characters or total_index + line_index == len(string):
lines.append(string[total_index:last_space])
line_index = 0
total_index = last_space + 1
last_space = 0
else:
line_index += 1
return '\n'.join(lines)
#print(addLines("""SMS-melding
"""
Av: Sigurd Enge, Dato: 04.12.2018 kl. 11:27
Til: 8SA-8D Samfunnsfag (Elever + Lærere)
Nordberg skole:
Hei, 8D! Nå har jeg lagt ut en plan for vurderingen i samf på its. Sjekk kriteriene. Vi starter på torsdag. Mvh, Sig
SMSen kan ikke besvares"""#, 10))
class PagesApp(App):
def build(self):
#Config.set("graphics", "fullscreen", "auto")
pages = PagesClass()
Clock.schedule_interval(pages.update, 1)
return pages
PagesApp().run()