Пример #1
0
        public async Task <IActionResult> AddParserWordsFromRow([FromBody] SubtitleRowDTO rowDto)
        {
            if (!ModelState.IsValid || rowDto == null || string.IsNullOrEmpty(rowDto.Value))
            {
                return(BadRequest(BaseStatusDto.CreateErrorDto("SubtitleRowDTO request object is not correct.")));
            }
            try
            {
                SubtitleRow row = _mapper.Map <SubtitleRow>(rowDto);

                var dataWords = await Task.Run(() => _wordService.AddParserWordsFromRow(row));

                return(Ok(new
                {
                    status = StatusCodes.Status201Created,
                    message = "The new records of the ParserWords table was successfully added.",
                    data = dataWords
                }));
            }
            catch (Exception ex)
            {
                return(BadRequest(new
                {
                    status = StatusCodes.Status400BadRequest,
                    message = ex.Message
                }));
            }
        }
Пример #2
0
        public static SubtitleRow ToSubtitleRow(this SubtitleItem item)
        {
            SubtitleRow result = new SubtitleRow();

            result.StartTime = new TimeSpan(0, 0, 0, 0, item.StartTime);
            result.EndTime   = new TimeSpan(0, 0, 0, 0, item.EndTime);
            result.Value     = String.Join(" ", item.Lines);

            return(result);
        }
Пример #3
0
        public void AddRow(SubtitleRow row)
        {
            if (row == null)
            {
                throw new ArgumentNullException("SubtitleRow object is null.");
            }

            _unitOfWork.SubtitleRows.Create(row);

            _unitOfWork.Save();
        }
Пример #4
0
        public IEnumerable <ParserWordDTO> AddParserWordsFromRow(SubtitleRow row)
        {
            if (row == null || string.IsNullOrEmpty(row.Value))
            {
                return(null);
            }

            string[] words;

            List <ParserWordDTO> wordsDto      = new List <ParserWordDTO>();
            List <ParserWord>    wordsToCreate = new List <ParserWord>();

            if (TryParseWords(row.Value, out words))
            {
                string language      = row.LanguageName;
                int    subtitleRowId = row.Id;

                foreach (string word in words)
                {
                    ParserWord newWord = new ParserWord
                    {
                        Name          = word,
                        LanguageName  = language,
                        SubtitleRowId = subtitleRowId
                    };

                    wordsToCreate.Add(newWord);

                    ParserWordDTO newWordDto = new ParserWordDTO()
                    {
                        Name          = word,
                        LanguageName  = language,
                        SubtitleRowId = subtitleRowId
                    };

                    wordsDto.Add(newWordDto);
                }

                AddRangeParserWords(wordsToCreate);

                IEnumerable <ParserWordDTO> results = (IEnumerable <ParserWordDTO>)wordsDto;

                return(results);
            }

            return(null);
        }
Пример #5
0
        public async Task <List <Episode> > GetSeasonSubtitles(string html)
        {
            var episodes = new List <Episode>();
            var document = await new HtmlParser().ParseAsync(html);

            var table = document.QuerySelector("#season").QuerySelector("table") as IHtmlTableElement;

            var subtitlesRows = new List <SubtitleRow>();

            if (table != null)
            {
                foreach (var row in table.Rows.Skip(1))
                {
                    if (row.Cells.Length > 2)
                    {
                        var subtitleRow = new SubtitleRow();

                        for (var i = 0; i < row.Cells.Length; i++)
                        {
                            switch (i)
                            {
                            case 0:
                                subtitleRow.Season = int.Parse(row.Cells[i].TextContent);
                                break;

                            case 1:
                                subtitleRow.Number = int.Parse(row.Cells[i].TextContent);
                                break;

                            case 2:
                                subtitleRow.Title = row.Cells[i].TextContent;
                                break;

                            case 3:
                                subtitleRow.Language = row.Cells[i].TextContent;
                                break;

                            case 4:
                                subtitleRow.Version = row.Cells[i].TextContent;
                                break;

                            case 5:
                                var state = row.Cells[i].TextContent;
                                if (!state.Contains("%") && state.Contains("Completed"))
                                {
                                    subtitleRow.Completed = true;
                                }
                                break;

                            case 6:
                                subtitleRow.HearingImpaired = row.Cells[i].TextContent.Length > 0;
                                break;

                            case 7:
                                subtitleRow.Corrected = row.Cells[i].TextContent.Length > 0;
                                break;

                            case 8:
                                subtitleRow.HD = row.Cells[i].TextContent.Length > 0;
                                break;

                            case 9:
                                var downloadUri = row.Cells[i].FirstElementChild.Attributes["href"].Value;
                                subtitleRow.EpisodeId =
                                    int.Parse(
                                        downloadUri.Replace("/updated/", "").Replace("/original/", "")
                                        .Split('/')[1]);
                                subtitleRow.DownloadUri = new Uri(downloadUri, UriKind.Relative);
                                break;
                            }
                        }
                        subtitlesRows.Add(subtitleRow);
                    }
                }
            }

            if (subtitlesRows.Any())
            {
                var episodeGroups = subtitlesRows.GroupBy(r => r.EpisodeId).ToList();
                foreach (var episodeGroup in episodeGroups)
                {
                    if (episodeGroup.Any())
                    {
                        var episode = new Episode
                        {
                            Title  = episodeGroup.First().Title,
                            Number = episodeGroup.First().Number,
                            Season = episodeGroup.First().Season,
                            Id     = episodeGroup.First().EpisodeId
                        };

                        foreach (var subtitle in episodeGroup.Select(subtitleRow => new Subtitle
                        {
                            Version = subtitleRow.Version,
                            Corrected = subtitleRow.Corrected,
                            DownloadUri = subtitleRow.DownloadUri,
                            HD = subtitleRow.HD,
                            HearingImpaired = subtitleRow.HearingImpaired,
                            Language = subtitleRow.Language,
                            Completed = subtitleRow.Completed
                        }))
                        {
                            episode.Subtitles.Add(subtitle);
                        }
                        episodes.Add(episode);
                    }
                }
            }
            return(episodes);
        }