Skip to content

Commit

Permalink
Added support to download subtitle => automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
rajat19 committed May 11, 2019
1 parent 00ce550 commit 752356b
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 33 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ python setup.py install
python search.py
```

---
### What's new
Added support to download subtitles for the movie as well

---
### What's next
- Allow user to stop movie search in between
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.1.4',
version='1.2.0',
url='http://github.com/rajat19/torrent-crawler',
download_url='https://github.com/rajat19/torrent-crawler/releases',
author='Rajat Srivastava',
Expand Down
9 changes: 9 additions & 0 deletions torrent_crawler/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class Constants:
order_by = ['rating', 'seeds', 'peers', 'year', 'likes', 'alphabetical', 'downloads']
search_url = 'https://yts.am/browse-movies/{0}/{1}/{2}/{3}/{4}'

# subtitle search constants
subtitle_base_url = 'http://www.yifysubtitles.com/{0}'
subtitle_search_url = 'http://www.yifysubtitles.com/search?q={0}'
subtitle_movie_url = 'http://www.yifysubtitles.com/movie-imdb/{0}'

# texts
search_string_text = 'Please enter search string: '
genre_selection_text = 'Do you want to search some specific genre: '
Expand All @@ -21,6 +26,10 @@ class Constants:
click_link_text = 'Torrent link will open automatically, If not then Click this link: '
restart_search_text = 'Do you want to start searching again'
thanks_text = 'Thanks for using torrent-search . Keep Seeding'
subtitles_selection_text = 'Do you want to download subtitles for this movie'
subtitle_language_text = 'Select language to get subtitles: '
download_zip_text = 'Subtitle would be downloaded in {0}{1}. Please check there.'
another_movies_text = 'Do you want to download another {0}{1} movie'

# note
specific_genre_note = 'Movies would be crawled for only {0} genre'
Expand Down
8 changes: 6 additions & 2 deletions torrent_crawler/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import re
import requests
from torrent_crawler.progress_bar import ProgressBar
from torrent_crawler.helper import update_progress


class Crawler:
Expand Down Expand Up @@ -119,7 +119,7 @@ def crawl_list(self, crawl_url):
print('{}: {}'.format(current_movie_count, movie_name))
if self.should_save_list:
self.save_list(movie)
ProgressBar.update_progress(current_movie_count, movies_count)
update_progress(current_movie_count, movies_count)
current_movie_count += 1
page_no += 1
return movies
Expand Down Expand Up @@ -150,6 +150,10 @@ def crawl_movie(self, movie_link, movie_details):
movie_details['ratings'] = rating_list
elif self.should_print_to_console:
print("{} got no info, not saving it".format(movie_details['name']))
movie_tech_specs = soup.find('div', {'id': 'movie-tech-specs'})
if movie_tech_specs:
tech_spec = movie_tech_specs.find('div', {'class': 'tech-spec-info'})
movie_details['subtitle_url'] = tech_spec.find('a').get('href')
return movie_details


Expand Down
44 changes: 14 additions & 30 deletions torrent_crawler/search.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import os
import sys
import subprocess
import traceback
from torrent_crawler.constants import Constants
from torrent_crawler.color import Color
from torrent_crawler.crawler import Crawler


def print_long_hash():
print('##########################################')


def print_wrong_option():
print(Constants.wrong_option_text)


def get_yes_no():
return '{0}\n{1}\n'.format(Color.get_colored_yes(), Color.get_colored_no())
from torrent_crawler.helper import open_magnet_link, print_wrong_option, get_yes_no, print_long_hash
from torrent_crawler.subtitle import Subtitle


class SearchQuery:
Expand All @@ -32,16 +19,6 @@ class Search:
def __init__(self, search_query: SearchQuery):
self.search_query = search_query

def open_magnet(self, magnet):
if sys.platform.startswith('win32') or sys.platform.startswith('cygwin'):
os.startfile(magnet)
elif sys.platform.startswith('darwin'):
subprocess.Popen(['open', magnet],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
subprocess.Popen(['xdg-open', magnet],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)

def get_available_torrents(self, movie_selected):
if self.search_query.quality == 'all':
return movie_selected['torrents']
Expand Down Expand Up @@ -76,7 +53,7 @@ def show_movies(self, movies):
if len(available_torrents.values()) == 0:
print('{0}{1}{2}'.format(Color.RED, Constants.no_torrent_text, Color.END))
else:
ati=1
ati = 1
for torrent_format in available_torrents:
print('{0}{1}: {2}{3}'.format(Color.YELLOW, ati, torrent_format, Color.END))
ati += 1
Expand All @@ -90,11 +67,18 @@ def show_movies(self, movies):
Color.print_bold_string(Constants.movie_quality_text)
qu = int(input())
torrent_link = list(available_torrents.values())[qu - 1]
self.open_magnet(torrent_link)
open_magnet_link(torrent_link)
Color.print_bold_string('{0}{1}{2}{3}'.format(
Constants.click_link_text, Color.RED, torrent_link, Color.END))
print_long_hash()
print('Want to download another {0}{1} movie'.format(
Color.print_bold_string(Constants.subtitles_selection_text)
download_subtitle = input(get_yes_no())
if download_subtitle == 'y' or download_subtitle == 'Y':
subtitle = Subtitle()
subtitle.search_subtitle(movie_selected['subtitle_url'])

print_long_hash()
print(Constants.another_movies_text.format(
Color.RED, Color.get_bold_string(self.search_query.search_term)))
reshow_movies = input(get_yes_no())
if reshow_movies == 'y' or reshow_movies == 'Y':
Expand Down Expand Up @@ -135,10 +119,10 @@ def take_genre_input():
while True:
g = int(input())
if 1 <= g <= 27:
break
else:
print_wrong_option()
continue
else:
break
Color.print_colored_note(Constants.specific_genre_note.format(genre_options[g]))
else:
Color.print_colored_note(Constants.all_genre_note)
Expand Down
76 changes: 76 additions & 0 deletions torrent_crawler/subtitle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from bs4 import BeautifulSoup
import requests
from torrent_crawler.color import Color
from torrent_crawler.constants import Constants
from torrent_crawler.helper import download_srt, print_wrong_option


class Subtitle:
@staticmethod
def get_search_url(search_term, page_no):
q = search_term
if page_no > 2:
q += page_no
return Constants.subtitle_search_url.format(q)

def crawl_list(self, search_term):
page_no = 1
has_next_page = True
while has_next_page:
url = self.get_search_url(search_term, page_no)
req = requests.get(url)
soup = BeautifulSoup(req.text, features='html5lib')
media_list = soup.find_all('li', {'class': 'media-movie-clickable'})
for media in media_list:
media_body = media.find('div', {'class': 'media-body'})
media_link = media_body.find('a').get('href')
media_name = media.find('h3', {'class': 'media-heading'}).text

@staticmethod
def crawl_movie(url):
req = requests.get(url)
soup = BeautifulSoup(req.text, features='html5lib')
subtitle_table = soup.find('table', {'class': 'other-subs'}).find('tbody').find_all('tr')
subtitles = {}
subtitle_languages = []
for subtitle in subtitle_table:
rating = subtitle.find('td', {'class': 'rating-cell'}).text
language = subtitle.find('td', {'class': 'flag-cell'})\
.find('span', {'class': 'sub-lang'}).text
download_link = subtitle.find('a').get('href').replace('/subtitles/', 'subtitle/')
link = Constants.subtitle_base_url.format(download_link + '.zip')
subtitle_languages.append(language)
if language not in subtitles:
subtitles[language] = []
subtitles[language].append({
'rating': rating,
'link': link
})
return subtitles

@staticmethod
def take_language_input(languages):
Color.print_bold_string(Constants.subtitle_language_text)
for i in range(len(languages)):
print('{0}{1}: {2}{3}'.format(Color.YELLOW, i+1, languages[i], Color.END))
while True:
lang = int(input())
if 1 <= lang <= len(languages):
break
else:
print_wrong_option()
continue
return languages[lang-1]

def search_subtitle(self, url):
# url = Constants.subtitle_movie_url.format(imdb_id)
subtitles = self.crawl_movie(url)
lang = self.take_language_input(list(subtitles.keys()))
# Download first subtitle for that language as it is highest rated
subtitle_link = subtitles[lang][0]['link']
download_srt(subtitle_link)


if __name__ == '__main__':
s = Subtitle()
s.search_subtitle('tt4154756')

0 comments on commit 752356b

Please # to comment.