-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
174 lines (134 loc) · 4.49 KB
/
Copy pathutils.py
File metadata and controls
174 lines (134 loc) · 4.49 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import hashlib
import logging
import json
import secrets
from functools import wraps
import multiprocessing.dummy as mp
from telegram import Bot, InputMediaPhoto, InlineKeyboardButton, InlineKeyboardMarkup
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
import bot_db as db
import settings
# Enable logging
logging.basicConfig(format='%(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Init bot
tg_bot = Bot(settings.TELEGRAM['token'])
def getListOfFiles(dirName):
"""
create a list of file and sub directories
names in the given directory
"""
listOfFile = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
allFiles.append(fullPath)
return allFiles
def sha256_checksum(filename, block_size=65536):
"""
return SHA-256 checksum of file
"""
sha256 = hashlib.sha256()
with open(filename, 'rb') as f:
for block in iter(lambda: f.read(block_size), b''):
sha256.update(block)
return sha256.hexdigest()
def jsonToRGB(string):
"""
decode color in JSON format stored in DB
"""
color = tuple(json.loads(string))
color1_rgb = sRGBColor(*color, is_upscaled=True)
return convert_color(color1_rgb, LabColor)
def _index_images_thread(file):
"""
single thread of image indexing
"""
db.Picture.addToIndex(filename=file)
def index_images():
"""
initiate image indexation
"""
file_list = getListOfFiles(settings.IMAGES_PATH)
with mp.Pool() as p:
p.map(_index_images_thread, file_list)
# db.session.commit()
tg_bot.send_message(
settings.TELEGRAM['sudo_users'][0],
'Indexing finished successfully')
def post_to_telegram(what=False):
"""
create post on channel
what: type of post. can be 'single' or 'compilation'
"""
# decide if it will be a single image or compilation
choice = secrets.randbelow(100)
if choice <= 30 or what == 'compilation': # post compilation
similar_images = db.Picture.getColorCompitation(
settings.COMPILATION_NUM)
media_to_send = [
InputMediaPhoto(open(image.filename, 'rb'))
for image in similar_images
]
msgs = tg_bot.send_media_group(
settings.TELEGRAM['channel_id'],
media_to_send)
for image in similar_images:
image.markAsPosted(msgs[0].media_group_id)
elif choice > 30 or what == 'single': # post single image
image = db.Picture.getRandomImage()
post = tg_bot.send_photo(
chat_id=settings.TELEGRAM['channel_id'],
photo=open(image.filename, 'rb'))
# Attach like button
update_like_button(settings.TELEGRAM['channel_id'], post.message_id)
# Mark image as posted
image.markAsPosted(post.message_id)
def update_like_button(chat_id, post_id):
keyboard = [[InlineKeyboardButton(
f"🤍 {(db.Likes.getCount(post_id) or '')}",
callback_data=f'like-{post_id}')]]
tg_bot.edit_message_reply_markup(
chat_id=chat_id,
message_id=post_id,
reply_markup=InlineKeyboardMarkup(keyboard))
def restricted(func):
"""
restrict access to commands
"""
@wraps(func)
def wrapped(update, context, *args, **kwargs):
user_id = update.effective_user.id
if user_id not in settings.TELEGRAM['sudo_users']:
logger.warning(f"Unauthorized access denied for {user_id}.")
return
return func(update, context, *args, **kwargs)
return wrapped
def getDBstats():
"""
get stats from database
"""
count_today = db.Picture.where(ts_posted__day_le=1).count()
count_total = (
db.session.query(db.func.count(db.Picture.id).label('count'))
.first().count)
last_post = (
db.session.query(db.Picture.ts_posted)
.filter(db.Picture.ts_posted.isnot(None))
.order_by(db.Picture.ts_posted.desc()).limit(1).first()).ts_posted
return {
'today': count_today,
'total': count_total,
'last_post': last_post,
}