the documentation:
https://trac.opensubtitles.org/projects/opensubtitles/wiki/XMLRPC
from xmlrpc.client import ServerProxy
url = 'http://api.opensubtitles.org/xml-rpc'
server = ServerProxy(url)
token = server.LogIn('username', 'Password', 'eng', 'TemporaryUserAgent')['token']
data = server.SearchSubtitles(token, [{'sublanguageid': 'english', 'query': 'Movie Name'}])
I print data and it has IDSubtitleFile in it. but then I try
id_subtitle_file = data.get('IDSubtitleFile')
and it prints None
I have seen it done this way too
id1= data.get('data')
subtitle_id = id1.get('IDSubtitleFile'))
but this throws 'list' object has no attribute 'get'
what am I doing wrong?
Please check the API documentation: here
The search request response structure:
{
status: ...
data: [ # <-- array
...records...
]
}
Code sample below:
from xmlrpc.client import ServerProxy
server = ServerProxy("http://api.opensubtitles.org/xml-rpc")
token = server.LogIn('username', 'pass', 'eng', 'TemporaryUserAgent')['token']
response = server.SearchSubtitles(token,
[{'sublanguageid': 'english', 'query': 'Movie Name'}]
)
for record in response["data"]:
print(record["IDSubtitleFile"])
Related
The documentation of the API is here, and I try to implement this line in python
//retrieve entries created on a specific day (use the date_created field)
//this example returns entries created on September 10, 2019
https://localhost/wp-json/gf/v2/entries?search={"field_filters": [{"key":"date_created","value":"09/10/2019","operator":"is"}]}
But when I try to do with python in the following code, I got an error:
import json
import oauthlib
from requests_oauthlib import OAuth1Session
consumer_key = ""
client_secret = ""
session = OAuth1Session(consumer_key,
client_secret=client_secret,signature_type=oauthlib.oauth1.SIGNATURE_TYPE_QUERY)
url = 'https://localhost/wp-json/gf/v2/entries?search={"field_filters": [{"key":"date_created","value":"09/01/2023","operator":"is"}]}'
r = session.get(url)
print(r.content)
The error message is :
ValueError: Error trying to decode a non urlencoded string. Found invalid characters: {']', '['} in the string: 'search=%7B%22field_filters%22:%20[%7B%22key%22:%22date_created%22,%22value%22:%2209/01/2023%22,%22operator%22:%22is%22%7D]%7D'. Please ensure the request/response body is x-www-form-urlencoded.
One solution is to parameterize the url:
import requests
import json
url = 'https://localhost/wp-json/gf/v2/entries'
params = {
"search": {"field_filters": [{"key":"date_created","value":"09/01/2023","operator":"is"}]}
}
headers = {'Content-type': 'application/json'}
response = session.get(url, params=params, headers=headers)
print(response.json())
But in the retrieved entries, the data is not filtered with the specified date.
In the official documentation, they gave a date in this format "09/01/2023", but in my dataset, the format is: "2023-01-10 19:16:59"
Do I have to transform the format ? I tried a different format for the date
date_created = "09/01/2023"
date_created = datetime.strptime(date_created, "%d/%m/%Y").strftime("%Y-%m-%d %H:%M:%S")
What alternative solutions can I test ?
What if you use urllib.parse.urlencode function, so your code would looks like:
import json
import oauthlib
from requests_oauthlib import OAuth1Session
import urllib.parse
consumer_key = ""
client_secret = ""
session = OAuth1Session(consumer_key,
client_secret=client_secret,signature_type=oauthlib.oauth1.SIGNATURE_TYPE_QUERY)
params = {
"search": {"field_filters": [{"key":"date_created","value":"09/01/2023","operator":"is"}]}
}
encoded_params = urllib.parse.urlencode(params)
url = f'https://localhost/wp-json/gf/v2/entries?{encoded_params}'
r = session.get(url)
print(r.content)
hope that helps
I had the same problem and found a solution with this code:
params = {
'search': json.dumps({
'field_filters': [
{ 'key': 'date_created', 'value': '2023-01-01', 'operator': 'is' }
],
'mode': 'all'
})
}
encoded_params = urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
url = 'http://localhost/depot_git/wp-json/gf/v2/forms/1/entries?' + encoded_params + '&paging[page_size]=999999999' # nombre de réponses par page forcé manuellement
I'm not really sure what permitted it to work as I'm an absolute beginner with Python, but I found that you need double quotes in the URL ( " ) instead of simple quotes ( ' ), so the solution by William Castrillon wasn't enough.
As for the date format, Gravity Forms seems to understand DD/MM/YYYY. It doesn't need a time either.
I'm trying to remove users from an audience in the Facebook Ads API, using the delete_users function.
This is my code so far:
'''
from collections import UserList
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.customaudience import CustomAudience
from facebook_business.api import FacebookAdsApi
from facebook_business.api import FacebookRequest
access_token = 'XXX'
id = 'XXX'
api = FacebookAdsApi.init(access_token=access_token)
fields = []
params = {
'name': 'Test Audience',
'subtype': 'CUSTOM',
'description': 'People who purchased on my website',
'customer_file_source': 'USER_PROVIDED_ONLY',
}
print ( AdAccount(id).create_custom_audience(
fields=fields,
params=params,
) )
fields = []
params = {
'name': 'Test Audience',
'payload': hashed_list_of_emails
}
CustomAudience.delete_users(
self
fields=fields,
params=params,
method='DELETE',
endpoint='/users',
)
'''
I am receiving an error which tells me to replace self with node_id=self['id'], but I am unclear on what this is. I am also unclear on which part of the delete_users function should contain the audience_id, and where to insert the payload of the list of hashed email to remove from the audience.
I want to execute a simple GraphQL query in which I pass a variable.
import requests
import json
var = "whatever"
query = """
query ($var: String!){
styles(locale: "en", styleNumbers: [ $var] ) {
styleOptions {
parms {
parm1
parm2
}
}
}
}
"""
url = 'https://sth_random.io/graphql?'
response = requests.get(url, json={'query': query, '$var': var})
response = response.json()
print(response)
but I am getting the following error:
{'errors': [{'message': 'Variable "$var" of required type "String!" was not provided.', 'locations': [{'line': 2, 'column': 12}]}]}
What am I missing?
Thank you in advance.
The request body should include a variables key that is itself a dictionary of the variable values:
variables = {'var': var}
response = requests.post(url, json={'query': query, 'variables': variables})
is anyone familiar with the Python Sketch Engine API and could tell us how to get the frequency of an n-gram?
So far we have this (example from website):
import requests
base_url = 'https://api.sketchengine.co.uk/bonito/run.cgi'
data = {
'corpname': 'bnc2',
'format': 'json',
'lemma': 'book',
'lpos': '-v',
'username': '...',
'api_key': '...'
# get it here: https://the.sketchengine.co.uk/auth/api_access/
}
d = requests.get(base_url + '/wsketch', params=data).json()
print("frequency=", d['freq'])
This gives us the frequency of a lemma, but not an n-gram.
The endpoint /wsketch only takes a single lemma as input. To work with n-grams a different endpoint should be used, for example /view.
import requests
base_url = 'https://api.sketchengine.co.uk/bonito/run.cgi'
data = {
'corpname': 'bnc2',
'format': 'json',
'q': 'q[lemma="read"][lemma="book"]',
'username': '...',
'api_key': '...'
# get it here: https://the.sketchengine.co.uk/auth/api_access/
}
d = requests.get(base_url + '/view', params=data).json()
print("frequency=", d['relsize'])
Here 'relsize' refers to frequency per million.
I am trying to make a program that will access http://mailfreeonline.com/free_anonymous_mail and send me an email. This is what I have so far, it says there is a syntax error with the second colon?
import urllib
params = urllib.urlencode(
{'tomailget': 'myemail#gmail.com',
'fromnameget': 'Matt'
'frommailget': 'test#test.com'
'subjectget': 'Subject'
'messageget': 'Message' })
f = urllib.urlopen("http://www.mailfreeonline.com/free_anonymous_mail", params)
You have to add comma after each parameter, you forgot to do that
params = urllib.urlencode( {'tomailget': 'myemail#gmail.com',
'fromnameget': 'Matt',
'frommailget': 'test#test.com',
'subjectget': 'Subject',
'messageget': 'Message' })
I don't see commas between the elements in the dictionary
{'tomailget': 'myemail#gmail.com',
'fromnameget': 'Matt'**,**
'frommailget': 'test#test.com'**,**
'subjectget': 'Subject'**,**
'messageget': 'Message' })