<commit_before>﻿using MediaBrowser.Common.Net;
using MediaBrowser.Common.Security;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Plugins.Trailers.Search;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace MediaBrowser.Plugins.Trailers.EntryPoints
{
    public class LocalTrailerEntryPoint : IServerEntryPoint
    {
        private readonly List<BaseItem> _newlyAddedItems = new List<BaseItem>();

        private const int NewItemDelay = 30000;

        private readonly ILibraryManager _libraryManager;
        private readonly ISecurityManager _securityManager;
        private readonly ILogger _logger;
        private readonly IHttpClient _httpClient;
        private readonly IDirectoryWatchers _directoryWatchers;
        private readonly IJsonSerializer _json;

        private Timer NewItemTimer { get; set; }

        public LocalTrailerEntryPoint(ILibraryManager libraryManager, ISecurityManager securityManager, ILogger logger, IHttpClient httpClient, IDirectoryWatchers directoryWatchers, IJsonSerializer json)
        {
            _libraryManager = libraryManager;
            _securityManager = securityManager;
            _logger = logger;
            _httpClient = httpClient;
            _directoryWatchers = directoryWatchers;
            _json = json;
        }

        public void Run()
        {
            _libraryManager.ItemAdded += libraryManager_ItemAdded;
            _libraryManager.ItemUpdated += libraryManager_ItemAdded;
        }

        void libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
        {
            lock (_newlyAddedItems)
            {
                _newlyAddedItems.Add(e.Item);

                if (NewItemTimer == null)
                {
                    NewItemTimer = new Timer(NewItemTimerCallback, null, NewItemDelay, Timeout.Infinite);
                }
                else
                {
                    NewItemTimer.Change(NewItemDelay, Timeout.Infinite);
                }
            }
        }

        private async void NewItemTimerCallback(object state)
        {
            List<BaseItem> newItems;

            // Lock the list and release all resources
            lock (_newlyAddedItems)
            {
                newItems = _newlyAddedItems.Distinct().ToList();
                _newlyAddedItems.Clear();

                NewItemTimer.Dispose();
                NewItemTimer = null;
            }

            var items = newItems.OfType<Movie>()
              .Where(i => i.LocationType == LocationType.FileSystem && i.LocalTrailerIds.Count == 0)
              .Take(2)
              .ToList();

            if (items.Count == 0 || !Plugin.Instance.Configuration.EnableLocalTrailerDownloads)<commit_msg>download trailers as movies are added<commit_after>﻿using MediaBrowser.Common.Net;
using MediaBrowser.Common.Security;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Plugins.Trailers.Search;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace MediaBrowser.Plugins.Trailers.EntryPoints
{
    public class LocalTrailerEntryPoint : IServerEntryPoint
    {
        private readonly List<BaseItem> _newlyAddedItems = new List<BaseItem>();

        private const int NewItemDelay = 30000;

        private readonly ILibraryManager _libraryManager;
        private readonly ISecurityManager _securityManager;
        private readonly ILogger _logger;
        private readonly IHttpClient _httpClient;
        private readonly IDirectoryWatchers _directoryWatchers;
        private readonly IJsonSerializer _json;

        private Timer NewItemTimer { get; set; }

        public LocalTrailerEntryPoint(ILibraryManager libraryManager, ISecurityManager securityManager, ILogger logger, IHttpClient httpClient, IDirectoryWatchers directoryWatchers, IJsonSerializer json)
        {
            _libraryManager = libraryManager;
            _securityManager = securityManager;
            _logger = logger;
            _httpClient = httpClient;
            _directoryWatchers = directoryWatchers;
            _json = json;
        }

        public void Run()
        {
            _libraryManager.ItemAdded += libraryManager_ItemAdded;
            _libraryManager.ItemUpdated += libraryManager_ItemAdded;
        }

        void libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
        {
            lock (_newlyAddedItems)
            {
                _newlyAddedItems.Add(e.Item);

                if (NewItemTimer == null)
                {
                    NewItemTimer = new Timer(NewItemTimerCallback, null, NewItemDelay, Timeout.Infinite);
                }
                else
                {
                    NewItemTimer.Change(NewItemDelay, Timeout.Infinite);
                }
            }
        }

        private async void NewItemTimerCallback(object state)
        {
            List<BaseItem> newItems;

            // Lock the list and release all resources
            lock (_newlyAddedItems)
            {
                newItems = _newlyAddedItems.Distinct().ToList();
                _newlyAddedItems.Clear();

                NewItemTimer.Dispose();
                NewItemTimer = null;
            }

            var items = newItems.OfType<Movie>()
              .Where(i => i.LocationType == LocationType.FileSystem && i.LocalTrailerIds.Count == 0)
              .Take(5)
              .ToList();

            if (items.Count == 0 || !Plugin.Instance.Configuration.EnableLocalTrailerDownloads)
{'fraction_non_alphanumeric': 0.06908971840209562, 'fraction_numerical': 0.0026195153896529143, 'mean_word_length': 2.323721436343852, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3523070', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1470, 'n_words': 332}