-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·386 lines (340 loc) · 14.4 KB
/
main.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
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
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatterlayout import ScatterLayout
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.spinner import Spinner
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.uix.popup import Popup
from kivy.properties import StringProperty
from android.permissions import request_permissions, Permission
import glob
import os
from os.path import join
from jnius import autoclass
import html2txt
Window.clearcolor = (1, 1, 1, 1)
class CustomTextInput(TextInput):
# https://github.com/kivy/kivy/issues/6473
def _hide_cut_copy_paste(self, win = None):
bubble = self._bubble
if not bubble:
return
Builder.load_string('''
<Urlpup>:
size_hint: .7, .7
auto_dismiss: False
title: 'Enter URL'
BoxLayout:
orientation: 'vertical'
CustomTextInput:
id: url_input
#focus: True
multiline: False
Button:
text: 'OK'
on_press: root.dismiss()
on_release: app.create_new_article(url_input.text)
''')
class Urlpup(Popup):
pass
class MyScatter(ScatterLayout):
pass
Builder.load_string('''
<MyScatter>:
scale: 1
do_scale: True
do_translation: True
do_rotation: False
auto_bring_to_front: False
Image:
id: img
source: ''
size_hint: (0.43, 0.6)
pos_hint: {'center_x': 0.33, 'center_y': 0.5}
keep_ratio: True
''')
Builder.load_string('''
<ScrollableLabel>:
text: ''
scroll_y: 1
Label:
text: root.text
text_size: self.width, None
size_hint: (1, None)
height: self.texture_size[1]
color: (0, 0, 0, 1)
halign: "center"
''')
class ScrollableLabel(ScrollView):
text = StringProperty('')
class Tedegraph(App):
def build(self):
mainbox = FloatLayout()
self.ms = MyScatter()
mainbox.add_widget(self.ms)
mainbox.add_widget(Button(text="Fwd",
font_size="17dp",
size_hint=(.075, .15),
pos_hint={"left":1,
"center_y":0.5},
on_press=self.forward))
self.text_label = ScrollableLabel(pos_hint = {"right":0.90, "top":0.720}, size_hint = (0.43, 0.6))
mainbox.add_widget(self.text_label)
mainbox.add_widget(Button(text="Fwd",
font_size="17dp",
size_hint=(.075, .15),
pos_hint={"right":1,
"center_y":0.5},
on_press=self.forward))
mainbox.add_widget(Button(text="Back",
font_size="17dp",
size_hint=(.075, .15),
pos_hint={"right":1,
"top":1},
on_press=self.backward))
mainbox.add_widget(Button(text="Bookmark",
font_size="17dp",
size_hint=(.15, .075),
pos_hint={"center_x":0.5,
"bottom":1},
on_press=self.add_bookmark))
mainbox.add_widget(Button(text="New",
font_size="17dp",
size_hint=(.15, .075),
pos_hint={"center_x":0.3,
"bottom":1},
on_press=self.create_new_article_popup))
self.init()
self.spinner_articles = Spinner(text ="Choose article",
values = sorted([d for d in os.listdir(self.working_directory) \
if os.path.isdir(os.path.join(self.working_directory, d))]),
size_hint = (0.4, 0.075),
pos_hint = {"right": 1, 'bottom':1}
)
self.spinner_articles.bind(text = self.on_spinner_select)
mainbox.add_widget(self.spinner_articles)
return mainbox
def download_nltk(self):
if not os.path.isdir(os.path.join(self.data_dir, "nltk_data")):
os.chdir(self.data_dir)
print(os.getcwd())
# https://stackoverflow.com/questions/38916452/nltk-download-ssl-certificate-verify-failed
import nltk
import ssl
try:
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
nltk.download('punkt', download_dir = self.data_dir)
return True
except Exception as e:
popup = Popup(title='NLTK download failed: ' + str(e),
size_hint = (None, None), size = (400, 400),
auto_dismiss=True)
popup.open()
return False
return True
def init(self):
request_permissions([Permission.WRITE_EXTERNAL_STORAGE,
Permission.READ_EXTERNAL_STORAGE])
try:
if autoclass('android.os.Build$VERSION').SDK_INT >= 29:
Context = autoclass('android.content.Context')
self.working_directory = os.path.join(Context.getExternalFilesDir(None).getAbsolutePath(), "tdg_articles")
self.data_dir = os.path.join(Context.getExternalFilesDir(None).getAbsolutePath(), "nltk")
else:
Environment = autoclass('android.os.Environment')
self.working_directory = os.path.join(Environment.getExternalStorageDirectory().getAbsolutePath(), "tdg_articles")
self.data_dir = os.path.join(Environment.getExternalStorageDirectory().getAbsolutePath(), "nltk")
except:
self.working_directory = os.path.join(App.get_running_app().user_data_dir, "tdg_articles")
self.data_dir = os.path.join(App.get_running_app().user_data_dir, "nltk")
if not os.path.exists(self.working_directory):
os.makedirs(self.working_directory)
if not os.path.exists(self.data_dir):
os.makedirs(self.data_dir)
os.chdir(self.working_directory)
self.lines = []
self.line_number = 0
self.article_part = 0
self.article_parts = []
self.current_article = None
def forward(self, *args):
if not self.current_article:
return
os.chdir(os.path.join(self.working_directory, self.current_article))
if self.line_number < len(self.lines) - 1:
self.line_number = self.line_number + 1
self.text_label.text = self.lines[self.line_number]
self.text_label.scroll_y = 1
elif self.article_part < len(self.article_parts) - 1:
self.article_part = self.article_part + 1
f = open(self.article_parts[self.article_part], encoding='utf-8', errors='ignore')
self.lines = f.readlines()
self.line_number = 0
f.close()
img_file_name = self.article_parts[self.article_part].replace(".txt", "")
images = [fn for fn in glob.glob(img_file_name + "*") if not fn.endswith("txt")]
if images:
img_file_name = images[0]
self.ms.ids.img.source = img_file_name
else:
self.ms.ids.img.source = ""
self.text_label.text = self.lines[self.line_number]
self.text_label.scroll_y = 1
def backward(self, *args):
if not self.current_article:
return
os.chdir(os.path.join(self.working_directory, self.current_article))
if self.line_number > 0:
self.line_number = self.line_number - 1
self.text_label.text = self.lines[self.line_number]
self.text_label.scroll_y = 1
elif self.article_part > 0:
self.article_part = self.article_part - 1
f = open(self.article_parts[self.article_part], encoding='utf-8', errors='ignore')
self.lines = f.readlines()
self.line_number = len(self.lines) - 1
f.close()
img_file_name = self.article_parts[self.article_part].replace(".txt", "")
images = [fn for fn in glob.glob(img_file_name + "*") if not fn.endswith("txt")]
if images:
img_file_name = images[0]
self.ms.ids.img.source = img_file_name
else:
self.ms.ids.img.source = ""
self.text_label.text = self.lines[self.line_number]
self.text_label.scroll_y = 1
def add_bookmark(self, *args):
if not self.current_article \
or self.spinner_articles.text == "Choose article" \
or not self.article_parts:
return
if self.lines[self.line_number] == "__BM__\n":
return
os.chdir(os.path.join(self.working_directory, self.current_article))
# removing old bookmark
for file in glob.glob("*.txt"):
f = open(file, "r", encoding='utf-8', errors='ignore')
lines = f.readlines()
f.close()
old_text = "".join(lines)
if "__BM__\n" in old_text:
with open(file, "w") as ff:
new_text = old_text.replace("__BM__\n", "")
ff.write(new_text)
# making new bookmark
f = open(self.article_parts[self.article_part], "r", encoding='utf-8', errors='ignore')
self.lines = f.readlines()
f.close()
if self.line_number > 0:
self.lines.insert(self.line_number - 1, "__BM__\n")
else:
self.lines.insert(0, "__BM__\n")
f = open(self.article_parts[self.article_part], "w")
new_text = "".join(self.lines)
f.write(new_text)
f.close()
self.line_number = self.lines.index("__BM__\n")
f = open(self.article_parts[self.article_part], "r", encoding='utf-8', errors='ignore')
self.lines = f.readlines()
f.close()
os.chdir(self.working_directory)
popup = Popup(title='Bookmark added successfully',
size_hint = (None, None), size = (400, 400),
auto_dismiss=True)
popup.open()
def on_spinner_select(self, spinner, text):
self.current_article = text
os.chdir(os.path.join(self.working_directory, self.current_article))
self.article_parts = []
for f in glob.glob("*.txt"):
self.article_parts.append(f)
self.article_parts.sort()
self.article_part = 0
self.lines = []
self.line_number = 0
breaker = False
for idy, article_part in enumerate(self.article_parts):
f = open(article_part, "r", encoding='utf-8', errors='ignore')
lines = f.readlines()
f.close()
for idx, line in enumerate(lines):
if line == "__BM__\n": # found bookmark
self.line_number = idx
self.article_part = idy
breaker = True
break
if breaker:
break
if self.article_parts:
f = open(self.article_parts[self.article_part], "r", encoding='utf-8', errors='ignore')
self.lines = f.readlines()
f.close()
self.text_label.text = self.lines[self.line_number]
img_file_name = self.article_parts[self.article_part].replace(".txt", "")
images = [fn for fn in glob.glob(img_file_name + "*") if not fn.endswith("txt")]
if images:
img_file_name = images[0]
self.ms.ids.img.source = os.path.join(self.working_directory, self.current_article, img_file_name)
else:
self.ms.ids.img.source = ""
else:
self.text_label.text = ""
self.ms.ids.img.source = ""
self.lines = []
self.line_number = 0
os.chdir(self.working_directory)
def create_new_article_popup(self, *args):
p = Urlpup()
p.open()
def create_new_article(self, url):
if not "wikiped" in url:
popup = Popup(title='Not a Wikipedia article',
size_hint = (None, None), size = (400, 400),
auto_dismiss=True)
popup.open()
return
try:
soup = html2txt.get_soup_from_url(url)
except:
popup = Popup(title='No Internet connection or bad URL',
size_hint = (None, None), size = (400, 400),
auto_dismiss=True)
popup.open()
return
if (not url.startswith("http://")) and (not url.startswith("https://")):
popup = Popup(title='URL does not start with http:// or https://',
size_hint = (None, None), size = (400, 400),
auto_dismiss=True)
popup.open()
return
if not self.download_nltk():
return
os.chdir(self.data_dir)
sentences = html2txt.node_to_sentences(soup.find(id = "content"))
os.chdir(self.working_directory)
title = url.split("/")[-1]
language_code = url.split("/")[2][:2]
title = title[0].upper() + title[1:] + "_" + language_code
html2txt.save_article(url, title, sentences)
os.chdir("..")
self.spinner_articles.values = sorted([d for d in os.listdir(self.working_directory) \
if os.path.isdir(os.path.join(self.working_directory, d))])
os.chdir(self.working_directory)
popup = Popup(title='Article added successfully',
size_hint = (None, None), size = (400, 400),
auto_dismiss=True)
popup.open()
if __name__ == "__main__":
Tedegraph().run()