# 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
  idlist = r.content.decode('utf-8').split("\"")[3]
  ids = [n for n in idlist.split(";") if len(n) > 3]
  return ids

def get_video_id_category_tuples(categoeries, max_num_videos_per_cat=3000):
  video_id_category_tuples = []
  for category in categoeries:
    ids = get_youtube_ids_of_category(category)
    if len(ids) > max_num_videos_per_cat:
      ids = random.sample(ids, max_num_videos_per_cat)
    video_id_category_tuples.extend([(id, category)for id in ids])
  return video_id_category_tuples

def remove_downloaded_videos_from_tuple_list(video_id_category_tuples, metadata_dict):
  if len(metadata_dict) == 0:  # no videos downloaded yet
    return video_id_category_tuples
  else:
    download_dict = dict(video_id_category_tuples)
    for video_name in metadata_dict.keys():
      if video_name.replace('.mp4','') in download_dict.keys():
        print(video_name.replace('.mp4',''))
        del download_dict[video_name.replace('.mp4','')]
    return download_dict.items()

def download_youtube_categories(categoeries, destination_directory, metadata_file_name="metadata.json",
                                max_num_videos_per_cat=3000):
  #prepare data to download
  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

# 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

  idlist = r.content.decode('utf-8').split("\"")[3]
  ids = [n for n in idlist.split(";") if len(n) > 3]
  return ids

def get_video_id_category_tuples(categoeries, max_num_videos_per_cat=3000):
  video_id_category_tuples = []
  for category in categoeries:
    ids = get_youtube_ids_of_category(category)
    if len(ids) > max_num_videos_per_cat:
      ids = random.sample(ids, max_num_videos_per_cat)
    video_id_category_tuples.extend([(id, category)for id in ids])
  return video_id_category_tuples

def remove_downloaded_videos_from_tuple_list(video_id_category_tuples, metadata_dict):
  if len(metadata_dict) == 0:  # no videos downloaded yet
    return video_id_category_tuples
  else:
    download_dict = dict(video_id_category_tuples)
    for video_name in metadata_dict.keys():
      if video_name.replace('.mp4','') in download_dict.keys():
        print(video_name.replace('.mp4',''))
        del download_dict[video_name.replace('.mp4','')]
    return download_dict.items()

def download_youtube_categories(categoeries, destination_directory, metadata_file_name="metadata.json",
                                max_num_videos_per_cat=3000):
  #prepare data to download
  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)