Skip to content

Commit

Permalink
Updated to v0.16
Browse files Browse the repository at this point in the history
  • Loading branch information
RafayGhafoor committed Sep 15, 2017
1 parent 3c71881 commit eb9eacc
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 39 deletions.
10 changes: 7 additions & 3 deletions README.md
@@ -1,4 +1,4 @@
# Subtitles [Subscene] Grabber (Sub-Grab v0.15):
# Subtitles [Subscene] Grabber (Sub-Grab v0.16):

A script that allows you to download subtitles for TV-Series, Anime and Movies from subscene site.

Expand Down Expand Up @@ -77,7 +77,9 @@ Taken from Manojmj subtitles script.

- Cross-platform (Tested on Linux and Windows).

- Logs generation on script execution (v0.15)
- Logs generation on script execution (v0.15)

- Added Support for the SubDb (v0.16), now first preference for downloading subtitles is SubDB in downloading subtitles from a directory.

# Requirements:

Expand All @@ -89,7 +91,9 @@ Taken from Manojmj subtitles script.

- [x] Adding support for more languages.
- [x] Adding flags.
- [ ] AllSubDB, OpenSubtitles, YIFY subtitles search.
- [X] Support for AllSubDB .
- [ ] Support for OpenSubtitles.
- [ ] Support for YifySubtitles.
- [X] Adding silent mode for downloading subtitles.
- [X] Adding CLI mode for manually downloading subtitles.
- [X] Implement Logging.
Expand Down
10 changes: 7 additions & 3 deletions README.rst
@@ -1,4 +1,4 @@
Subtitles [Subscene] Grabber (Sub-Grab v0.15):
Subtitles [Subscene] Grabber (Sub-Grab v0.16):
==============================================

A script that allows you to download subtitles for TV-Series, Anime and
Expand Down Expand Up @@ -89,7 +89,9 @@ Features:

- Cross-platform (Tested on Linux and Windows).

- Logs generation on script execution (v0.15)
- Logs generation on script execution (v0.15).

- Supports SubDB now.

Requirements:
=============
Expand All @@ -103,7 +105,9 @@ TODO:

- [x] Adding support for more languages.
- [x] Adding flags.
- [ ] AllSubDB, OpenSubtitles, YIFY subtitles search.
- [X] Support for AllSubDB.
- [ ] Support for OpenSubtitles.
- [ ] Support for YIFY subtitles search.
- [X] Adding silent mode for downloading subtitles.
- [X] Adding CLI mode for manually downloading subtitles.
- [X] Implement Logging.
Expand Down
9 changes: 7 additions & 2 deletions changelog.rst
Expand Up @@ -4,14 +4,19 @@ Sub-Grab Changelog
:Info: Changelog for sub-grab project.
:Author: Rafay Ghafoor <rafayghafoor@protonmail.com>
:Copyright: © 2017, Rafay Ghafoor.
:Date: 2017-09-14
:Version: 0.15
:Date: 2017-09-15
:Version: 0.16

.. index:: CHANGELOG

Version History
===============

0.16 / 2017-09-15
* Improve project structure.
* Added support for allSubDB.
* Now prefers allSubDB api over subscene site in downloading subtitles in a directory.

0.15 / 2017-09-14
* Improve subtitles selection.
* Implemented logging.
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -6,13 +6,13 @@ def readme():

setup(
name='subgrab',
version='0.15',
version='0.16',
description='A python script for automating subtitles downloading.',
long_description=readme(),
url='https://github.com/RafayGhafoor/Subscene-Subtitle-Grabber',
author='Rafay Ghafoor',
author_email='rafayghafoor@protonmail.com',
packages=['subgrab', 'subgrab.source', 'subgrab.modules'],
packages=['subgrab', 'subgrab.providers', 'subgrab.utils'],
entry_points = {'console_scripts': ['subgrab = subgrab.__main__:main']},
zip_safe = False,
license='GPL',
Expand Down
9 changes: 5 additions & 4 deletions subgrab/SubGrab.py
Expand Up @@ -32,6 +32,7 @@ def main():
args = parser.parse_args()
logger.debug("Input with flags: %s" % (sys.argv))
logger.info("Initialized SubGrab script")

if args.silent:
# If mode is silent
logger.debug("Executing Silent Mode")
Expand All @@ -47,15 +48,16 @@ def main():

if args.dir != '.':
# Searches for movies in specified directory.
directory.create_folder() # Create folder for the files in the current
# directory (which are not in a folder).
logger.debug("Running in directory: %s" % (args.dir))
try:
os.chdir(args.dir)
directory.create_folder() # Create folder for the files in the current
# directory (which are not in a folder).
directory.get_media_files()
directory.dir_dl()
except Exception as e:
print 'Invalid Directory Input.', e
logger.debug('Invalid Directory Input - %s' % (e))
print('Invalid Directory Input - %s' % (e))

elif args.dir == '.' and not args.media_name:
# Searches for movies in current directory.
Expand All @@ -69,7 +71,6 @@ def main():
logger.info("Searching For: %s" % (args.media_name))
sub_link = subscene.sel_title(name=args.media_name.replace(' ', '.'))
logger.info("Subtitle Link for %s : %s" % (args.media_name, sub_link))
# print sub_link
if sub_link:
for i in subscene.sel_sub(page=sub_link, sub_count=args.count, name=args.media_name):
logger.debug("Downloading Subtitle: %s\n" % (i))
Expand Down
42 changes: 27 additions & 15 deletions subgrab/providers/subdb.py
@@ -1,11 +1,13 @@
import logging
import os.path
import hashlib
import requests
import os.path

headers = {'User-agent': "SubDB/1.0 (subgrab/1.0; http://github.com/RafayGhafoor/Subscene-Subtitle-Grabber)"}
languages = ('en', 'es', 'fr', 'it', 'nl', 'pl', 'pt', 'ro', 'sv', 'tr')
download_url = "http://api.thesubdb.com/?action=download"

HEADERS = {'User-agent': "SubDB/1.0 (subgrab/1.0; http://github.com/RafayGhafoor/Subscene-Subtitle-Grabber)"}
LANGUAGES = ('en', 'es', 'fr', 'it', 'nl', 'pl', 'pt', 'ro', 'sv', 'tr')
DOWNLOAD_URL = "http://api.thesubdb.com/?action=download"
logger = logging.getLogger("subdb.py")

def get_hash(name):
readsize = 64 * 1024
Expand All @@ -17,14 +19,24 @@ def get_hash(name):
return hashlib.md5(data).hexdigest()


def download(hash, filename, language='en'):
r = requests.get(download_url + '&hash=' + hash + '&language=' + language, headers=headers)
with open("Dog.srt", 'wb') as f:
for chunk in r.iter_content(chunk_size=150):
if chunk:
f.write(chunk)


if __name__ == '__main__':
hash = get_hash("A.Dogs.Purpose.2017.720p.WEB-DL.900MB.ShAaNiG.mkv")
s = download(hash=hash, filename="A.Dogs.Purpose.2017.720p.WEB-DL.900MB.ShAaNiG.mkv", language='en')
def get_sub(hash, filename="filename.mkv", language='en'):
logger.info("Downloading subtitles from SubDb")
logger.debug("Language selected for subtitles: %s" % (language))
if language.lower() in LANGUAGES:
r = requests.get(DOWNLOAD_URL + '&hash=' + hash + '&language=' + language.lower(), headers=HEADERS)
logger.debug("Status code for %s is %s" % (filename, r.status_code))
if r.status_code == 200:
with open(os.path.splitext(filename)[0] + '.srt', 'wb') as f:
for chunk in r.iter_content(chunk_size=150):
if chunk:
f.write(chunk)
logger.info("Downloaded Subtitles for %s" % (filename))
return 200
elif r.status_code == 404:
logger.info("[SubDB] Subtitle not found for %s" % (filename))
else:
logger.debug("Invalid file %s" % (filename))
print "Invalid file"
else:
print "Language not supported"
return
2 changes: 1 addition & 1 deletion subgrab/providers/subscene.py
Expand Up @@ -143,7 +143,7 @@ def sel_title(name):
return

except Exception as e:
logger.debug("Returning -", e)
logger.debug("Returning - %s" % (e))
return

title_lst = soup.findAll("div", {"class": "search-result"}) # Creates a list of titles
Expand Down
25 changes: 16 additions & 9 deletions subgrab/utils/directory.py
@@ -1,10 +1,11 @@
import shutil
import os
import subgrab.providers.subscene as subscene
import subgrab.providers.subdb as subdb
import logging

logger = logging.getLogger("directory.py")
EXT = ['.mp4', '.mkv', '.avi', '.flv']
ACTIVEDIR_FILES = [i for extension in EXT for i in os.listdir('.') if extension in i]
MOVIES_DIR = {} # Contains Movies Directories (keys) and the
# files inside them (values = [list])
REMOVALS = [] # Which already contains subtitles
Expand All @@ -17,7 +18,7 @@ def create_folder():
for them and paste the respective file in the corresponding
folder.
'''
for files in ACTIVEDIR_FILES:
for files in [i for extension in EXT for i in os.listdir('.') if extension in i]:
for extension in EXT:
if files.endswith(extension):
# Creates a folder of same name as file (excluding file extension)
Expand Down Expand Up @@ -64,15 +65,21 @@ def dir_dl(sub_count=1):
for folders, movies in MOVIES_DIR.iteritems():
os.chdir(folders)
print "Downloading Subtitles for [%s]" % folders
logger.info("Downloading Subtitles for [%s]" % folders)
for mov in movies:
sub_link = subscene.sel_title(mov)
if sub_link:
if subscene.sel_sub(page=sub_link, name=mov):
for i in subscene.sel_sub(page=sub_link, sub_count=sub_count, name=mov):
subscene.dl_sub(i)
if subdb.get_sub(hash=subdb.get_hash(mov), filename=mov, language='en') != 200:
logger.info("Subtitles for [%s] not found on AllSubDB" % (mov))
logger.info("Searching for subtitles on subscene - now")
sub_link = subscene.sel_title(os.path.splitext(mov)[0])
if sub_link:
if subscene.sel_sub(page=sub_link, name=mov):
for i in subscene.sel_sub(page=sub_link, sub_count=sub_count, name=mov):
subscene.dl_sub(i)
else:
print "Subtitle not found for [%s]" % (mov.capitalize())
logger.debug("Subtitle not found for [%s]" % (mov))
else:
print "Subtitle not found for [%s]" % (mov.capitalize())
else:
print "Subtitle not found for [%s]" % (mov.capitalize())
logger.debug("Subtitle not found for [%s]" % (mov))
os.chdir(cwd)
# print("--- Function (DOWNLOAD_SUB) took %s seconds ---" % (time.time() - start_time))

0 comments on commit eb9eacc

Please sign in to comment.