Skip to content

Commit

Permalink
Prep for v6 release
Browse files Browse the repository at this point in the history
  • Loading branch information
emericg committed Jan 6, 2024
1 parent 72c0acb commit cc9e4a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
50 changes: 26 additions & 24 deletions OpenSubtitlesDownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

# You can browse the project's GitHub page:
# - https://github.com/emericg/OpenSubtitlesDownload
# Learn much more about configuring OpenSubtitlesDownload.py on its wiki:

# Learn much more about it on the wiki:
# - https://github.com/emericg/OpenSubtitlesDownload/wiki

# Copyright (c) 2024 by Emeric GRANGE <emeric.grange@gmail.com>
Expand Down Expand Up @@ -46,13 +47,15 @@

# API endpoints
API_URL = 'https://api.opensubtitles.com/api/v1/'
API_URL_LOGIN_ENDPOINT = API_URL + 'login'
API_URL_LOGOUT_ENDPOINT = API_URL + 'logout'
API_URL_SEARCH_ENDPOINT = API_URL + 'subtitles'
API_URL_DOWNLOAD_ENDPOINT = API_URL + 'download'
API_URL_LOGIN = API_URL + 'login'
API_URL_LOGOUT = API_URL + 'logout'
API_URL_SEARCH = API_URL + 'subtitles'
API_URL_DOWNLOAD = API_URL + 'download'

# API key (required)
API_KEY = 'FNyoC96mlztsk3ALgNdhfSNapfFY9lOi'
# This application is registered
APP_NAME = 'OpenSubtitlesDownload'
APP_VERSION = '6.0'
APP_API_KEY = 'FNyoC96mlztsk3ALgNdhfSNapfFY9lOi'

# ==== OpenSubtitles.com account (required) ====================================

Expand Down Expand Up @@ -488,8 +491,8 @@ def dependencyChecker():
def getUserToken(username, password):
try:
headers = {
"User-Agent": "OpenSubtitlesDownload v6.0",
"Api-key": f"{API_KEY}",
"User-Agent": f"{APP_NAME} v{APP_VERSION}",
"Api-key": f"{APP_API_KEY}",
"Accept": "application/json",
"Content-Type": "application/json"
}
Expand All @@ -499,7 +502,7 @@ def getUserToken(username, password):
}

data = json.dumps(payload).encode('utf-8')
req = urllib.request.Request(API_URL_LOGIN_ENDPOINT, data=data, headers=headers)
req = urllib.request.Request(API_URL_LOGIN, data=data, headers=headers)
with urllib.request.urlopen(req) as response:
response_data = json.loads(response.read().decode('utf-8'))

Expand All @@ -517,14 +520,14 @@ def getUserToken(username, password):
def destroyUserToken(USER_TOKEN):
try:
headers = {
"User-Agent": "OpenSubtitlesDownload v6.0",
"Api-key": f"{API_KEY}",
"User-Agent": f"{APP_NAME} v{APP_VERSION}",
"Api-key": f"{APP_API_KEY}",
"Authorization": f"Bearer {USER_TOKEN}",
"Accept": "application/json",
"Content-Type": "application/json"
}

req = urllib.request.Request(API_URL_LOGOUT_ENDPOINT, headers=headers)
req = urllib.request.Request(API_URL_LOGOUT, headers=headers)
with urllib.request.urlopen(req) as response:
response_data = json.loads(response.read().decode('utf-8'))

Expand All @@ -536,12 +539,12 @@ def destroyUserToken(USER_TOKEN):
def searchSubtitles(**kwargs):
try:
headers = {
"User-Agent": "OpenSubtitlesDownload v6.0",
"Api-key": f"{API_KEY}"
"User-Agent": f"{APP_NAME} v{APP_VERSION}",
"Api-key": f"{APP_API_KEY}"
}

query_params = urllib.parse.urlencode(kwargs)
url = f"{API_URL_SEARCH_ENDPOINT}?{query_params}"
url = f"{API_URL_SEARCH}?{query_params}"
req = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(req) as response:
response_data = json.loads(response.read().decode('utf-8'))
Expand All @@ -556,8 +559,8 @@ def searchSubtitles(**kwargs):
def getSubtitlesInfo(USER_TOKEN, file_id):
try:
headers = {
"User-Agent": "OpenSubtitlesDownload v6.0",
"Api-key": f"{API_KEY}",
"User-Agent": f"{APP_NAME} v{APP_VERSION}",
"Api-key": f"{APP_API_KEY}",
"Authorization": f"Bearer {USER_TOKEN}",
"Accept": "application/json",
"Content-Type": "application/json"
Expand All @@ -567,7 +570,7 @@ def getSubtitlesInfo(USER_TOKEN, file_id):
}

data = json.dumps(payload).encode('utf-8')
req = urllib.request.Request(API_URL_DOWNLOAD_ENDPOINT, data=data, headers=headers)
req = urllib.request.Request(API_URL_DOWNLOAD, data=data, headers=headers)
with urllib.request.urlopen(req) as response:
result = response.read().decode('utf-8')

Expand All @@ -581,8 +584,8 @@ def getSubtitlesInfo(USER_TOKEN, file_id):
def downloadSubtitles(USER_TOKEN,subURL, subPath):
try:
headers = {
"User-Agent": "OpenSubtitlesDownload v6.0",
"Api-key": f"{API_KEY}",
"User-Agent": f"{APP_NAME} v{APP_VERSION}",
"Api-key": f"{APP_API_KEY}",
"Authorization": f"Bearer {USER_TOKEN}",
"Accept": "application/json",
"Content-Type": "application/json"
Expand Down Expand Up @@ -939,8 +942,7 @@ def downloadSubtitles(USER_TOKEN,subURL, subPath):
sys.exit(1)

except urllib.error.HTTPError as e:
superPrint("error", "Network error",
"Network error: " + e.reason)
superPrint("error", "Network error", "Network error: " + e.reason)

except (OSError, IOError, RuntimeError, AttributeError, TypeError, NameError, KeyError):
# An unknown error occur, let's apologize before exiting
Expand All @@ -951,7 +953,7 @@ def downloadSubtitles(USER_TOKEN,subURL, subPath):
"Just to be safe, please check:\n" + \
"- Your Internet connection status\n" + \
"- www.opensubtitles.com availability\n" + \
"- Your download limits (5 subtitles per 24h for free (non VIP) users, 5 subtitles per 10s)\n" + \
"- Your download limits (10 subtitles per 24h for non VIP users)\n" + \
"- That are using the latest version of this software ;-)")

except Exception:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ The subtitles search is done by precisly **identifying your video files** by com

The subtitles search and download service is powered by [opensubtitles.com](https://www.opensubtitles.com). Big thanks to their hard work on this amazing project! Be sure to [give them your support](https://www.opensubtitles.com/en/users/vip) if you appreciate the service provided, they sure need donations for handling the ever growing hosting costs!

> OpenSubtitlesDownload v6 use the new REST API from the [opensubtitles.com](https://www.opensubtitles.com) website.
> OpenSubtitlesDownload v6 use the new REST API from the [opensubtitles.com](https://www.opensubtitles.com) website
> Require an account from [opensubtitles.com](https://www.opensubtitles.com).
> Requires an account from [opensubtitles.com](https://www.opensubtitles.com)
### Features

Expand Down

0 comments on commit cc9e4a7

Please sign in to comment.