From 17ccba568e48ee2af6c0eb2596aaeccc3fe22fde Mon Sep 17 00:00:00 2001 From: DEADSEC-SECURITY Date: Sun, 23 Jan 2022 21:23:46 +0000 Subject: [PATCH] Deprecated get_best_quality_torrent and made a better version --- README.md | 13 ++++---- SECURITY.md | 1 + popcorntime/popcorntime.py | 67 +++++++++++++++++++++++++++++++++++--- setup.py | 2 +- 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a4e4676..cc05379 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Popcorn Time API ![Version](https://img.shields.io/badge/Version-v0.1.2-orange?style=flat-square&url=https://github.com/DEADSEC-SECURITY/popcorn-time-api/blob/main/CHANGELOG.md) ![Python_Version](https://img.shields.io/badge/Python-3.7%2B-blue?style=flat-square) ![License](https://img.shields.io/badge/License-MIT-red?style=flat-square) ![Donate](https://img.shields.io/badge/Donate-Crypto-yellow?style=flat-square) [![CodeQL](https://github.com/DEADSEC-SECURITY/popcorn-time-api/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/DEADSEC-SECURITY/popcorn-time-api/actions/workflows/codeql-analysis.yml) [![Downloads](https://pepy.tech/badge/popcorn-time)](https://pepy.tech/project/popcorn-time) [![Downloads](https://pepy.tech/badge/popcorn-time/month)](https://pepy.tech/project/popcorn-time) +# Popcorn Time API ![Version](https://img.shields.io/badge/Version-v0.2.0-orange?style=flat-square&url=https://github.com/DEADSEC-SECURITY/popcorn-time-api/blob/main/CHANGELOG.md) ![Python_Version](https://img.shields.io/badge/Python-3.7%2B-blue?style=flat-square) ![License](https://img.shields.io/badge/License-MIT-red?style=flat-square) ![Donate](https://img.shields.io/badge/Donate-Crypto-yellow?style=flat-square) [![CodeQL](https://github.com/DEADSEC-SECURITY/popcorn-time-api/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/DEADSEC-SECURITY/popcorn-time-api/actions/workflows/codeql-analysis.yml) [![Downloads](https://pepy.tech/badge/popcorn-time)](https://pepy.tech/project/popcorn-time) [![Downloads](https://pepy.tech/badge/popcorn-time/month)](https://pepy.tech/project/popcorn-time) ## 📝 CONTRIBUTIONS @@ -52,9 +52,6 @@ popAPI = PopcornTime() - #### FUNCTION `set_min_seeds` - **value** : int, required - Minimum number of seeds to select torrent -- #### FUNCTION `set_min_peers` - - **value** : int, required - - Minimum number of peers to select torrent - #### FUNCTION `get_server_status` - Returns the server status in json format - #### FUNCTION `get_shows_stats` @@ -79,10 +76,14 @@ popAPI = PopcornTime() - Returns the show in json format - #### FUNCTION `get_random_movie` - Returns the movie in json format -- #### FUNCTION `get_best_quality_torrent` +- #### FUNCTION `get_best_torrent` - **torrents** : dict, required - The dictionary of torrents provided by the API (get_show or get_movie) - - Returns the best quality torrent is json format + - **min_quality** : int, optional + - Minimum quality to select torrent (Default: '1080') + - **revert_to_default** : bool, optional + - Revert to default item if no torrents are found (Default: False) + - Returns the best torrent is json format ## Legal Notice This SDK is not meant to be used for illegal purposes, use it at your own risk and check your local regulations first. diff --git a/SECURITY.md b/SECURITY.md index 341e8a5..6f22a51 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -6,6 +6,7 @@ Check the following table for information on which versions of this software are | Version | Supported | | ------- | ------------------ | +| v0.2.0 | :white_check_mark: | | v0.1.2 | :white_check_mark: | | v0.1.1 | :white_check_mark: | | v0.1.0 | :white_check_mark: | diff --git a/popcorntime/popcorntime.py b/popcorntime/popcorntime.py index a42aacb..17b2a8b 100644 --- a/popcorntime/popcorntime.py +++ b/popcorntime/popcorntime.py @@ -2,10 +2,21 @@ import logging import sys import unittest +import functools from urllib.parse import urljoin +# Wrapper for deprecated function +def deprecated(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + logging.warning(f'The function {func.__name__} is deprecated and will be removed in the next major release.') + return func(*args, **kwargs) + + return wrapper + + class PopcornTime: _BASE_URL: str = 'https://popcorn-ru.tk/' _MIN_SEEDS: int = 0 @@ -59,6 +70,7 @@ def _get_torrent_seeds(torrent): return torrent['seed'] @staticmethod + @deprecated def _get_torrent_peers(torrent): """ Movie have different seed name for the same thing so @@ -96,6 +108,7 @@ def set_base_url(self, url: str) -> str: return self._BASE_URL + @deprecated def set_min_seeds(self, value: int) -> int: """ Sets the base URL @@ -108,6 +121,7 @@ def set_min_seeds(self, value: int) -> int: return self._MIN_SEEDS + @deprecated def set_min_peers(self, value: int) -> int: """ Sets the base URL @@ -192,6 +206,49 @@ def get_random_show(self) -> (requests.Response.json, None): return show return None + def get_best_torrent(self, torrents: dict, min_quality: int = 1080, revert_to_default: bool = False) -> (dict, None): + """ + Get the best torrent + + :param revert_to_default: bool (If True, it will revert to popcorn default torrent) + :param torrents: dict (Example: {"720p": ..., "1080p": ...}) + :param min_quality: int (Example: 1080) + :return: dict (Example: {"720p": ..., "1080p": ...}) + """ + self.log.info(f'Getting best torrent for quality {min_quality}') + + # Try to select torrents language first + try: + torrents = torrents['en'] + except KeyError: + pass + + # Make list of torrents with quality > min_quality + filtered_torrents = [] + for quality, torrent in torrents.items(): + quality = int(quality.replace('p', '')) + if quality >= min_quality: + filtered_torrents.append(torrent) + + if len(filtered_torrents) == 0: + if revert_to_default: + self.log.info('No torrents found, reverting to default torrent') + try: + return torrents['0'] + except KeyError: + return None + return None + + self.log.info(f'Got {len(filtered_torrents)} torrents with quality >= {min_quality}') + + # Get the torrents with the most seeds + filtered_torrents.sort(key=self._get_torrent_seeds, reverse=True) + + self.log.debug(f'Got torrent with most seeds: {filtered_torrents[0]}') + + return filtered_torrents[0] + + @deprecated def get_best_quality_torrent(self, torrents: dict) -> (tuple, None): """ Gets the show torrent with the best quality @@ -321,10 +378,12 @@ def test_get_show(self): def test_get_random_show(self): self.assertIsNotNone(self.popAPI.get_random_show) - def test_get_best_quality_torrent(self): - show = self.popAPI.get_movie("tt0111161") - torrents = show["torrents"] - self.assertIsNotNone(self.popAPI.get_best_quality_torrent(torrents)) + def test_get_best_torrent(self): + movie = self.popAPI.get_movie("tt0111161") + torrents = movie["torrents"] + best = self.popAPI.get_best_torrent(torrents) + print(best) + self.assertIsNotNone(best) def test_get_movies_stats(self): self.assertIsNotNone(self.popAPI.get_movies_stats) diff --git a/setup.py b/setup.py index 1d8fb3b..ab7c0ba 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='popcorn-time', packages=find_packages(), - version='0.1.2', + version='0.2.0', description='Interact with the Popcorn Time API with python', long_description=README, long_description_content_type='text/markdown',