calculate image hash for images sent in chat

This commit is contained in:
bakatrouble 2022-12-06 13:13:46 +03:00
parent ed7035b74e
commit d1fa23e399
4 changed files with 512 additions and 299 deletions

View File

@ -43,6 +43,7 @@ qrtools = "*"
vkwave = "*" vkwave = "*"
imagehash = "*" imagehash = "*"
tqdm = "*" tqdm = "*"
tiktokapi = "*"
[dev-packages] [dev-packages]

800
Pipfile.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -98,7 +98,8 @@ class ChannelHelperBotModuleConfig(TelegramBotModuleConfig):
io.name = 'file.jpg' io.name = 'file.jpg'
bot.get_file(p[-1]).download(out=io) bot.get_file(p[-1]).download(out=io)
im = Image.open(io) im = Image.open(io)
if self.queued_items.filter(image_hash=imagehash.phash(im), type='photo').count() > 0: i.image_hash = imagehash.phash(im)
if self.queued_items.filter(image_hash=i.image_hash, type='photo').count() > 0:
return return
elif hasattr(m, 'sticker') and m.sticker: elif hasattr(m, 'sticker') and m.sticker:
s = m.sticker s = m.sticker

View File

@ -5,7 +5,9 @@ from tempfile import TemporaryDirectory
import boto3 import boto3
import filetype import filetype
import requests
import twitter import twitter
from TikTokApi import TikTokApi
from django.db import models from django.db import models
# from qrtools import QR # from qrtools import QR
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton, MessageEntity from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton, MessageEntity
@ -83,6 +85,7 @@ class RobotBotModuleConfig(TelegramBotModuleConfig):
for link in links: for link in links:
try: try:
twitter_matches = re.match(r'https?://twitter.com/[A-Za-z0-9_]{1,15}/status/(\d+).*', link) twitter_matches = re.match(r'https?://twitter.com/[A-Za-z0-9_]{1,15}/status/(\d+).*', link)
tiktok_matches = re.match(r'https?://vm.tiktok.com/.*', link)
if link.endswith('.gif'): if link.endswith('.gif'):
update.message.reply_document(link) update.message.reply_document(link)
elif twitter_matches: elif twitter_matches:
@ -102,6 +105,10 @@ class RobotBotModuleConfig(TelegramBotModuleConfig):
update.message.reply_video(max_bitrate_link) update.message.reply_video(max_bitrate_link)
except KeyError: except KeyError:
update.message.reply_text('Unable to retrieve video info from tweet') update.message.reply_text('Unable to retrieve video info from tweet')
elif tiktok_matches:
api = TikTokApi()
vid = api.video(link)
else: else:
update.message.reply_text('Can\'t detect link type') update.message.reply_text('Can\'t detect link type')
except BadRequest: except BadRequest: