Home > Software design >  Update python dictionary from JSON file every hour
Update python dictionary from JSON file every hour

Time:12-21

I've been working on a Telegram bot, to serve the prices of gasoline and diesel of different petrol stations. I download a JSON file hourly with updated prices, and put them into a dictionary like this:

import json

with open('output1.json', encoding='utf-8') as f:
    data = json.loads(f.read())
f.close()

def rotul_2668():
    return (data['ListaEESSPrecio'][37]['Rótulo'])
def direccio_2668():
    return (data['ListaEESSPrecio'][37]['Dirección'])
def benzina_2668():
    return (data['ListaEESSPrecio'][37]['Precio Gasolina 95 E5'])
def dieselA_2668():
    return (data['ListaEESSPrecio'][37]['Precio Gasoleo A'])
def dieselB_2668():
    return (data['ListaEESSPrecio'][37]['Precio Gasoleo B'])
def dieselPremium_2668():
    return (data['ListaEESSPrecio'][37]['Precio Gasoleo Premium'])
def GLP_2668():
    return (data['ListaEESSPrecio'][37]['Precio Gases licuados del petróleo'])

ciutadella_2668 = {
    'rotul' : rotul_2668(),
    'direccio' : direccio_2668(),
    'benzina' : benzina_2668(),
    'dieselA' : dieselA_2668(),
    'dieselB' : dieselB_2668(),
    'dieselPremium' : dieselPremium_2668(),
    'GLP': GLP_2668()
}

Then call the dictionary entries from an external function. Everything runs good so far until dictionary needs to be updated and serve the new prices, which never does. Any idea why is that happening? Could be because the data stored in var "data" never gets reloaded? If that's so, what would be a possible solution?

Thank you for your time

CodePudding user response:

It's not reloaded because you put your code at file level. So the first time you access ciutadella_2668 all file is imported, output1.json is read and dictionary is populated. Quick fix (not optimal thought) would be put it into the function

import json

def get_ciutadella_2668():
  with open('output1.json', encoding='utf-8') as f:
    data = json.loads(f.read())

  def rotul_2668():
    return (data['ListaEESSPrecio'][37]['Rótulo'])
  def direccio_2668():
    return (data['ListaEESSPrecio'][37]['Dirección'])
  def benzina_2668():
    return (data['ListaEESSPrecio'][37]['Precio Gasolina 95 E5'])
  def dieselA_2668():
    return (data['ListaEESSPrecio'][37]['Precio Gasoleo A'])
  def dieselB_2668():
    return (data['ListaEESSPrecio'][37]['Precio Gasoleo B'])
  def dieselPremium_2668():
    return (data['ListaEESSPrecio'][37]['Precio Gasoleo Premium'])
  def GLP_2668():
    return (data['ListaEESSPrecio'][37]['Precio Gases licuados del petróleo'])

  return {
    'rotul' : rotul_2668(),
    'direccio' : direccio_2668(),
    'benzina' : benzina_2668(),
    'dieselA' : dieselA_2668(),
    'dieselB' : dieselB_2668(),
    'dieselPremium' : dieselPremium_2668(),
    'GLP': GLP_2668()
  }

Another way could be use some python scheduler that runs every hour and update this specific variable not while reading it but when new file is fetched.

  • Related