bash

argparse is a module in Python used for handling command-line arguments. It allows your program to accept input from the command line, making it more flexible and interactive.

Old code:

# file name: controller.py
import datetime
import pytz
import os
from facebookreporter.fb_data_extractor import FacebookDataExtractor

if __name__ == "__main__":
    access_token = os.getenv("ACCESS_TOKEN")
    page_id = os.getenv("PAGE_ID")
    extractor = FacebookDataExtractor(access_token, page_id)

    # Define the time zone for Germany
    germany_tz = pytz.timezone("Europe/Berlin")

    start_date = datetime.datetime(2024, 5, 7, 15, 27, 0)  # yyyy, m, d , h, m, s
    end_date = datetime.datetime(2024, 5, 7, 15, 28, 0)

    # Localize this time to the German time zone
    start_date_germany = germany_tz.localize(start_date)
    end_date_germany = germany_tz.localize(end_date)

    extractor.extract_data(start_date_germany, end_date_germany, export_format="excel")

New code (with argparse):

import argparse
import datetime
import pytz
import os
from facebookreporter.fb_data_extractor import FacebookDataExtractor

def main(date_since, date_until, output):
    # Retrieve the Facebook API access token and page ID from environment variables
    access_token = os.getenv("ACCESS_TOKEN")
    page_id = os.getenv("PAGE_ID")

    # Initialize the Facebook data extractor with the access token and page ID
    extractor = FacebookDataExtractor(access_token, page_id)

    # Define the time zone for Germany
    germany_tz = pytz.timezone("Europe/Berlin")

    # Parse the start and end dates from the provided strings
    start_date = datetime.datetime.strptime(date_since, "%Y-%m-%d")
    end_date = datetime.datetime.strptime(date_until, "%Y-%m-%d")

     # Localize the start and end dates to the German time zone
    start_date_germany = germany_tz.localize(start_date)
    end_date_germany = germany_tz.localize(end_date)

    # Set the output filename for the extracted data
    FacebookDataExtractor.EXCEL_FILENAME=output

    # Extract data between the specified dates and save it in Excel format
    extractor.extract_data(start_date_germany, end_date_germany, export_format="excel")

if __name__ == "__main__":
    # Set up argument parsing
    parser = argparse.ArgumentParser(description='Extract Facebook data.')
    parser.add_argument('--date_since', type=str, required=True, help='Start date in the format "YYYY-MM-DD HH:MM:SS"')
    parser.add_argument('--date_until', type=str, required=True, help='End date in the format "YYYY-MM-DD HH:MM:SS"')
    parser.add_argument('-o', '--output', type=str, required=True, help='Output format for extract_data')

    # Parse the command line arguments
    args = parser.parse_args()
    
    # Call the main function with the parsed arguments
    main(args.date_since, args.date_until, args.output)

You can start the script in the terminal with:

python [controller.py](<http://controller.py/>) --date_since "2024-05-07" --date_until "2024-05-08" --output "excel"

This way we can run the scrip in a job like this!

report_to_dap.sh (this is a bash file):

# Run the Python script to generate the report
python ${script_path} --date_since ${date_since} --date_until ${date_until} -o ${report_filename}