Skip to content

Commit

Permalink
added USE_CAPTION_FILTER & AUTH_USERS
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahesh0253 committed Dec 13, 2020
1 parent e9e6da4 commit da7e48e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.session-journal
.vscode
*test.py
setup.cfg

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
```sh
python3 -m venv env
. ./env/bin/activate
pip install -r requirements.txt
# <Edit info.py with variables as given below>
python bot.py
pip3 install -r requirements.txt
# Edit info.py with variables as given below
python3 bot.py
```
Check `sample_info.py` before editing `info.py` file

Expand All @@ -37,6 +37,8 @@ Check `sample_info.py` before editing `info.py` file
* `COLLECTION_NAME`: Name of the collections. Defaults to Telegram_files. If you going to use same database, then use different collection name for each bot
* `MAX_RESULTS`: Maximum limit for inline search results
* `CACHE_TIME`: The maximum amount of time in seconds that the result of the inline query may be cached on the server
* `USE_CAPTION_FILTER`: Whether bot should use captions to improve search results. (True/False)
* `AUTH_USERS`: Username or ID of users to give access of inline search. Separate multiple users by space. Leave it empty if you don't want to restrict bot usage.

### Admin commands
```
Expand Down
12 changes: 11 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
"description": "Username or ID of Admin. Separate multiple Admins by space.",
"value": ""
},
"AUTH_USERS": {
"description": "Username or ID of users to give access of inline search. Separate multiple users by space.\nLeave it empty if you don't want to restrict bot usage.",
"value": "",
"required": false
},
"USE_CAPTION_FILTER": {
"description": "Whether bot should use captions to improve search results. (True False)",
"value": "False",
"required": false
},
"DATABASE_URI": {
"description": "mongoDB URI. Get this value from https://www.mongodb.com. For more help watch this video - https://youtu.be/dsuTn4qV2GA",
"value": ""
Expand All @@ -44,7 +54,7 @@
"value": ""
},
"COLLECTION_NAME": {
"description": "Name of the collections. Defaults to Telegram_files. If you going to use same database, then use different collection name for each bot",
"description": "Name of the collections. Defaults to Telegram_files. If you are using the same database, then use different collection name for each bot",
"value": "Telegram_files",
"required": false
},
Expand Down
6 changes: 4 additions & 2 deletions info.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
# Bot settings
MAX_RESULTS = int(environ.get('MAX_RESULTS', 10))
CACHE_TIME = int(environ.get('CACHE_TIME', 300))
USE_CAPTION_FILTER = bool(environ.get('USE_CAPTION_FILTER', False))

# Admins & Channels
# Admins, Channels & Users
ADMINS = [int(admin) if re.search('^\d+$', admin) else admin for admin in environ['ADMINS'].split()]
CHANNELS = [int(channel) if re.search('^.\d+$', channel) else channel for channel in environ['CHANNELS'].split()]
CHANNELS = [int(ch) if re.search('^.\d+$', ch) else ch for ch in environ['CHANNELS'].split()]
AUTH_USERS = [int(user) if re.search('^\d+$', user) else user for user in environ['AUTH_USERS'].split()]

# MongoDB information
DATABASE_URI = environ['DATABASE_URI']
Expand Down
4 changes: 2 additions & 2 deletions one_time_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

async def main():
"""Save old files in database with the help of user bot"""

user_bot = Client(USER_SESSION, API_ID, API_HASH)
bot = Client(SESSION, API_ID, API_HASH, bot_token=BOT_TOKEN)

Expand All @@ -26,7 +26,7 @@ async def main():
message = await bot.get_messages(
channel,
user_message.message_id,
replies=0
replies=0,
)
for file_type in ("document", "video", "audio"):
media = getattr(message, file_type, None)
Expand Down
3 changes: 1 addition & 2 deletions plugins/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ async def answer(bot, query):
reply_markup=reply_markup))

if results:
count = len(results)
switch_pm_text = f"{emoji.FILE_FOLDER} {count} Result{'s' if count > 1 else ''}"
switch_pm_text = f"{emoji.FILE_FOLDER} Results"
if string:
switch_pm_text += f" for {string}"

Expand Down
8 changes: 5 additions & 3 deletions sample_info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Bot information
SESSION ='Media_search'
SESSION = 'Media_search'
USER_SESSION = 'User_Bot'
API_ID = 12345
API_HASH = '0123456789abcdef0123456789abcdef'
Expand All @@ -8,15 +8,17 @@
# Bot settings
MAX_RESULTS = 10
CACHE_TIME = 300
USE_CAPTION_FILTER = False

# Admins & Channels
# Admins, Channels & Users
ADMINS = [12345789, 'admin123', 98765432]
CHANNELS = [-10012345678, -100987654321, 'channelusername']
AUTH_USERS = []

# MongoDB information
DATABASE_URI = "mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb]?retryWrites=true&w=majority"
DATABASE_NAME = 'Telegram'
COLLECTION_NAME ='channel_files' # If you going to use same database, then use different collection name for each bot
COLLECTION_NAME = 'channel_files' # If you are using the same database, then use different collection name for each bot

# Messages
START_MSG = """
Expand Down
20 changes: 15 additions & 5 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from umongo import Instance, Document, fields
from motor.motor_asyncio import AsyncIOMotorClient
from marshmallow.exceptions import ValidationError
from info import DATABASE_URI, DATABASE_NAME, COLLECTION_NAME
from info import DATABASE_URI, DATABASE_NAME, COLLECTION_NAME, USE_CAPTION_FILTER

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -67,7 +67,11 @@ async def get_search_results(query, file_type=None, max_results=10, offset=0):
except:
return []

filter = {'file_name': regex}
if USE_CAPTION_FILTER:
filter = {'$or': [{'file_name': regex}, {'caption': regex}]}
else:
filter = {'file_name': regex}

if file_type:
filter['file_type'] = file_type

Expand All @@ -77,6 +81,12 @@ async def get_search_results(query, file_type=None, max_results=10, offset=0):
if next_offset > total_results:
next_offset = ''

results = await Media.find(filter).sort(
'$natural', -1).skip(offset).limit(max_results).to_list(length=max_results)
return results, next_offset
cursor = Media.find(filter)
# Sort by recent
cursor.sort('$natural', -1)
# Slice files according to offset and max results
cursor.skip(offset).limit(max_results)
# Get list of files
files = await cursor.to_list(length=max_results)

return files, next_offset

0 comments on commit da7e48e

Please sign in to comment.