Page 1 of 1

Old and New Api - Python

Posted: Fri Mar 26, 2021 1:36 am
by bbbirkan
I used old API with Python
My code:

Code: Select all

from xmlrpc.client import ServerProxy from pprint import pprint import json imbd='tt0499549'#-->Avatar #'tt1723811'-->Shame 2011 server = ServerProxy("http://api.opensubtitles.org/xml-rpc") token = server.LogIn('username', 'password', 'eng', 'xname ')['token'] response = server.SearchSubtitles(token, [{'sublanguageid': 'eng', 'query':imbd }]) pprint(response)
Problem:
When I try some movie id such as imbd='tt0499549'>Avatar
respond = {'data': [], 'seconds': 0.004, 'status': '200 OK'}
When I try
imbd=tt1723811'>Shame 2011
All good but I see a lot of file information I don't need. I need just one English subtitle file. I want to create a learning application. But still ok for me like that.
-----------------------------------------------------------
One guy response to me in 'StackOverflow. He said me to use the new app. I tried today

https://stackoverflow.com/questions/667 ... titles-api

Code: Select all

headers = { 'Api-Key': api_key, } params = ( ('imdb_id', movie_id), ) response = requests.get('https://www.opensubtitles.com/api/v1/subtitles', headers=headers, params=params) print(response) #-------------------------------------------------- headers = { 'Api-Key': api_key, 'Authorization': auth, 'Content-Type': 'application/json', } data = '{"file_id":5274788}' response = requests.post('https://www.opensubtitles.com/api/v1/download', headers=headers, data=data) print(response)
Result
<Response [200]>
<Response [503]>

I have an API key, API name. I don't know what I'm gonna write for Authorization. I tried all combinations.

How I can reach the subtitle information?

Re: Old and New Api - Python

Posted: Sat Mar 27, 2021 2:54 am
by bbbirkan
I found the solition

Code: Select all

import requests import json from pprint import pprint url = "https://www.opensubtitles.com/api/v1/login" headers = {'api-key':'YOUR API KEY', 'content-type': 'application/json'} user = {'username': 'YOUR USERNAME', 'password': "YOUR USER PASSWORD"} try: login_response = requests.post(url, data=json.dumps(user), headers=headers) login_response.raise_for_status() login_json_response = login_response.json() login_token = login_json_response['token'] except: print("Something wrong check again...") imdb_id="tt0499549" headers = { 'Api-Key': 'YOUR API KEY', } params = ( ('imdb_id', imdb_id), ) query_response = requests.get('https://www.opensubtitles.com/api/v1/subtitles?', params=params, headers=headers) query_json_response = query_response.json() print("Report:",query_response) #pprint(query_json_response)# All data here... query_file_name = query_json_response['data'][0]['attributes']['files'][0]['file_name'] query_file_no = query_json_response['data'][0]['attributes']['files'][0]['file_id'] movie_img = query_json_response['data'][0]['attributes']['related_links']['img_url'] print ("Movie Image url:",movie_img) print("File Number:",query_file_no) print("Subtile File Name:",query_file_name) download_url = "https://www.opensubtitles.com/api/v1/download" download_headers = {'api-key': 'YOUR API KEY', 'authorization':login_token, 'content-type': 'application/json'} download_file_id = {'file_id': query_file_no} download_response = requests.post(download_url, data=json.dumps(download_file_id), headers=download_headers) download_json_response = download_response.json() print("Report:",download_response) print(download_json_response) link=download_json_response['link'] saved_file_name = "subtitle.srt" r = requests.get(link) with open(saved_file_name, 'wb') as f: f.write(r.content)