2

This is one of my first projects in Python and I am encountering this issue. The code:

def get_sub(path):
        server = xmlrpclib.Server(url)
        token = server.LogIn('', '', 'en', 'OSTestUserAgent')['token']
        print server.LogIn('', '', 'en', 'OSTestUserAgent')
        sub_id = get_hash(path)
        print sub_id
        resp = server.DownloadSubtitles(token, [sub_id])
        print resp
        data = resp['data'][0]['data']
        print data

The variable 'data' should be a base64 encoded and gzipped data but instead it outputs 'H4sIAAAAAAAAAwMAAAAAAAAAAAA=' (200 OK status code) which basically is a blank data. Tried checking the hash function with the sample from the API and it has no problem. I can't get my head around this one, any help would be appreciated. You can check the API here.

1 Answers1

0

What are you setting sub_id to? It looks like it's a hash, however, it should be an integer representing a subtitle file ID (see the documentation).

An example subtitle file ID is 1951894257.

import xmlrpclib, io, gzip

url = 'https://api.opensubtitles.org/xml-rpc'
server = xmlrpclib.Server(url)
token = server.LogIn('', '', 'en', 'OSTestUserAgent')['token']
sub_id = 1951894257
resp = server.DownloadSubtitles(token, [1951894257])
if resp['status'] == '200 OK':
    compressed_data = resp['data'][0]['data'].decode('base64')
    sub_text = gzip.GzipFile(fileobj=io.BytesIO(compressed_data)).read()
    print sub_text
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • So subtitle file ID is not the hash? Where do I get the subtitle file id? – Antony Tejakusuma Jul 27 '15 at 03:49
  • No, it is the ID used by OSDb. You can search using [SearchSubtitles](http://trac.opensubtitles.org/projects/opensubtitles/wiki/XmlRpcSearchSubtitles) with the hash of a video file, or an IMDB ID. That should give you a subtitle ID if known to OSDb. – mhawke Jul 27 '15 at 03:52
  • NEVERMIND I GOT IT! The IDSubtitleFile is obtained through the SearchSubtitle function, thank you so much. Anyway can you explain the sub_text variable, i'm still learning how to gzip and ungzip files. – Antony Tejakusuma Jul 27 '15 at 04:12
  • As you know, the API returns a base64 encoded gzip compressed subtitle string. Decoding that requires a base64 decode, then running that through gzip decompression. A `gzip.GzipFile` object is instantiated and the decompressed data read from that. Wrapping the compressed string in a `BytesIO` is required because `GzipFile` will only work with "file-like" objects, not strings. – mhawke Jul 27 '15 at 04:24