├── LICENSE
├── README.md
├── locales
├── base.pot
├── en_US
│ └── LC_MESSAGES
│ │ ├── messages.mo
│ │ └── messages.po
├── it_IT
│ └── LC_MESSAGES
│ │ ├── messages.mo
│ │ └── messages.po
└── tr_TR
│ └── LC_MESSAGES
│ ├── messages.mo
│ └── messages.po
├── requirements.txt
└── sbox.py
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Sameera Madushan
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | *This project is archived due to the **SubDB API** being inactive. Thanks to everyone who contributed!*
2 | # SBOX - Subtitle Box
3 |
4 | SBOX is a python script to download subtitles for your movies from [SubDB](http://thesubdb.com/) database using their API. SubDB is a free, centralized subtitle database intended to be used only by opensource and non-commercial softwares.
5 |
6 | Please Note: Subtitle file will be downloaded in to the same folder as the correspondent video file.
7 |
8 | ## Features
9 | - [x] Download subtitle files of different languages.
10 | - [x] Download subtitles for multiple movies at once.
11 | - [x] Command line mode for terminal users.
12 |
13 | 
14 |
15 | ## Git Installation
16 | ```
17 | # clone the repo
18 | $ git clone https://github.com/sameera-madushan/SubtitleBOX.git
19 |
20 | # change the working directory to SubtitleBOX
21 | $ cd SubtitleBOX
22 |
23 | # install the requirements
24 | $ pip3 install -r requirements.txt
25 | ```
26 | ## (OSX/Linux only) Install tkinter
27 | If you are running python 3.7 or later, nothing has to be done. Earlier python3 versions require installation.
28 | ```
29 | # Debian/Ubuntu
30 | $ sudo apt install python3-tk
31 |
32 | # macOS
33 | # Follow the instructions on https://tkdocs.com/tutorial/install.html
34 | ```
35 |
36 | ## Usage
37 |
38 | ```
39 | python sbox.py
40 | ```
41 |
42 | ```
43 | usage: sbox.py [-h] [-f FILE_PATH] [-lang LANGUAGE_CODE]
44 |
45 | SubtitleBOX CLI
46 |
47 | optional arguments:
48 | -h, --help show this help message and exit
49 | -f FILE_PATH, --file_path FILE_PATH
50 | Path of the video file for which subtitles should be
51 | looked for
52 | -lang LANGUAGE_CODE, --language_code LANGUAGE_CODE
53 | Language code for subtitles. Can be en, es, fr, it,
54 | nl, pl, pt, ro, sv, tr
55 | ```
56 | ## Contributors
57 |
58 | Thanks goes to these wonderful people. :heart:
59 |
60 |
69 |
--------------------------------------------------------------------------------
/locales/base.pot:
--------------------------------------------------------------------------------
1 | # Translations template for PROJECT.
2 | # Copyright (C) 2020 ORGANIZATION
3 | # This file is distributed under the same license as the PROJECT project.
4 | # FIRST AUTHOR , 2020.
5 | #
6 | #, fuzzy
7 | msgid ""
8 | msgstr ""
9 | "Project-Id-Version: PROJECT VERSION\n"
10 | "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
11 | "POT-Creation-Date: 2020-04-18 15:36+0200\n"
12 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13 | "Last-Translator: FULL NAME \n"
14 | "Language-Team: LANGUAGE \n"
15 | "MIME-Version: 1.0\n"
16 | "Content-Type: text/plain; charset=utf-8\n"
17 | "Content-Transfer-Encoding: 8bit\n"
18 | "Generated-By: Babel 2.8.0\n"
19 |
20 | #: sbox.py:32 sbox.py:40 sbox.py:106
21 | msgid "Cancelled"
22 | msgstr ""
23 |
24 | #: sbox.py:104
25 | msgid "The selected file cannot be used to find subtitles:"
26 | msgstr ""
27 |
28 | #: sbox.py:134
29 | msgid "There is no common language available for the selected files."
30 | msgstr ""
31 |
32 | #: sbox.py:158
33 | msgid "File does not exist."
34 | msgstr ""
35 |
36 | #: sbox.py:173
37 | msgid "Subtitle file is not available for following files:"
38 | msgstr ""
39 |
40 | #: sbox.py:181
41 | msgid "Subtitles are available for following files:"
42 | msgstr ""
43 |
44 | #: sbox.py:187
45 | msgid "Subtitles are available in following languages..."
46 | msgstr ""
47 |
48 | #: sbox.py:189
49 | msgid "Subtitles for all selected files are available in following languages..."
50 | msgstr ""
51 |
52 | #: sbox.py:191
53 | msgid "Error"
54 | msgstr ""
55 |
56 | #: sbox.py:200
57 | msgid "Choose your language (Please use language codes): "
58 | msgstr ""
59 |
60 | #: sbox.py:214
61 | msgid "Subtitle downloaded successfully"
62 | msgstr ""
63 |
64 | #: sbox.py:216
65 | msgid "Unknown Error"
66 | msgstr ""
67 |
68 | #: sbox.py:218
69 | msgid "Invalid language code selected. Please try again."
70 | msgstr ""
71 |
72 | #: sbox.py:224
73 | msgid "Path of the video file for which subtitles should be looked for"
74 | msgstr ""
75 |
76 | #: sbox.py:226
77 | msgid "Language code for subtitles. Can be en, es, fr, it, nl, pl, pt, ro, sv, tr"
78 | msgstr ""
79 |
80 |
--------------------------------------------------------------------------------
/locales/en_US/LC_MESSAGES/messages.mo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sameera-madushan/SubtitleBOX/fb25abc154e36c2dedd00addae89832341e48312/locales/en_US/LC_MESSAGES/messages.mo
--------------------------------------------------------------------------------
/locales/en_US/LC_MESSAGES/messages.po:
--------------------------------------------------------------------------------
1 | # English (United States) translations for PROJECT.
2 | # Copyright (C) 2020 ORGANIZATION
3 | # This file is distributed under the same license as the PROJECT project.
4 | # FIRST AUTHOR , 2020.
5 | #
6 | msgid ""
7 | msgstr ""
8 | "Project-Id-Version: PROJECT VERSION\n"
9 | "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
10 | "POT-Creation-Date: 2020-04-18 15:36+0200\n"
11 | "PO-Revision-Date: 2020-04-16 23:26+0200\n"
12 | "Last-Translator: FULL NAME \n"
13 | "Language: en_US\n"
14 | "Language-Team: en_US \n"
15 | "Plural-Forms: nplurals=2; plural=(n != 1)\n"
16 | "MIME-Version: 1.0\n"
17 | "Content-Type: text/plain; charset=utf-8\n"
18 | "Content-Transfer-Encoding: 8bit\n"
19 | "Generated-By: Babel 2.8.0\n"
20 |
21 | #: sbox.py:32 sbox.py:40 sbox.py:106
22 | msgid "Cancelled"
23 | msgstr "Cancelled"
24 |
25 | #: sbox.py:104
26 | msgid "The selected file cannot be used to find subtitles:"
27 | msgstr "The selected file cannot be used to find subtitles:"
28 |
29 | #: sbox.py:134
30 | msgid "There is no common language available for the selected files."
31 | msgstr "There is no common language available for the selected files."
32 |
33 | #: sbox.py:158
34 | msgid "File does not exist."
35 | msgstr "File does not exist."
36 |
37 | #: sbox.py:173
38 | msgid "Subtitle file is not available for following files:"
39 | msgstr "Subtitle file is not available for following files:"
40 |
41 | #: sbox.py:181
42 | msgid "Subtitles are available for following files:"
43 | msgstr "Subtitles are available for following files:"
44 |
45 | #: sbox.py:187
46 | msgid "Subtitles are available in following languages..."
47 | msgstr "Subtitles are available in following languages..."
48 |
49 | #: sbox.py:189
50 | msgid "Subtitles for all selected files are available in following languages..."
51 | msgstr "Subtitles for all selected files are available in following languages..."
52 |
53 | #: sbox.py:191
54 | msgid "Error"
55 | msgstr "Error"
56 |
57 | #: sbox.py:200
58 | msgid "Choose your language (Please use language codes): "
59 | msgstr "Choose your language (Please use language codes): "
60 |
61 | #: sbox.py:214
62 | msgid "Subtitle downloaded successfully"
63 | msgstr "Subtitle downloaded successfully"
64 |
65 | #: sbox.py:216
66 | msgid "Unknown Error"
67 | msgstr "Unknown Error"
68 |
69 | #: sbox.py:218
70 | msgid "Invalid language code selected. Please try again."
71 | msgstr "Invalid language code selected. Please try again."
72 |
73 | #: sbox.py:224
74 | msgid "Path of the video file for which subtitles should be looked for"
75 | msgstr "Path of the video file for which subtitles should be looked for"
76 |
77 | #: sbox.py:226
78 | msgid "Language code for subtitles. Can be en, es, fr, it, nl, pl, pt, ro, sv, tr"
79 | msgstr "Language code for subtitles. Can be en, es, fr, it, nl, pl, pt, ro, sv, tr"
80 |
81 | #~ msgid "Subtitle files are available in following languages..."
82 | #~ msgstr "Subtitle files are available in following languages..."
83 |
84 | #~ msgid "Oops!! Subtitle not found."
85 | #~ msgstr "Oops!! Subtitle not found."
86 |
87 |
--------------------------------------------------------------------------------
/locales/it_IT/LC_MESSAGES/messages.mo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sameera-madushan/SubtitleBOX/fb25abc154e36c2dedd00addae89832341e48312/locales/it_IT/LC_MESSAGES/messages.mo
--------------------------------------------------------------------------------
/locales/it_IT/LC_MESSAGES/messages.po:
--------------------------------------------------------------------------------
1 | # Italian (Italy) translations for PROJECT.
2 | # Copyright (C) 2020 ORGANIZATION
3 | # This file is distributed under the same license as the PROJECT project.
4 | # FIRST AUTHOR , 2020.
5 | #
6 | msgid ""
7 | msgstr ""
8 | "Project-Id-Version: PROJECT VERSION\n"
9 | "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
10 | "POT-Creation-Date: 2020-04-18 15:36+0200\n"
11 | "PO-Revision-Date: 2020-04-16 23:27+0200\n"
12 | "Last-Translator: FULL NAME \n"
13 | "Language: it_IT\n"
14 | "Language-Team: it_IT \n"
15 | "Plural-Forms: nplurals=2; plural=(n != 1)\n"
16 | "MIME-Version: 1.0\n"
17 | "Content-Type: text/plain; charset=utf-8\n"
18 | "Content-Transfer-Encoding: 8bit\n"
19 | "Generated-By: Babel 2.8.0\n"
20 |
21 | #: sbox.py:32 sbox.py:40 sbox.py:106
22 | msgid "Cancelled"
23 | msgstr "Annullato"
24 |
25 | #: sbox.py:104
26 | msgid "The selected file cannot be used to find subtitles:"
27 | msgstr "Non ci sono sottotitoli disponibili per il file selezionato:"
28 |
29 | #: sbox.py:134
30 | msgid "There is no common language available for the selected files."
31 | msgstr "Non ci sono sottitotli della stessa lingua per tutti i file selezionati."
32 |
33 | #: sbox.py:158
34 | msgid "File does not exist."
35 | msgstr "Il file non esiste."
36 |
37 | #: sbox.py:173
38 | msgid "Subtitle file is not available for following files:"
39 | msgstr "I sottotitoli non sono disponibili per i seguenti files:"
40 |
41 | #: sbox.py:181
42 | msgid "Subtitles are available for following files:"
43 | msgstr "I sottotitoli sono disponibili per i seguenti files:"
44 |
45 | #: sbox.py:187
46 | msgid "Subtitles are available in following languages..."
47 | msgstr "I sottotitoli sono disponibili nei seguenti linguaggi..."
48 |
49 | #: sbox.py:189
50 | msgid "Subtitles for all selected files are available in following languages..."
51 | msgstr "I sottotitoli per tutti i file selezionati sono disponibili nei seguenti linguaggi..."
52 |
53 | #: sbox.py:191
54 | msgid "Error"
55 | msgstr "Errore"
56 |
57 | #: sbox.py:200
58 | msgid "Choose your language (Please use language codes): "
59 | msgstr "Scegli la lingua (Per favore, usa il codice del linguaggio scelto)"
60 |
61 | #: sbox.py:214
62 | msgid "Subtitle downloaded successfully"
63 | msgstr "Sottotitoli scaricati con successo"
64 |
65 | #: sbox.py:216
66 | msgid "Unknown Error"
67 | msgstr "Errore sconosciuto"
68 |
69 | #: sbox.py:218
70 | msgid "Invalid language code selected. Please try again."
71 | msgstr "Codice linguaggio non valido. Per favore, prova di nuovo."
72 |
73 | #: sbox.py:224
74 | msgid "Path of the video file for which subtitles should be looked for"
75 | msgstr "Percorso del file video per cui cercare i sottotitoli"
76 |
77 | #: sbox.py:226
78 | msgid "Language code for subtitles. Can be en, es, fr, it, nl, pl, pt, ro, sv, tr"
79 | msgstr ""
80 | "Codice del linguaggio per i sottotitoli. Può essere en, es, fr, it, nl, "
81 | "pl, pt, ro, sv, tr"
82 |
83 | #~ msgid "Subtitle files are available in following languages..."
84 | #~ msgstr "I files dei sottotitoli sono disponibili nelle seguenti lingue..."
85 |
86 | #~ msgid "Oops!! Subtitle not found."
87 | #~ msgstr "Oops! Sottotitoli non trovati."
88 |
89 |
--------------------------------------------------------------------------------
/locales/tr_TR/LC_MESSAGES/messages.mo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sameera-madushan/SubtitleBOX/fb25abc154e36c2dedd00addae89832341e48312/locales/tr_TR/LC_MESSAGES/messages.mo
--------------------------------------------------------------------------------
/locales/tr_TR/LC_MESSAGES/messages.po:
--------------------------------------------------------------------------------
1 | # Turkish (Turkey) translations for PROJECT.
2 | # Copyright (C) 2020 ORGANIZATION
3 | # This file is distributed under the same license as the PROJECT project.
4 | # FIRST AUTHOR , 2020.
5 | #
6 | msgid ""
7 | msgstr ""
8 | "Project-Id-Version: PROJECT VERSION\n"
9 | "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
10 | "POT-Creation-Date: 2020-04-18 15:36+0200\n"
11 | "PO-Revision-Date: 2020-04-16 23:27+0200\n"
12 | "Last-Translator: FULL NAME \n"
13 | "Language: tr_TR\n"
14 | "Language-Team: tr_TR \n"
15 | "Plural-Forms: nplurals=1; plural=0\n"
16 | "MIME-Version: 1.0\n"
17 | "Content-Type: text/plain; charset=utf-8\n"
18 | "Content-Transfer-Encoding: 8bit\n"
19 | "Generated-By: Babel 2.8.0\n"
20 |
21 | #: sbox.py:32 sbox.py:40 sbox.py:106
22 | msgid "Cancelled"
23 | msgstr "Reddedildi!"
24 |
25 | #: sbox.py:104
26 | msgid "The selected file cannot be used to find subtitles:"
27 | msgstr "The selected file cannot be used to find subtitles:""
28 |
29 | #: sbox.py:134
30 | msgid "There is no common language available for the selected files."
31 | msgstr "There is no common language available for the selected files."
32 |
33 | #: sbox.py:158
34 | msgid "File does not exist."
35 | msgstr "Dosya mevcut değil."
36 |
37 | #: sbox.py:173
38 | msgid "Subtitle file is not available for following files:"
39 | msgstr "Subtitle file is not available for following files:"
40 |
41 | #: sbox.py:181
42 | msgid "Subtitles are available for following files:"
43 | msgstr "Subtitles are available for following files:"
44 |
45 | #: sbox.py:187
46 | msgid "Subtitles are available in following languages..."
47 | msgstr "Subtitles are available in following languages..."
48 |
49 | #: sbox.py:189
50 | msgid "Subtitles for all selected files are available in following languages..."
51 | msgstr "Subtitles for all selected files are available in following languages..."
52 |
53 | #: sbox.py:191
54 | msgid "Error"
55 | msgstr "Error"
56 |
57 | #: sbox.py:200
58 | msgid "Choose your language (Please use language codes): "
59 | msgstr "İndirmek istediğiniz dili seçiniz (Lütfen dilin kısaltmasını "
60 |
61 | #: sbox.py:214
62 | msgid "Subtitle downloaded successfully"
63 | msgstr "Başarıyla İndirildi"
64 |
65 | #: sbox.py:216
66 | msgid "Unknown Error"
67 | msgstr "Bilinmeyen Hata"
68 |
69 | #: sbox.py:218
70 | msgid "Invalid language code selected. Please try again."
71 | msgstr "Geçersiz dil kodu seçildi. Lütfen tekrar deneyiniz!"
72 |
73 | #: sbox.py:224
74 | msgid "Path of the video file for which subtitles should be looked for"
75 | msgstr "Altyazıların aranması gereken video dosyasının konumu"
76 |
77 | #: sbox.py:226
78 | msgid "Language code for subtitles. Can be en, es, fr, it, nl, pl, pt, ro, sv, tr"
79 | msgstr "Altyazilar için diller : en, es, fr, it, nl, pl, pt, ro, sv, tr"
80 |
81 | #~ msgid "Subtitle files are available in following languages..."
82 | #~ msgstr "Altyazi dosyasi alttaki dillerde mevcuttur..."
83 |
84 | #~ msgid "Oops!! Subtitle not found."
85 | #~ msgstr "Tüh!! Altyazi bulunamadi"
86 |
87 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | certifi==2023.7.22
2 | chardet==3.0.4
3 | idna==2.9
4 | requests==2.23.0
5 | urllib3==1.26.5
6 | numpy==1.22.0
7 |
--------------------------------------------------------------------------------
/sbox.py:
--------------------------------------------------------------------------------
1 | # coded by sameera madushan
2 | #Important: During development and for tests purposes, please use http://sandbox.thesubdb.com/ as the API url.
3 |
4 | import os
5 | import re
6 | import sys
7 | import time
8 | import locale
9 | import hashlib
10 | import gettext
11 | import requests
12 | import argparse
13 | import numpy as np
14 | from os import path
15 | from pathlib import Path
16 | from tkinter import Tk, filedialog
17 |
18 | locales_path = sys.path[0] + '/locales'
19 | try:
20 | user_locale = locale.getdefaultlocale()[0]
21 | lang = gettext.translation('messages', localedir=locales_path, languages=[user_locale])
22 | lang.install()
23 | _ = lang.gettext
24 | except FileNotFoundError:
25 | lang = gettext.translation('messages', localedir=locales_path, languages=["en_US"])
26 | lang.install()
27 | _ = lang.gettext
28 |
29 |
30 | def tk_get_file_path():
31 | """
32 | This function will ask the user for the movie file/s and return the
33 | file path/s of them. Tkinter module is used to show the File dialog.
34 |
35 | Parameters
36 | ----------
37 | None
38 | """
39 |
40 | root = Tk()
41 | root.withdraw()
42 |
43 | file_path = root.tk.splitlist(
44 | filedialog.askopenfilenames(parent=root, title='Choose a file'))
45 | if not file_path:
46 | print(_("Cancelled"))
47 | sys.exit()
48 |
49 | try:
50 | for file in file_path:
51 | with open(file, 'r') as f:
52 | pass
53 | except IOError:
54 | print(_("Cancelled"))
55 | sys.exit()
56 |
57 | return file_path
58 |
59 |
60 | languages = {
61 | "en": "English",
62 | "es": "Spanish",
63 | "fr": "French",
64 | "it": "Italian",
65 | "nl": "Dutch",
66 | "pl": "Polish",
67 | "pt": "Portuguese",
68 | "ro": "Romanian",
69 | "sv": "Swedish",
70 | "tr": "Turkish"
71 | }
72 | lang = list(languages.keys())
73 |
74 |
75 | def get_hash(name):
76 | """
77 | This function receives the name of the file and returns the hash code
78 | Hash is composed by taking the first and the last 64kb of the video file,
79 | putting all together and generating a md5 of the resulting data (128kb).
80 |
81 | Parameters
82 | ----------
83 | name : str
84 | The path of the movie file/s.
85 | """
86 |
87 | readsize = 64 * 1024
88 | with open(name, 'rb') as f:
89 | data = f.read(readsize)
90 | f.seek(-readsize, os.SEEK_END)
91 | data += f.read(readsize)
92 | return hashlib.md5(data).hexdigest()
93 |
94 |
95 | def create_url(file_path):
96 | """
97 | This function will append the hash generated in get_hash function
98 | to the subtitle search URL and return the URL
99 |
100 | Parameters
101 | ----------
102 | file_path : str
103 | The path of the movie file/s.
104 | """
105 |
106 | film_hash = get_hash(name=file_path)
107 | url = "http://api.thesubdb.com/?action=search&hash={}".format(film_hash)
108 | return url
109 |
110 |
111 | def request_subtitle_languages(file_path):
112 | """
113 | Return a list with all the available languages in which
114 | subs are available for a file.
115 | Return None is no subs are available.
116 |
117 | Parameters
118 | ----------
119 | file_path : str
120 | The path of the file/s for which sub/s should be found.
121 | """
122 |
123 | url = create_url(file_path)
124 | header = {"user-agent": "SubDB/1.0 (SubtitleBOX/1.0; https://github.com/sameera-madushan/SubtitleBOX.git)"}
125 | req = requests.get(url, headers=header)
126 | if req.status_code == 200:
127 | k = req.content.decode('utf-8')
128 | available_languages = k.split(",")
129 | return available_languages
130 | else:
131 | available_languages = None
132 | return available_languages
133 |
134 |
135 | def download(file_path, data):
136 | """
137 | This function download the subtitle file from SubDB database.
138 | Filename of the subtitle file will be same as the movie name and append '.srt' extention at the end.
139 |
140 | Pythonic approach for filename generation is by https://www.reddit.com/user/panzerex/
141 |
142 | Parameters
143 | ----------
144 | file_path : str
145 | The path of the file/s for which sub/s should be found.
146 |
147 | data : bytes
148 | Binary data of the subtitle file.
149 | """
150 |
151 | filename = Path(file_path).with_suffix('.srt')
152 | with open(filename, 'wb') as f:
153 | f.write(data)
154 | f.close()
155 |
156 |
157 | def check_existence_of_subtitles(files_path):
158 | """
159 | Check if there exists at least one subtitle available for every
160 | file in files_path.
161 |
162 | If a file is not a video file or is corrupted, abort and exit.
163 | Otherwise, returns 3 output parameters:
164 | 1 - List of all available languages for each selected file.
165 | 2 - A list of Booleans, True if subtitles for specific file exists and False if not.
166 | 3 - The sequence number of the file whose subtitles do not exist.
167 |
168 | Parameters
169 | ----------
170 | files_path : str
171 | The path containing all the files for which subs should
172 | be found.
173 | """
174 |
175 | all_available_languages_selection = []
176 | bool_find_subtitles = np.ones(
177 | len(files_path), dtype=bool)
178 | loc_none = []
179 | for i, file_path in enumerate(files_path):
180 | try:
181 | found_languages = request_subtitle_languages(file_path)
182 | all_available_languages_selection.append(found_languages)
183 | if found_languages == None:
184 | loc_none.append(i)
185 | bool_find_subtitles[i] = False
186 | except:
187 | print(_("The selected file cannot be used to find subtitles:"))
188 | print(f" x {os.path.basename(file_path)}")
189 | print(_("Cancelled"))
190 | sys.exit()
191 | return all_available_languages_selection, bool_find_subtitles, loc_none
192 |
193 |
194 | def select_files_with_subtitles(all_files, bool_loc):
195 | """
196 | Returns the selected elements from the all_file list that are selected
197 | based on the values of the elements in bool_loc (list of Booleans)
198 |
199 | Parameters
200 | ----------
201 | all_files : list
202 | The path containing all the files for which subs should
203 | be found. Or a list of all available languages for each selected file.
204 |
205 | bool_loc : list
206 | List of Booleans, True if subtitles for specific file exists and False if not.
207 | """
208 |
209 | return np.asarray(all_files)[bool_loc]
210 |
211 |
212 | def get_common_languages_for_all_files(all_available_languages_selection, lang):
213 | """
214 | This function checks which common languages are available for all selected files.
215 | Returns a list of common languages.
216 |
217 | Parameters
218 | ----------
219 | all_available_languages_selection : list
220 | List containing all the languages available for the individual
221 | files for which subtitles exist at all.
222 |
223 | lang : list
224 | List of all language codes that can be found in the database.
225 | """
226 |
227 | availability_in_all_files = []
228 | if all_available_languages_selection.size > 0:
229 | for _lang in lang:
230 | availability_in_all_files.append(all((_lang in x)
231 | for x in all_available_languages_selection))
232 | return np.asarray(lang)[availability_in_all_files]
233 | else:
234 | print(_("There is no common language available for the selected files."))
235 | sys.exit()
236 |
237 |
238 | def main(cli_file_path, language_code_cli):
239 | banner = r'''
240 | ___ ___ ___ __ __
241 | / __| | _ ) / _ \ \ \/ /
242 | \__ \ | _ \ | (_) | > <
243 | |___/ |___/ \___/ /_/\_\
244 | _|"""""|_|"""""|_|"""""|_|"""""|
245 | "`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'
246 | Subtitles BOX
247 | '''
248 |
249 | print(banner)
250 | time.sleep(1)
251 |
252 | # If no file path was given as CLI argument, show the dialog window and ask for a file.
253 | if cli_file_path is None:
254 | files_path = tk_get_file_path()
255 | else:
256 | files_path = (cli_file_path,)
257 | if not path.exists(cli_file_path):
258 | print(_("File does not exist."))
259 | sys.exit()
260 |
261 | # Check, which files do and do not have subtitle file
262 | files_with_available_subs, find_subtitles, loc_none = check_existence_of_subtitles(
263 | files_path)
264 |
265 | # Check, which languages appears in all requested episodes
266 | all_available_languages = select_files_with_subtitles(
267 | files_with_available_subs, find_subtitles)
268 | available_languages = get_common_languages_for_all_files(
269 | all_available_languages, lang)
270 |
271 | if False in find_subtitles:
272 | print(_("Subtitle file is not available for following files:"))
273 | print("\n", end="")
274 | for i in loc_none:
275 | print(f" x {os.path.basename(files_path[i])}")
276 | if True in find_subtitles:
277 | print("________________________________________________________\n")
278 |
279 | files_path = select_files_with_subtitles(files_path, find_subtitles)
280 |
281 | print(_("Subtitles are available for following files:"))
282 | print("\n", end="")
283 | for file in files_path:
284 | print(f" - {os.path.basename(file)}")
285 | print("\n", end="")
286 |
287 | try:
288 | if len(files_path) == 1:
289 | print(_("Subtitles are available in following languages..."))
290 | print("\n", end="")
291 | elif len(files_path) > 1:
292 | print(_("Subtitles for all selected files are available in following languages..."))
293 | print("\n", end="")
294 | except:
295 | print(_("Error"))
296 |
297 | for i in available_languages:
298 | for k, v in languages.items():
299 | if i == k:
300 | print(" " + "\u2022" + " " + k + " (" + v + ")")
301 | print("\n", end="")
302 |
303 | # If no language code was given as CLI argument, ask it to the user
304 | if language_code_cli is None:
305 | selected_language = input(
306 | _("Choose your language (Please use language codes): ")).lower()
307 | else:
308 | selected_language = language_code_cli
309 |
310 | if selected_language in available_languages:
311 | for index, file_path in enumerate(files_path):
312 | url = create_url(file_path)
313 | search = re.sub(r'search', "download", url)
314 | final_url = search + "&language={}".format(selected_language)
315 | header = {"user-agent": "SubDB/1.0 (SubtitleBOX/1.0; https://github.com/sameera-madushan/SubtitleBOX.git)"}
316 | req = requests.get(final_url, headers=header)
317 | if req.status_code == 200:
318 | data = req.content
319 | download(file_path=file_path, data=data)
320 | print(f"\n{index+1}/{len(files_path)}" + " " +
321 | _("Subtitle downloaded successfully"))
322 | else:
323 | print("\n", end="")
324 | print(_("Unknown Error"))
325 | else:
326 | print("\n", end="")
327 | print(_("Invalid language code selected. Please try again."))
328 |
329 |
330 | if __name__ == "__main__":
331 | parser = argparse.ArgumentParser(description='SubtitleBOX CLI')
332 | parser.add_argument("-f", "--file_path",
333 | help=_("Path of the video file for which subtitles should be looked for"))
334 | parser.add_argument("-lang", "--language_code",
335 | help=_("Language code for subtitles. Can be en, es, fr, it, nl, pl, pt, ro, sv, tr"))
336 |
337 | args = parser.parse_args()
338 |
339 | main(args.file_path, args.language_code)
340 |
--------------------------------------------------------------------------------