This is my first ask. I am still new to python so it may be that I just didn't know how to ask the question correctly and missed it on stackoverflow!
What I want: to automate checking a website for changes. I want it to send me a notification every time there is a change and tell me what that change is.
So far I have 2 separate pieces of code that work:
- An API call that returns a list of results in json format. (there are 30 results in list always)
- A diff tool that checks if the json files are the same, and spits out the difference if they are not.
If I run the API call by itself, it works beautifully and saves json results to a file.
If I diff each file one at a time, the diff code works and spits out the change.
I want to make them work together - the end result being that I can set up a cron job + notification and go about my life, saving time on not checking these sites unless I know there has been a change.
My idea is that I am constantly checking the most recent pull against the last pull, and so I am storing the results in a folder.
In trying to get different parts to work, I separated the old results from the new results in folders, realizing I'm not sure how to tell the code to differentiate between the old and new.
I want to iterate through the folders, find the matching old file and new file pair, make each a json object, and then diff the two.
Parts of what I've tried work, but I am stuck on how to pair the old+new file together.
here's what I'm working with:
new_files = []
old_files = []
docs = for_docs[0]
for unid in uid_list:
with open('%s_my_results' % uid, 'w+') as outfile:
json.dump(docs, outfile)
for newFiles in os.walk('FILEPATH/new_files'):
newfiles.append(newFiles)
unpack_newFiles = sorted(newfiles[2])
os.chdir('FILEPATH/old_files'):
for oldfiles in os.walk('FILEPATH/old_files'):
old_files.append(oldfiles[2])
for fname in unpack_oldFiles:
if fname.endswith('.json'):
with open(fname, mode='rb+') as oldFile:
try:
unpack_oldFiles = json.load(oldFile)
except json.decoder.JSONDecodeError:
continue
This works - but the unpacked json object is still an unsorted list of json objects i think. So I'm definitely confused here, and trying to extricate myself from the knot.
the reason I used sorted was hoping that i could just force them to match in order, because they will always download in the same order. I think I found sorted was not the right tool but I have definitely confused myself out of a solution.
this is code that works to diff my json files:
with open('FILEPATH/old_file.json') as f:
old_docs = json.load(f)
with open('FILEPATH/new_file.json') as fc:
new_docs = json.load(fc)
# compare the two objects
thing = (old_docs==new_docs)
# log time and result
if thing is not True:
with open('logfile.txt', 'a+') as sys.stdout:
print(f'{date} this item was added: ')
print((DeepDiff(old_docs,new_docs)))
sys.stdout.close()
if thing is True:
with open('logfile.txt', 'a+') as sys.stdout:
print(f'{date} No Change')
sys.stdout.close()
I know what I want, which is:
#for file in list:
# if uid in file name matches:
# decode each file to json
# diff the two files
# spit out the result
To that end, I started writing variations of the below and I am definitely missing something. I found fnmatch but I am not sure how to use it.
for fname in folder 1, folder2:
if UID-in-filename matches: # I do not know how to set this up
thing = (oldfile == newfile)
if thing is not True:
with open('logfile.txt', 'a+') as sys.stdout:
print(f'{date} {UID} this item was added:')
print((DeepDiff(oldfile, newfile)
print(no change)
if thing is True:
with open('logfile.txt', 'a+') as sys.stdout:
print(f'{date} {UID} no change')
sys.stdout.close()
I hope I have done justice for my first ask. Thanks to all!