Reputation: 71
I am making an API request to a server and then parsing the json response and saving it to a csv file. Everything was working but now suddenly, the program is throwing errors.
Weird part is that when I run the same code on my laptop it works but on the vps, it's throwing exception (it was working on vps as well just moments ago, and nothing has changed on the vps that could cause the exception).
my python code:
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
import requests
import json
import csv
from datetime import datetime
print("Started...")
def fn():
print(" Writing Data...")
session_id = "auZsJ4F2RsQNJxSPTMDt2238324"
Outlook='http://www.myfxbook.com/api/get-community-outlook.json?session=' + session_id
Outlook_response = requests.get(Outlook)
Outlook_data = Outlook_response.json()['symbols']
now = datetime.now()
current_time = now.strftime("%H:%M")
EURUSD=Outlook_data[0]
SHORTPERC0=EURUSD['shortPercentage']
LONGPERC0 =EURUSD['longPercentage']
SHORTvolume0=EURUSD['shortVolume']
longVolume0=EURUSD['longVolume']
longPositions0=EURUSD['longPositions']
shortPositions0=EURUSD['shortPositions']
with open('myfile.csv', 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow([current_time, SHORTvolume0, longVolume0, longPositions0, shortPositions0])
print(" done...")
sched = BlockingScheduler()
# Execute fn() at the start of each minute.
sched.add_job(fn, trigger=CronTrigger(second=1))
sched.start()
these are the errors:
Is the error related to APScheduler? Or is it a key error?
json response looks like this:
{
"error": false,
"message": "",
"symbols": [{
"name": "EURUSD",
"shortPercentage": 45,
"longPercentage": 55,
"shortVolume": 23273.25,
"longVolume": 28123.5,
"longPositions": 74045,
"shortPositions": 60996,
"totalPositions": 135041,
"avgShortPrice": 1.1709,
"avgLongPrice": 1.1975
}
Upvotes: 0
Views: 645
Reputation: 1659
this may work for you :
Request = 'https://www.myfxbook.com/api/login.json?email=username&password=password'
response = requests.get(Request, verify = False)
data = response.json()
Session = data['session']
path = ('https://www.myfxbook.com/api/get-community-outlook.json?session=' + Session)
Outlook_response = requests.get(path, verify = False)
Outlook_data = Outlook_response.json()['symbols']
to not get blocked for making many request use defferent user-agent header
for example :
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
}
Outlook_response = requests.get(Outlook, headers=headers)
Outlook_data = Outlook_response.json()['symbols']
Upvotes: 1
Reputation:
That has something to do with the server, and it can't even get the JSON response itself, not the symbols
key missing. That error may occur most of the time, you can just ignore it, or handle it as follows.
try:
Outlook_data = Outlook_response.json()['symbols']
except:
print("An error has occurred while getting the data. Please try again later.")
Upvotes: 1