0

I was wondering, whether it is possible to compare two text files(the index stock file created when the program is started and one created every 15 seconds) and trigger an event if the available line is different for a product?

I'm looking forward to your answers : )

import bs4 as bs
import urllib.request
import discord
from discord.ext import commands
from dhooks import Webhook
import requests
import json
import time
from datetime import datetime
linex = 1
liney = 1

r = requests.get("https://www.feature.com/products.json")
products = json.loads((r.text))["products"]
now = datetime.now()
data = ("available")
for product in products:
    print("============================================")
    print(product["title"])
    print(product["tags"])
    print(product["published_at"])
    print(product["created_at"])
    print(product["product_type"])
    for variant in product["variants"]:
        print(variant['title'])
        print(variant['available'],"\n")
        data =("available")

with open("stock_index.txt","w") as f:
    f.write(str(now)+"\n")
    f.write("============================================\n")
    f.write("=============STOCK-AVAILABILITY============\n")
    f.write("============================================\n\n\n")
    for product in products:        
        for variant in product["variants"]:
            if variant['available'] == True:
                f.write(product["title"]+"\n")
                f.write(variant['title']+"\n")
                f.write((str(variant["available"])+("\n")))
                f.write("--------------------\n")

file1 = open('stock_index.txt', 'r')
file2 = open('stock_index_hourly-update.txt', 'r')
FO = open('some_output_file.txt', 'w')

for linex in file1:
    for liney in file2:
        if linex == liney:
            FO.write("%s\n" %(linex))
            linex +=1
            liney +=1

FO.close()
file1.close()
file2.close()
ValleWest
  • 11
  • 4
  • Why compare text files when what you've got from the web is a perfectly good JSON which can be converted to a data structure? – Błotosmętek May 04 '20 at 10:52
  • we are awaiting your try to solve that problem, post a clear [mre] and problem description if your code goes wonky and you can't fix it yourself after debugging and researching the errors you get. Thanks. – Patrick Artner May 04 '20 at 10:53
  • @PatrickArtner I'm unsure how to start the approach, as I'm relatively new to coding. Thats why I was looking for help. Thanks! – ValleWest May 04 '20 at 11:25
  • @Błotosmętek What data structure do you mean? SQL? How could I start this approach? – ValleWest May 04 '20 at 11:25
  • No, I mean a Python data structure. A dictionary containing lists and dictionaries containing… etc. Try using `json.loads` on your JSON and printing the result. – Błotosmętek May 04 '20 at 11:39
  • Plenty of things to research for you then: [compare-two-different-files-line-by-line-in-python](https://stackoverflow.com/questions/19007383/compare-two-different-files-line-by-line-in-python) and [how-to-compare-two-json-objects-with-the-same-elements](https://stackoverflow.com/questions/25851183/how-to-compare-two-json-objects-with-the-same-elements-in-a-different-order-equa) etc. Use google search with `site:Stackoverflow.com` to find fitting article/topics/questions on SO. – Patrick Artner May 04 '20 at 11:59
  • Does this answer your question? [How to compare two JSON objects with the same elements in a different order equal?](https://stackoverflow.com/questions/25851183/how-to-compare-two-json-objects-with-the-same-elements-in-a-different-order-equa) – Błotosmętek May 04 '20 at 13:19
  • I just updated it. could you please have a look. I'm sadly not getting any text inside the some_output_file.txt , even though there should be two differences listed. @Błotosmętek – ValleWest May 04 '20 at 13:20
  • @PatrickArtner I just updated it. could you please have a look. I'm sadly not getting any text inside the some_output_file.txt , even though there should be two differences listed. – ValleWest May 04 '20 at 13:23

0 Answers0