def update_entry(title,
                 isCorrupt,
                 to_delete_nom,
                 img_hash,
                 page_id=None,
                 was_fixed=None):
    """
    Updates existing entry in database. This is currently called in image_followup when an image has been changed.
    :param title: filename
    :param isCorrupt: (bool)
    :param to_delete_nom: date string for when to nominate for deletion (NULL if not corrupt)
    :param img_hash: hash of the image to compare with the stored database value
    :param page_id: page id (optional)
    :param was_fixed: whether image was fixed
    :return: None
    """
    if page_id is None:
        page_id = manapi.getPageID(title)
    global logger
    cnx = mariadb.connect(**config.config)
    cursor = cnx.cursor()
    try:
        cursor.execute(
            update_entry,
            (title, isCorrupt, to_delete_nom, img_hash, was_fixed, page_id))
        cnx.commit()
    except mariadb.Error as error:
        logger.error("Error: {}".format(error))
    finally:
        cnx.close()
    logger.info("Record updated")
    logger.debug("Record updated -- " + str(page_id))
def have_seen_image(site, title, page_id=None):
    """
    Checks if we have previously viewed this image with its current hash value. This is done through connecting to the
    database for this application.
    :param site: site object
    :param title: filename to check
    :param page_id: page id to check (optional)
    :return: True if seen, False if not
    """
    if page_id is None:
        page_id = manapi.getPageID(title)
    res = False
    global logger
    cnx = mariadb.connect(**config.config)
    cursor = cnx.cursor()
    img_hash = get_remote_hash(site, title)
    sql = "SELECT title FROM images_viewed WHERE page_id=%s AND hash=%s"
    try:
        cursor.execute(sql, (page_id, img_hash))
        msg = cursor.fetchone()
        if msg is None:
            res = False
        else:
            res = True
    except mariadb.Error as error:
        logger.error("Error: {}".format(error))
    finally:
        cnx.close()
    return res
def store_image(title,
                isCorrupt,
                img_hash,
                day_count=30,
                page_id=None,
                not_image=False):
    """
    Stores current image information (provided in header) into the database for this application.
    :param title: filename
    :param isCorrupt: (bool)
    :param img_hash: sha1 hash of image
    :param page_id: page id (optional)
    :param day_count: how long a grace period pre-nomination if unresolved by then (default: 30 days, not stored
    if image isn't corrupt in the first place (database defaults to NULL if no value provided)
    :param not_image: False if file is an image, true otherwise
    :return: None
    """
    global logger
    if page_id is None and not not_image:
        try:
            page_id = manapi.getPageID(title)
        except KeyError:
            page_id = -1
            logger.error("KeyError - cannot get page ID from API")
    if page_id is None:
        page_id = -1
    cnx = mariadb.connect(**config.config)
    cursor = cnx.cursor()
    if not_image:
        image_data = {
            'title': title,
            'isCorrupt': False,
            'date_scanned':
            datetime.now(timezone.utc).date().strftime('%m/%d/%Y'),
            'to_delete_nom': None,
            'hash': str(img_hash),
            'page_id': int(page_id),
            'not_image': not_image
        }
    elif isCorrupt:
        image_data = {
            'title': title,
            'isCorrupt': isCorrupt,
            'date_scanned':
            datetime.now(timezone.utc).date().strftime('%m/%d/%Y'),
            'to_delete_nom': get_next_month(day_count),
            'hash': str(img_hash),
            'page_id': int(page_id),
            'not_image': not_image
        }
    else:
        image_data = {
            'title': title,
            'isCorrupt': isCorrupt,
            'date_scanned':
            datetime.now(timezone.utc).date().strftime('%m/%d/%Y'),
            'to_delete_nom': None,
            'hash': str(img_hash),
            'page_id': int(page_id),
            'not_image': not_image
        }
    try:
        cursor.execute(insert_image, image_data)
        cnx.commit()
        logger.debug(cursor.rowcount, "record inserted.")
    except mariadb.Error as error:
        logger.error("Error: {}".format(error))
    finally:
        cnx.close()