Reputation: 105
I keep getting this error for my python code here -
Traceback (most recent call last):
File "/Users/ragz/cowin.py", line 70, in <module>
vaccine_check()
File "/Users/ragz/cowin.py", line 43, in vaccine_check
data = json.load(file)
File "/usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 293, in load
return loads(fp.read(),
File "/usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
I am creating a python script that utilizes the COWIN api (for the Indian Government COVID Vaccine distributions) and the twilio whatsapp api to inform me about any updates on vaccine availability - heres my code -
from cowin_api import CoWinAPI
import json
import datetime
import numpy as np
import os
from twilio.rest import Client
import selenium
from selenium import webdriver
import time
import io
import requests
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.common.keys import Keys
from threading import Thread
state_id = '21'
district_id = '395'
min_age_limit = 18
time = datetime.datetime.now()
cowin = CoWinAPI()
def vaccine_check():
try:
available_centers = cowin.get_availability_by_district(district_id)
#outputing it to a json file and bringing it back
json_output = json.dumps(available_centers, indent=4)
f = open(f'tests/vaccinecheck[{time.strftime("%b %d %Y %H|%M")}].json', 'a')
f.write(json_output)
f.close()
with open(f.name) as file:
data = json.load(file)
n = np.arange(100)
for x in np.nditer(n):
if data["centers"][x]["sessions"][0]["min_age_limit"] == 45:
print('')
else:
print(f'[{time.strftime("%b %d %Y %H:%M")}]', data["centers"][x]["name"], '-- vaccines:', data["centers"][x]["sessions"][0]['available_capacity'], '-- age-limit:', data["centers"][x]["sessions"][0]["min_age_limit"])
if data["centers"][x]["sessions"][0]["available_capacity"] >= 1:
twilio_send()
except IndexError: # catch the error
pass # pass will basically ignore it
def twilio_send():
client = Client()
from_whatsapp_number='whatsapp:TWILIO NUMBER'
to_whatsapp_number='whatsapp:PHONE NUMBER GOES HERE'
client.messages.create(body='VACCINE AVAILABLE!',
from_=from_whatsapp_number,
to=to_whatsapp_number)
vaccine_check()
Does anyone have any idea on how to fix this? I am also wondering if there is a way to loop it infinitely, if anyone knows.
Upvotes: 0
Views: 503
Reputation: 863
this is wrong (mode a
):
f = open(f'tests/vaccinecheck[{time.strftime("%b %d %Y %H|%M")}].json', 'a')
you can't append to a json file without making the content non-json
Either edit the content of the file or save it as a new json every time.
Note: you can use json.dump
instead of file.write
and json.dumps
.
Upvotes: 1