Skip to content

Commit

Permalink
Merge pull request #19 from rajat19/input-validations
Browse files Browse the repository at this point in the history
Add input validations
  • Loading branch information
rajat19 authored Nov 29, 2020
2 parents 17915a3 + d34adec commit 77926fa
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 43 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ An easy way to get your favourite movie torrents. Try it now !!

> Search for torrents using CLI
```bash
pip3 install search-torrent
pip install search-torrent
search-torrent
```

> Use api to get data (movies and subtitles)
```bash
python app.py
python api.py
```

<p align="center"><img src="https://github.com/rajat19/torrent-crawler/blob/master/img/search-torrent-colorized.gif?raw=true"/></p>

---
### Contributing
```bash
git clone https://github.com/rajat19/torrent-search.git
cd torrent-search
git clone https://github.com/rajat19/torrent-crawler.git
cd torrent-crawler
pip install -r requirements.txt
python setup.py install
python search.py
python torrent_crawler/search.py
```

---
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='search-torrent',
description='Search for torrents using command line',
version='1.4.2',
version='1.4.3',
url='http://github.com/rajat19/torrent-crawler',
download_url='https://github.com/rajat19/torrent-crawler/releases',
author='Rajat Srivastava',
Expand Down
2 changes: 1 addition & 1 deletion torrent_crawler/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Color:
END = '\033[0m'

@staticmethod
def get_bold_string(string) -> str:
def get_bold_string(string: str) -> str:
return '{0}{1}{2}'.format(Color.BOLD, string, Color.END)

@staticmethod
Expand Down
6 changes: 4 additions & 2 deletions torrent_crawler/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ class Constants:
# movie search constants
input_types = ['genre', 'order', 'subtitle']
options = {
'quality': ['all', '720p', '1080p', '3D'],
'quality': ['all', '720p', '1080p', '2160p', '3D'],
'genre': ['all', 'action', 'adventure', 'animation', 'biography', 'comedy', 'crime', 'documentary', 'drama',
'family', 'fantasy', 'film-noir', 'game-show', 'history', 'horror', 'music', 'musical', 'mystery',
'news', 'reality-tv', 'romance', 'sci-fi', 'sport', 'talk-show', 'thriller', 'war', 'western'],
'order': ['rating', 'seeds', 'peers', 'year', 'likes', 'alphabetical', 'downloads']
'order': ['latest', 'oldest', 'seeds', 'peers', 'year', 'rating', 'likes', 'alphabetical', 'downloads']
}
list_url = 'https://yts.mx/browse-movies'
search_url = 'https://yts.mx/browse-movies/{0}/{1}/{2}/{3}/{4}/{5}/{6}'

# yts constants
Expand Down Expand Up @@ -45,6 +46,7 @@ class Constants:
'genre': 'Movies would be crawled for all genre',
'order': 'Movies would be downloaded by IMDB rating'
}
choose_option_text = 'Choose an option: '
wrong_option_text = 'Wrong option, Try again'
movie_download_text = 'Enter movie to download: '
available_torrents_text = 'Available torrents: '
Expand Down
6 changes: 3 additions & 3 deletions torrent_crawler/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import requests
from typing import List
from torrent_crawler.constants import Constants
from torrent_crawler.helper import Helper
from torrent_crawler.models import Movie

Expand All @@ -14,7 +15,7 @@
class Crawler:
def __init__(self, api_flag=False, save_list=False, print_console=False):
self.api_flag = api_flag
self.list_url = 'https://yts.am/browse-movies'
self.list_url = Constants.list_url
self.all_formats = []
self.id = 1
self.should_save_list = save_list or False
Expand All @@ -28,9 +29,8 @@ def crawl_list(self, crawl_url: str) -> MoviesList:
movies = []
current_movie_count = 1
movies_count = 0
crawl_url = crawl_url or self.list_url
while has_next_page:
if not crawl_url:
crawl_url = self.list_url
request_url = crawl_url
if page_no > 1:
request_url = '{0}?page={1}'.format(crawl_url, page_no)
Expand Down
33 changes: 21 additions & 12 deletions torrent_crawler/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,23 @@ def ask_for_options() -> bool:
continue

@staticmethod
def take_input(input_type, options):
def take_int_input(no_of_options) -> int:
index = None
while True:
try:
index = int(input(Color.get_bold_string(Constants.choose_option_text)))
if 1 <= index <= no_of_options:
break
else:
Print.wrong_option()
continue
except ValueError:
Print.wrong_option()
continue
return index

@staticmethod
def take_input(input_type, options) -> int:
if input_type not in Constants.input_types:
Print.bold_string('Wrong input type: {0}'.format(input_type))
exit(1)
Expand All @@ -31,14 +47,8 @@ def take_input(input_type, options):
Print.bold_string(specific_text)
for i in range(1, len(options)):
Print.option(i, options[i])
while True:
index = int(input())
if 1 <= index <= no_of_options:
break
else:
Print.wrong_option()
continue
return options[index-1]
index = Helper.take_int_input(no_of_options)
return options[index]

@staticmethod
def take_optional_input(input_type):
Expand All @@ -51,13 +61,12 @@ def take_optional_input(input_type):
special_final_option = Constants.special_final_option[input_type]
Print.bold_string(selection_text)
want = Helper.ask_for_options()
index = 0
options = Constants.options[input_type]
if not want:
Print.colored_note(special_final_option)
return options[0]
final_option = Helper.take_input(options)
Print.colored_note(specific_final_option.format(options[index]))
final_option = Helper.take_input(input_type, options)
Print.colored_note(specific_final_option.format(final_option))
return final_option

@staticmethod
Expand Down
25 changes: 6 additions & 19 deletions torrent_crawler/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,7 @@ def show_movies(self, movies: MoviesList):
for ind, movie in enumerate(movies):
print('{0}{1}: {2} ({3}){4}'.format(
Color.PURPLE, ind + 1, movie.name, movie.year, Color.END))
while True:
mid = int(input(Color.get_bold_string(Constants.movie_download_text)))
if mid > len(movies) or mid < 1:
Print.wrong_option()
continue
else:
break
mid = Helper.take_int_input(len(movies))
movie_selected = movies[mid - 1]
Print.bold_string(Constants.available_torrents_text)

Expand All @@ -83,9 +77,10 @@ def show_movies(self, movies: MoviesList):
torrent_link = list(available_torrents.values())[0]
Print.bold_string('{0}{1}{2}{3}'.format(
Constants.click_link_text, Color.RED, torrent_link, Color.END))
else: exit(1)
else:
Print.bold_string(Constants.movie_quality_text)
qu = int(input())
# Print.bold_string(Constants.movie_quality_text)
qu = Helper.take_int_input(len(available_torrents.values()))
torrent_link = list(available_torrents.values())[qu - 1]
Helper.open_magnet_link(torrent_link)
Print.bold_string('{0}{1}{2}{3}'.format(
Expand Down Expand Up @@ -124,18 +119,10 @@ class SearchInput:
def create_query() -> SearchQuery:
Print.long_hash()
s = input(Color.get_bold_string(Constants.search_string_text))
while not s:
s = input(Color.get_bold_string(Constants.search_string_text))

q = 'all'
"""
print('Please enter any specific quality of your torrent: ')
quality_options = Constants.quality
for i in range(len(quality_options)):
print('{0}: {1}'.format(i, quality_options[i]))
q = int(input())
while q > 4 or q < 0:
print('Wrong option, Try again')
q = quality_options[q]
"""
g = Helper.take_optional_input('genre')
o = Helper.take_optional_input('order')

Expand Down

0 comments on commit 77926fa

Please # to comment.