[Python] Using APScheduler to Track Changes

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    I have a script that utilizes the APScheduler module to run certain scripts at set intervals. These scripts are used for all kinds of purposes that make my life easier I also use configparser module to save the information to a file and this is how I see if the data has changed, and if it has, it will send me a message using Telegram's API. Here is a basic rundown of how the script works

    import configparser
    config = configparser.ConfigParser()

    from apscheduler.schedulers.background import BackgroundScheduler
    sched = BackgroundScheduler()


    def compareChanges(site, new_value, url, subname=False, silent=False):
    config.read('values.ini') # Load in current values
    if subname:
    field = subname
    else:
    field = site
    try:
    old_value = config[site][field].replace('%','%%') # Configparser needs %% to be read as %
    except KeyError:
    # Create section
    old_value = 'NOT SET'
    try:
    config.add_section(site)
    except configparser.DuplicateSectionError:
    pass
    config.set(site, field, old_value)

    new_value = str(new_value).strip().replace('%','%%')
    new_value_msg = new_value.replace('%%','%')
    if old_value != new_value:
    # Log and notify of change
    logger.info(f"[{site}][{field}] has new value of {new_value}")
    if not silent:
    if subname:
    pushMsg(f"{site} | {field} => {new_value_msg}", url)
    else:
    pushMsg(f"{field} => {new_value_msg}", url)
    # Set change and save file
    config.set(site, field, new_value)
    with open('values.ini', 'w') as configfile:
    config.write(configfile)
    return True # Return True if changed
    else:
    return False # Return false on no change

    def Function1():
    import requests
    response = requests.get("https://example.com/api/endpoint").json()
    compareChanges("Website Status Check", response['status'], "https://example.com")

    def Bestbuy_Latest_Price():
    import requests
    response = requests.get("https://bestbuy.com/product/page")
    # This is pseudocode but it loads products on page and compares price for each product find
    for item in response:
    compareChanges("Bestbuy_Latest_Price", {product: price}, "https://bestbuy.com/product", subname = sku)


    sched.add_job(Function1, 'cron', hour='*')
    sched.add_job(Bestbuy_Latest_Price, 'cron', hour='*')
    sched.start()


    My values.ini file looks something like

    [Function1]
    function1 = 200

    [Bestbuy_Latest_Price]
    12345 = {'GeForce RTX 3070': 700}
    54321 = {'Some_Random_Product': 350}
    00000 = {'Another Product': 420}


    The way this script works is that every hour, Function1() will be called, pull some data, compare it to the stored value in the ConfigParser file, and then notify if this value was changed. However I think using ConfigParser for this has been causing issues with different data types and escaping strings (for example, % needs to be replaced with %%) and also I don't think it's the right application.

    My question is: what is a better way to keep track of if data has changed. I currently have about 20 different functions and some functions have multiple subitems. The scripts vary in run frequency where some are run every minute and others are run every month.

    Continue reading...

Compartilhe esta Página