[
  {
    "path": ".gitignore",
    "content": "/youtube-key.txt\n"
  },
  {
    "path": "Contents/Code/__init__.py",
    "content": "# -*- coding: utf-8 -*-\n\n### Imports ###\nimport sys                  # getdefaultencoding, getfilesystemencoding, platform, argv\nimport os                   # path.abspath, join, dirname\nimport re                   #\nimport inspect              # getfile, currentframe\nimport urllib2              #\nfrom   lxml    import etree #\nfrom   io      import open  # open\nimport hashlib\nimport unicodedata\n\n###Mini Functions ###\ndef sanitize_xml_string(s):\n  \"\"\"Remove characters not allowed in XML attributes and normalize to NFC.\"\"\"\n  if s is None:\n    return u''\n  # Convert to str to prevent iteration issues in Plex sandbox\n  if not isinstance(s, basestring):\n    s = str(s)\n  try:\n    s = s.decode('utf-8') if isinstance(s, str) else s\n  except:\n    pass\n  out = u''\n  for i in range(len(s)):\n    c = s[i]\n    if c >= u' ' and c not in u'\\uFFFE\\uFFFF' and ord(c) not in range(0x00, 0x20) and c not in u'\\u2028\\u2029':\n      out += c\n  out = unicodedata.normalize('NFC', out)\n  return out\ndef natural_sort_key     (s):  return [int(text) if text.isdigit() else text for text in re.split(re.compile('([0-9]+)'), str(s).lower())]  ### Avoid 1, 10, 2, 20... #Usage: list.sort(key=natural_sort_key), sorted(list, key=natural_sort_key)\ndef sanitize_path        (p):  return p if isinstance(p, unicode) else p.decode(sys.getfilesystemencoding()) ### Make sure the path is unicode, if it is not, decode using OS filesystem's encoding ###\ndef js_int               (i):  return int(''.join([x for x in list(i or '0') if x.isdigit()]))  # js-like parseInt - https://gist.github.com/douglasmiranda/2174255\n\n### Return dict value if all fields exists \"\" otherwise (to allow .isdigit()), avoid key errors\ndef Dict(var, *arg, **kwarg):  #Avoid TypeError: argument of type 'NoneType' is not iterable\n  \"\"\" Return the value of an (imbricated) dictionnary, return \"\" if doesn't exist unless \"default=new_value\" specified as end argument\n      Ex: Dict(variable_dict, 'field1', 'field2', default = 0)\n  \"\"\"\n  for key in arg:\n    if isinstance(var, dict) and key and key in var or isinstance(var, list) and isinstance(key, int) and 0<=key<len(var):  var = var[key]\n    else:  return kwarg['default'] if kwarg and 'default' in kwarg else \"\"   # Allow Dict(var, tvdbid).isdigit() for example\n  return kwarg['default'] if var in (None, '', 'N/A', 'null') and kwarg and 'default' in kwarg else \"\" if var in (None, '', 'N/A', 'null') else var\n\n### Used to Convert Crowd Sourced Video Titles to Title Case from Sentence Case\ndef uppercase_regex(a):\n    return a.group(1) + a.group(2).upper()\n\ndef titlecase(input_string):\n    return re.sub(\"(^|\\s)(\\S)\", uppercase_regex, input_string)\n\n### These calls use DeArrow Created By Ajay Ramachandran to Obtain a Crowd Sourced Video Title\ndef DeArrow(video_id):\n  api_url = 'https://sponsor.ajay.app'\n\n  hash = hashlib.sha256(video_id.encode('ascii')).hexdigest()\n  \n  # DeArrow API recommends using first 4 hash characters.\n  url = '{api_url}/api/branding/{hash}'.format(api_url = api_url, hash = hash[:4])\n\n  #HTTP.ClearCache()\n  HTTP.CacheTime = 0\n\n  crowd_sourced_title = ''\n  \n  try:\n    data_json = JSON.ObjectFromURL(url)\n  except:\n    Log.Error(u'DeArrow(): Error while loading JSON.ObjectFromURL. URL: '+ url)\n\n  try:\n    first_title_obj = data_json[video_id]['titles'][0]\n    if (first_title_obj['votes'] >= 0 and first_title_obj['locked'] == False and first_title_obj['original'] == False):\n      crowd_sourced_title = titlecase(first_title_obj['title'])\n  except:\n    Log.Info(u'DeArrow(): No Crowd Sourced Title Found for Video ID: ' + video_id)\n\n  HTTP.CacheTime = CACHE_1MONTH\n  \n  return crowd_sourced_title\n\n### Convert ISO8601 Duration format into seconds ###\ndef ISO8601DurationToSeconds(duration):\n  try:     match = re.match('PT(\\d+H)?(\\d+M)?(\\d+S)?', duration).groups()\n  except:  return 0\n  else:    return 3600 * js_int(match[0]) + 60 * js_int(match[1]) + js_int(match[2])\n\n### Get media directory ###\ndef GetMediaDir (media, movie, file=False):\n  if movie:  return os.path.dirname(media.items[0].parts[0].file)\n  else:\n    for s in media.seasons if media else []: # TV_Show:\n      for e in media.seasons[s].episodes:\n        Log.Info(media.seasons[s].episodes[e].items[0].parts[0].file)\n        return media.seasons[s].episodes[e].items[0].parts[0].file if file else os.path.dirname(media.seasons[s].episodes[e].items[0].parts[0].file)\n\n### Get media root folder ###\ndef GetLibraryRootPath(dir):\n  library, root, path = '', '', ''\n  for root in [os.sep.join(dir.split(os.sep)[0:x+2]) for x in range(0, dir.count(os.sep))]:\n    if root in PLEX_LIBRARY:\n      library = PLEX_LIBRARY[root]\n      path    = os.path.relpath(dir, root)\n      break\n  else:  #401 no right to list libraries (windows)\n    Log.Info(u'[!] Library access denied')\n    filename = os.path.join(CachePath, '_Logs', '_root_.scanner.log')\n    if os.path.isfile(filename):\n      Log.Info(u'[!] ASS root scanner file present: \"{}\"'.format(filename))\n      line = Core.storage.load(filename)  #with open(filename, 'rb') as file:  line=file.read()\n      for root in [os.sep.join(dir.split(os.sep)[0:x+2]) for x in range(dir.count(os.sep)-1, -1, -1)]:\n        if \"root: '{}'\".format(root) in line:  path = os.path.relpath(dir, root).rstrip('.');  break  #Log.Info(u'[!] root not found: \"{}\"'.format(root))\n      else: path, root = '_unknown_folder', ''\n    else:  Log.Info(u'[!] ASS root scanner file missing: \"{}\"'.format(filename))\n  return library, root, path\n\n\ndef youtube_api_key():\n  path = os.path.join(PluginDir, \"youtube-key.txt\")\n  if os.path.isfile(path):\n    value = Data.Load(path)\n    if value:\n      value = value.strip()\n    if value:\n      Log.Debug(u\"Loaded token from youtube-token.txt file\")\n\n      return value\n\n  # Fall back to Library preference\n  return Prefs['YouTube-Agent_youtube_api_key']\n\n\n###\ndef json_load(template, *args):\n  url = template.format(*args + tuple([youtube_api_key()]))\n  url = sanitize_path(url)\n  iteration = 0\n  json_page = {}\n  json      = {}\n  while not json or Dict(json_page, 'nextPageToken') and Dict(json_page, 'pageInfo', 'resultsPerPage') !=1 and iteration<50:\n    #Log.Info(u'{}'.format(Dict(json_page, 'pageInfo', 'resultsPerPage')))\n    try:                    json_page = JSON.ObjectFromURL(url+'&pageToken='+Dict(json_page, 'nextPageToken') if Dict(json_page, 'nextPageToken') else url)  #Log.Info(u'items: {}'.format(len(Dict(json_page, 'items'))))\n    except Exception as e:  json = JSON.ObjectFromString(e.content);  raise ValueError('code: {}, message: {}'.format(Dict(json, 'error', 'code'), Dict(json, 'error', 'message')))\n    if json:  json ['items'].extend(json_page['items'])\n    else:     json = json_page\n    iteration +=1\n  #Log.Info(u'total items: {}'.format(len(Dict(json, 'items'))))\n  return json\n\n### load image if present in local dir\ndef img_load(series_root_folder, filename):\n  Log(u'img_load() - series_root_folder: {}, filename: {}'.format(series_root_folder, filename))\n  for ext in ['jpg', 'jpeg', 'png', 'tiff', 'gif', 'jp2']:\n    filename = os.path.join(series_root_folder, filename.rsplit('.', 1)[0]+\".\"+ext)\n    if os.path.isfile(filename):  Log(u'local thumbnail found for file %s', filename);  return filename, Core.storage.load(filename)\n  return \"\", None\n\n### get biggest thumbnail available\ndef get_thumb(json_video_details):\n  thumbnails = Dict(json_video_details, 'thumbnails')\n  for thumbnail in reversed(thumbnails):\n    return thumbnail['url']\n\n  Log.Error(u'get_thumb(): No thumb found')\n  return None\n\ndef Start():\n  HTTP.CacheTime                  = CACHE_1MONTH\n  HTTP.Headers['User-Agent'     ] = 'Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.54'\n  HTTP.Headers['Accept-Language'] = 'en-us'\n\n### Assign unique ID ###\ndef Search(results, media, lang, manual, movie):\n  \n  displayname = sanitize_path(os.path.basename((media.name if movie else media.show) or \"\") )\n  filename    = media.items[0].parts[0].file if movie else media.filename or media.show\n  dir         = GetMediaDir(media, movie)\n  try:                    filename = sanitize_path(filename)\n  except Exception as e:  Log('search() - Exception1: filename: \"{}\", e: \"{}\"'.format(filename, e))\n  try:                    filename = os.path.basename(filename)\n  except Exception as e:  Log('search() - Exception2: filename: \"{}\", e: \"{}\"'.format(filename, e))\n  try:                    filename = urllib2.unquote(filename)\n  except Exception as e:  Log('search() - Exception3: filename: \"{}\", e: \"{}\"'.format(filename, e))\n  Log(u''.ljust(157, '='))\n  Log(u\"Search() - dir: {}, filename: {}, displayname: {}\".format(dir, filename, displayname))\n    \n  try:\n    for regex, url in [('PLAYLIST', YOUTUBE_PLAYLIST_REGEX), ('CHANNEL', YOUTUBE_CHANNEL_REGEX), ('VIDEO', YOUTUBE_VIDEO_REGEX)]:\n      result = url.search(filename)\n      if result:\n        guid = result.group('id')\n        Log.Info(u'search() - YouTube ID found - regex: {}, youtube ID: \"{}\"'.format(regex, guid))\n        safe_id = sanitize_xml_string('youtube|{}|{}'.format(guid,os.path.basename(dir)))\n        results.Append( MetadataSearchResult( id=safe_id, name=displayname, year=None, score=100, lang=lang ) )\n        Log(u''.ljust(157, '='))\n        return\n      else: Log.Info('search() - YouTube ID not found - regex: \"{}\"'.format(regex))  \n  except Exception as e:  Log('search() - filename: \"{}\" Regex failed to find YouTube id, error: \"{}\"'.format(filename, e))\n  \n  if movie:  Log.Info(filename)\n  else:    \n    s = media.seasons.keys()[0] if media.seasons.keys()[0]!='0' else media.seasons.keys()[1] if len(media.seasons.keys()) >1 else None\n    if s:\n      result = YOUTUBE_PLAYLIST_REGEX.search(os.path.basename(os.path.dirname(dir)))\n      guid   = result.group('id') if result else ''\n      if result or os.path.exists(os.path.join(dir, 'youtube.id')):\n        Log(u'search() - filename: \"{}\", found season YouTube playlist id, result.group(\"id\"): {}'.format(filename, result.group('id')))\n        safe_id = sanitize_xml_string('youtube|{}|{}'.format(guid,dir))\n        results.Append( MetadataSearchResult( id=safe_id, name=filename, year=None, score=100, lang=lang ) )\n        Log(u''.ljust(157, '='))\n        return\n      else:  Log('search() - id not found')\n  \n  ### Try loading local JSON file if present\n  json_filename = os.path.join(dir, os.path.splitext(filename)[0]+ \".info.json\")\n  Log(u'Searching for info file: {}'.format(json_filename))\n  if os.path.exists(json_filename):\n    try:     json_video_details = JSON.ObjectFromString(Core.storage.load(json_filename))  #with open(json_filename) as f:  json_video_details = JSON.ObjectFromString(f.read())\n    except Exception as e:\n      Log('search() - Unable to load info.json, e: \"{}\"'.format(e))\n    else:\n      video_id = Dict(json_video_details, 'id')\n      Log('search() - Loaded json_video_details: {}'.format(video_id))\n      safe_id = sanitize_xml_string('youtube|{}|{}'.format(video_id, os.path.basename(dir)))\n      results.Append( MetadataSearchResult( id=safe_id, name=displayname, year=Datetime.ParseDate(Dict(json_video_details, 'upload_date')).year, score=100, lang=lang ) )\n      Log(u''.ljust(157, '='))\n      return\n  \n  try:\n    json_video_details = json_load(YOUTUBE_VIDEO_SEARCH, String.Quote(filename, usePlus=False))\n    if Dict(json_video_details, 'pageInfo', 'totalResults'):\n      Log.Info(u'filename: \"{}\", title:        \"{}\"'.format(filename, Dict(json_video_details, 'items', 0, 'snippet', 'title')))\n      Log.Info(u'filename: \"{}\", channelTitle: \"{}\"'.format(filename, Dict(json_video_details, 'items', 0, 'snippet', 'channelTitle')))\n      if filename == Dict(json_video_details, 'items', 0, 'snippet', 'channelTitle'):\n        Log.Info(u'filename: \"{}\", found exact matching YouTube title: \"{}\", description: \"{}\"'.format(filename, Dict(json_video_details, 'items', 0, 'snippet', 'channelTitle'), Dict(json_video_details, 'items', 0, 'snippet', 'description')))\n        safe_id = sanitize_xml_string('youtube|{}|{}'.format(Dict(json_video_details, 'items', 0, 'id', 'channelId'),dir))\n        results.Append( MetadataSearchResult( id=safe_id, name=filename, year=None, score=100, lang=lang ) )\n        Log(u''.ljust(157, '='))\n        return\n      else:  Log.Info(u'search() - no id in title nor matching YouTube title: \"{}\", closest match: \"{}\", description: \"{}\"'.format(filename, Dict(json_video_details, 'items', 0, 'snippet', 'channelTitle'), Dict(json_video_details, 'items', 0, 'snippet', 'description')))\n    elif 'error' in json_video_details:  Log.Info(u'search() - code: \"{}\", message: \"{}\"'.format(Dict(json_video_details, 'error', 'code'), Dict(json_video_details, 'error', 'message')))\n  except Exception as e:  Log(u'search() - Could not retrieve data from YouTube for: \"{}\", Exception: \"{}\"'.format(filename, e))\n\n  library, root, path = GetLibraryRootPath(dir)\n  Log(u'Putting folder name \"{}\" as guid since no assign channel id or playlist id was assigned'.format(path.split(os.sep)[-1]))\n  safe_id = sanitize_xml_string('youtube|{}|{}'.format(path.split(os.sep)[-2] if os.sep in path else '', dir))\n  results.Append( MetadataSearchResult( id=safe_id, name=os.path.basename(filename), year=None, score=80, lang=lang ) )\n  Log(''.ljust(157, '='))\n\n### Download metadata using unique ID ###\ndef Update(metadata, media, lang, force, movie):\n  # Sanitize metadata.id to prevent XML errors\n  if hasattr(metadata, 'id'):\n    metadata.id = sanitize_xml_string(metadata.id)\n  Log(u'=== update(lang={}, force={}, movie={}) ==='.format(lang, force, movie))\n  temp1, guid, series_folder = metadata.id.split(\"|\")\n  dir                        = sanitize_path(GetMediaDir(media, movie))\n  channel_id                 = guid if guid.startswith('UC') or guid.startswith('HC') else ''\n  channel_title              = \"\"\n  json_playlist_details      = {}\n  json_playlist_items        = {}\n  json_channel_items         = {}\n  json_channel_details       = {}\n  json_video_details         = {}\n  series_folder              = sanitize_path(series_folder)\n  if not (len(guid)>2 and guid[0:2] in ('PL', 'UU', 'FL', 'LP', 'RD')):  metadata.title = re.sub(r'\\[.*\\]', '', dir).strip()  #no id mode, update title so ep gets updated\n  Log(u''.ljust(157, '='))\n    \n  ### Movie Library ###\n  if movie:\n\n    ### Movie - JSON call ###############################################################################################################\n    filename = media.items[0].parts[0].file if movie else media.filename or media.show\n    dir = GetMediaDir(media, movie)\n    try:                    filename = sanitize_path(filename)\n    except Exception as e:  Log('update() - Exception1: filename: \"{}\", e: \"{}\"'.format(filename, e))\n    try:                    filename = os.path.basename(filename)\n    except Exception as e:  Log('update() - Exception2: filename: \"{}\", e: \"{}\"'.format(filename, e))\n    try:                    filename = urllib2.unquote(filename)\n    except Exception as e:  Log('update() - Exception3: filename: \"{}\", e: \"{}\"'.format(filename, e))\n\n    json_filename = os.path.join(dir, os.path.splitext(filename)[0]+ \".info.json\")\n    Log(u'Update: Searching for info file: {}, dir:{}'.format(json_filename, GetMediaDir(media, movie, True)))\n    if os.path.exists(json_filename):\n      try:             json_video_details = JSON.ObjectFromString(Core.storage.load(json_filename))\n      except IOError:  guid = None\n      else:    \n        guid          = Dict(json_video_details, 'id')\n        channel_id    = Dict(json_video_details, 'channel_id')\n\n        ### Movie - Local JSON\n        Log.Info(u'update() using json file json_video_details - Loaded video details from: \"{}\"'.format(json_filename))\n        metadata.title                   = Dict(json_video_details, 'title');                                  Log(u'series title:       \"{}\"'.format(Dict(json_video_details, 'title')))\n        metadata.summary                 = Dict(json_video_details, 'description');                            Log(u'series description: '+Dict(json_video_details, 'description').replace('\\n', '. '))\n\n        if Prefs['use_crowd_sourced_titles'] == True:\n          crowd_sourced_title = DeArrow(guid)\n          if crowd_sourced_title != '':\n            metadata.original_title = metadata.title\n            metadata.summary = 'Original Title: ' + metadata.title + '\\r\\n\\r\\n' + metadata.summary\n            metadata.title = crowd_sourced_title\n\n        metadata.duration                = Dict(json_video_details, 'duration');                               Log(u'series duration:    \"{}\"->\"{}\"'.format(Dict(json_video_details, 'duration'), metadata.duration))\n        metadata.genres                  = Dict(json_video_details, 'categories');                             Log(u'genres: '+str([x for x in metadata.genres]))\n        date                             = Datetime.ParseDate(Dict(json_video_details, 'upload_date'));        Log(u'date:  \"{}\"'.format(date))\n        metadata.originally_available_at = date.date()\n        metadata.year                    = date.year  #test avoid:  AttributeError: 'TV_Show' object has no attribute named 'year'\n        thumb                            = get_thumb(json_video_details)\n        if thumb and thumb not in metadata.posters:\n          Log(u'poster: \"{}\" added'.format(thumb))\n          metadata.posters[thumb]        = Proxy.Media(HTTP.Request(thumb).content, sort_order=1)\n        else:  Log(u'thumb: \"{}\" already present'.format(thumb))\n        if Dict(json_video_details, 'statistics', 'likeCount') and int(Dict(json_video_details, 'like_count')) > 0 and Dict(json_video_details, 'dislike_count') and int(Dict(json_video_details, 'dislike_count')) > 0:\n          metadata.rating                = float(10*int(Dict(json_video_details, 'like_count'))/(int(Dict(json_video_details, 'dislike_count'))+int(Dict(json_video_details, 'like_count'))));  Log(u'rating: {}'.format(metadata.rating))\n        if Prefs['add_user_as_director']:\n          metadata.directors.clear()\n          try:\n            director            = Dict(json_video_details, 'uploader');\n            meta_director       = metadata.directors.new()\n            meta_director.name  = director\n            Log('director: '+ director)\n          except:  pass\n        return\n\n    ### Movie - API call ################################################################################################################\n    Log(u'update() using api - guid: {}, dir: {}, metadata.id: {}'.format(guid, dir, metadata.id))\n    try:     json_video_details = json_load(YOUTUBE_json_video_details, guid)['items'][0]\n    except:  Log(u'json_video_details - Could not retrieve data from YouTube for: ' + guid)\n    else:\n      Log('Movie mode - json_video_details - Loaded video details from: \"{}\"'.format(YOUTUBE_json_video_details.format(guid, 'personal_key')))\n      date                             = Datetime.ParseDate(json_video_details['snippet']['publishedAt']);  Log('date:  \"{}\"'.format(date))\n      metadata.originally_available_at = date.date()\n      metadata.title                   = json_video_details['snippet']['title'];                                                      Log(u'series title:       \"{}\"'.format(json_video_details['snippet']['title']))\n      metadata.summary                 = json_video_details['snippet']['description'];                                                Log(u'series description: '+json_video_details['snippet']['description'].replace('\\n', '. '))\n\n      if Prefs['use_crowd_sourced_titles'] == True:\n        crowd_sourced_title = DeArrow(guid)\n        if crowd_sourced_title != '':\n          metadata.original_title = metadata.title\n          metadata.summary = 'Original Title: ' + metadata.title + '\\r\\n\\r\\n' + metadata.summary\n          metadata.title = crowd_sourced_title\n\n      metadata.duration                = ISO8601DurationToSeconds(json_video_details['contentDetails']['duration'])*1000;             Log(u'series duration:    \"{}\"->\"{}\"'.format(json_video_details['contentDetails']['duration'], metadata.duration))\n      metadata.genres                  = [YOUTUBE_CATEGORY_ID[id] for id in json_video_details['snippet']['categoryId'].split(',')];  Log(u'genres: '+str([x for x in metadata.genres]))\n      metadata.year                    = date.year;                                                                              Log(u'movie year: {}'.format(date.year))\n      thumb                            = json_video_details['snippet']['thumbnails']['default']['url'];                               Log(u'thumb: \"{}\"'.format(thumb))\n      if thumb and thumb not in metadata.posters:\n        Log(u'poster: \"{}\"'.format(thumb))\n        metadata.posters[thumb]        = Proxy.Media(HTTP.Request(Dict(json_video_details, 'snippet', 'thumbnails', 'maxres', 'url') or Dict(json_video_details, 'snippet', 'thumbnails', 'medium', 'url') or Dict(json_video_details, 'snippet', 'thumbnails', 'standard', 'url') or Dict(json_video_details, 'snippet', 'thumbnails', 'high', 'url') or Dict(json_video_details, 'snippet', 'thumbnails', 'default', 'url')).content, sort_order=1)\n      if Dict(json_video_details, 'statistics', 'likeCount') and int(json_video_details['statistics']['likeCount']) > 0 and Dict(json_video_details, 'statistics', 'dislikeCount') and int(Dict(json_video_details, 'statistics', 'dislikeCount')) > 0:\n        metadata.rating                = float(10*int(json_video_details['statistics']['likeCount'])/(int(json_video_details['statistics']['dislikeCount'])+int(json_video_details['statistics']['likeCount'])));  Log('rating: {}'.format(metadata.rating))\n      if Prefs['add_user_as_director']:\n        metadata.directors.clear()\n        try:\n          meta_director       = metadata.directors.new()\n          meta_director.name  = json_video_details['snippet']['channelTitle']\n          Log(u'director: '+json_video_details['snippet']['channelTitle'])\n        except Exception as e:  Log.Info(u'[!] add_user_as_director exception: {}'.format(e))\n      return\n  \n  ### TV series Library ###\n  else:\n    title=\"\"\n    ### Collection tag for grouping folders ###\n    library, root, path = GetLibraryRootPath(dir)\n    series_root_folder=''\n    Log.Info(u'[ ] library:    \"{}\"'.format(library))\n    Log.Info(u'[ ] root:       \"{}\"'.format(root   ))\n    Log.Info(u'[ ] path:       \"{}\"'.format(path   ))\n    Log.Info(u'[ ] dir:        \"{}\"'.format(dir    ))\n    metadata.studio = 'YouTube'\n    if not path in ('_unknown_folder', '.'):\n      #Log.Info('[ ] series root folder:        \"{}\"'.format(os.path.join(root, path.split(os.sep, 1)[0])))\n      series_root_folder  = os.path.join(root, path.split(os.sep, 1)[0] if os.sep in path else path) \n      Log.Info(u'[ ] series_root_folder: \"{}\"'.format(series_root_folder))\n      list_files      = os.listdir(series_root_folder) if os.path.exists(series_root_folder) else []\n      subfolder_count = len([file for file in list_files if os.path.isdir(os.path.join(series_root_folder, file))])\n      Log.Info(u'[ ] subfolder_count:    \"{}\"'.format(subfolder_count   ))\n\n      ### Extract season and transparent folder to reduce complexity and use folder as serie name ###\n      reverse_path, season_folder_first = list(reversed(path.split(os.sep))), False\n      SEASON_RX = [ '^Specials',                                                                                                                                           # Specials (season 0)\n                    '^(?P<show>.*)?[\\._\\-\\— ]*?(Season|Series|Book|Saison|Livre|Temporada|[Ss])[\\._\\—\\- ]*?(?P<season>\\d{1,4}).*?',                                        # (title) S01\n                    '^(?P<show>.*)?[\\._\\-\\— ]*?Volume[\\._\\-\\— ]*?(?P<season>(?=[MDCLXVI])M*D?C{0,4}L?X{0,4}V?I{0,4}).*?',                                                  # (title) S01\n                    '^(Saga|(Story )?Ar[kc])']                                                                                                                             # Last entry, folder name droped but files kept: Saga / Story Ar[kc] / Ar[kc]\n      for folder in reverse_path[:-1]:                 # remove root folder from test, [:-1] Doesn't thow errors but gives an empty list if items don't exist, might not be what you want in other cases\n        for rx in SEASON_RX[:-1]:                      # in anime, more specials folders than season folders, so doing it first\n          if re.match(rx, folder, re.IGNORECASE):      # get season number but Skip last entry in seasons (skipped folders)\n            reverse_path.remove(folder)                # Since iterating slice [:] or [:-1] doesn't hinder iteration. All ways to remove: reverse_path.pop(-1), reverse_path.remove(thing|array[0])\n            if rx!=SEASON_RX[-1] and len(reverse_path)>=2 and folder==reverse_path[-2]:  season_folder_first = True\n            break\n\n      if len(reverse_path)>1 and not season_folder_first and subfolder_count>1:  ### grouping folders only ###\n        Log.Info(\"Grouping folder found, root: {}, path: {}, Grouping folder: {}, subdirs: {}, reverse_path: {}\".format(root, path, os.path.basename(series_root_folder), subfolder_count, reverse_path))\n        collection = re.sub(r'\\[.*\\]', '', reverse_path[-1]).strip()\n        Log.Info('[ ] collections:        \"{}\"'.format(collection))\n        if collection not in metadata.collections:  metadata.collections=[collection]\n      else:  Log.Info(\"Grouping folder not found or single folder, root: {}, path: {}, Grouping folder: {}, subdirs: {}, reverse_path: {}\".format(root, path, os.path.basename(series_root_folder), subfolder_count, reverse_path))\n\n    ### Series - Playlist ###############################################################################################################\n    if len(guid)>2 and guid[0:2] in ('PL', 'UU', 'FL', 'LP', 'RD'):\n      Log.Info('[?] json_playlist_details')\n      try:                    json_playlist_details = json_load(YOUTUBE_PLAYLIST_DETAILS, guid)['items'][0]\n      except Exception as e:  Log('[!] json_playlist_details exception: {}, url: {}'.format(e, YOUTUBE_PLAYLIST_DETAILS.format(guid, 'personal_key')))\n      else:\n        Log.Info('[?] json_playlist_details: {}'.format(json_playlist_details.keys()))\n        channel_id                       = Dict(json_playlist_details, 'snippet', 'channelId');                               Log.Info('[ ] channel_id: \"{}\"'.format(channel_id))\n        title                            = sanitize_path(Dict(json_playlist_details, 'snippet', 'title'));                    Log.Info('[ ] title:      \"{}\"'.format(metadata.title))\n        if title: metadata.title = title\n        metadata.originally_available_at = Datetime.ParseDate(Dict(json_playlist_details, 'snippet', 'publishedAt')).date();  Log.Info('[ ] publishedAt:  {}'.format(Dict(json_playlist_details, 'snippet', 'publishedAt' )))\n        metadata.summary                 = Dict(json_playlist_details, 'snippet', 'description');                             Log.Info('[ ] summary:     \"{}\"'.format((Dict(json_playlist_details, 'snippet', 'description').replace('\\n', '. '))))\n\n        if Prefs['use_crowd_sourced_titles'] == True:\n          crowd_sourced_title = DeArrow(guid)\n          if crowd_sourced_title != '':\n            metadata.summary = 'Original Title: ' + metadata.title + '\\r\\n\\r\\n' + metadata.summary\n            metadata.title = crowd_sourced_title\n\n      Log.Info('[?] json_playlist_items')\n      try:                    json_playlist_items = json_load(YOUTUBE_PLAYLIST_ITEMS, guid)\n      except Exception as e:  Log.Info('[!] json_playlist_items exception: {}, url: {}'.format(e, YOUTUBE_PLAYLIST_ITEMS.format(guid, 'personal_key')))\n      else:\n        Log.Info('[?] json_playlist_items: {}'.format(json_playlist_items.keys()))\n        first_video = sorted(Dict(json_playlist_items, 'items'), key=lambda i: Dict(i, 'contentDetails', 'videoPublishedAt'))[0]\n        thumb = Dict(first_video, 'snippet', 'thumbnails', 'maxres', 'url') or Dict(first_video, 'snippet', 'thumbnails', 'medium', 'url') or Dict(first_video, 'snippet', 'thumbnails', 'standard', 'url') or Dict(first_video, 'snippet', 'thumbnails', 'high', 'url') or Dict(first_video, 'snippet', 'thumbnails', 'default', 'url')\n        if thumb and thumb not in metadata.posters:  Log('[ ] posters:   {}'.format(thumb));  metadata.posters [thumb] = Proxy.Media(HTTP.Request(thumb).content, sort_order=1 if Prefs['media_poster_source']=='Episode' else 2)\n        else:                                        Log('[X] posters:   {}'.format(thumb))\n    \n    ### Series - Channel ###############################################################################################################\n    if channel_id.startswith('UC') or channel_id.startswith('HC'):\n      try:\n        json_channel_details  = json_load(YOUTUBE_CHANNEL_DETAILS, channel_id)['items'][0]\n        json_channel_items    = json_load(YOUTUBE_CHANNEL_ITEMS, channel_id)\n      except Exception as e:  Log('exception: {}, url: {}'.format(e, guid))\n      else:\n        \n        if not title:\n          title          = re.sub( \"\\s*\\[.*?\\]\\s*\",\" \",series_folder)  #instead of path use series foldername\n          metadata.title = title\n        Log.Info('[ ] title:        \"{}\", metadata.title: \"{}\"'.format(title, metadata.title))\n        if not Dict(json_playlist_details, 'snippet', 'description'):\n          if Dict(json_channel_details, 'snippet', 'description'):  metadata.summary = sanitize_path(Dict(json_channel_details, 'snippet', 'description'))\n          else:\n            summary  = u'Channel with {} videos, '.format(Dict(json_channel_details, 'statistics', 'videoCount'))\n            summary += u'{} subscribers, '.format(Dict(json_channel_details, 'statistics', 'subscriberCount'))\n            summary += u'{} views'.format(Dict(json_channel_details, 'statistics', 'viewCount'))\n            metadata.summary = sanitize_path(summary);  Log.Info(u'[ ] summary:     \"{}\"'.format(summary))  #\n\n        if Prefs['use_crowd_sourced_titles'] == True:\n          crowd_sourced_title = DeArrow(guid)\n          if crowd_sourced_title != '':\n            metadata.summary = 'Original Title: ' + metadata.title + '\\r\\n\\r\\n' + metadata.summary\n            metadata.title = crowd_sourced_title\n\n        if Dict(json_channel_details,'snippet','country') and Dict(json_channel_details,'snippet','country') not in metadata.countries:\n          metadata.countries.add(Dict(json_channel_details,'snippet','country'));  Log.Info('[ ] country: {}'.format(Dict(json_channel_details,'snippet','country') ))\n\n        ### Playlist with cast coming from multiple chan entries in youtube.id file ###############################################################################################################\n        if os.path.exists(os.path.join(dir, 'youtube.id')):\n          with open(os.path.join(dir, 'youtube.id')) as f:\n            metadata.roles.clear()\n            for line in f.readlines():\n              try:                    json_channel_details = json_load(YOUTUBE_CHANNEL_DETAILS, line.rstrip())['items'][0]\n              except Exception as e:  Log('exception: {}, url: {}'.format(e, guid))\n              else:\n                Log.Info('[?] json_channel_details: {}'.format(json_channel_details.keys()))\n                Log.Info('[ ] title:       \"{}\"'.format(Dict(json_channel_details, 'snippet', 'title'      )))\n                if not Dict(json_playlist_details, 'snippet', 'description'):\n                  if Dict(json_channel_details, 'snippet', 'description'):  metadata.summary =  sanitize_path(Dict(json_channel_details, 'snippet', 'description'))\n                  #elif guid.startswith('PL'):  metadata.summary = 'No Playlist nor Channel summary'\n                  else:\n                    summary  = u'Channel with {} videos, '.format(Dict(json_channel_details, 'statistics', 'videoCount'     ))\n                    summary += u'{} subscribers, '.format(Dict(json_channel_details, 'statistics', 'subscriberCount'))\n                    summary += u'{} views'.format(Dict(json_channel_details, 'statistics', 'viewCount'      ))\n                    metadata.summary = sanitize_path(summary) #or 'No Channel summary'\n                    Log.Info(u'[ ] summary:     \"{}\"'.format(Dict(json_channel_details, 'snippet', 'description').replace('\\n', '. ')))  #\n                \n                if Dict(json_channel_details,'snippet','country') and Dict(json_channel_details,'snippet','country') not in metadata.countries:\n                  metadata.countries.add(Dict(json_channel_details,'snippet','country'));  Log.Info('[ ] country: {}'.format(Dict(json_channel_details,'snippet','country') ))\n                \n                thumb_channel = Dict(json_channel_details, 'snippet', 'thumbnails', 'medium', 'url') or Dict(json_channel_details, 'snippet', 'thumbnails', 'high', 'url')   or Dict(json_channel_details, 'snippet', 'thumbnails', 'default', 'url')\n                role       = metadata.roles.new()\n                role.role  = sanitize_path(Dict(json_channel_details, 'snippet', 'title'))\n                role.name  = sanitize_path(Dict(json_channel_details, 'snippet', 'title'))\n                role.photo = thumb_channel\n                Log.Info('[ ] role:        {}'.format(Dict(json_channel_details,'snippet','title')))\n                \n                thumb = Dict(json_channel_details, 'brandingSettings', 'image', 'bannerTvLowImageUrl' ) or Dict(json_channel_details, 'brandingSettings', 'image', 'bannerTvMediumImageUrl') \\\n                  or Dict(json_channel_details, 'brandingSettings', 'image', 'bannerTvHighImageUrl') or Dict(json_channel_details, 'brandingSettings', 'image', 'bannerTvImageUrl'      )\n                external_banner_url = Dict(json_channel_details, 'brandingSettings', 'image', 'bannerExternalUrl')\n                if not thumb and external_banner_url: thumb = '{}=s1920'.format(external_banner_url)\n                if thumb and thumb not in metadata.art:      Log('[X] art:       {}'.format(thumb));  metadata.art [thumb] = Proxy.Media(HTTP.Request(thumb).content, sort_order=1)\n                else:                                        Log('[ ] art:       {}'.format(thumb))\n                if thumb and thumb not in metadata.banners:  Log('[X] banners:   {}'.format(thumb));  metadata.banners [thumb] = Proxy.Media(HTTP.Request(thumb).content, sort_order=1)\n                else:                                        Log('[ ] banners:   {}'.format(thumb))\n                if thumb_channel and thumb_channel not in metadata.posters:\n                  Log('[X] posters:   {}'.format(thumb_channel))\n                  metadata.posters [thumb_channel] = Proxy.Media(HTTP.Request(thumb_channel).content, sort_order=1 if Prefs['media_poster_source']=='Channel' else 2)\n                  #metadata.posters.validate_keys([thumb_channel])\n                else:                                                        Log('[ ] posters:   {}'.format(thumb_channel))\n        \n        ### Cast comes from channel\n        else:    \n          thumb         = Dict(json_channel_details, 'brandingSettings', 'image', 'bannerTvLowImageUrl' ) or Dict(json_channel_details, 'brandingSettings', 'image', 'bannerTvMediumImageUrl') \\\n                       or Dict(json_channel_details, 'brandingSettings', 'image', 'bannerTvHighImageUrl') or Dict(json_channel_details, 'brandingSettings', 'image', 'bannerTvImageUrl'      )\n          external_banner_url = Dict(json_channel_details, 'brandingSettings', 'image', 'bannerExternalUrl')\n          if not thumb and external_banner_url: thumb = '{}=s1920'.format(external_banner_url)\n          if thumb and thumb not in metadata.art:      Log(u'[X] art:       {}'.format(thumb));  metadata.art [thumb] = Proxy.Media(HTTP.Request(thumb).content, sort_order=1)\n          else:                                        Log(u'[ ] art:       {}'.format(thumb))\n          if thumb and thumb not in metadata.banners:  Log(u'[X] banners:   {}'.format(thumb));  metadata.banners [thumb] = Proxy.Media(HTTP.Request(thumb).content, sort_order=1)\n          else:                                        Log(u'[ ] banners:   {}'.format(thumb))\n          thumb_channel = Dict(json_channel_details, 'snippet', 'thumbnails', 'medium', 'url') or Dict(json_channel_details, 'snippet', 'thumbnails', 'high', 'url')   or Dict(json_channel_details, 'snippet', 'thumbnails', 'default', 'url')\n          if thumb_channel and thumb_channel not in metadata.posters:\n            #thumb_channel = sanitize_path(thumb_channel)\n            Log(u'[X] posters:   {}'.format(thumb_channel))\n            metadata.posters [thumb_channel] = Proxy.Media(HTTP.Request(thumb_channel).content, sort_order=1 if Prefs['media_poster_source']=='Channel' else 2)\n            #metadata.posters.validate_keys([thumb_channel])\n          else:                                        Log('[ ] posters:   {}'.format(thumb_channel))\n          metadata.roles.clear()\n          role       = metadata.roles.new()\n          role.role  = sanitize_path(Dict(json_channel_details, 'snippet', 'title'))\n          role.name  = sanitize_path(Dict(json_channel_details, 'snippet', 'title'))\n          role.photo = thumb_channel\n          Log.Info(u'[ ] role:        {}'.format(Dict(json_channel_details,'snippet','title')))\n          #if not Dict(json_playlist_details, 'snippet', 'publishedAt'):  metadata.originally_available_at = Datetime.ParseDate(Dict(json_channel_items, 'snippet', 'publishedAt')).date();  Log.Info('[ ] publishedAt:  {}'.format(Dict(json_channel_items, 'snippet', 'publishedAt' )))\n           \n    #NOT PLAYLIST NOR CHANNEL GUID\n    else:  \n      Log.Info('No GUID so random folder')\n      metadata.title = series_folder  #instead of path use series foldername\n \n    ### Season + Episode loop ###\n    genre_array = {}\n    episodes    = 0\n\n    for s in sorted(media.seasons, key=natural_sort_key):\n      Log.Info(u\"\".ljust(157, '='))\n      Log.Info(u\"Season: {:>2}\".format(s))\n    \n      for e in sorted(media.seasons[s].episodes, key=natural_sort_key):\n        filename  = os.path.basename(media.seasons[s].episodes[e].items[0].parts[0].file)\n        episode   = metadata.seasons[s].episodes[e]\n        episodes += 1\n        Log.Info('metadata.seasons[{:>2}].episodes[{:>3}] \"{}\"'.format(s, e, filename))\n        \n        for video in Dict(json_playlist_items, 'items') or Dict(json_channel_items, 'items') or {}:\n          \n          # videoId in Playlist/channel\n          videoId = Dict(video, 'id', 'videoId') or Dict(video, 'snippet', 'resourceId', 'videoId')\n          if videoId and videoId in filename:\n            episode.title                   = sanitize_path(Dict(video, 'snippet', 'title'       ));                                                                  Log.Info(u'[ ] title:        {}'.format(Dict(video, 'snippet', 'title'       )))\n            episode.summary                 = sanitize_path(Dict(video, 'snippet', 'description' ));                                                                  Log.Info(u'[ ] description:  {}'.format(Dict(video, 'snippet', 'description' ).replace('\\n', '. ')))\n            episode.originally_available_at = Datetime.ParseDate(Dict(video, 'contentDetails', 'videoPublishedAt') or Dict(video, 'snippet', 'publishedAt')).date();  Log.Info('[ ] publishedAt:  {}'.format(Dict(video, 'contentDetails', 'videoPublishedAt' )))\n            thumb                           = Dict(video, 'snippet', 'thumbnails', 'maxres', 'url') or Dict(video, 'snippet', 'thumbnails', 'medium', 'url')or Dict(video, 'snippet', 'thumbnails', 'standard', 'url') or Dict(video, 'snippet', 'thumbnails', 'high', 'url') or Dict(video, 'snippet', 'thumbnails', 'default', 'url')\n            if thumb and thumb not in episode.thumbs:  episode.thumbs[thumb] = Proxy.Media(HTTP.Request(thumb).content, sort_order=1);                                Log.Info('[ ] thumbnail:    {}'.format(thumb))\n            Log.Info(u'[ ] channelTitle: {}'.format(Dict(video, 'snippet', 'channelTitle')))\n            break\n        \n        else:  # videoId not in Playlist/channel item list\n\n          #Loading json file if available\n          json_filename = filename.rsplit('.', 1)[0] + \".info.json\"\n          Log.Info(u'populate_episode_metadata_from_info_json() - series_root_folder: {}, filename: {}'.format(series_root_folder, filename))\n          Log.Info(u'Searching for \"{}\". Searching in \"{}\".'.format(json_filename, series_root_folder))\n          for root, dirnames, filenames in os.walk(series_root_folder):\n            Log.Info(u'Directory {} contains {} files'.format(root, len(filenames)))  #for filename in filenames: Log.Info('File: {}'.format(filename))\n            if json_filename in filenames :\n              json_file = os.path.join(root, json_filename)\n              try:  json_video_details = JSON.ObjectFromString(Core.storage.load(json_file))  #\"JSONDecodeError: Unexpected end of input\" if empty\n              except: json_video_details = None\n              if json_video_details:\n                Log.Info('Attempting to read metadata from {}'.format(os.path.join(root, json_filename)))\n                videoId = Dict(json_video_details, 'id')\n                Log.Info('# videoId [{}] not in Playlist/channel item list so loading json_video_details'.format(videoId))\n                Log.Info('[?] link:     \"https://www.youtube.com/watch?v={}\"'.format(videoId))\n                thumb, picture = img_load(series_root_folder, filename)  #Load locally\n                if thumb is None:\n                  thumb = get_thumb(json_video_details)\n                  if thumb not in episode.thumbs: picture = HTTP.Request(thumb).content  \n                if thumb and thumb not in episode.thumbs:\n                  Log.Info(u'[ ] thumbs:   \"{}\"'.format(thumb))\n                  episode.thumbs[thumb] = Proxy.Media(picture, sort_order=1)\n                  episode.thumbs.validate_keys([thumb])\n                  \n                episode.title                   = sanitize_path(Dict(json_video_details, 'title'));            Log.Info(u'[ ] title:    \"{}\"'.format(Dict(json_video_details, 'title')))\n                episode.summary                 = sanitize_path(Dict(json_video_details, 'description'));      Log.Info(u'[ ] summary:  \"{}\"'.format(Dict(json_video_details, 'description').replace('\\n', '. ')))\n                if len(e)>3: episode.originally_available_at = Datetime.ParseDate(Dict(json_video_details, 'upload_date')).date();  Log.Info(u'[ ] date:     \"{}\"'.format(Dict(json_video_details, 'upload_date')))\n                episode.duration                = int(Dict(json_video_details, 'duration'));                           Log.Info(u'[ ] duration: \"{}\"'.format(episode.duration))\n                if Dict(json_video_details, 'likeCount') and int(Dict(json_video_details, 'like_count')) > 0 and Dict(json_video_details, 'dislike_count') and int(Dict(json_video_details, 'dislike_count')) > 0:\n                  episode.rating                = float(10*int(Dict(json_video_details, 'like_count'))/(int(Dict(json_video_details, 'dislike_count'))+int(Dict(json_video_details, 'like_count'))));  Log('[ ] rating:   \"{}\"'.format(episode.rating))\n                if channel_title and channel_title not in [role_obj.name for role_obj in episode.directors]:\n                  meta_director      = episode.directors.new()\n                  meta_director.name = sanitize_path(channel_title)\n                  Log.Info(u'[ ] director: \"{}\"'.format(channel_title))\n\n                for category  in Dict(json_video_details, 'categories') or []:  genre_array[category] = genre_array[category]+1 if category in genre_array else 1\n                for tag       in Dict(json_video_details, 'tags')       or []:  genre_array[tag     ] = genre_array[tag     ]+1 if tag      in genre_array else 1\n                \n                Log.Info(u'[ ] genres:   \"{}\"'.format([x for x in metadata.genres]))  #metadata.genres.clear()\n                for id in [id for id in genre_array if genre_array[id]>episodes/2 and id not in metadata.genres]:  metadata.genres.add(id)\n                break\n          \n          #Loading from API\n          else:\n            Log(u'populate_episode_metadata_from_api() - filename: {}'.format(filename))\n            result = YOUTUBE_VIDEO_REGEX.search(filename)\n            if result:\n              videoId = result.group('id')\n              Log.Info(u'# videoId [{}] not in Playlist/channel item list so loading json_video_details'.format(videoId))\n              try:                    json_video_details = json_load(YOUTUBE_json_video_details, videoId)['items'][0]\n              except Exception as e:  Log('Error: \"{}\"'.format(e))\n              else:\n                Log.Info('[?] link:     \"https://www.youtube.com/watch?v={}\"'.format(videoId))\n                thumb                           = Dict(json_video_details, 'snippet', 'thumbnails', 'maxres', 'url') or Dict(json_video_details, 'snippet', 'thumbnails', 'medium', 'url') or Dict(json_video_details, 'snippet', 'thumbnails', 'standard', 'url') or Dict(json_video_details, 'snippet', 'thumbnails', 'high', 'url') or Dict(json_video_details, 'snippet', 'thumbnails', 'default', 'url')\n                episode.title                   = sanitize_path(json_video_details['snippet']['title']);                                 Log.Info('[ ] title:    \"{}\"'.format(json_video_details['snippet']['title']))\n                episode.summary                 = sanitize_path(json_video_details['snippet']['description']);                           Log.Info('[ ] summary:  \"{}\"'.format(json_video_details['snippet']['description'].replace('\\n', '. ')))\n                if len(e)>3:  episode.originally_available_at = Datetime.ParseDate(json_video_details['snippet']['publishedAt']).date();                       Log.Info('[ ] date:     \"{}\"'.format(json_video_details['snippet']['publishedAt']))\n                episode.duration                = ISO8601DurationToSeconds(json_video_details['contentDetails']['duration'])*1000;               Log.Info('[ ] duration: \"{}\"->\"{}\"'.format(json_video_details['contentDetails']['duration'], episode.duration))\n                if Dict(json_video_details, 'statistics', 'likeCount') and int(json_video_details['statistics']['likeCount']) > 0 and Dict(json_video_details, 'statistics', 'dislikeCount') and int(Dict(json_video_details, 'statistics', 'dislikeCount')) > 0:\n                  episode.rating                = 10*float(json_video_details['statistics']['likeCount'])/(float(json_video_details['statistics']['dislikeCount'])+float(json_video_details['statistics']['likeCount']));  Log('[ ] rating:   \"{}\"'.format(episode.rating))\n                if thumb and thumb not in episode.thumbs:\n                  picture = HTTP.Request(thumb).content\n                  episode.thumbs[thumb]         = Proxy.Media(picture, sort_order=1);                                                     Log.Info('[ ] thumbs:   \"{}\"'.format(thumb))\n                  episode.thumbs.validate_keys([thumb])\n                  Log.Info(u'[ ] Thumb: {}'.format(thumb))\n                if Dict(json_video_details, 'snippet',  'channelTitle') and Dict(json_video_details, 'snippet',  'channelTitle') not in [role_obj.name for role_obj in episode.directors]:\n                  meta_director       = episode.directors.new()\n                  meta_director.name  = sanitize_path(Dict(json_video_details, 'snippet',  'channelTitle'))\n                  Log.Info('[ ] director: \"{}\"'.format(Dict(json_video_details, 'snippet',  'channelTitle')))\n                \n                for id  in Dict(json_video_details, 'snippet', 'categoryId').split(',') or []:  genre_array[YOUTUBE_CATEGORY_ID[id]] = genre_array[YOUTUBE_CATEGORY_ID[id]]+1 if YOUTUBE_CATEGORY_ID[id] in genre_array else 1\n                for tag in Dict(json_video_details, 'snippet', 'tags')                  or []:  genre_array[tag                    ] = genre_array[tag                    ]+1 if tag                     in genre_array else 1\n\n              Log.Info(u'[ ] genres:   \"{}\"'.format([x for x in metadata.genres]))  #metadata.genres.clear()\n              genre_array_cleansed = [id for id in genre_array if genre_array[id]>episodes/2 and id not in metadata.genres]  #Log.Info('[ ] genre_list: {}'.format(genre_list))\n              for id in genre_array_cleansed:  metadata.genres.add(id)\n            else:  Log.Info(u'videoId not found in filename')\n\n  Log('=== End Of Agent Call, errors after that are Plex related ==='.ljust(157, '='))\n\n### Agent declaration ##################################################################################################################################################\nclass YouTubeSeriesAgent(Agent.TV_Shows):\n  name, primary_provider, fallback_agent, contributes_to, accepts_from, languages = 'YouTubeSeries', True, None, None, ['com.plexapp.agents.localmedia'], [Locale.Language.NoLanguage]\n  def search (self, results,  media, lang, manual):  Search (results,  media, lang, manual, False)\n  def update (self, metadata, media, lang, force ):  Update (metadata, media, lang, force,  False)\n\nclass YouTubeMovieAgent(Agent.Movies):\n  name, primary_provider, fallback_agent, contributes_to, accepts_from, languages = 'YouTubeMovie', True, None, None, ['com.plexapp.agents.localmedia'], [Locale.Language.NoLanguage]\n  def search (self, results,  media, lang, manual):  Search (results,  media, lang, manual, True)\n  def update (self, metadata, media, lang, force ):  Update (metadata, media, lang, force,  True)\n\n### Variables ###\nPluginDir                = os.path.abspath(os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), \"..\", \"..\"))\nPlexRoot                 = os.path.abspath(os.path.join(PluginDir, \"..\", \"..\"))\nCachePath                = os.path.join(PlexRoot, \"Plug-in Support\", \"Data\", \"com.plexapp.agents.hama\", \"DataItems\")\nPLEX_LIBRARY             = {}\nPLEX_LIBRARY_URL         = \"http://127.0.0.1:32400/library/sections/\"    # Allow to get the library name to get a log per library https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-account-token-X-Plex-Token\nYOUTUBE_API_BASE_URL     = \"https://www.googleapis.com/youtube/v3/\"\nYOUTUBE_CHANNEL_ITEMS    = YOUTUBE_API_BASE_URL + 'search?order=date&part=snippet&type=video&maxResults=50&channelId={}&key={}'\nYOUTUBE_CHANNEL_DETAILS  = YOUTUBE_API_BASE_URL + 'channels?part=snippet%2CcontentDetails%2Cstatistics%2CbrandingSettings&id={}&key={}'\nYOUTUBE_CHANNEL_REGEX    = Regex('\\[(?:youtube(|2)\\-)?(?P<id>UC[a-zA-Z0-9\\-_]{22}|HC[a-zA-Z0-9\\-_]{22})\\]')\nYOUTUBE_PLAYLIST_ITEMS   = YOUTUBE_API_BASE_URL + 'playlistItems?part=snippet,contentDetails&maxResults=50&playlistId={}&key={}'\nYOUTUBE_PLAYLIST_DETAILS = YOUTUBE_API_BASE_URL + 'playlists?part=snippet,contentDetails&id={}&key={}'\nYOUTUBE_PLAYLIST_REGEX   = Regex('\\[(?:youtube(|3)\\-)?(?P<id>PL[^\\[\\]]{16}|PL[^\\[\\]]{32}|UU[^\\[\\]]{22}|FL[^\\[\\]]{22}|LP[^\\[\\]]{22}|RD[^\\[\\]]{22}|UC[^\\[\\]]{22}|HC[^\\[\\]]{22})\\]',  Regex.IGNORECASE)  # https://regex101.com/r/37x8wI/2\nYOUTUBE_VIDEO_SEARCH     = YOUTUBE_API_BASE_URL + 'search?&maxResults=1&part=snippet&q={}&key={}'\nYOUTUBE_json_video_details    = YOUTUBE_API_BASE_URL + 'videos?part=snippet,contentDetails,statistics&id={}&key={}'\nYOUTUBE_VIDEO_REGEX      = Regex('(?:^\\d{8}_|\\[(?:youtube\\-)?)(?P<id>[a-z0-9\\-_]{11})(?:\\]|_)', Regex.IGNORECASE) # https://regex101.com/r/zlHKPD/1\nYOUTUBE_CATEGORY_ID      = {  '1': 'Film & Animation',  '2': 'Autos & Vehicles',  '10': 'Music',          '15': 'Pets & Animals',        '17': 'Sports',                 '18': 'Short Movies',\n                             '19': 'Travel & Events',  '20': 'Gaming',            '21': 'Videoblogging',  '22': 'People & Blogs',        '23': 'Comedy',                 '24': 'Entertainment',\n                             '25': 'News & Politics',  '26': 'Howto & Style',     '27': 'Education',      '28': 'Science & Technology',  '29': 'Nonprofits & Activism',  '30': 'Movies',\n                             '31': 'Anime/Animation',  '32': 'Action/Adventure',  '33': 'Classics',       '34': 'Comedy',                '35': 'Documentary',            '36': 'Drama', \n                             '37': 'Family',           '38': 'Foreign',           '39': 'Horror',         '40': 'Sci-Fi/Fantasy',        '41': 'Thriller',               '42': 'Shorts',\n                             '43': 'Shows',            '44': 'Trailers'}\n### Plex Library XML ###\nLog.Info(u\"Library: \"+PlexRoot)  #Log.Info(file)\ntoken_file_path = os.path.join(PlexRoot, \"X-Plex-Token.id\")\nif os.path.isfile(token_file_path):\n  Log.Info(u\"'X-Plex-Token.id' file present\")\n  token_file=Data.Load(token_file_path)\n  if token_file:  PLEX_LIBRARY_URL += \"?X-Plex-Token=\" + token_file.strip()\n  #Log.Info(PLEX_LIBRARY_URL) ##security risk if posting logs with token displayed\ntry:\n  library_xml = etree.fromstring(urllib2.urlopen(PLEX_LIBRARY_URL).read())\n  for library in library_xml.iterchildren('Directory'):\n    for path in library.iterchildren('Location'):\n      PLEX_LIBRARY[path.get(\"path\")] = library.get(\"title\")\n      Log.Info(u\"{} = {}\".format(path.get(\"path\"), library.get(\"title\")))\nexcept Exception as e:  Log.Info(u\"Place correct Plex token in {} file or in PLEX_LIBRARY_URL variable in Code/__init__.py to have a log per library - https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-account-token-X-Plex-Token, Error: {}\".format(token_file_path, str(e)))\n"
  },
  {
    "path": "Contents/DefaultPrefs.json",
    "content": "[\n  { \"id\":\"add_user_as_director\",          \"label\":\"Set YouTube usernames as director in metadata\",  \"type\":\"bool\", \"default\":\"false\"                                     },\n  { \"id\":\"use_crowd_sourced_titles\",      \"label\":\"Use Crowd Sourced Video Titles from DeArrow\",    \"type\":\"bool\", \"default\":\"false\"                                     },\n  { \"id\":\"media_poster_source\",           \"label\":\"Media Poster\",                                   \"type\":\"enum\", \"default\":\"Channel\", \"values\": [\"Channel\", \"Episode\"] },\n  { \"id\":\"YouTube-Agent_youtube_api_key\", \"label\":\"YouTube API Key (put your own key)\",             \"type\":\"text\", \"default\":\"AIzaSyC2q8yjciNdlYRNdvwbb7NEcDxBkv1Cass\"   },\n]\n"
  },
  {
    "path": "Contents/Info.plist",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n\t<key>CFBundleIdentifier</key>\n\t<string>com.plexapp.agents.youtube</string>\n\t<key>PlexFrameworkVersion</key>\n\t<string>2</string>\n\t<key>PlexPluginClass</key>\n\t<string>Agent</string>\n  <key>PlexPluginCodePolicy</key>\n  <string>Elevated</string>\n  \n</dict>\n</plist>\n"
  },
  {
    "path": "README.md",
    "content": "# YouTube-Agent.bundle: Plex Movie & TV Series library agent\n\nThis is a Metadata Agent for downloaded YouTube videos. It works by looking up\nmetadata on YouTube using the YouTube video id. It is important to have this id\nin the filename, otherwise this agent can't do the lookup.\nThis plugin also supports looking up metadata from `.info.json` files,\nsee `--write-info-json` usage below.\n\nThis supports the following formats in file or folder names:\n- `[xxxxxxxx]`\n- `[youtube-xxx]`\n- `[YouTube-xxx]`\n- `[Youtube-xxx]`\n\nThis will find the YouTube id in names, like for example:\n- `Person Of Interest  Soundtrack - John Reese Themes (Compilation) [OR5EnqdnwK0].mp4`\n\nWhen using this Plugin, please respect YouTube Terms and conditions: https://www.youtube.com/t/terms\n\nInstallation\n============\n\nThe plugin code needs to be put into `Plex Media Server/Plug-ins` folder:\n- https://support.plex.tv/articles/201187656-how-do-i-manually-install-a-plugin/\n\nHere is how to find the Plug-in folder location:\n- https://support.plex.tv/articles/201106098-how-do-i-find-the-plug-ins-folder/\n\nPlex main folder location could be one of:\n\n    * '%LOCALAPPDATA%\\Plex Media Server\\'                                        # Windows Vista/7/8\n    * '%USERPROFILE%\\Local Settings\\Application Data\\Plex Media Server\\'         # Windows XP, 2003, Home Server\n    * '$HOME/Library/Application Support/Plex Media Server/'                     # Mac OS\n    * '$PLEX_HOME/Library/Application Support/Plex Media Server/',               # Linux\n    * '/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/', # Debian,Fedora,CentOS,Ubuntu\n    * '/usr/local/plexdata/Plex Media Server/',                                  # FreeBSD\n    * '/usr/pbi/plexmediaserver-amd64/plexdata/Plex Media Server/',              # FreeNAS\n    * '${JAIL_ROOT}/var/db/plexdata/Plex Media Server/',                         # FreeNAS\n    * '/c/.plex/Library/Application Support/Plex Media Server/',                 # ReadyNAS\n    * '/share/MD0_DATA/.qpkg/PlexMediaServer/Library/Plex Media Server/',        # QNAP\n    * '/volume1/Plex/Library/Application Support/Plex Media Server/',            # Synology, Asustor\n    * '/raid0/data/module/Plex/sys/Plex Media Server/',                          # Thecus\n    * '/raid0/data/PLEX_CONFIG/Plex Media Server/'                               # Thecus Plex community\n\nTo obtain the code:\n1. Download the Zip file: https://github.com/ZeroQI/YouTube-Agent.bundle/archive/refs/heads/master.zip\n1. Unpack the downloaded Zip and rename the contents as `Youtube-Agent.bundle` (remove `-master`)\n1. Place it inside `Plug-ins` folder\n1. Restart Plex Media Server to make sure that the new plugin will be loaded.\n1. [Create your own YouTube API token](#youtube-api-key) (recommended)\n\nTo enable for Library:\n1. Create a new (or update an existing) library\n2. Choose `Manage Library` -> `Edit`\n3. Click on the `Advanced` tab and select Agent: `YoutubeMovie` or `YouTubeSeries` depending on library type\n\nRepeat this for all libraries you wish to use this agent.\n\nUsage\n=====\n\nTo download a playlist:\n1. Take video link: https://www.youtube.com/watch?v=f-wWBGo6a2w&list=PL22J3VaeABQD_IZs7y60I3lUrrFTzkpat\n1. Click on top right on playlist name or remove `v=video_id` from URL: https://www.youtube.com/watch?list=PL22J3VaeABQD_IZs7y60I3lUrrFTzkpat\n1. Run `youtube-dl` command: `youtube-dl https://www.youtube.com/watch?list=PL22J3VaeABQD_IZs7y60I3lUrrFTzkpat`\n\nAdditionally, you may want to use:\n\n1. `--restrict-filenames`:\n   Necessary, when storing media files on Windows filesystem.\n   Restrict filenames to only ASCII characters, and avoid \"&\" and\n   spaces in filenames, makes the filenames slightly messy but no crash due to\n   unsupported character.\n1. `--write-info-json`:\n   The agent will load metadata from the local file if exists.\n   This can reduce YouTube API request rate if metadata is obtained from local `.info.json` files.\n\nA `My_Plex_Pass` user script from\n[forums.plex.com](https://forums.plex.tv/t/rel-youtube-metadata-agent/44574/184)\nfor both channels and playlists in format `channel name [chanid]\\video title [videoid].ext`:\n- `youtube-dl -v --dateafter 20081004 --download-archive /volume1/Youtube/.Downloaded -i -o \"/volume1/Youtube/%(uploader)s [%(channel_id)s]/%(playlist_index)s - %(title)s [%(id)s].%(ext)s\" -f bestvideo+bestaudio -ci --batch-file=/volume1/Youtube/Channels_to_DL.txt`\n- Example files: `Youtube\\Errant Signal [UCm4JnxTxtvItQecKUc4zRhQ]\\001 - Thanksgiving Leftovers - Battlefield V [Qgdr8xdqGDE]`\n\nYouTube IDs\n- Playlist id: PL and 16 hexadecimal characters 0-9 and A-F or 32 chars 0-9 a-Z _ - (Example: https://www.youtube.com/watch?v=aCl4SD7SkLE&list=PLMBYlcH3smRxxcXT7G-HHAj5czGS0sZsB)\n- Channel id: PL and 32 hexadecimal characters 0-9 and A-F or 32 chars 0-9 a-Z _ - (Example: (https://www.youtube.com/channel/UCYzPXprvl5Y-Sf0g4vX-m6g)\n- Video id: 11 chars long 0-9 a-Z _ -\n\nRequirements\n- Do create your own YouTube API key and replace in [Absolute Series Scanner] (ASS) code and agent settings\n- Please use the Absolute Series Scanner to scan your media and leave the YouTube id in the series/movie title\n- leave the YouTube video ID on every file\n- Playlist (preferred) id OR Channel id on series folder name (as `Search()` need to assign an id to the series)\n\nNaming convention for Movie/Home Video library:\n- filename without extension named exactly the same as the YouTube video\n- filename with youtube video id `[xxxxxxxxxx]` or `[youtube-xxxxxxxxxx]`\n\nNaming convention for TV Series library:\n- movies have to be put in identically named folder named exactly the same as the YouTube video or have YouTube video id\n- series folder name with YouTube playlist id `[PLxxxxxxxxxxxxxxxx]` in title or inside a `youtube.id` file at its root\n- series folder name with YouTube channel id `[UCxxxxxxxxxxxxxxxx]` in title or inside a `youtube.id` file at its root\n\nNotes:\n- The Absolute Series Scanner will support `youtube.id` file in series folder and pass it to the agent through the series title\n- [!] register your own API key and also replace `API_KEY='AIzaSyC2q8yjciNdlYRNdvwbb7NEcDxBkv1Cass'` in `Absolute Series Scanner` codebase and the agent setting `[Agent_youtube_api_key]` OR you will deplete the quota of requests in MY account and metadata will stop for ALL users using default settings.\n- You can use grouping folders and a collection field will be created. If the logs complain about `INFO (__init__:527) - Place correct Plex token in X-Plex-Token.id file in logs folder or in PLEX_LIBRARY_URL variable to have a log per library - https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-account-token-X-Plex-Token`, then create a `Plex Media Server/X-Plex-Token.id` containing the Plex token id by logging on https://app.plex.tv/desktop, then visit https://plex.tv/devices.xml, and find `<MediaContainer><Device ... token=\"xxxxxxxxxxxxxx\">` value.\n\nMovie Library Fields supported:\n- `title`\n- `summary`\n- `poster`\n- `rating`\n- `originally_available_at`\n- `year`\n- `genres` (many? to test)\n- `directors` (1)\n\nExample\n=======\n\nThis shows example file layout:\n\n```\nCaRtOoNz [UCdQWs2nw6w77Rw0t-37a4OA]/\n- Ben and Ed/\n  - Ben and Ed _ 'My Zombie Best Friend!' (I Didn't Need Those Legs Anyway!) [fRFr7L_qgEo].mkv\n  - Ben and Ed _ 'Clownin Around!' (F_ck You Neck-Beard!) [Nh9eILgD5N4].mkv\n- Golf With Your Friends/\n  - Golf With Friends _ HOLE IN ONE...THOUSAND! (w_ H2O Delirious, Bryce, & Ohmwrecker) [81er8CP24h8].mkv\n  - Golf With Friends _ GOLF LIKE AN EGYPTIAN! (w_ H2O Delirious, Bryce, & Ohmwrecker) [gKYid-SjDiE].mkv\n\nH2ODelirious [UCClNRixXlagwAd--5MwJKCw]/\n- Ben and Ed/\n  - Ben And Ed Ep.1 (MUST SAVE BEN) BRAINNNNNSSSS [9YeXl28l9Yg].mkv\n  - Ben And Ed - Blood Party - ANGRYLIRIOUS!!!!! (I CAN DO THIS!) [BEDE2z3G3hY].mkv\n- Golf With Your Friends/\n  - Golf With Your Friends - 1st Time Playing! 'Professionals' [wxS52xI_W_Y].mkv\n  - Golf With Your Friends - Hitting Balls, Stroking Out! [GdLon0CCEXE].mkv\n```\n\nHistory\n=======\n\nForked initially from [@paulds8] and [@sander1]'s `YouTube-Agent.bundle` movie-only agent:\n\n[@sander1] did the initial movie only agent using a given YouTube video id:\n- https://github.com/sander1/YouTube-Agent.bundle\n- https://forums.plex.tv/discussion/83106/rel-youtube-metadata-agent\n\n[@paulds8] did the initial title search fork that [@ZeroQI] had to fix:\n- https://github.com/paulds8/YouTube-Agent.bundle/tree/namematch\n- https://forums.plex.tv/discussion/300800/youtube-agent-matching-on-name\n\nMade it into a series agent straight away...\n\n[@paulds8]: https://github.com/paulds8\n[@sander1]: https://github.com/sander1\n[@ZeroQI]: https://github.com/ZeroQI\n\nYouTube API key\n===============\n\nTo avoid depleting usage quota of the built-in API key, you should register\nyour own YouTube API key and configure this plugin to use it.\n\nWhen quota has reached, all users (including you) using default API key will\nhave metadata requests blocked.\n\nThe setup is moderately complicated:\n1. Go to [Google Developer Console].\n1. From the top bar choose or create a new project\n1. Follow \"API keys\" instructions from [registering an application]\n1. Skip the API restrictions part\n1. [Enable YouTube Data API] for the project\n1. Copy the API key from \"API key created\" dialog\n1. Place the value to `youtube-key.txt` file in the plugin directory\n1. If you need to obtain the API key any time later, visit [credentials] page\n\n[Google Developer Console]: https://console.developers.google.com/\n[registering an application]: https://developers.google.com/youtube/registering_an_application\n[credentials]: https://console.cloud.google.com/apis/credentials\n[Enable YouTube Data API]: https://console.cloud.google.com/apis/library/youtube.googleapis.com\n\nTroubleshooting:\n================\nIf you ask for something already answered in the readme, or post scanner issues on the agent page or vice-versa, please donate (will be refered to as the RTFM tax)\n\nIf files and series are showing in Plex GUI with the right season, the scanner did its job\nIf you miss metadata (serie title wrong, no posters, summary, wrong episode title or summaries, ep screenshot, etc...), that is the Agent doing.\n\nTo avoid already solved issues, and make sure you do include all relevant logs in one go, please do the following:\n- Update to the latest Absolute Series Scanner, Youtube-Agent\n- deleting all Plex logs leaving folders intact\n- restart Plex\n- Update the series Metadata\n- including all the following logs: (location: https://support.plex.tv/hc/en-us/articles/200250417-Plex-Media-Server-Log-Files)\n   - [...]/Plex Media Server/Logs/PMS Plugin Logs/com.plexapp.agents.Youtube-Agent.log (Agent logs)\n   - [...]/Plex Media Server/Logs/PMS Plugin Logs/com.plexapp.system.log (show why the agent cannot launch)\n   - Screen capture to illustrate if needed. Above logs are still mandatory\n\nSupport thread for agent:\n- https://github.com/ZeroQI/YouTube-Agent.bundle/issues (proven or confident enough it's a bug. Include the symptoms, the logs mentionned above)\n- https://forums.plex.tv/discussion/83106/rel-youtube-metadata-agent/p5 (not sure if bug, if bug will create a gihub issue ticket)\n\nDonation\n========\n\nYou can choose either:\n\n1. Pay link: https://PayPal.Me/ZeroQI\n1. Donate link: A [PayPal] payment, but marked as donation.\n   Having money sent as donation you could be eligible for tax return depending\n   on the country you pay taxes for.\n\n[PayPal]: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=S8CUKCX4CWBBG&lc=IE&item_name=Plex%20movies%20and%20TV%20series%20Youtube%20Agent&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted\n[Absolute Series Scanner]: https://github.com/ZeroQI/Absolute-Series-Scanner\n"
  }
]