forked from JasurbekNURBOYEV/hops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.py
281 lines (251 loc) · 9.82 KB
/
book.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
# BOOKS.py - to do all and all about books, our little library
# You are not gonna touch any bit of the code,
# otherwise you'll have to deal with tons of nightmares, trust me
from globals import *
import json
import time
from files import File as f
from datetime import datetime, timedelta
from dreamgraph import LogIn
from tools import log
client = LogIn('b3ece8779d454736c76d48b288028a250ec7bf5f25e75d897110826ade2d')
lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et' \
' dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ' \
'ex ea commodo consequat.'
langs = {'uz': 'o\'zbek', 'en': 'ingliz', 'ru': 'rus'}
books = [
{'type': 'pdf', 'download': 'https://t.me/HopsBooks/5', 'url': False, 'lang': 'en', 'size': '24.8', 'title':
'Learning Python', 'author': 'Mark Lutz'}
]
def maker(book=None, ind=0, comments=False):
if not comments:
try:
comments = json.loads(f.open('./books/comments/book_{}.db'.format(ind), 'r').read())
except:
f.open('./books/comments/book_{}.db'.format(ind), 'w').write(json.dumps({'all': 0}))
comments = {'all': 0}
if not book:
all_books = start()
book = all_books[ind]
result = []
br = {'tag': 'br'}
result.append(br)
result.append({'tag': 'b', 'children': ['Muallif: {}'.format(book['author'])]})
result.append(br)
result.append({'tag': 'b', 'children': ['Til: {}'.format(langs[book['lang']])]})
result.append(br)
result.append({'tag': 'b', 'children': ['Turi: {}'.format(book['type'].upper())]})
result.append(br)
result.append({'tag': 'b', 'children': ['Hajmi: {} MB'.format(book['size'])]})
result.append(br)
result.append({'tag': 'a', 'attrs':{'href': book['download']}, 'children': ['⬇️YUKLAB OLISH']})
result.append({'tag': 'br'})
result.append({'tag': 'aside', 'children': ['Kitob haqida fikrlar']})
if comments['all'] > 0:
for person in sorted(list(comments.keys())):
if person == 'all':
continue
log(0, person, comments)
log(list(comments.keys()))
log(comments[person])
try:
result.append({'tag': 'pre', 'children': [comments[person]['name']]})
except Exception as e:
return '1. ' + str(e)
for post in comments[person]['posts']:
try:
result.append({'tag': 'code', 'children': [post['date']]})
result.append({'tag': 'blockquote', 'children': [post['text']]})
except Exception as e:
return '2. ' + str(e)
else:
result.append({'tag': 'i', 'children': ['Hozircha izohlar mavjud emas']})
return result
def start():
global books
try:
books = json.loads(f.open('./books/all.db', 'r').read())
except:
bot.send_message(dev, str(books))
result = maker(book=books[0], ind=0)
page = client.create_page(
title=books[0]['title'],
content=result,
author_name='Hops',
author_url='https://t.me/Hopsrobot'
)
books[0]['url'] = page.url
f.open('./books/all.db', 'w').write(json.dumps(books))
return books
start()
def represent(book, ind=0):
if book['url']:
return book['url']
else:
result = maker(book)
page = client.create_page(
title=book['title'],
author_name='Hops',
author_url='https://t.me/Hopsrobot',
content=result
)
return page.url
def books_in_keyboards(all_books, first=False, go=False, back=False, page_number=0):
counter = page_number
mark = telebot.types.InlineKeyboardMarkup()
for i in range(page_number, page_number + 5):
try:
single = telebot.types.InlineKeyboardButton(
text='{}. {}'.format(counter, all_books[i]['title']),
url=all_books[i]['url']
)
mark.add(single)
counter += 1
except:
pass
if go:
if len(all_books[page_number:]) > 5:
go_button = telebot.types.InlineKeyboardButton('Keyingisi >',
callback_data='booknextpage_{}'.format(page_number + 5))
back_button = telebot.types.InlineKeyboardButton('< Oldingisi',
callback_data='bookbackpage_{}'.format(page_number - 5))
mark.row(back_button, go_button)
else:
back_button = telebot.types.InlineKeyboardButton('< Oldingisi',
callback_data='bookbackpage_{}'.format(page_number - 5))
mark.row(back_button)
elif back:
if len(all_books[:page_number]) >= 5:
go_button = telebot.types.InlineKeyboardButton('Keyingisi >',
callback_data='booknextpage_{}'.format(page_number + 5))
back_button = telebot.types.InlineKeyboardButton('< Oldingisi',
callback_data='bookbackpage_{}'.format(page_number - 5))
mark.row(back_button, go_button)
else:
go_button = telebot.types.InlineKeyboardButton('Keyingisi >',
callback_data='booknextpage_{}'.format(page_number + 5))
mark.row(go_button)
elif first:
if len(all_books[page_number:]) > 5:
go_button = telebot.types.InlineKeyboardButton('Keyingisi >',
callback_data='booknextpage_{}'.format(page_number + 5))
mark.row(go_button)
return mark
def add_comment(book_id, uid, text, name):
all_books = json.loads(f.open('./books/all.db', 'r').read())
atm = str(time.time()).split('.')[0]
date = (datetime.now() + timedelta(hours=5)).strftime('%Y.%m.%d')
user = '{}_{}'.format(atm, uid)
try:
comments = json.loads(f.open('./books/comments/book_{}.db'.format(book_id), 'r').read())
except:
f.open('./books/comments/book_{}.db'.format(book_id), 'w').write(json.dumps({'all': 0}))
comments = {'all': 0}
if comments['all'] > 0:
recorded = False
for key in list(comments.keys()):
if str(uid) in key:
comments[key]['posts'].append({'text': text, 'date': date})
recorded = True
if not recorded:
comments[user] = {
'name': name,
'posts': [
{
'text': text,
'date': date
}
]
}
else:
comments[user] = {
'name': name,
'posts': [
{
'text': text,
'date': date
}
]
}
comments['all'] += 1
f.open('./books/comments/book_{}.db'.format(book_id), 'w').write(json.dumps(comments))
result = maker(all_books[book_id], ind=book_id, comments=comments)
if all_books[book_id]['url']:
client.edit_page(
path=all_books[book_id]['url'][18:],
title=all_books[book_id]['title'],
content=result,
author_name='Hops',
author_url='https://t.me/Hopsrobot'
)
else:
page = client.create_page(
title=all_books[book_id]['title'],
content=result,
author_name='Hops',
author_url='https://t.me/Hopsrobot'
)
all_books[book_id]['url'] = page.url
f.open('./books/all.db', 'w').write(json.dumps(all_books))
def add_book(data):
data = data.split('\n')
title = data[1]
author = data[2]
lang = data[3]
tip = data[4]
size = data[5]
download = data[6]
book_id = len(books)
new_book = {
'title': title,
'author': author,
'lang': lang,
'type': tip,
'size': size,
'download': download
}
result = maker(book=new_book, ind=book_id)
page = client.create_page(
title=title,
content=result,
author_name='Hops',
author_url='https://t.me/Hopsrobot'
)
new_book['url'] = page.url
books.append(new_book)
f.open('./books/all.db', 'w').write(json.dumps(books))
def del_book(book_id):
all_books = start()
books.pop(book_id)
f.open('./books/all.db', 'w').write(json.dumps(all_books))
def del_comment(data):
data = data.split('\n')
book_id = int(data[1])
starting_text = data[2]
date = data[3]
try:
comments = json.loads(f.open('./books/comments/book_{}.db'.format(book_id), 'r').read())
except:
f.open('./books/comments/book_{}.db'.format(book_id), 'w').write(json.dumps({'all': 0}))
comments = {'all': 0}
for user in list(comments.keys()):
if user == 'all':
continue
for post in comments[user]['posts']:
if post['text'].lower().startswith(starting_text.lower()) and post['date'] == date:
if len(comments[user]['posts']) > 1:
comments[user]['posts'].remove(post)
else:
comments.pop(user)
comments['all'] -= 1
result = maker(ind=book_id, comments=comments)
all_books = start()
client.edit_page(
path=all_books[book_id]['url'][18:],
title=all_books[book_id]['title'],
content=result,
author_name='Hops',
author_url='https://t.me/Hopsrobot'
)
f.open('./books/comments/book_{}.db'.format(book_id), 'w').write(json.dumps(comments))
# The End