File size: 1,383 Bytes
68a2a86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import time
import pymongo
import motor.motor_asyncio
from bson.objectid import ObjectId
from bson.errors import InvalidId
from bson.json_util import dumps

from pymongo.errors import PyMongoError

class PUBLICFilesDB:
    def __init__(self, uri, database_name):
        self._client = motor.motor_asyncio.AsyncIOMotorClient(uri)
        self.db = self._client[database_name]
        self.files = self.db.Public_Files

    async def GetMoviesAllBy10(self, offset=None):
        try:
            filter = {"type": "Movie"}  # Define the filter for movies
            total_results = await self.files.count_documents(filter)
            offset=int( offset if offset else 0)
            next_offset = offset + 10
            if next_offset >= total_results:  # Changed > to >= to handle the last page correctly
                next_offset = ''
            cursor = self.files.find(filter)  # Apply the filter
            cursor.sort('$natural', pymongo.DESCENDING)  # Use pymongo.DESCENDING
            cursor.skip(offset).limit(10)
            files = await cursor.to_list(length=10)
            return files, next_offset

        except PyMongoError as e:
            print(f"MongoDB error: {e}")
            return [], ''  # Return empty list and '' on error
        except Exception as e:
            print(f"An unexpected error occurred: {e}")
            return [], ''