I'm been trying to get data from the SendinBlue API. The problem is the API have a limit of 100 registers per call and my Python loop is not working properly. This is what I have so far, the call works fine.
import requests
import pandas as pd
from pandas import json_normalize
import json
results = []
pagination = 0
url = "https://api.sendinblue.com/v3/smtp/statistics/events"
querystring = {"limit":"100","offset":pagination,"days":"15"}
headers = {
"Accept": "application/json",
"api-key": "XXXXXXX"
}
#respuesta de la API
response = requests.request("GET", url, headers=headers, params=querystring)
#convertir json a diccionario
data = json.loads(response.text)
#convertir diccionario a DataFrame
base = pd.json_normalize(data,record_path='events')
The data structure is like this:
{'events': [
{'email': 'chusperu#gmail.com',
'date': '2020-10-18T17:18:58.000-05:00',
'subject': 'Diego, ¡Gracias por registrarte! đŸ˜‰',
'messageId': '<202010181429.12179607081#smtp-relay.mailin.fr>',
'event': 'opened',
'tag': '',
'from': 'ventas01#grupodymperu.com',
{'email': 'cynthiaapurimac#gmail.com',
'date': '2020-10-18T17:52:56.000-05:00',
'subject': 'Alvarado, ¡Gracias por registrarte! đŸ˜‰',
'messageId': '<202010182252.53640747487#smtp-relay.mailin.fr>',
'event': 'requests',
'tag': '',
'from': 'ventas01#grupodymperu.com'},
....
The loop I have tried is this, but it only paginated the first 200 registers. What I'm doing wrong?
for i in data['events']:
results.append(i)
while response.status_code == 200:
pagination += 100
querystring ['offset'] = pagination
response = requests.request("GET", url, headers=headers, params=querystring)
data = json.loads(response.text)
for i in data['events']:
results.append(i)
else:
break
print(results)
Finally get it.
import requests
import pandas as pd
from pandas import json_normalize
import json
# Excel = "C:/Users/User/PycharmProjects/Primero/DataSendin.xlsx"
pagination = 0
url = "https://api.sendinblue.com/v3/smtp/statistics/events"
querystring = {"limit":"100","offset":f"{pagination}","days":"3"}
headers = {
"Accept": "application/json",
"api-key": "Your API key"
}
response = requests.request("GET", url, headers=headers, params=querystring)
#respuesta de la API
try:
#convertir json a diccionario
results = []
data = json.loads(response.text)
results.append(data)
if not data:
print("no hay data")
else:
while response.status_code == 200:
pagination += 100
querystring ['offset'] = pagination
response = requests.request("GET", url, headers=headers, params=querystring)
data = json.loads(response.text)
results.append(data)
if not data:
break
except ValueError:
"no data"
#convertir diccionario a DataFrame
final = list(filter(None, results))
base = pd.json_normalize(final,record_path='events')
base
Related
When I'm trying to create a deal through the hubspot api, all that is being created is a completely blank deal even though I am passing through populated data
Api Url: https://developers.hubspot.com/docs/api/crm/deals
Here is the following code that I am trying:
import json
import requests
hubspot_api_key = "MY_API_KEY"
url = 'https://api.hubapi.com/crm/v3/objects/deals?hapikey={}'.format(hubspot_api_key)
headers = {"Content-Type": "application/json"}
deals_post = {
'amount': "4034.75",
'closedate': '2021-05-10T12:04:00.000Z',
'dealname': 'Custom data integrations',
'dealstage': 'closedwon',
'hubspot_owner_id': "5448459615",
'pipeline': 'default'
}
response = requests.post(url, headers=headers, data=json.dumps(deals_post))
print(response.text)
And here is the result of it:
The solution to this issue would be adding properties to the data dictionary
import json
import requests
hubspot_api_key = "MY_API_KEY"
url = 'https://api.hubapi.com/crm/v3/objects/deals?hapikey={}'.format(hubspot_api_key)
headers = {"Content-Type": "application/json"}
deals_post = {
'properties': {
'amount': "4034.75",
'closedate': '2021-05-10T12:04:00.000Z',
'dealname': 'Custom data integrations',
'dealstage': 'closedwon',
'hubspot_owner_id': 83849850,
'pipeline': 'default'
}
}
response = requests.post(url, headers=headers, data=json.dumps(deals_post))
print(response.text)
This results in a filled out deal according to the data that was passed in
I have a while loop for getting JSON response from API. Here is my code.
import json
import requests
import pandas as pd
url = "https://org.zendesk.com/api/v2/tickets?sort_by=created_at"
payload = ""
headers = {
'Authorization': "Basic blablabla"
}
params="page[size]=3"
while url:
response = requests.request("GET", url, data=payload, headers=headers, params=params)
data = response.json()
#get only tickets array and drop everything else
dataTickets = json.dumps(data['tickets'],indent=2)
#print(dataTickets)
#get next page of data
url = data['links']['next']
Each loop gives me the following.
[
{'url': 'https://org.zendesk.com/api/v2/tickets/4025.json'},
{'url': 'https://org.zendesk.com/api/v2/tickets/4026.json'},
{'url': 'https://org.zendesk.com/api/v2/tickets/4027.json'}
]
How to merge each loop run (inside the loop) to get merged array as follows:
[
{'url': 'https://org.zendesk.com/api/v2/tickets/4025.json'},
{'url': 'https://org.zendesk.com/api/v2/tickets/4026.json'},
{'url': 'https://org.zendesk.com/api/v2/tickets/4027.json'},
{'url': 'https://org.zendesk.com/api/v2/tickets/4028.json'},
{'url': 'https://org.zendesk.com/api/v2/tickets/4029.json'},
{'url': 'https://org.zendesk.com/api/v2/tickets/4030.json'},
{'url': 'https://org.zendesk.com/api/v2/tickets/4031.json'},
{'url': 'https://org.zendesk.com/api/v2/tickets/4032.json'},
{'url': 'https://org.zendesk.com/api/v2/tickets/4033.json'}
]
I think I got it. I went about it slightly differently.
I am processing the full request where I merge the "tickets" array.
After I merge it, I take just the "tickets" out and print it. This helps me to manipulate just the tickets later.
The data in one request loop looks as follows:
{
"tickets": [
{
"url": "https://org.zendesk.com/api/v2/tickets/3058.json"
},
{
"url": "https://org.zendesk.com/api/v2/tickets/3059.json"
}
],
"next_page": "https://org.zendesk.com/api/v2/tickets.json?page=2",
"previous_page": null,
"count": 3234
}
import json
import requests
import pandas as pd
url = "https://org.zendesk.com/api/v2/tickets?sort_by=created_at"
payload = ""
headers = {
'Authorization': "Basic blablabla"
}
params="page[size]=3"
while url:
response = requests.request("GET", url, data=payload, headers=headers, params=params)
data = response.json()
#merge (extend) tickets array for every loop
data["tickets"] += data["tickets"]
#get next page of data
url = data['links']['next']
# Format the merged JSON and take out just the tickets array
dataout = json.dumps(data["tickets"],indent=2)
# Write formatted JSON into file
with open('tickets1.json', 'w') as f:
f.write(dataout)
I am looking to loop through about 5 stock tickers using an API. Currently I have "MSFT" as the only stock being called; however, I would like to make a stock list to return multiple responses.
For example:
stock_list = ["MSFT", "AAPL", "LMD", "TSLA", "FLGT"]
How can I request all 5 of these stocks to the querystring to print each response? Here is what I have currently which prints only "MSFT" into a json format...
import requests
#Use RapidAPI request to call info on Stocks
url = "https://alpha-vantage.p.rapidapi.com/query"
querystring = {"function":"GLOBAL_QUOTE","symbol": "MSFT"}
headers = {
'x-rapidapi-key': "KEY INSERTED HERE,
'x-rapidapi-host': "alpha-vantage.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
Try using a for loop.
import requests
url = 'https://alpha-vantage.p.rapidapi.com/query'
headers = {
'x-rapidapi-key': '<API KEY>',
'x-rapidapi-host': 'alpha-vantage.p.rapidapi.com',
}
tickers = ['MSFT', 'AAPL', 'LMD', 'TSLA', 'FLGT']
for ticker in tickers:
querystring = {'function': 'GLOBAL_QUOTE', 'symbol': ticker}
r = requests.get(url, headers=headers, params=querystring)
print(r.json())
You can also try pretty printing the json output using the json module.
import json
# ... your code ...
for ticker in tickers:
# ... your code ...
print(json.dumps(r.json(), indent=2))
Also, you should delete your API key before its abused by anyone! These have to be kept safe somewhere.
I am trying to scrape domain search page (where you can enter the keyword, and get some random results) and
I found this api url in network tab https://api.leandomainsearch.com/search?query=computer&count=all (for keyword: computer), but I am getting this error
{'error': True, 'message': 'Invalid API Credentials'}
here is the code
import requests
r = requests.get("https://api.leandomainsearch.com/search?query=cmputer&count=all")
print(r.json())
The site needs that you set Authorization and Referer HTTP headers.
For example:
import re
import json
import requests
kw = 'computer'
url = 'https://leandomainsearch.com/search/'
api_url = 'https://api.leandomainsearch.com/search'
api_key = re.search(r'"apiKey":"(.*?)"', requests.get(url, params={'q': kw}).text)[1]
headers = {'Authorization': 'Key ' + api_key, 'Referer': 'https://leandomainsearch.com/search/?q={}'.format(kw)}
data = requests.get(api_url, params={'query': kw, 'count': 'all'}, headers=headers).json()
# uncomment this to print all data:
# print(json.dumps(data, indent=4))
for d in data['domains']:
print(d['name'])
print()
print('Total:', data['_meta']['total_records'])
Prints:
...
blackopscomputer.com
allegiancecomputer.com
northpolecomputer.com
monumentalcomputer.com
fissioncomputer.com
hedgehogcomputer.com
blackwellcomputer.com
reflectionscomputer.com
towerscomputer.com
offgridcomputer.com
redefinecomputer.com
quantumleapcomputer.com
Total: 1727
Can anyone please tell me how to write a cURL to get events (only modified) list with nextSyncToken?
This is my code that's not working:
def get_headers():
headers = {
#'Content-Type': "application/json",
'Authorization': access_token_json
}
return headers
def get_nexttokensync_list_event():
url_get_list_event = "https://www.googleapis.com/calendar/v3/calendars/id#gmail.com/events"
querystring = {"nextSyncToken": "CMCEh************jd4CGAU="}
response = requests.request("GET", url_get_list_event, headers=headers, params=querystring)
json_event_list_formatted = response.text
print(json_event_list_formatted)
Yes, i've done it!
Here is my code:
import requests
url = "https://www.googleapis.com/calendar/v3/calendars/here_calendar_id/events"
querystring = {"syncToken":"here_token"}
headers = {
'Content-Type': "application/json",
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)