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

Use XmlRpc DownloadSubtitles instead of direct link #71

Merged
merged 1 commit into from
Mar 30, 2021
Merged
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
58 changes: 19 additions & 39 deletions OpenSubtitlesDownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
# Carlos Acedo <carlos@linux-labs.net> for his work on the original script

import os
import re
import sys
import time
import gzip
import time
import base64
import struct
import hashlib
import argparse
Expand Down Expand Up @@ -801,7 +801,7 @@ def dependencyChecker():
subIndexTemp += 1

# Prepare download
subURL = subtitlesResultList['data'][subIndex]['SubDownloadLink']
subID = subtitlesResultList['data'][subIndex]['IDSubtitleFile']
subEncoding = subtitlesResultList['data'][subIndex]['SubEncoding']
subLangName = subtitlesResultList['data'][subIndex]['LanguageName']
subPath = ''
Expand All @@ -821,39 +821,22 @@ def dependencyChecker():

subPath = subPath.rsplit('.', 1)[0] + subLangId + '.' + subtitlesResultList['data'][subIndex]['SubFormat']

# Escape non-alphanumeric characters from the subtitles download path
if opt_gui != 'cli':
subPath = re.escape(subPath)
subPath = subPath.replace('"', '\\"')
subPath = subPath.replace("'", "\\'")
subPath = subPath.replace('`', '\\`')

# Make sure we are downloading an UTF8 encoded file
if opt_force_utf8:
downloadPos = subURL.find("download/")
if downloadPos > 0:
subURL = subURL[:downloadPos+9] + "subencoding-utf8/" + subURL[downloadPos+9:]

## Download and unzip the selected subtitles (with progressbar)
if opt_gui == 'gnome':
process_subtitlesDownload = subprocess.call("(wget -q -O - " + subURL + " | gunzip > " + subPath + ") 2>&1" + ' | (zenity --auto-close --progress --pulsate --title="Downloading subtitles, please wait..." --text="Downloading <b>' + subtitlesResultList['data'][subIndex]['LanguageName'] + '</b> subtitles for <b>' + videoTitle + '</b>...")', shell=True)
elif opt_gui == 'kde':
process_subtitlesDownload = subprocess.call("(wget -q -O - " + subURL + " | gunzip > " + subPath + ") 2>&1", shell=True)
else: # CLI
print(">> Downloading '" + subtitlesResultList['data'][subIndex]['LanguageName'] + "' subtitles for '" + videoTitle + "'")

if sys.version_info >= (3, 0):
tmpFile1, headers = urllib.request.urlretrieve(subURL)
tmpFile2 = gzip.GzipFile(tmpFile1)
byteswritten = open(subPath, 'wb').write(tmpFile2.read())
if byteswritten > 0:
process_subtitlesDownload = 0
else:
process_subtitlesDownload = 1
else: # python 2
tmpFile1, headers = urllib.urlretrieve(subURL)
tmpFile2 = gzip.GzipFile(tmpFile1)
open(subPath, 'wb').write(tmpFile2.read())
# Download and unzip the selected subtitles
process_subtitlesDownload = 1
downloadResult = osd_server.DownloadSubtitles(session['token'], [subID])
if('data' in downloadResult) \
and (downloadResult['data']) \
and (len(downloadResult['data']) > 0) \
and ('data' in downloadResult['data'][0]) \
and (downloadResult['data'][0]['data']):
decodedBytes = base64.b64decode(downloadResult['data'][0]['data'])
decompressed = gzip.decompress(decodedBytes)
if len(decompressed) > 0:
decodedStr = str(decompressed, subEncoding)

with open(subPath, 'w') as f:
f.write(decodedStr)

process_subtitlesDownload = 0

# If an error occurs, say so
Expand All @@ -862,9 +845,6 @@ def dependencyChecker():
osd_server.LogOut(session['token'])
sys.exit(2)

# Use a secondary tool after a successful download?
#process_subtitlesDownload = subprocess.call("(custom_command" + " " + subPath + ") 2>&1", shell=True)

## Print a message if no subtitles have been found, for any of the languages
if languageCount_results == 0:
superPrint("info", "No subtitles available :-(", '<b>No subtitles found</b> for this video:\n<i>' + videoFileName + '</i>')
Expand Down