Skip to content

Commit

Permalink
Updated to pyrogram v1.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahesh0253 committed Jan 1, 2021
1 parent a1e013a commit 107c79a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
https://github.com/Mahesh0253/pyrogram/archive/beta.zip
https://github.com/Mahesh0253/pyrogram/archive/inline.zip
tgcrypto
umongo
motor
Expand Down
49 changes: 47 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import re
import base64
import logging
from struct import pack

from pyrogram.file_id import FileId
from pymongo.errors import DuplicateKeyError
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, USE_CAPTION_FILTER

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -31,10 +36,13 @@ class Meta:
async def save_file(media):
"""Save file in database"""

# TODO: Find better way to get same file_id for same media to avoid duplicates
file_id, file_ref = unpack_new_file_id(media.file_id)

try:
file = Media(
file_id=media.file_id,
file_ref=media.file_ref,
file_id=file_id,
file_ref=file_ref,
file_name=media.file_name,
file_size=media.file_size,
file_type=media.file_type,
Expand Down Expand Up @@ -90,3 +98,40 @@ async def get_search_results(query, file_type=None, max_results=10, offset=0):
files = await cursor.to_list(length=max_results)

return files, next_offset


def encode_file_id(s: bytes) -> str:
r = b""
n = 0

for i in s + bytes([22]) + bytes([4]):
if i == 0:
n += 1
else:
if n:
r += b"\x00" + bytes([n])
n = 0

r += bytes([i])

return base64.urlsafe_b64encode(r).decode().rstrip("=")


def encode_file_ref(file_ref: bytes) -> str:
return base64.urlsafe_b64encode(file_ref).decode().rstrip("=")


def unpack_new_file_id(new_file_id):
"""Return file_id, file_ref"""
decoded = FileId.decode(new_file_id)
file_id = encode_file_id(
pack(
"<iiqq",
int(decoded.file_type),
decoded.dc_id,
decoded.media_id,
decoded.access_hash
)
)
file_ref = encode_file_ref(decoded.file_reference)
return file_id, file_ref

0 comments on commit 107c79a

Please sign in to comment.