Beispiel #1
0
        /// <summary>
        /// Close and delete the trace (needs to be closed before deleting)
        /// NOP if trace wasn't registered
        /// </summary>
        /// <param name="cancel"></param>
        /// <returns></returns>
        public async Task UnregisterTraceAsync(CancellationToken cancel)
        {
            if (_traceId == null)
            {
                Log.Debug("Trace wasn't registered. Nothing to stop.");
                return;
            }

            try
            {
                Log.Debug("Stopping trace with sp_trace_setstatus, status = 0");
                await DbTasks.ControlTraceAsync(_connection, _traceId.Value, 0 /* stop */, cancel);
            }
            catch (Exception ex)
            {
                Log.Warn($"Cannot stop trace. TraceId: {_traceId}", ex);
                throw;
            }

            try
            {
                Log.Debug("Deleting trace with sp_trace_setstatus, status = 2");
                await DbTasks.ControlTraceAsync(_connection, _traceId.Value, 2 /* close and delete */, cancel);
            }
            catch
            {
                Log.Warn($"Cannot delete trace. TraceId: {_traceId}");
                throw;
            }
        }
Beispiel #2
0
        public async Task OpenDbReaderAsync(CancellationToken cancel)
        {
            Log.Debug($"Opening DB reader");

            // setup single DB reader and DB command
            var r = await DbTasks.GetReaderAsync(_connection, _traceId.Value, cancel);

            _dbCommand = r.Item1;
            _dbReader  = r.Item2;
        }
Beispiel #3
0
 public ActionResult AddTask(DoTask account)
 {
     if (ModelState.IsValid)
     {
         using (DbTasks dbt = new DbTasks())
         {
             dbt.TaskAccount.Add(account);
             dbt.SaveChanges();
         }
         ModelState.Clear();
         ViewBag.Message = "Succesfully added task.";
     }
     return(View());
 }
Beispiel #4
0
        public ActionResult DeleteTask(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                using (DbTasks dbt = new DbTasks())
                {
                    string[] ids = formCollection["taskIdDelete"].Split(new char[] { ',' });
                    foreach (string id in ids)
                    {
                        var task = dbt.TaskAccount.Find(int.Parse(id));
                        dbt.TaskAccount.Remove(task);
                        dbt.SaveChanges();
                    }
                }
            }

            return(RedirectToAction("ViewTask"));
        }
Beispiel #5
0
        /// <summary>
        /// Registers trace with the database, opens command and DB data readers.
        /// Setups events/columns to be captured
        /// </summary>
        public async Task RegisterTraceAsync(Dictionary <EventClassType, List <EventColumnType> > registeredEvents, CancellationToken cancel)
        {
            Log.Debug("Creating new trace");
            _traceId = await DbTasks.CreateTraceAsync(_connection, cancel);

            Log.Debug($"Trace ID: {_traceId}");

            // prepare new buffer for receiving data
            _buffer = new ProfilerEvent(EventClassType.Unknown);

            // register each event-class
            foreach (var evClass in registeredEvents.Keys)
            {
                cancel.ThrowIfCancellationRequested();

                // convert all columns for this event-class into a flat array for registration
                var columns = registeredEvents[evClass].Select(x => (int)x).ToArray();

                try
                {
                    Log.Debug($"Registering event ID: {evClass} with {columns.Count()} columns");
                    await DbTasks.SetEventAsync(_connection, cancel, _traceId.Value, (int)evClass, columns);
                }
                catch
                {
                    Log.Warn($"Event registration failed. ID: {evClass}");
                    throw;
                }
            }

            try
            {
                await DbTasks.ControlTraceAsync(_connection, _traceId.Value, 1 /* start */, cancel);
            }
            catch
            {
                Log.Warn("Cannot start registered trace");
                throw;
            }
        }
Beispiel #6
0
        private static void ParseKinogoMovie(ref HttpClient client, string href)
        {
            try
            {
                var document  = client.GetDocument(href, "Windows-1251");
                var movieName = document.QuerySelector("h1").TextContent.Split('(')[0].Trim();
                var nodes     = document.QuerySelector(".fullimg div[id]").ChildNodes;
                var cred      = new Dictionary <INode, string>();

                foreach (var node in nodes)
                {
                    if (node.GetType().FullName == "AngleSharp.Dom.Comment")
                    {
                        continue;
                    }
                    else
                    if (node.GetType().FullName == "AngleSharp.Html.Dom.HtmlBoldElement")
                    {
                        cred.Add(node, "");
                    }
                    else if (!string.IsNullOrWhiteSpace(node.TextContent.Trim()))
                    {
                        if (cred.Count == 0)
                        {
                            cred.Add(node, node.TextContent.Trim());
                        }
                        else
                        {
                            cred[cred.Last().Key] += node.TextContent.Trim();
                        }
                    }
                }

                #region User

                var user = new User()
                {
                    Id       = Guid.NewGuid(),
                    Language = Language.Ru,
                    UserName = "******",
                    Login    = "******",
                    Password = "******"
                };
                DbTasks.AddUsersIntoDb(ref user);

                #endregion

                #region Movie&Description

                var rate = document.QuerySelector("ul[itemprop='rating'] li.current-rating").GetStyle("width");
                var date = DateTime.TryParseExact(cred.FirstOrDefault(x => x.Key.TextContent?.IndexOf("Премьера", StringComparison.Ordinal) > -1).Value, "dd MMMM yyyy", CultureInfo.GetCultureInfo("RU-ru"), DateTimeStyles.AssumeLocal, out var ini);

                var movie = new Movie()
                {
                    Id           = Guid.NewGuid(),
                    OriginalName = movieName,
                    Country      = cred.FirstOrDefault(x => x.Key.TextContent == "Страна:").Value?.Trim() ?? "",
                    Date         = date ? ini : DateTime.Now,
                    Duration     = TimeSpan.Parse(cred.FirstOrDefault(x => x.Key.TextContent == "Продолжительность:").Value?.Trim().Trim('~').Trim() ?? "0"),
                };
                movie.Rates = new List <Rate>()
                {
                    new Rate()
                    {
                        Value = Convert.ToDecimal(new Regex("[a-zA-Z]+").Replace(rate, ""),
                                                  CultureInfo.InvariantCulture) / 20.0M,
                        Id       = Guid.NewGuid(),
                        MovieId  = movie.Id,
                        UserId   = user.Id,
                        RateType = RateType.Kinogo
                    }
                };

                DbTasks.AddMovieAndDescriptionIntoDb(ref movie, ref Movies, new Description()
                {
                    Id              = Guid.NewGuid(),
                    MovieId         = movie.Id,
                    Language        = Language.Ru,
                    MovieName       = movieName,
                    UserId          = user.Id,
                    DescriptionText = cred.First().Value.Trim()
                });

                #endregion

                #region Actors

                DbTasks.AddPersonsIntoDb(cred.FirstOrDefault(x => x.Key.TextContent == "В ролях:").Value?.Split(','),
                                         movie.Id, JobType.Actor);

                #endregion

                #region Scenarists

                //DbTasks.AddPersonsIntoDb(
                //    cred.FirstOrDefault(x => x.Key.TextContent == "Сценарий:").Value?.Split(','), movie.Id, JobType.Scenarist);

                #endregion

                #region Directors

                DbTasks.AddPersonsIntoDb(cred.FirstOrDefault(x => x.Key.TextContent == "Режиссер:").Value?.Split(','),
                                         movie.Id, JobType.Director);

                #endregion

                #region Tags

                DbTasks.AddTagsIntoDb(cred.FirstOrDefault(x => x.Key.TextContent == "Жанр:").Value?.Split(','), Language.Ru, movie.Id);

                #endregion

                #region Pictures
                var    items = new List <Item>();
                string videoHref;
                var    pictureHrefs = document.QuerySelectorAll(".screens a").Select(x => x.GetAttribute("href"));
                var    pictures     = pictureHrefs.Select((pictureHref, index) => new Picture()
                {
                    Id         = Guid.NewGuid(),
                    MovieId    = movie.Id,
                    Name       = pictureHref.Split("/").Last(),
                    Href       = pictureHref,
                    Sort       = index,
                    SourceType = SourceType.Kinogo,
                    Type       = PictureType.Preview
                }).ToList();


                var bannerHref = document.QuerySelector($"#news-id-{href.Split("-").First().Split("/").Last()} a").GetAttribute("href");
                var banner     = new Picture()
                {
                    Id         = Guid.NewGuid(),
                    MovieId    = movie.Id,
                    Name       = bannerHref.Split("/").Last(),
                    Href       = bannerHref,
                    Sort       = 1,
                    SourceType = SourceType.Kinogo,
                    Type       = PictureType.Banner
                };

                pictures.Add(banner);

                var boxes  = document.QuerySelectorAll(".box");
                var script = boxes.First()?.Children?.First()?.InnerHtml ?? "";
                if (!string.IsNullOrWhiteSpace(script))
                {
                    if (script.Length < 30)
                    {
                        //sdafsdf
                    }
                    var text    = script.Substring(30, script.Length - 4 - 30);
                    var encoded = Encoding.UTF8.GetString(Convert.FromBase64String(text));

                    var poster = encoded.InnerString("poster=", "\"");
                    if (!string.IsNullOrWhiteSpace(poster))
                    {
                        pictures.Add(new Picture()
                        {
                            Id         = Guid.NewGuid(),
                            MovieId    = movie.Id,
                            Name       = poster.Split("/").Last(),
                            Href       = poster,
                            Sort       = 1,
                            SourceType = SourceType.Kinogo,
                            Type       = PictureType.Poster
                        });
                    }

                    videoHref = encoded.InnerString("file=", "&amp;");
                    if (string.IsNullOrWhiteSpace(videoHref))
                    {
                        videoHref = encoded.InnerString("pl=", "&amp;");
                    }
                    if (!string.IsNullOrWhiteSpace(videoHref))
                    {
                        items.Add(new Item()
                        {
                            Id         = Guid.NewGuid(),
                            Value      = videoHref,
                            ItemType   = ItemType.Link,
                            MovieId    = movie.Id,
                            SourceType = SourceType.Kinogo
                        });
                    }
                }
                else
                {
                    var encoded = boxes.First()?.Children.Last().TextContent;

                    var poster = encoded.InnerString("\"poster\":\"", "\"");
                    if (!string.IsNullOrWhiteSpace(poster))
                    {
                        pictures.Add(new Picture()
                        {
                            Id         = Guid.NewGuid(),
                            MovieId    = movie.Id,
                            Name       = poster.Split("/").Last(),
                            Href       = poster,
                            Sort       = 1,
                            SourceType = SourceType.Kinogo,
                            Type       = PictureType.Poster
                        });
                    }

                    videoHref = encoded.InnerString("\"file\":\"", "\"");
                    if (!string.IsNullOrWhiteSpace(videoHref))
                    {
                        items.Add(new Item()
                        {
                            Id         = Guid.NewGuid(),
                            Value      = videoHref,
                            ItemType   = ItemType.Link,
                            MovieId    = movie.Id,
                            SourceType = SourceType.Kinogo
                        });
                    }

                    videoHref = encoded.InnerString("file:\"", "\"");
                    items.Add(new Item()
                    {
                        Id         = Guid.NewGuid(),
                        Value      = videoHref,
                        ItemType   = ItemType.Link,
                        MovieId    = movie.Id,
                        SourceType = SourceType.Kinogo
                    });
                }

                var content = boxes.Last().Children.Last().TextContent;
                videoHref = content.InnerString("file:\"", "\"");
                if (!string.IsNullOrWhiteSpace(videoHref))
                {
                    items.Add(new Item()
                    {
                        Id         = Guid.NewGuid(),
                        Value      = videoHref,
                        ItemType   = ItemType.Trailer,
                        MovieId    = movie.Id,
                        SourceType = SourceType.Kinogo
                    });
                }


                DbTasks.AddPictures(pictures);
                DbTasks.AddItems(items);

                #endregion
            }
            catch (Exception e)
            {
                // ignored
            }
        }
Beispiel #7
0
        private static void ParseMultiplexMovie(ref HttpClient client, string href)
        {
            try
            {
                var document  = client.GetDocument(href);
                var movieName = document.QuerySelector("h1").TextContent;
                var results   = document
                                .QuerySelectorAll("ul.movie_credentials li")
                                .ToDictionary(elem => elem.FirstElementChild.TextContent);

                #region User

                var user = new User()
                {
                    Id       = Guid.NewGuid(),
                    Language = Language.Ru,
                    UserName = "******",
                    Login    = "******",
                    Password = "******"
                };
                DbTasks.AddUsersIntoDb(ref user);


                #endregion

                #region Movie&Description

                var movie = new Movie()
                {
                    Id           = Guid.NewGuid(),
                    OriginalName =
                        results.FirstOrDefault(cred => cred.Key == "Оригинальное название:").Value
                        ?.LastElementChild?.TextContent.Trim() ?? movieName,
                    Country = results.FirstOrDefault(cred => cred.Key == "Производство:").Value
                              ?.LastElementChild?.TextContent.Trim() ?? "",
                    Date = DateTime.Parse(
                        results.FirstOrDefault(cred => cred.Key == "Дата выхода:").Value?.LastElementChild
                        ?.TextContent.Trim() ?? DateTime.Now.ToString("d")),
                    Duration = TimeSpan.Parse(results.FirstOrDefault(cred => cred.Key == "Длительность:").Value
                                              ?.LastElementChild?.TextContent.Trim() ?? "0"),
                };
                movie.Rates = results.Where(y => y.Key == "Рейтинг Imdb:" || y.Key == "Рейтинг Kinopoisk:").Select(cr =>
                                                                                                                   new Rate()
                {
                    Id       = Guid.NewGuid(),
                    MovieId  = movie.Id,
                    Value    = Convert.ToDecimal(cr.Value),
                    UserId   = user.Id,
                    RateType = cr.Key.Contains("Kinopoisk") ? RateType.Kinopoisk : cr.Key.Contains("Imdb") ? RateType.Imdb : RateType.Multiplex
                }
                                                                                                                   ).ToList();

                DbTasks.AddMovieAndDescriptionIntoDb(ref movie, ref Movies, new Description()
                {
                    Id              = Guid.NewGuid(),
                    MovieId         = movie.Id,
                    Language        = Language.Ru,
                    MovieName       = movieName,
                    UserId          = user.Id,
                    DescriptionText = document.QuerySelector(".movie_description").TextContent
                });

                #endregion

                #region Actors

                DbTasks.AddPersonsIntoDb(
                    results.FirstOrDefault(cred => cred.Key == "В главных ролях:").Value?.LastElementChild?.TextContent
                    .Split(','), movie.Id, JobType.Actor);

                #endregion

                #region Scenarists

                DbTasks.AddPersonsIntoDb(
                    results.FirstOrDefault(cred => cred.Key == "Сценарий:").Value?.LastElementChild?.TextContent
                    .Split(','), movie.Id, JobType.Scenarist);

                #endregion

                #region Directors

                DbTasks.AddPersonsIntoDb(
                    results.FirstOrDefault(cred => cred.Key == "Режиссер:").Value?.LastElementChild?.TextContent
                    .Split(','), movie.Id, JobType.Director);

                #endregion

                #region Tags

                DbTasks.AddTagsIntoDb(
                    results.FirstOrDefault(cred => cred.Key == "Жанр:").Value?.LastElementChild.Children
                    .Select(item => item.GetAttribute("href").Split('/').Last()), Language.En, movie.Id);

                DbTasks.AddTagsIntoDb(
                    results.FirstOrDefault(cred => cred.Key == "Жанр:").Value?.LastElementChild.Children
                    .Select(item => item.TextContent), Language.Ru, movie.Id);

                #endregion

                #region Pictures

                var pictureHref = document.QuerySelector(".poster")?.GetAttribute("src") ?? "";
                if (!string.IsNullOrWhiteSpace(pictureHref))
                {
                    pictureHref = client.BaseAddress.AbsoluteUri + pictureHref;
                }

                var picture = new Picture()
                {
                    Id         = Guid.NewGuid(),
                    MovieId    = movie.Id,
                    Name       = pictureHref.Split("/").Last(),
                    Href       = pictureHref,
                    Sort       = 1,
                    SourceType = SourceType.Multiplex,
                    Type       = PictureType.Poster
                };
                var pictureHref2 = document.QuerySelector("#mvi_poster")?.GetAttribute("value") ?? "";
                if (!string.IsNullOrWhiteSpace(pictureHref2))
                {
                    pictureHref2 = client.BaseAddress.AbsoluteUri + pictureHref2;
                }

                var picture2 = new Picture()
                {
                    Id         = Guid.NewGuid(),
                    MovieId    = movie.Id,
                    Name       = pictureHref2.Split("/").Last(),
                    Href       = pictureHref2,
                    Sort       = 1,
                    SourceType = SourceType.Multiplex,
                    Type       = PictureType.Poster
                };
                var pictureHref3 = document.QuerySelector(".movie_mask")?.GetStyle("background")?.Replace("url('", "").Replace("')", "").Trim() ?? "";
                if (!string.IsNullOrWhiteSpace(pictureHref3))
                {
                    pictureHref3 = client.BaseAddress.AbsoluteUri + pictureHref3;
                }

                var picture3 = new Picture()
                {
                    Id         = Guid.NewGuid(),
                    MovieId    = movie.Id,
                    Name       = pictureHref3.Split("/").Last(),
                    Href       = pictureHref3,
                    Sort       = 1,
                    SourceType = SourceType.Multiplex,
                    Type       = PictureType.BackGroung
                };
                DbTasks.AddPictures(new List <Picture> {
                    picture, picture2, picture3
                });

                var trailer = document.QuerySelector("#desktop_trailer")?.GetAttribute("data-fullyturl") ?? "";
                var review  = document.QuerySelector("#desktop_review")?.GetAttribute("data-fullyturl") ?? "";

                var items = new List <Item>();
                if (!string.IsNullOrWhiteSpace(trailer))
                {
                    items.Add(
                        new Item()
                    {
                        Id         = Guid.NewGuid(),
                        Value      = trailer,
                        ItemType   = ItemType.Trailer,
                        MovieId    = movie.Id,
                        SourceType = SourceType.Multiplex,
                    }
                        );
                }
                if (!string.IsNullOrWhiteSpace(review))
                {
                    items.Add(
                        new Item()
                    {
                        Id         = Guid.NewGuid(),
                        Value      = review,
                        ItemType   = ItemType.Review,
                        MovieId    = movie.Id,
                        SourceType = SourceType.Multiplex,
                    }
                        );
                }
                items.Add(new Item()
                {
                    Id         = Guid.NewGuid(),
                    Value      = href,
                    ItemType   = ItemType.Link,
                    MovieId    = movie.Id,
                    SourceType = SourceType.Multiplex,
                });
                DbTasks.AddItems(items);

                #endregion
            }
            catch (Exception e)
            {
                // ignored
            }
        }