-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
289 lines (211 loc) · 8.28 KB
/
backend.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
import os
import json
import telebot
from telebot.types import InputMediaPhoto, InputMediaVideo
def create_dirs():
tg_path = ['accounts', 'tg']
vk_path = ['accounts', 'vk']
buffer_path = ['accounts', 'buffer']
unify_profiles_path = ['accounts', 'profiles']
tg_path = os.path.join(*tg_path)
vk_path = os.path.join(*vk_path)
buffer_path = os.path.join(*buffer_path)
unify_profiles_path = os.path.join(*unify_profiles_path)
os.makedirs(tg_path, exist_ok=True)
os.makedirs(vk_path, exist_ok=True)
os.makedirs(buffer_path, exist_ok=True)
os.makedirs(unify_profiles_path, exist_ok=True)
return tg_path, vk_path, buffer_path, unify_profiles_path
def create_media_group(media_list):
media_group = []
for media in media_list:
media_type = list(media.keys())[0]
file_id = media[media_type]
if media_type == 'photo':
media_group.append(InputMediaPhoto(media=file_id))
elif media_type == 'video':
media_group.append(InputMediaVideo(media=file_id))
return media_group
# region tg json
def tg_json_add_media(user_id, channel, media_type, media_id):
'''
:param username: юзернейм пользователя
:param channel: подключенный канал пользователя
:param file_type: приемлeмые типы: Photo, Video
:param file_address: адресс файла на серверах телеграмм. Получается через message
:return:
'''
json_path = os.path.join(TG_BASE_PATH, str(user_id))
with open(json_path, "r") as file:
data = json.load(file)
new_file = {
media_type: media_id
}
data[channel]["media"].append(new_file)
with open(json_path, "w") as file:
json.dump(data, file, indent=4)
def tg_json_add_video_note(user_id, channel, video_note_id):
json_path = os.path.join(TG_BASE_PATH, str(user_id))
with open(json_path, "r") as file:
data = json.load(file)
new_file = {
'video_note': video_note_id
}
data[channel]["video_note"].append(new_file)
with open(json_path, "w") as file:
json.dump(data, file, indent=4)
def tg_json_get_media(user_id, channel_id):
json_path = os.path.join(TG_BASE_PATH, str(user_id))
with open(json_path, "r") as file:
data = json.load(file)
media = data[channel_id]['media']
return media
def tg_json_add_text(user_id, channel, text):
json_path = os.path.join(TG_BASE_PATH, str(user_id))
with open(json_path, "r") as file:
data = json.load(file)
data[channel]['text'] = text
with open(json_path, "w") as file:
json.dump(data, file, indent=4)
def tg_json_get_text(user_id, channel):
json_path = os.path.join(TG_BASE_PATH, str(user_id))
with open(json_path, "r") as file:
data = json.load(file)
text = data[channel]['text']
return text
def tg_json_get_video_note(user_id, channel):
json_path = os.path.join(TG_BASE_PATH, str(user_id))
with open(json_path, "r") as file:
data = json.load(file)
video_note = data[channel]['video_note']
return video_note
def tg_json_delete_media(user_id, channel):
json_path = os.path.join(TG_BASE_PATH, str(user_id))
with open(json_path, "r") as file:
data = json.load(file)
data[channel]["media"] = []
with open(json_path, "w") as file:
json.dump(data, file, indent=4)
def tg_json_delete_text(user_id, channel):
json_path = os.path.join(TG_BASE_PATH, str(user_id))
with open(json_path, "r") as file:
data = json.load(file)
data[channel]["text"] = None
with open(json_path, "w") as file:
json.dump(data, file, indent=4)
def tg_json_delete_video_note(user_id, channel):
json_path = os.path.join(TG_BASE_PATH, str(user_id))
with open(json_path, "r") as file:
data = json.load(file)
data[channel]["video_note"] = []
with open(json_path, "w") as file:
json.dump(data, file, indent=4)
def tg_json_add_channel(user_id, channel):
json_path = os.path.join(TG_BASE_PATH, str(user_id))
with open(json_path, "r") as file:
data = json.load(file)
data[channel] = {'text': None, 'media': [], 'video_note': []}
with open(json_path, "w") as file:
json.dump(data, file, indent=4)
def create_tg_json(user_id):
tg_user_path = os.path.join(TG_BASE_PATH, str(user_id))
with open(tg_user_path, 'w') as file:
data = {}
json.dump(data, file, indent=4)
# endregion
#region buffer
def create_buffer(user_id):
buffer_path = os.path.join(BUFFER_BASE_PATH, str(user_id))
with open(buffer_path, 'w') as file:
data = {'text': None,
'media':[],
'specials':[]}
json.dump(data, file, indent=4)
def update_buffer_text(user_id, text):
buffer_path = os.path.join(BUFFER_BASE_PATH, str(user_id))
with open(buffer_path, 'r') as file:
data = json.load(file)
data['text'] = text
with open(buffer_path, 'w') as file:
json.dump(data, file, indent=4)
def get_buffer_text(user_id):
buffer_path = os.path.join(BUFFER_BASE_PATH, str(user_id))
with open(buffer_path, 'r') as file:
data = json.load(file)
text = data['text']
return text
def update_buffer_media(user_id, media_type, media_id):
buffer_path = os.path.join(BUFFER_BASE_PATH, str(user_id))
with open(buffer_path, 'r') as file:
data = json.load(file)
data['media'].append({media_type:media_id})
with open(buffer_path, 'w') as file:
json.dump(data, file, indent=4)
def update_buffer_specials(user_id, special_type, special_id):
buffer_path = os.path.join(BUFFER_BASE_PATH, str(user_id))
with open(buffer_path, 'r') as file:
data = json.load(file)
data['specials'].append({special_type:special_id})
with open(buffer_path, 'w') as file:
json.dump(data, file, indent=4)
def update_buffer_video_note(user_id, video_id):
update_buffer_specials(user_id, 'video_note', video_id)
def update_buffer_story(user_id, video_id):
update_buffer_specials(user_id, 'story', video_id)
def clear_buffer(user_id):
buffer_path = os.path.join(BUFFER_BASE_PATH, str(user_id))
with open(buffer_path, 'r') as file:
data = json.load(file)
data['media'] = []
data['specials'] = []
data['text'] = None
with open(buffer_path, 'w') as file:
json.dump(data, file, indent=4)
def get_buffer_media(user_id):
buffer_path = os.path.join(BUFFER_BASE_PATH, str(user_id))
with open(buffer_path, 'r') as file:
data = json.load(file)
media = data['media']
return media
def get_buffer_special(user_id):
buffer_path = os.path.join(BUFFER_BASE_PATH, str(user_id))
with open(buffer_path, 'r') as file:
data = json.load(file)
media = data['specials']
return media
# endregion
# region unify profiles
def create_profile_json(user_id):
prof_path = os.path.join(PROFILES_BASE_PATH, str(user_id))
with open(prof_path, 'w') as file:
data = {'unify':'not',
'name':None,
'sex':None,
'age':None,
'profession':None,
'hobbies':None,
'hashtags':None,
'content': None,
'emoji':None}
json.dump(data, file, indent=4)
def update_profile(user_id, type, text):
prof_path = os.path.join(PROFILES_BASE_PATH, str(user_id))
with open(prof_path, 'r') as file:
data = json.load(file)
data[type] = text
with open(prof_path, 'w') as file:
json.dump(data, file, indent=4)
def get_profile_atr(user_id, type):
prof_path = os.path.join(PROFILES_BASE_PATH, str(user_id))
with open(prof_path, 'r') as file:
data = json.load(file)
info = data[type]
return info
def need_unify(user_id):
unify = get_profile_atr(user_id, 'unify')
if unify == 'ok':
return True
else:
return False
#endregion
TG_BASE_PATH, VK_BASE_PATH, BUFFER_BASE_PATH, PROFILES_BASE_PATH = create_dirs()