def game_details(link: str = None, reference_id: str = None, console_code: str = None) -> Game: page = ScraperServices.__html_result( f'https://jogorama.com.br/{link}/') data_sheet = page.select('.ficha') title = re.sub( '[^A-Za-z0-9]+', ' ', str(page.findAll('span', attrs={"itemprop": "name"})[0].text)) owner = re.sub( '[^A-Za-z0-9]+', ' ', str(page.findAll('span', attrs={"itemprop": "author"})[0].text)) publisher = re.sub( '[^A-Za-z0-9]+', ' ', str(page.findAll('span', attrs={"itemprop": "publisher"})[0].text)) genre = '' if len(page.findAll( 'span', attrs={"itemprop": "genre"})) == 0 else str( page.findAll('span', attrs={"itemprop": "genre"})[0].text) game_detail: Game = Game() release_year = 0 release_month = 0 release_day = 0 release_date = '' if re.search('<b>Lançamento:</b>([^,]+)<br/>', str(data_sheet[0])) is not None: released = re.search('<b>Lançamento:</b>([^,]+)<br/>', str(data_sheet[0])).group(1) result = re.findall(r'\d+', released)[0] release_day = int(result) if len(result) == 2 else 0 release_year = result if len(result) == 4 else re.findall( r'\d+', released)[1] if re.search(r'(?<=de)([\S\s]*)(?=de)', released) is not None: release_month = Utils.month_converter( re.search(r'(?<=de)([\S\s]*)(?=de)', released).group(1).replace(' ', '')) if release_month > 0 and release_day > 0: release_date = maya.parse( f'{release_year}-{release_month}-{release_day}').datetime() game_uuid = f'{reference_id} - {title} - {owner} - {publisher} - {release_year}' game_detail.game_id = str(uuid.uuid5(uuid.NAMESPACE_URL, game_uuid)) game_detail.reference_id = reference_id game_detail.title = title game_detail.console_code = str( uuid.uuid5(uuid.NAMESPACE_URL, console_code)) game_detail.release_date = release_date game_detail.release_year = release_year game_detail.cover_image = base64.b64encode( requests.get( f"https://jogorama.com.br/thumbr.php?l=180&a=400&img=capas/{reference_id}.jpg" ).content) game_detail.owner = owner game_detail.publisher = publisher game_detail.genre = genre #print(f'{reference_id} - {title} - {owner} - {publisher} - {genre} - {release_date} - {release_year}') return game_detail
def get_user_by_id(cls, user_id): connection = DB.get_connection() collection = connection.get_collection(cls.collection_name) user = collection.find_one({"_id": ObjectId(user_id)}) if user is None: raise NoDocumentFound(404, "user not found with id : {}".format(user_id)) user = Utils.parse_document(user) return user
def get_role_by_id(cls, role_id): data_to_return = [] connection = DB.get_connection() collection = connection.get_collection(cls.collection_name) result = collection.find_one({"_id": ObjectId(role_id)}) if result is None: raise NoDocumentFound(404, "No role found with id : {}".format(role_id)) data_to_return.append(Utils.parse_document(result)) return data_to_return
def get_all_user(cls): connection = DB.get_connection() collection = connection.get_collection(cls.collection_name) data_to_return = [] all_user = collection.find({}).sort("created_at", -1) if all_user.count() == 0: raise NoDocumentFound(404, "No user found") for each_user in all_user: data_to_return.append(Utils.parse_document(each_user)) return data_to_return
def get_all_role(cls): data_to_return = [] connection = DB.get_connection() collection = connection.get_collection(cls.collection_name) all_roles = collection.find({}).sort("created_at", -1) if all_roles.count() is 0: raise NoDocumentFound(404, "No role found") for each_role in all_roles: data_to_return.append(Utils.parse_document(each_role)) return data_to_return
def get_permission_by_id(cls, permission_id): conn = DB.get_connection() collection = conn.get_collection(cls.collection_name) data_to_return = [] permission = collection.find_one({"_id": ObjectId(permission_id)}) if permission is None: raise NoDocumentFound( 404, "No permission found with id : {}".format(permission_id)) permission = Utils.parse_document(permission) data_to_return.append(permission) return data_to_return
def get_all_permission(cls): conn = DB.get_connection() collection = conn.get_collection(cls.collection_name) data_to_return = [] all_document = collection.find({}).sort("created_at", -1) if all_document.count() == 0: raise NoDocumentFound(404, "No permission found") for each_document in all_document: data_to_return.append(Utils.parse_document(each_document)) return data_to_return
def test_parse_document_should_remap_id(self): document = {"_id": "test_id"} actual_document = Utils.parse_document(document) self.assertEqual("test_id", actual_document["id"]) self.assertTrue("_id" not in document)