-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_parser.py
100 lines (92 loc) · 3.99 KB
/
file_parser.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
import os
import telegram
class Parser:
def __init__(self, path):
self.__image = None
self.__text = ""
self.__markup = []
self.__file = None
if path[:5] != "file=":
file = open("files" + path, "r", encoding="utf-8")
while True:
line = file.readline()
if not line:
break
if line.strip() == "Image=":
self.__image = file.readline().strip()
file.readline()
elif line.strip() == "Text=":
while True:
textline = file.readline()
if not textline or textline.strip() == "=EndText":
break
self.__text += textline
elif line.strip() == "Buttons=":
while True:
button_type = file.readline().strip()
if not button_type or button_type == "=EndButtons":
break
goto = file.readline().strip()
text = file.readline().strip()
if button_type == "link":
self.__markup.append(
[telegram.InlineKeyboardButton(text=text, url=goto)]
)
elif button_type == "local":
self.__markup.append(
[
telegram.InlineKeyboardButton(
text=text, callback_data=goto
)
]
)
elif line.strip() == "DoubleButtons=":
while True:
button_row = []
button_type = file.readline().strip()
if not button_type or button_type == "=EndDoubleButtons":
break
goto = file.readline().strip()
text = file.readline().strip()
if button_type == "link":
button_row.append(
telegram.InlineKeyboardButton(text=text, url=goto)
)
elif button_type == "local":
button_row.append(
telegram.InlineKeyboardButton(
text=text, callback_data=goto
)
)
button_type = file.readline().strip()
goto = file.readline().strip()
text = file.readline().strip()
if button_type == "link":
button_row.append(
telegram.InlineKeyboardButton(text=text, url=goto)
)
elif button_type == "local":
button_row.append(
telegram.InlineKeyboardButton(
text=text, callback_data=goto
)
)
self.__markup.append(button_row)
file.close()
else:
self.__file = path[5:]
def get_file(self):
if self.__file and os.path.isfile("files" + self.__file):
return open("files" + self.__file, "rb")
return False
def get_text(self):
return self.__text
def get_markup(self):
return telegram.InlineKeyboardMarkup(self.__markup)
def get_image(self, as_media_photo=False):
if not self.__image:
return None
if not as_media_photo:
return open("files" + self.__image, "rb")
else:
return telegram.InputMediaPhoto(media=open("files" + self.__image, "rb"))