Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some minor improvements to the code #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
# SBOX - Subtitle Box
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.
Please Note: Subtitle file will be downloaded in to the same folder as the correspondent video file.
![ezgif com-gif-maker](https://user-images.githubusercontent.com/55880211/79194420-21ebc280-7e4a-11ea-84b2-f155d43dcd0a.gif)
### Git Installation
```
# clone the repo
$ git clone https://github.com/sameera-madushan/SubtitleBOX.git
# change the working directory to SubtitleBOX
$ cd SubtitleBOX
# install the requirements
$ pip3 install -r requirements.txt
```
### (OSX/Linux only) Install tkinter
If you are running python 3.7 or later, nothing has to be done. Earlier python3 versions require installation.
```
# Debian/Ubuntu
$ sudo apt install python3-tk
# macOS
# Follow the instructions on https://tkdocs.com/tutorial/install.html
```
## Usage
```
python sbox.py
```
### Support & Contributions
- Please ⭐️ this repository if this project helped you!
- Contributions of any kind welcome!
## License
MIT ©[sameera-madushan](https://github.com/sameera-madushan)
# SBOX - Subtitle Box

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.

Please Note: Subtitle file will be downloaded in to the same folder as the correspondent video file.

![ezgif com-gif-maker](https://user-images.githubusercontent.com/55880211/79194420-21ebc280-7e4a-11ea-84b2-f155d43dcd0a.gif)

### Git Installation
```
# clone the repo
$ git clone https://github.com/sameera-madushan/SubtitleBOX.git

# change the working directory to SubtitleBOX
$ cd SubtitleBOX

# install the requirements
$ pip3 install -r requirements.txt
```
### (OSX/Linux only) Install tkinter
If you are running python 3.7 or later, nothing has to be done. Earlier python3 versions require installation.
```
# Debian/Ubuntu
$ sudo apt install python3-tk

# macOS
# Follow the instructions on https://tkdocs.com/tutorial/install.html
```

## Usage

```
python sbox.py
```

### Support & Contributions
- Please ⭐️ this repository if this project helped you!
- Contributions of any kind welcome!

## License
MIT ©[sameera-madushan](https://github.com/sameera-madushan)
304 changes: 152 additions & 152 deletions sbox.py
Original file line number Diff line number Diff line change
@@ -1,152 +1,152 @@
# coded by sameera madushan
import os
import re
import time
import hashlib
import requests
import platform
import sys
from pathlib import Path
banner = r'''
___ ___ ___ __ __
/ __| | _ ) / _ \ \ \/ /
\__ \ | _ \ | (_) | > <
|___/ |___/ \___/ /_/\_\
_|"""""|_|"""""|_|"""""|_|"""""|
"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'
Subtitles BOX
'''
print(banner)
time.sleep(1)
def tk_get_file_path():
try:
import tkinter as tk
from tkinter import filedialog
except:
print("Error: tkinter is not installed/available. Please install and try again")
sys.exit()
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
try:
with open(file_path, 'r') as f:
pass
except:
print("Cancelled")
sys.exit()
return file_path
# got this from https://stackoverflow.com/a/58861718/13276219
def file_path():
# Get operating system
operating_system = platform.system()
if operating_system == 'Windows': # Windows, use default
import ctypes
co_initialize = ctypes.windll.ole32.CoInitialize
co_initialize(None)
import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import OpenFileDialog
file_dialog = OpenFileDialog()
ret = file_dialog.ShowDialog()
if ret != 1:
print("Cancelled")
sys.exit()
return file_dialog.FileName
else: # posix/linux/macos, use tkinter
return tk_get_file_path()
file_path = file_path()
languages = {
"en" : "English",
"es" : "Spanish",
"fr" : "French",
"it" : "Italian",
"nl" : "Dutch",
"pl" : "Polish",
"pt" : "Portuguese",
"ro" : "Romanian",
"sv" : "Swedish",
"tr" : "Turkish"
}
def get_hash(name):
readsize = 64 * 1024
with open(name, 'rb') as f:
size = os.path.getsize(name)
data = f.read(readsize)
f.seek(-readsize, os.SEEK_END)
data += f.read(readsize)
return hashlib.md5(data).hexdigest()
def create_url():
film_hash = get_hash(name=file_path)
url = "http://api.thesubdb.com/?action=search&hash={}".format(film_hash)
return url
def request_subtitile():
url = create_url()
header = { "user-agent": "SubDB/1.0 (SubtitleBOX/1.0; https://github.com/sameera-madushan/SubtitleBOX.git)" }
req = requests.get(url, headers=header)
if req.status_code == 200:
k = req.content.decode('utf-8')
global l
l = k.split(",")
print("\nSubtitle files are available in following languages...\n")
for i in l:
for k,v in languages.items():
if i == k:
print(" " + k + " (" + v + ")")
else:
print("Oops!! Subtitle not found.")
exit()
def download(data):
# from https://www.reddit.com/user/panzerex/
filename = Path(file_path).with_suffix('.srt')
with open(filename, 'wb') as f:
f.write(data)
f.close()
request_subtitile()
while True:
try:
select_langauge = input("\nChoose your langauge (Please use language codes): ").lower()
if select_langauge in l:
url = create_url()
search = re.sub(r'search', "download", url)
final_url = search + "&language={}".format(select_langauge)
header = { "user-agent": "SubDB/1.0 (SubtitleBOX/1.0; https://github.com/sameera-madushan/SubtitleBOX.git)" }
req = requests.get(final_url, headers=header)
if req.status_code == 200:
data = req.content
download(data=data)
print("\nSubtitle downloaded successfully")
break
else:
print("\nUnknown Error")
break
else:
print("\nInvalid language code selected. Please try again.")
except KeyboardInterrupt:
print("\nProgramme Interrupted")
break
# coded by sameera madushan

import os
import re
import time
import hashlib
import requests
import platform
import sys
from pathlib import Path


banner = r'''
___ ___ ___ __ __
/ __| | _ ) / _ \ \ \/ /
\__ \ | _ \ | (_) | > <
|___/ |___/ \___/ /_/\_\
_|"""""|_|"""""|_|"""""|_|"""""|
"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'
Subtitles BOX
'''

print(banner)
time.sleep(1)

def tk_get_file_path():
try:
import tkinter as tk
from tkinter import filedialog
except:
print("Error: tkinter is not installed/available. Please install and try again")
sys.exit()

root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()

try:
with open(file_path, 'r') as f:
pass
except:
print("Cancelled")
sys.exit()

return file_path

# got this from https://stackoverflow.com/a/58861718/13276219
def file_path():
# Get operating system
operating_system = platform.system()

if operating_system == 'Windows': # Windows, use default
import ctypes

co_initialize = ctypes.windll.ole32.CoInitialize
co_initialize(None)

import clr

clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import OpenFileDialog

file_dialog = OpenFileDialog()
ret = file_dialog.ShowDialog()
if ret != 1:
print("Cancelled")
sys.exit()
return file_dialog.FileName

else: # posix/linux/macos, use tkinter
return tk_get_file_path()


file_path = file_path()

languages = {
"en" : "English",
"es" : "Spanish",
"fr" : "French",
"it" : "Italian",
"nl" : "Dutch",
"pl" : "Polish",
"pt" : "Portuguese",
"ro" : "Romanian",
"sv" : "Swedish",
"tr" : "Turkish"
}

def get_hash(name):
readsize = 64 * 1024
with open(name, 'rb') as f:
size = os.path.getsize(name)
data = f.read(readsize)
f.seek(-readsize, os.SEEK_END)
data += f.read(readsize)
return hashlib.md5(data).hexdigest()

def create_url():
film_hash = get_hash(name=file_path)
url = "http://api.thesubdb.com/?action=search&hash={}".format(film_hash)
return url

def request_subtitile():
url = create_url()
header = { "user-agent": "SubDB/1.0 (SubtitleBOX/1.0; https://github.com/sameera-madushan/SubtitleBOX.git)" }
req = requests.get(url, headers=header)
if req.status_code == 200:
k = req.content.decode('utf-8')
global l
l = k.split(",")
print("\nSubtitle files are available in following languages...\n")
for i in l:
for k,v in languages.items():
if i == k:
print(" " + k + " (" + v + ")")
else:
print("Oops!! Subtitle not found.")
exit()

def download(data):
# from https://www.reddit.com/user/panzerex/
filename = Path(file_path).with_suffix('.srt')
with open(filename, 'wb') as f:
f.write(data)
f.close()

request_subtitile()

while True:
try:
select_langauge = input("\nChoose your langauge (Please use language codes): ").lower()
if select_langauge in l:
url = create_url()
search = re.sub(r'search', "download", url)
final_url = search + "&language={}".format(select_langauge)
header = { "user-agent": "SubDB/1.0 (SubtitleBOX/1.0; https://github.com/sameera-madushan/SubtitleBOX.git)" }
req = requests.get(final_url, headers=header)
if req.status_code == 200:
data = req.content
download(data=data)
print("\nSubtitle downloaded successfully")
break
else:
print("\nUnknown Error")
break
else:
print("\nInvalid language code selected. Please try again.")

except KeyboardInterrupt:
print("\nProgramme Interrupted")
break