How can we reach the information with the opensubtitles API? - python

I'm trying to take the first download 'str' zip link. I don't need more than one file of information. When I tried a not famous movie such as Shame 2011 My code worked but when I tried Avatar doesn't work. I think the code trying to take a lot of 'str' files information, API after that blocks this request.
**How I can reach the first English str file download link? **
from xmlrpc.client import ServerProxy
from pprint import pprint
imdb='tt0499549'#-->Avatar
#'tt1723811'-->Shame 2011
server = ServerProxy("http://api.opensubtitles.org/xml-rpc")
token = server.LogIn('yourusername', 'yourpassword', 'eng', 'TemporaryUserAgent')['token']
response = server.SearchSubtitles(token, [{'sublanguageid': 'eng', 'query':imdb }])#'moviehash':"0"
pprint(response)
You only have five attempts with TemporaryUserAgent.

Check out opensubtitle's new API - here's the documentation. It's way easier to use than the older API.
Grabbing subtitles is as easy as
headers = {
'Api-Key': api_key,
}
params = (
('imdb_id', movie_id),
)
response = requests.get('https://www.opensubtitles.com/api/v1/subtitles', headers=headers, params=params)
Where api_key is your api_key from their website, and movie_id is the movie's IMDB id (e.g., Titanic's ID is 0120338, and can be found within the URL of its movie page on IMDb - https://www.imdb.com/title/tt0120338/)
An example of the response returned looks like this:
{'id': '5164746',
'type': 'subtitle',
'attributes': {'subtitle_id': '5164746',
'language': 'en',
'download_count': 9608,
'new_download_count': 46,
'hearing_impaired': False,
'hd': True,
'format': None,
'fps': 23.976,
'votes': 0,
'points': 0,
'ratings': 0.0,
'from_trusted': False,
'foreign_parts_only': False,
'auto_translation': False,
'ai_translated': False,
'machine_translated': None,
'upload_date': '2020-02-09T13:59:42Z',
'release': '2160p.4K.BluRay.x265.10bit.AAC5.1-[YTS.MX]',
'comments': "Slightly resynced the 1080p.WEBRip.x264-[YTS.LT] version by explosiveskull to this 4K release. HI removed. I didn't do 4K sync for Infinity War, as they're already on site here:\r\nHi: https://www.opensubtitles.org/en/subtitles/7436082/avengers-infinity-war-en\r\nNo HI: https://www.opensubtitles.org/en/subtitles/7436058/avengers-infinity-war-en",
'legacy_subtitle_id': 8092829,
'uploader': {'uploader_id': 66694,
'name': 'pooond',
'rank': 'bronze member'},
'feature_details': {'feature_id': 626618,
'feature_type': 'Movie',
'year': 2019,
'title': 'Avengers: Endgame',
'movie_name': '2019 - Avengers: Endgame',
'imdb_id': 4154796,
'tmdb_id': 299534},
'url': 'https://www.opensubtitles.com/en/subtitles/legacy/8092829',
'related_links': {'label': 'All subtitles for Avengers: Endgame',
'url': 'https://www.opensubtitles.com/en/movies/2019-untitled-avengers-movie',
'img_url': 'https://s9.osdb.link/features/8/1/6/626618.jpg'},
'files': [{'file_id': 5274788,
'cd_number': 1,
'file_name': 'Avengers.Endgame.2019.2160p.4K.BluRay.x265.10bit.AAC5.1-[YTS.MX].srt'}]}}
To download a file you would take the 'file_id' and input it into a download request to the Open Subtitle API like this:
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)
Where auth is the authorization key you get from their API (/api/v1/login endpoint):
headers = {
'Api-Key': api_key,
'Content-Type': 'application/json',
}
data = '{"username":"__USERNAME","password":"__PASSWORD"}'
response = requests.post('https://www.opensubtitles.com/api/v1/login', headers=headers, data=data)
and __USERNAME and __PASSWORD is your account's username and password.

There is a solution
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)

Related

Using Python, how do I make a request to the shopee API to get a list of products on offer with my affiliate link

Using Python, how do I make a request to the shopee API to get a list of products on offer with my affiliate link?
I've made several scripts, but they all have a signature issue or unsupported authentication attempt. Does anyone have a working example of how to do this?
Below are two code examples I made, but they don't work.
Query: productOfferV2 and shopeeOfferV2
code1:
import requests
import time
import hashlib
appID = '18341090114'
secret = 'XMAEHHWQD3OEGQX5P33AFRREJEDSQX76'
# Set the API endpoint URL
url = "https://open-api.affiliate.shopee.com.my/graphql"
payload = """
{
"query": "query Fetch($page:2){
productOfferV2(
listType: 0,
sortType: 2,
page: $page,
limit: 50
) {
nodes {
commissionRate
commission
price
productLink
offerLink
}
}
}",
"operationName": null,
"variables":{
"page":0
}
}
"""
payload = payload.replace('\n', '').replace(':0', f':{2}')
timestamp = int(time.time())
factor = appID+str(timestamp)+payload+secret
signature = hashlib.sha256(factor.encode()).hexdigest()
# Set the request headers
headers = {
'Content-type': 'application/json',
'Authorization': f'SHA256 Credential={appID},Timestamp={timestamp},Signature={factor}'
}
# Send the POST request
response = requests.post(url, payload, headers=headers)
data = response.json()
print(data)
return = error-invalid-signature-python
code2:
import requests
import json
import hmac
import hashlib
appID = '18341090114'
secret = 'XMAEHHWQD3OEGQX5P33AFRREJEDSQX76'
query = '''query { productOfferV2(item_id: ALL) {
offers {
shop_id
item_price
discount_price
offer_id
shop_location
shop_name
}
}
}'''
def generate_signature(query, secret):
signature = hmac.new(secret.encode(), query.encode(), hashlib.sha256).hexdigest()
return signature
signature = generate_signature(query, secret)
url = 'https://open-api.affiliate.shopee.com.my/graphql'
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ' + appID + ':' + signature
}
payload = {
'query': query
}
response = requests.post(url, data=json.dumps(payload), headers=headers)
data = response.json()
print(data)

How to convert HTML request to JSON in python

I've been working with the HTML requests system for a while and facing issues regards on HTML requests and JSON requests. I want to convert name=dev&role=director to
{
'name': 'dev',
'role': 'director'
}
Code:
def do_POST(self):
self.send_response(200)
self.send_header('Content-Type', "application/json")
self.end_headers
content_len = int(self.headers.get('Content-Length', 0))
post_body = self.rfile.read(content_len,)
posttype = str(post_body, 'utf-8')
print(posttype)
Request Code:
header = {
'Content-Type': "application/json"
}
content ={
'name': 'dev',
'role': 'director'
}
r = requests.post(
"http://localhost:8080/",
headers=header,
data= content
)
Please help me to solve this issue.

How to create a file in github repo with content using Python CLI

I am trying to create a github repo with a file, repo created successfully but when I try to create the file it returning, "Not Found" message.
{"message":"Not Found","documentation_url":"https://docs.github.com/rest"}
create_repo
def create_repo(repo_name, file_name, file_content):
url = 'https://api.github.com/user/repos'
headers = {"Accept": "application/vnd.github.v3+json"}
data = {'name': repo_name}
r = requests.post(url, auth=(USERNAME,TOKEN), headers=headers, data=json.dumps(data))
print("Repo created")
# https://api.github.com/repos/InViN-test/test_repo1/contents/test.txt
data = {
"branch": "master",
'message': 'Initial commit',
"path": file_name,
'content': b64encode(file_content.encode('utf-8')).decode('utf-8'),
"committer": json.dumps({"name": USERNAME, "email": "myemail#gmail.com"}),
}
response = requests.put(f"{url}/{USERNAME}/{repo_name}/contents/{file_name}", auth=(USERNAME,TOKEN), headers=headers, data=json.dumps(data))
print(response.text)
I am not sure, what am I missing here?
My bad the URL was different. I keeping this question in-case if someone trying to do same as me.
def create_repo(repo_name, file_name, file_content):
headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {TOKEN}"}
data = {'name': repo_name}
r = requests.post("https://api.github.com/user/repos", headers=headers, data=json.dumps(data))
print("Repo created")
data = {
'message': 'Initial commit',
'content': b64encode(file_content.encode('utf-8')).decode('utf-8'),
}
response = requests.put(f"https://api.github.com/repos/{USERNAME}/{repo_name}/contents/{file_name}", headers=headers, data=json.dumps(data))
pprint(response.json())

Getting eBay Access Token (Exchanging auth token) with python requests

I'm trying to use this guide to get access token.
Here is my main file:
import requests
from utils import make_basic_auth_header, conf
code = '<Auth code here>'
url = "%s/identity/v1/oauth2/token" % conf('EBAY_API_PREFIX')
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': make_basic_auth_header()
}
data = {
'grant_type': 'authorization_code',
# 'grant_type': 'refresh_token',
'state': None,
'code': code,
'redirect_uri': conf('EBAY_RUNAME')
}
r = requests.post(
url,
data=data,
headers=headers,
)
Here's the make_basic_auth_header() function:
def make_basic_auth_header():
auth_header_payload = '%s:%s' % (conf('EBAY_APP_ID'), conf('EBAY_CERT_ID'))
auth_header_base64 = base64.b64encode(auth_header_payload)
auth_header = 'Basic %s' % auth_header_base64
return auth_header
But all I get in r.json() is:
{u'error_description': u'request is missing a required parameter or malformed.', u'error': u'invalid_request'}
I'm frustrated - what am I doing wrong?
sorry, I was stupid enough and I didn't see the tickbox on ebay.

File upload to python-eve using requests

I'm trying to upload a picture to a python-eve server using the requests library. In order to do that, I send a multipart/form-data request. This seems to be a problem for my schema, which looks like this:
schema = {
'name': {
'type': 'string',
'required': True
},
'description': {
'type': 'string'
},
'picture': {
'type': 'media'
},
'properties': {
'type' : 'dict'
}
}
The request looks like this:
import requests
file = open('/home/user/Desktop/1500x500.jpeg', 'rb')
payload = {'name': 'hello', 'properties': {'status': 'on_hold'}}
r = requests.post("http://localhost:5001/node", data=payload, files={'picture': file})
What I get is a ResourceInvalid exception:
ResourceInvalid: Failed. Response status: 422. Response message: UNPROCESSABLE ENTITY. Error message: {"_status": "ERR", "_issues": {"properties": "must be of dict type"}, "_error": {"message": "Insertion failure: 1 document(s) contain(s) error(s)", "code": 422}}
Is there any solution for this? Am I missing something about the request format?
Something like this should work just fine:
import requests
file = open('/home/user/Desktop/1500x500.jpeg', 'rb')
payload = {'name': 'hello'}
r = requests.post("http://localhost:5001/node", data=payload, files={'picture': file})
I have just had a similar issue. I suggest you try to change your code this way: dump your dictionary into a json object, and add an header to describe the content you are sending.
import requests
import json
file = open('/home/user/Desktop/1500x500.jpeg', 'rb')
payload = {'name': 'hello', 'properties': {'status': 'on_hold'}}
headers = {'Content-type': 'application/json; charset=utf-8'}
r = requests.post("http://localhost:5001/node", data=json.dumps(payload), files={'picture': file}, headers=headers)

Categories