1
0
Fork 0

Indexing chats: work in progress

This commit is contained in:
Profitroll 2022-09-01 15:21:22 +02:00
parent 6848f4a124
commit 99764a9523
3 changed files with 131 additions and 12 deletions

View File

@ -1,11 +1,11 @@
from traceback import print_exc
from ujson import loads, dumps # type: ignore
from ujson import loads, dumps, JSONDecodeError # type: ignore
from shutil import copyfileobj
from gzip import open as gzipopen
from sys import exit
from os import makedirs, stat
from time import localtime, strftime
from time import localtime, strftime, time
from datetime import datetime
from modules.colors import *
@ -38,7 +38,7 @@ def jsonLoad(filename):
# Save data to json file
def jsonSave(filename, value):
with open(filename, 'w', encoding="utf-8") as f:
f.write(dumps(value, indent=4))
f.write(dumps(value, indent=4, ensure_ascii=False))
f.close()
# Get config key's value
@ -49,6 +49,31 @@ def configGet(key: str):
print("Could not find config.json in script's directory. Please copy config_example.json to config.json and configure it. After that restart this program.")
exit()
def indexId(chatid, title):
makedirs("data", exist_ok=True)
try:
index = jsonLoad("data/index.json")
except FileNotFoundError:
index = {}
except JSONDecodeError:
index = {}
if str(chatid) in index:
if str(title) in index[str(chatid)]:
return
else:
index[str(chatid)][str(title)] = time()
else:
index[str(chatid)] = {
str(title): time()
}
jsonSave("data/index.json", index)
# Check latest log size
def checkSize(file="latest"):

View File

@ -15,6 +15,8 @@ def any_message_handler(_, message):
if configGet("debug") == True:
print(message)
indexId(message.chat.id, getChatName(message))
try:
lowered = message.text.lower()
except:

View File

@ -16,28 +16,48 @@ def getpic(_, msg):
@app.on_message(filters.photo)
def get_image(_, photo):
try:
from_name = photo.from_user.first_name
except:
from_name = "UNKNOWN"
try:
from_name = photo.author_signature
except:
from_name = "UNKNOWN"
indexId(photo.chat.id, getChatName(photo))
#print(photo)
print(f'{BBLACK}[{CYAN}{getTime(photo.date)}{BBLACK}] [{BGREEN}IMAGE{BBLACK}] {RESET}{from_name} ({getChatName(photo)}) {BBLACK}» {RESET}{photo.caption} {BBLACK}| {RESET}{photo.photo.file_id}')
appendLog(f'[{getDateTime(photo.date)}] [IMAGE] {from_name} ({getChatName(photo)}) » {photo.caption} | {photo.photo.file_id}', file=photo.chat.id)
if configGet("save_media"):
makedirs(f"logs/chats/{photo.chat.id}/images", exist_ok=True)
_.download_media(photo, file_name=f"logs/chats/{photo.chat.id}/images/")
#=================================================================================
@app.on_message(filters.document)
def get_file(_, document):
try:
from_name = document.from_user.first_name
except:
from_name = "UNKNOWN"
try:
from_name = document.author_signature
except:
from_name = "UNKNOWN"
indexId(document.chat.id, getChatName(document))
#print(document)
print(f'{BBLACK}[{CYAN}{getTime(document.date)}{BBLACK}] [{BGREEN}FILE{BBLACK}] {RESET}{from_name} ({getChatName(document)}) {BBLACK}» {RESET}{document.caption} {BBLACK}| {RESET}{document.document.file_id}')
appendLog(f'[{getDateTime(document.date)}] [FILE] {from_name} ({getChatName(document)}) » {document.caption} | {document.document.file_id}', file=document.chat.id)
if configGet("save_media"):
makedirs(f"logs/chats/{document.chat.id}/files", exist_ok=True)
_.download_media(document, file_name=f"logs/chats/{document.chat.id}/files/")
# Get file from id
@app.on_message(filters.command(["doc", "document", "file"], prefixes=".") & filters.me)
def getfile(_, msg):
@ -47,15 +67,29 @@ def getfile(_, msg):
@app.on_message(filters.video)
def get_video(_, video):
try:
from_name = video.from_user.first_name
except:
try:
from_name = video.author_signature
except:
from_name = "UNKNOWN"
try:
from_name = video.from_user.first_name
except:
from_name = "UNKNOWN"
indexId(video.chat.id, getChatName(video))
#print(video)
print(f'{BBLACK}[{CYAN}{getTime(video.date)}{BBLACK}] [{BGREEN}VIDEO{BBLACK}] {RESET}{from_name} ({getChatName(video)}) {BBLACK}» {RESET}{video.caption} {BBLACK}| {RESET}{video.video.file_id}')
appendLog(f'[{getDateTime(video.date)}] [VIDEO] {from_name} ({getChatName(video)}) » {video.caption} | {video.video.file_id}', file=video.chat.id)
if configGet("save_media"):
makedirs(f"logs/chats/{video.chat.id}/videos", exist_ok=True)
_.download_media(video, file_name=f"logs/chats/{video.chat.id}/videos/")
# Get video from id
@app.on_message(filters.command(["vid", "mov", "video", "movie"], prefixes=".") & filters.me)
def getvideo(_, msg):
@ -65,15 +99,25 @@ def getvideo(_, msg):
@app.on_message(filters.animation)
def get_animation(_, animation):
try:
from_name = animation.from_user.first_name
except:
from_name = "UNKNOWN"
try:
from_name = animation.author_signature
except:
from_name = "UNKNOWN"
indexId(animation.chat.id, getChatName(animation))
#print(animation)
print(f'{BBLACK}[{CYAN}{getTime(animation.date)}{BBLACK}] [{BGREEN}GIF{BBLACK}] {RESET}{from_name} ({getChatName(animation)}) {BBLACK}» {RESET}{animation.caption} {BBLACK}| {RESET}{animation.animation.file_id}')
appendLog(f'[{getDateTime(animation.date)}] [GIF] {from_name} ({getChatName(animation)}) » {animation.caption} | {animation.animation.file_id}', file=animation.chat.id)
if configGet("save_media"):
makedirs(f"logs/chats/{animation.chat.id}/gifs", exist_ok=True)
_.download_media(animation, file_name=f"logs/chats/{animation.chat.id}/gifs/")
# Get animation from id
@app.on_message(filters.command(["gif", "ani", "animation"], prefixes=".") & filters.me)
def getanimation(_, msg):
@ -83,15 +127,25 @@ def getanimation(_, msg):
@app.on_message(filters.voice)
def get_voice(_, voice):
try:
from_name = voice.from_user.first_name
except:
from_name = "UNKNOWN"
try:
from_name = voice.author_signature
except:
from_name = "UNKNOWN"
indexId(voice.chat.id, getChatName(voice))
#print(voice)
print(f'{BBLACK}[{CYAN}{getTime(voice.date)}{BBLACK}] [{BGREEN}VOICE{BBLACK}] {RESET}{from_name} ({getChatName(voice)}) {BBLACK}» {RESET}{voice.caption} {BBLACK}| {RESET}{voice.voice.file_id}')
appendLog(f'[{getDateTime(voice.date)}] [VOICE] {from_name} ({getChatName(voice)}) » {voice.caption} | {voice.voice.file_id}', file=voice.chat.id)
if configGet("save_media"):
makedirs(f"logs/chats/{voice.chat.id}/voice", exist_ok=True)
_.download_media(voice, file_name=f"logs/chats/{voice.chat.id}/voice/")
# Get voice from id
@app.on_message(filters.command(["voi", "rec", "voice", "recording"], prefixes=".") & filters.me)
def getvoice(_, msg):
@ -101,15 +155,25 @@ def getvoice(_, msg):
@app.on_message(filters.audio)
def get_audio(_, audio):
try:
from_name = audio.from_user.first_name
except:
from_name = "UNKNOWN"
try:
from_name = audio.author_signature
except:
from_name = "UNKNOWN"
indexId(audio.chat.id, getChatName(audio))
#print(audio)
print(f'{BBLACK}[{CYAN}{getTime(audio.date)}{BBLACK}] [{BGREEN}AUDIO{BBLACK}] {RESET}{from_name} ({getChatName(audio)}) {BBLACK}» {RESET}{audio.caption} {BBLACK}| {RESET}{audio.audio.file_id}')
appendLog(f'[{getDateTime(audio.date)}] [AUDIO] {from_name} ({getChatName(audio)}) » {audio.caption} | {audio.audio.file_id}', file=audio.chat.id)
if configGet("save_media"):
makedirs(f"logs/chats/{audio.chat.id}/audio", exist_ok=True)
_.download_media(audio, file_name=f"logs/chats/{audio.chat.id}/audio/")
# Get audio from id
@app.on_message(filters.command(["aud", "snd", "audio", "sound", "music"], prefixes=".") & filters.me)
def getaudio(_, msg):
@ -119,15 +183,25 @@ def getaudio(_, msg):
@app.on_message(filters.video_note)
def get_video_note(_, video_note):
try:
from_name = video_note.from_user.first_name
except:
from_name = "UNKNOWN"
try:
from_name = video_note.author_signature
except:
from_name = "UNKNOWN"
indexId(video_note.chat.id, getChatName(video_note))
#print(video_note)
print(f'{BBLACK}[{CYAN}{getTime(video_note.date)}{BBLACK}] [{BGREEN}VIDNT{BBLACK}] {RESET}{from_name} ({getChatName(video_note)}) {BBLACK}» {RESET}{video_note.caption} {BBLACK}| {RESET}{video_note.video_note.file_id}')
appendLog(f'[{getDateTime(video_note.date)}] [VIDNT] {from_name} ({getChatName(video_note)}) » {video_note.caption} | {video_note.video_note.file_id}', file=video_note.chat.id)
if configGet("save_media"):
makedirs(f"logs/chats/{video_note.chat.id}/circles", exist_ok=True)
_.download_media(video_note, file_name=f"logs/chats/{video_note.chat.id}/circles/")
# Get video note from id
@app.on_message(filters.command(["vdn", "vnt", "videonote", "face", "circle"], prefixes=".") & filters.me)
def getvideonote(_, msg):
@ -137,10 +211,16 @@ def getvideonote(_, msg):
@app.on_message(filters.sticker)
def get_sticker(_, sticker):
try:
from_name = sticker.from_user.first_name
except:
from_name = "UNKNOWN"
try:
from_name = sticker.author_signature
except:
from_name = "UNKNOWN"
indexId(sticker.chat.id, getChatName(sticker))
#print(sticker)
print(f'{BBLACK}[{CYAN}{getTime(sticker.date)}{BBLACK}] [{BGREEN}STICK{BBLACK}] {RESET}{from_name} ({getChatName(sticker)}) {BBLACK}» {RESET}{sticker.caption} {BBLACK}| {RESET}{sticker.sticker.file_id}')
@ -155,10 +235,16 @@ def getsticker(_, msg):
@app.on_message(filters.location)
def get_location(_, location):
try:
from_name = location.from_user.first_name
except:
from_name = "UNKNOWN"
try:
from_name = location.author_signature
except:
from_name = "UNKNOWN"
indexId(location.chat.id, getChatName(location))
#print(location)
print(f'{BBLACK}[{CYAN}{getTime(location.date)}{BBLACK}] [{BGREEN}PLACE{BBLACK}] {RESET}{from_name} ({getChatName(location)}) {BBLACK}» {RESET}{location.caption} {BBLACK}| {RESET}longitude: {location.location.longitude}, latitude: {location.location.latitude}')
@ -168,10 +254,16 @@ def get_location(_, location):
@app.on_message(filters.contact)
def get_contact(_, contact):
try:
from_name = contact.from_user.first_name
except:
from_name = "UNKNOWN"
try:
from_name = contact.author_signature
except:
from_name = "UNKNOWN"
indexId(contact.chat.id, getChatName(contact))
#print(contact)
print(f'{BBLACK}[{CYAN}{getTime(contact.date)}{BBLACK}] [{BGREEN}STICK{BBLACK}] {RESET}{from_name} ({getChatName(contact)}) {BBLACK}» {RESET}{contact.caption} {BBLACK}| {RESET}name: {contact.contact.first_name}, number: {contact.contact.phone_number}, uid: {contact.contact.user_id}')