def count_start_time_trending()

in python/fb_ads_library_api_operators.py [0:0]


def count_start_time_trending(generator_ad_archives, args, is_verbose=False):
    """
    output the count trending of ads by start date;
    Accept one parameters:
        output_file: path to write the csv
    """
    if len(args) != 1:
        raise Exception("start_time_trending action takes 1 arguments: output_file")

    total_count = 0
    output_file = args[0]
    date_to_count = Counter({})
    for ad_archives in generator_ad_archives:
        total_count += len(ad_archives)
        if is_verbose:
            print("Item processed: %d" % total_count)
        start_dates = list(
            map(
                lambda data: datetime.datetime.strptime(
                    data["ad_delivery_start_time"], "%Y-%m-%d"
                ).strftime("%Y-%m-%d"),
                ad_archives,
            )
        )
        date_to_count.update(start_dates)

    with open(output_file, "w") as csvfile:
        csvfile.write("date, count\n")
        for date in date_to_count.keys():
            csvfile.write("%s, %s\n" % (date, date_to_count[date]))

    print("Successfully wrote data to file: %s" % output_file)