Reputation: 119
Trying to loop through the json results from a url then print the results only if there is a change since the last result every 1 minute. Everything works except the results are just being re-written over so it always shows 'nothing changed'.
import time
import sys
import pandas as pd
def main():
result = []
print('Checking for available campsites....')
url = "https://api.reserve.albertaparks.ca/api/public/campgrounds/67/availability?campingType=campsite" \
"&arrivalDt=01-Aug-2021&departureDt=13-Sept-2021&unitSize=40&siteType=VEH"
result = pd.read_json(url)
df = pd.DataFrame.from_records(result['availability'])
df = df[df['available'] == True]
df = df[df['fcfs'] == False]
df.set_index('date', inplace=True)
print(df)
res_before = ""
while True:
res = main()
if res != res_before:
print(res)
res_before = res
else:
print("nothing changed")
# time.sleep(300)
for i in range(60):
msg = "Pausing for 1 minute"
sys.stdout.write("\r{} {} seconds ".format(msg, i))
time.sleep(1)
sys.stdout.flush()
available fcfs
date
01-Aug-2021 True False
02-Aug-2021 True False
03-Aug-2021 True False
04-Aug-2021 True False
05-Aug-2021 True False
06-Aug-2021 True False
07-Aug-2021 True False
08-Aug-2021 True False
09-Aug-2021 True False
10-Aug-2021 True False
Checking for available campsites....
None
Pausing for 1 minute 59 seconds Checking for available campsites....
nothing changed
Upvotes: 2
Views: 191
Reputation: 5648
As @Praveen noted, you didn't return anything from your function. Below is a section of code with a couple changes that make your code run correctly. See commented lines for needed changes.
df.set_index('date', inplace=True)
print(df)
return df #>>>> return dataframe
res_before = pd.DataFrame() #>>>> want to compare dataframes correctly
while True:
res = main()
if not res.equals(res_before): #>>>> want to compare dataframes correctly
print(res)
res_before = res
Upvotes: 1
Reputation: 959
You did not return anything from the main() function. So, you can't assign a function as a value to a variable
Upvotes: 1