# Path: data_prep/activity_net_download.py
# def meta_already_downloaded(destination_directory, metadata_file_name):
#     if os.path.isfile(os.path.join(destination_directory, metadata_file_name)):  # dump metadata dict already exists
#         with open(os.path.join(destination_directory, metadata_file_name), 'r') as f:
#             already_downloaded_dict = json.load(f)
#         return already_downloaded_dict
#     else:
#         return {}


import pafy, json, sys, os, os.path, moviepy, imageio
import pandas as pd
import numpy as np
import random
import requests
import glob
from pprint import pprint
from collections import defaultdict
from io import StringIO
from data_prep.activity_net_download import meta_already_downloaded
  video_id_category_tuples = get_video_id_category_tuples(categoeries, max_num_videos_per_cat=max_num_videos_per_cat)

  success_count, fail_count = 0, 0
  metadata_dict = meta_already_downloaded(destination_directory, metadata_file_name)

  video_id_category_tuples = remove_downloaded_videos_from_tuple_list(video_id_category_tuples, metadata_dict)

  video_count = len(video_id_category_tuples)
  for video_id, category in video_id_category_tuples:
    try:
      # attempt to download and store video
      file_name = str(video_id)+'.mp4'
      file_path, duration = download_video(video_id, destination_directory, file_name=file_name)
      # extend metadata dict
      metadata_dict[file_name] = {'path': file_path, 'label': category, 'duration': duration,
                                  'url': 'https://www.youtube.com/watch?v=' + video_id}

      # report success
      print(str(success_count + fail_count) + ' of ' + str(video_count) + ': ' + file_path)
      success_count += 1

    except Exception as e:
      fail_count += 1
      try:
        print(str(success_count + fail_count) + ' of ' + str(video_count) + ': Failed to download video:' + str(e))
      except:
        print(str(success_count + fail_count) + ' of ' + str(video_count) + ': Failed to download video:')

    # dump metadata dict as json file
    if (success_count % 20 == 0) or (success_count + fail_count == video_count):

# Path: data_prep/activity_net_download.py
# def meta_already_downloaded(destination_directory, metadata_file_name):
#     if os.path.isfile(os.path.join(destination_directory, metadata_file_name)):  # dump metadata dict already exists
#         with open(os.path.join(destination_directory, metadata_file_name), 'r') as f:
#             already_downloaded_dict = json.load(f)
#         return already_downloaded_dict
#     else:
#         return {}


# Path: data_prep/download_videos.py
import pafy, json, sys, os, os.path, moviepy, imageio
import pandas as pd
import numpy as np
import random
import requests
import glob
from pprint import pprint
from collections import defaultdict
from io import StringIO
from data_prep.activity_net_download import meta_already_downloaded

  video_id_category_tuples = get_video_id_category_tuples(categoeries, max_num_videos_per_cat=max_num_videos_per_cat)

  success_count, fail_count = 0, 0
  metadata_dict = meta_already_downloaded(destination_directory, metadata_file_name)

  video_id_category_tuples = remove_downloaded_videos_from_tuple_list(video_id_category_tuples, metadata_dict)

  video_count = len(video_id_category_tuples)
  for video_id, category in video_id_category_tuples:
    try:
      # attempt to download and store video
      file_name = str(video_id)+'.mp4'
      file_path, duration = download_video(video_id, destination_directory, file_name=file_name)
      # extend metadata dict
      metadata_dict[file_name] = {'path': file_path, 'label': category, 'duration': duration,
                                  'url': 'https://www.youtube.com/watch?v=' + video_id}

      # report success
      print(str(success_count + fail_count) + ' of ' + str(video_count) + ': ' + file_path)
      success_count += 1

    except Exception as e:
      fail_count += 1
      try:
        print(str(success_count + fail_count) + ' of ' + str(video_count) + ': Failed to download video:' + str(e))
      except:
        print(str(success_count + fail_count) + ' of ' + str(video_count) + ': Failed to download video:')

    # dump metadata dict as json file
    if (success_count % 20 == 0) or (success_count + fail_count == video_count):

      with open(os.path.join(destination_directory, metadata_file_name), 'w') as f: