pythonsubtitlexmlrpclib

DownloadSubtitle method from opensubtitles api returning blank data


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.


Solution

  • 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