Skip to content

Commit

Permalink
Deprecated get_best_quality_torrent and made a better version
Browse files Browse the repository at this point in the history
  • Loading branch information
DEADSEC-SECURITY committed Jan 23, 2022
1 parent e9a37f5 commit 17ccba5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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`
Expand All @@ -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.
1 change: 1 addition & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
67 changes: 63 additions & 4 deletions popcorntime/popcorntime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 17ccba5

Please sign in to comment.