public bool InsertAllElements(T element, int playListId)
        {
            bool result = false;

            ProgressHelper.SetProgress("Loading playlist info...", 0);
            var playListInfo = element.GetAllElements();
            int idx          = 0;

            foreach (var item in playListInfo)
            {
                double percent = ((double)++idx / (double)playListInfo.Count) * 100.0;
                ProgressHelper.SetProgress($"Saving playlist info {idx + 1} of {playListInfo.Count}", (int)percent);
                int id = -1;
                id = _playlistItemRepository.InsertItem(new Persistence.Models.PlaylistItem()
                {
                    NewName    = item.Name,
                    Address    = item.Id,
                    PlayListId = playListId,
                });

                result |= id != -1;
            }
            ProgressHelper.SetProgress("Finished.", 0);
            return(result);
        }
Пример #2
0
        public async Task InsertAsync <T>(DbContext context, Type type, IList <T> entities, TableInfo tableInfo, Action <decimal> progress, CancellationToken cancellationToken, bool isAsync)
        {
            SqliteConnection connection = isAsync ? await OpenAndGetSqliteConnectionAsync(context, tableInfo.BulkConfig, cancellationToken).ConfigureAwait(false)
                                                        : OpenAndGetSqliteConnection(context, tableInfo.BulkConfig);

            bool doExplicitCommit = false;

            try
            {
                if (context.Database.CurrentTransaction == null)
                {
                    //context.Database.UseTransaction(connection.BeginTransaction());
                    doExplicitCommit = true;
                }
                var dbTransaction = doExplicitCommit ? connection.BeginTransaction()
                                                     : context.Database.CurrentTransaction.GetUnderlyingTransaction(tableInfo.BulkConfig);
                var transaction = (SqliteTransaction)dbTransaction;

                var command = GetSqliteCommand(context, type, entities, tableInfo, connection, transaction);

                type = tableInfo.HasAbstractList ? entities[0].GetType() : type;
                int rowsCopied = 0;

                foreach (var item in entities)
                {
                    LoadSqliteValues(tableInfo, item, command, context);
                    if (isAsync)
                    {
                        await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        command.ExecuteNonQuery();
                    }
                    ProgressHelper.SetProgress(ref rowsCopied, entities.Count, tableInfo.BulkConfig, progress);
                }
                if (doExplicitCommit)
                {
                    transaction.Commit();
                }
            }
            finally
            {
                if (isAsync)
                {
                    await context.Database.CloseConnectionAsync().ConfigureAwait(false);
                }
                else
                {
                    context.Database.CloseConnection();
                }
            }
        }
Пример #3
0
        private IList <YoutubeItem> GetPLItems()
        {
            IList <YoutubeItem> res = new List <YoutubeItem>();

            string parameters = $"?part=snippet%2CcontentDetails&maxResults=25&playlistId={this.PlaylistId}&key={YouTubeAPIServiceHelper.API_KEY}";

            string uri = PLALIST_ITEMS_URL + parameters;

            ProgressHelper.SetProgress("Requesting Youtube API...", 0);

            StringBuilder resBuilder = new StringBuilder();

            HttpWebRequest myReq   = (HttpWebRequest)WebRequest.Create(uri);
            WebResponse    myResp  = myReq.GetResponse();
            var            decoder = Encoding.UTF8.GetDecoder();

            using (var reader = myResp.GetResponseStream())
            {
                byte[] buffer       = new byte[254];
                int    idx          = 0;
                int    recevedBytes = 0;

                while ((recevedBytes = reader.Read(buffer, 0, buffer.Length)) != 0)
                {
                    double percent = ((double)(++idx * buffer.Length) / (double)myResp.ContentLength) * 100.0;
                    ProgressHelper.SetProgress($"Loading data from API {idx * buffer.Length} of " +
                                               $"{myResp.ContentLength}",
                                               (int)percent);


                    resBuilder.Append(Encoding.UTF8.GetString(buffer, 0, recevedBytes));
                }
            }
            var text = resBuilder.ToString();
            //var response = client.DownloadString(uri);
            PlaylistItemsResponse r = JObject.Parse(text).ToObject <PlaylistItemsResponse>();

            foreach (var item in r.items)
            {
                res.Add(new YoutubeMovie()
                {
                    Name   = item.snippet.title,
                    ImgSrc = [email protected],
                    Id     = item.contentDetails.videoId,
                });
            }

            return(res);
        }
Пример #4
0
        public void Insert <T>(DbContext context, Type type, IList <T> entities, TableInfo tableInfo, Action <decimal> progress)
        {
            var  connection       = OpenAndGetSqliteConnection(context, tableInfo.BulkConfig);
            bool doExplicitCommit = false;

            try
            {
                if (context.Database.CurrentTransaction == null)
                {
                    //context.Database.UseTransaction(connection.BeginTransaction());
                    doExplicitCommit = true;
                }
                var transaction = (SqliteTransaction)(context.Database.CurrentTransaction == null ?
                                                      connection.BeginTransaction() :
                                                      context.Database.CurrentTransaction.GetUnderlyingTransaction(tableInfo.BulkConfig));

                using (var command = GetSqliteCommand(context, type, entities, tableInfo, connection, transaction))
                {
                    type = tableInfo.HasAbstractList ? entities[0].GetType() : type;
                    int rowsCopied = 0;
                    foreach (var item in entities)
                    {
                        LoadSqliteValues(tableInfo, item, command);
                        command.ExecuteNonQuery();
                        ProgressHelper.SetProgress(ref rowsCopied, entities.Count, tableInfo.BulkConfig, progress);
                    }
                    if (doExplicitCommit)
                    {
                        transaction.Commit();
                    }
                }
            }
            finally
            {
                context.Database.CloseConnection();
            }
        }
Пример #5
0
        public void Merge <T>(DbContext context, Type type, IList <T> entities, TableInfo tableInfo, OperationType operationType,
                              Action <decimal> progress) where T : class
        {
            var  connection       = OpenAndGetSqliteConnection(context, tableInfo.BulkConfig);
            bool doExplicitCommit = false;

            try
            {
                if (context.Database.CurrentTransaction == null)
                {
                    //context.Database.UseTransaction(connection.BeginTransaction());
                    doExplicitCommit = true;
                }
                var transaction = (SqliteTransaction)(context.Database.CurrentTransaction == null ?
                                                      connection.BeginTransaction() :
                                                      context.Database.CurrentTransaction.GetUnderlyingTransaction(tableInfo.BulkConfig));

                var command = GetSqliteCommand(context, type, entities, tableInfo, connection, transaction);

                type = tableInfo.HasAbstractList ? entities[0].GetType() : type;
                int rowsCopied = 0;
                foreach (var item in entities)
                {
                    LoadSqliteValues(tableInfo, item, command);
                    command.ExecuteNonQuery();
                    ProgressHelper.SetProgress(ref rowsCopied, entities.Count, tableInfo.BulkConfig, progress);
                }

                if (operationType != OperationType.Delete && tableInfo.BulkConfig.SetOutputIdentity && tableInfo.IdentityColumnName != null)
                {
                    command.CommandText = SqlQueryBuilderSqlite.SelectLastInsertRowId();
                    long   lastRowIdScalar          = (long)command.ExecuteScalar();
                    string identityPropertyName     = tableInfo.IdentityColumnName;
                    var    identityPropertyInteger  = false;
                    var    identityPropertyUnsigned = false;
                    var    identityPropertyByte     = false;
                    var    identityPropertyShort    = false;

                    if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(ulong))
                    {
                        identityPropertyUnsigned = true;
                    }
                    else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(uint))
                    {
                        identityPropertyInteger  = true;
                        identityPropertyUnsigned = true;
                    }
                    else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(int))
                    {
                        identityPropertyInteger = true;
                    }
                    else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(ushort))
                    {
                        identityPropertyShort    = true;
                        identityPropertyUnsigned = true;
                    }
                    else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(short))
                    {
                        identityPropertyShort = true;
                    }
                    else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(byte))
                    {
                        identityPropertyByte     = true;
                        identityPropertyUnsigned = true;
                    }
                    else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(sbyte))
                    {
                        identityPropertyByte = true;
                    }

                    for (int i = entities.Count - 1; i >= 0; i--)
                    {
                        if (identityPropertyByte)
                        {
                            if (identityPropertyUnsigned)
                            {
                                tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (byte)lastRowIdScalar);
                            }
                            else
                            {
                                tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (sbyte)lastRowIdScalar);
                            }
                        }
                        else if (identityPropertyShort)
                        {
                            if (identityPropertyUnsigned)
                            {
                                tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (ushort)lastRowIdScalar);
                            }
                            else
                            {
                                tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (short)lastRowIdScalar);
                            }
                        }
                        else if (identityPropertyInteger)
                        {
                            if (identityPropertyUnsigned)
                            {
                                tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (uint)lastRowIdScalar);
                            }
                            else
                            {
                                tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (int)lastRowIdScalar);
                            }
                        }
                        else
                        {
                            if (identityPropertyUnsigned)
                            {
                                tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (ulong)lastRowIdScalar);
                            }
                            else
                            {
                                tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], lastRowIdScalar);
                            }
                        }

                        lastRowIdScalar--;
                    }
                }
                if (doExplicitCommit)
                {
                    transaction.Commit();
                }
            }
            finally
            {
                context.Database.CloseConnection();
            }
        }
Пример #6
0
        protected async Task MergeAsync <T>(DbContext context, Type type, IList <T> entities, TableInfo tableInfo, OperationType operationType, Action <decimal> progress, CancellationToken cancellationToken, bool isAsync) where T : class
        {
            SqliteConnection connection = isAsync ? await OpenAndGetSqliteConnectionAsync(context, tableInfo.BulkConfig, cancellationToken).ConfigureAwait(false)
                                                        : OpenAndGetSqliteConnection(context, tableInfo.BulkConfig);

            bool doExplicitCommit = false;

            try
            {
                if (context.Database.CurrentTransaction == null)
                {
                    //context.Database.UseTransaction(connection.BeginTransaction());
                    doExplicitCommit = true;
                }
                var dbTransaction = doExplicitCommit ? connection.BeginTransaction()
                                                     : context.Database.CurrentTransaction.GetUnderlyingTransaction(tableInfo.BulkConfig);
                var transaction = (SqliteTransaction)dbTransaction;

                var command = GetSqliteCommand(context, type, entities, tableInfo, connection, transaction);

                type = tableInfo.HasAbstractList ? entities[0].GetType() : type;
                int rowsCopied = 0;

                foreach (var item in entities)
                {
                    LoadSqliteValues(tableInfo, item, command, context);
                    if (isAsync)
                    {
                        await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        command.ExecuteNonQuery();
                    }
                    ProgressHelper.SetProgress(ref rowsCopied, entities.Count, tableInfo.BulkConfig, progress);
                }

                if (operationType == OperationType.Insert && tableInfo.BulkConfig.SetOutputIdentity && tableInfo.IdentityColumnName != null) // For Sqlite Identity can be set by Db only with pure Insert method
                {
                    command.CommandText = SqlQueryBuilderSqlite.SelectLastInsertRowId();

                    object lastRowIdScalar = isAsync ? await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false)
                                                           : command.ExecuteScalar();

                    SetIdentityForOutput(entities, tableInfo, lastRowIdScalar);
                }

                if (doExplicitCommit)
                {
                    transaction.Commit();
                }
            }
            finally
            {
                if (isAsync)
                {
                    await context.Database.CloseConnectionAsync().ConfigureAwait(false);
                }
                else
                {
                    context.Database.CloseConnection();
                }
            }
        }
Пример #7
0
        private List <YoutubeItem> Run()
        {
            List <YoutubeItem> result = new List <YoutubeItem>();
            var youtubeService        = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey          = YouTubeAPIServiceHelper.API_KEY,
                ApplicationName = YouTubeAPIServiceHelper.APP_NAME
            });

            var searchListRequest = youtubeService.Search.List("snippet");

            searchListRequest.Q          = _pattern; // Replace with your search term.
            searchListRequest.MaxResults = 25;

            // Call the search.list method to retrieve results matching the specified query term.
            String obj = null;

            //SearchListResponse searchListResponse = searchListRequest.Execute();
            ProgressHelper.SetProgress("Getting data from Youtube API", 0);
            using (var stream = searchListRequest.ExecuteAsStream())
            {
                using (MemoryStream writeStream = new MemoryStream())
                {
                    byte[] buffer = new byte[256];
                    int    readed = 0;
                    int    index  = 0;
                    while ((readed = stream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        writeStream.Write(buffer, 0, readed);

                        double percent = (double)index / (double)stream.Length * 100.0;
                        ProgressHelper.SetProgress($"Loading data {percent.ToString("0.00")}%", (int)percent);
                    }

                    obj = Encoding.Default.GetString(writeStream.ToArray());
                }
            }

            SearchListResponse returnedItems = Newtonsoft.Json.JsonConvert.DeserializeObject <SearchListResponse>(obj);
            string             baseURI       = @"https://www.youtube.com/watch?v={0}";
            // Add each result to the appropriate list, and then display the lists of
            // matching videos, channels, and playlists.

            int idx = 0;

            foreach (var searchResult in returnedItems.Items)
            {
                double percent = ((double)idx / (double)searchListRequest.MaxResults) * 100.0;
                ProgressHelper.SetProgress($"Getting data {++idx} of {searchListRequest.MaxResults}", (int)percent);

                switch (searchResult.Id.Kind)
                {
                case "youtube#video":
                    result.Add(new YoutubeMovie()
                    {
                        Name   = searchResult.Snippet.Title,
                        ImgSrc = searchResult.Snippet.Thumbnails.Default__.Url,
                        Source = string.Format(baseURI, searchResult.Id)
                    });
                    break;

                case "youtube#channel":

                    result.Add(new YoutubeChanel()
                    {
                        ChanelId = searchResult.Id.ChannelId,
                        Name     = searchResult.Snippet.ChannelTitle,
                        ImgSrc   = searchResult.Snippet.Thumbnails.Default__.Url,
                        Source   = string.Format(baseURI, searchResult.Snippet.ChannelId)
                    });
                    break;

                case "youtube#playlist":

                    result.Add(new YoutubePlaylist()
                    {
                        PlaylistId = searchResult.Id.PlaylistId,
                        Name       = searchResult.Snippet.ChannelTitle,
                        ImgSrc     = searchResult.Snippet.Thumbnails?.Default__?.Url,
                        Source     = string.Format(baseURI, searchResult.Snippet.Title)
                    });
                    break;
                }
            }
            ProgressHelper.SetProgress($"Finished.", 0);
            return(result);
        }