Source code for update_database

from typing import Optional

import database_sync
import sys
import datetime
from datetime import date, timedelta


[docs] class UpdateDatabase: """ Wrapper for :class:`.DatabaseSync` that runs all the find_new functions to put file data in beeDB. Need to be in the honeyDBee conda environment to run, and simply running `python3 update_database.py` will run through all the possible update paths. It can also be run in the form `python3 update_database.py <hive>` for a specific hive, `python3 update_database.py <start_date> <end_date>` for a specific time frame, or lastly `python3 update_database.py <hive> <start_date> <end_date>` for both a specific hive and time frame. """
[docs] def __init__(self): self.__database_sync = database_sync.DatabaseSync()
[docs] def update_hives(self): """ Wrapper function for :meth:`.DatabaseSync.find_new_hives`. """ self.__database_sync.find_new_hives()
[docs] def update_audio(self): """ Wrapper function for :meth:`.DatabaseSync.find_new_audio`. """ self.__database_sync.find_new_audio()
[docs] def update_video(self): """ Wrapper function for :meth:`.DatabaseSync.find_new_video`. """ self.__database_sync.find_new_videos()
[docs] def update_all(self): """ This method is used to update all the hives, audio files, and video files. Calls :meth:`.update_hives`, :meth:`.update_audio`, and :meth:`.update_video` to use their respective methods in :class:`.DatabaseSync` and create entries in the database for any missing hives, audio files, and video files. """ self.update_hives() print("Done updating hives.") self.update_audio() print("Done updating audio files.") self.update_video() print("Done updating video files.")
[docs] def update_date_range(self, start_date: Optional[str] = "default", end_date: Optional[str] = "default", hive_name: Optional[str] = "All"): """ Finds missing audio and video files from the database given a specific date range or hive, and creates and inserts entries for each. Creates a set of dates for the hive(s) requested and uses :meth:`.DatabaseSync.check_hive_videos` and :meth:`.DatabaseSync.check_hive_audio` to find all the missing videos and audio in the database and insert them. Args: start_date (Optional[str]): The date to begin searching for lost files (inclusive). end_date (Optional[str]): The date to end searching for lost files (also inclusive). "default" for both of these options causes the program to run on all dates. hive_name (Optional[str]): Name of a specific hive to run """ # Checking for default values for the dates. if start_date == "default" and end_date == "default": # First recorded date in the database currently. start_date = '2022-03-09' # Get current date and removing the time from the string end_date = datetime.datetime.now().isoformat()[:10] # Updating possible hives before checking for default hive value. if hive_name == "All": self.update_hives() hive_list = self.__database_sync.return_hives() else: # Just to make it easier, put the hive name into a list by itself if the hive is specified. hive_list = [hive_name] if start_date == end_date: # Checking if we can skip the date range creation. date_range = [start_date] else: # Need to grab the datetime objects from the strings to correctly calculate a date range. start = date.fromisoformat(start_date) end = date.fromisoformat(end_date) date_range = [ (end - timedelta(days=i)).isoformat() for i in range((end - start).days) ] # Add start date last to keep reverse order. date_range.append(start_date) if len(date_range) == 0: print("No dates between the two dates given. Are you sure your dates " "are in the correct iso 'YYYY-MM-DD' format?") print(f"Beginning database update for the dates:\n{date_range}\nfor hives:\n{hive_list}") for hive in hive_list: print(f"Now updating hive {hive}!") for one_date in date_range: # This runs through all dates in reverse order. print(f"Querying and updating videos for hive {hive} on date {one_date}.") self.__database_sync.check_hive_videos(hive, one_date) print(f"Querying and updating audio for hive {hive} on date {one_date}.") self.__database_sync.check_hive_audio(hive, one_date)
if __name__ == "__main__": update_database = UpdateDatabase() if len(sys.argv) == 1: print("Updating all hives on all dates. (This will take a while)") update_database.update_all() elif len(sys.argv) == 2: print(f"Updating database for hive {sys.argv[1]}.") update_database.update_date_range(hive_name=sys.argv[1]) elif len(sys.argv) == 3: print(f"Updating database for all hives between the dates {sys.argv[1]} and {sys.argv[2]}") update_database.update_date_range(start_date=sys.argv[1], end_date=sys.argv[2]) elif len(sys.argv) == 4: print(f"Updating database for hive {sys.argv[1]} between the dates {sys.argv[2]} and {sys.argv[3]}") update_database.update_date_range(start_date=sys.argv[2], end_date=sys.argv[3], hive_name=sys.argv[1]) else: print("USAGE: \npython3 update_database.py <start_date> <end_date>\n" "python3 update_database.py <hive_name>\n" "python3 update_database.py <hive_name> <start_date> <end_date>\n" "Dates are of the format 'YYYY-MM-DD'.\n" "Providing no arguments runs the program on all dates.")