예제 #1
0
        private Data.Video.Video GetOrCreateVideo(VideoForUser videoForUser,
                                                  byte[] image,
                                                  int?rating,
                                                  byte videoType,
                                                  StudyLanguageContext c)
        {
            string title    = videoForUser.Title;
            string htmlCode = videoForUser.HtmlCode;

            Data.Video.Video video =
                c.Video.FirstOrDefault(
                    e =>
                    (e.Title == title || e.HtmlCode == htmlCode) && e.LanguageId == _languageId && e.Type == videoType);
            if (video == null)
            {
                video = new Data.Video.Video {
                    IsVisible  = true,
                    Rating     = rating,
                    LanguageId = _languageId,
                    Type       = videoType
                };
                c.Video.Add(video);
            }

            video.Title        = title;
            video.HtmlCode     = htmlCode;
            video.Image        = image;
            video.LastModified = DateTime.Now;
            c.SaveChanges();
            return(video);
        }
예제 #2
0
        public VideoForUser Get(string title)
        {
            VideoForUser videoForUser = Adapter.ReadByContext(c => {
                Data.Video.Video video = GetByTitle(title);
                if (video == null)
                {
                    return(null);
                }

                long videoId = video.Id;
                IOrderedQueryable <VideoSentence> videosQuery = (from vs in c.VideoSentence
                                                                 where vs.VideoId == videoId
                                                                 orderby vs.Order
                                                                 select vs);

                var innerResult = new VideoForUser(video);
                foreach (VideoSentence videoSentence in videosQuery.ToList())
                {
                    innerResult.Sentences.Add(new Tuple <string, string>(videoSentence.Source, videoSentence.Translation));
                }
                return(innerResult);
            });

            return(videoForUser);
        }
예제 #3
0
        public DocumentationGenerator Download(DocumentType docType,
                                               string fileName,
                                               VideoForUser video)
        {
            DocumentationGenerator documentGenerator = DocumentationGenerator.Create(_domain, _fontPath, docType, fileName);

            var fields = new List <string> {
                "Отрывок текста"
            };

            if (video.HasAnyTranslation)
            {
                fields.Add("Перевод");
            }
            var tableData = new TableData(fields.Count, false);

            tableData.AddHeader(fields);
            foreach (var sentence in video.Sentences)
            {
                fields = new List <string> {
                    sentence.Item1
                };
                if (video.HasTranslation(sentence))
                {
                    fields.Add(sentence.Item2);
                }
                tableData.AddRow(fields);
            }

            documentGenerator.AddHeader(string.Format("Текст из видео «{0}»", video.Title));
            documentGenerator.AddTable(tableData);

            return(documentGenerator);
        }
예제 #4
0
        private static VideoForUser GetVideoForUser(string group)
        {
            if (string.IsNullOrEmpty(group))
            {
                return(null);
            }
            IVideosQuery videosQuery = GetVideosQuery();
            VideoForUser result      = videosQuery.Get(group);

            return(result);
        }
예제 #5
0
        public VideoForUser Read()
        {
            string[] lines;
            if (!IsFileValid(out lines))
            {
                return(null);
            }

            string title    = TrimLine(lines[0]);
            string htmlCode = GetHtmlCode(TrimLine(lines[1]));

            if (string.IsNullOrWhiteSpace(title) || string.IsNullOrWhiteSpace(htmlCode))
            {
                Console.WriteLine("Из файла {0} не удалось получить заголовок или код-вставки", _fileName);
                return(null);
            }

            var result = new VideoForUser(title, htmlCode);

            if (lines.Length < 3)
            {
                return(result);
            }

            for (int i = 2; i < lines.Length; i++)
            {
                string line        = lines[i];
                string trimmedLine = TrimLine(line);

                string[] sourceWithTranslation = trimmedLine.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                if (sourceWithTranslation.Length == 0)
                {
                    continue;
                }

                if (sourceWithTranslation.Length > 2)
                {
                    Console.WriteLine("В файле {0} раскололи строку на {1} частей. Строка {2}: {3}", _fileName,
                                      sourceWithTranslation.Length, i, line);
                }

                var sentence = new Tuple <string, string>(sourceWithTranslation[0].Trim(),
                                                          sourceWithTranslation.Length > 1
                                                             ? sourceWithTranslation[1].Trim()
                                                             : null);
                result.Sentences.Add(sentence);
            }
            return(result);
        }
예제 #6
0
        public ActionResult Detail(string group)
        {
            if (WebSettingsConfig.Instance.IsSectionForbidden(SectionId.Video))
            {
                return(RedirectToAction("Index", RouteConfig.MAIN_CONTROLLER_NAME));
            }

            VideoForUser video = GetVideoForUser(group);

            if (video == null)
            {
                return(RedirectToAction("Index", new { type = VideoType.Clip }));
            }
            return(View("../Video/Detail", video));
        }
예제 #7
0
        public VideoForUser ReadSubtitles()
        {
            string[] lines;
            if (!IsFileValid(out lines))
            {
                return(null);
            }

            if (lines.Length > 2)
            {
                Console.WriteLine("В файле {0} уже есть субтитры", _fileName);
                return(null);
            }

            var videoGetter = new YouTubeGetter();

            StringBuilder rows = videoGetter.GetSubtitles(lines[1]);

            if (rows == null)
            {
                Console.WriteLine("Для файла {0} не удалось получить субтитры", _fileName);
            }

            VideoForUser result = null;

            if (rows != null && rows.Length > 0)
            {
                if (!lines[lines.Length - 1].EndsWith(Environment.NewLine))
                {
                    rows.Insert(0, Environment.NewLine);
                }
                try {
                    rows.Length = rows.Length - Environment.NewLine.Length;
                    string text = rows.ToString();
                    File.AppendAllText(_fileName, text);
                    result = Read();
                    Console.WriteLine("Файл {0} успешно обработан", _fileName);
                } catch (Exception e) {
                    Console.WriteLine("Для файла {0} не удалось записать субтитры. Исключение:\r\n{1}",
                                      _fileName, e);
                }
            }
            return(result);
        }
예제 #8
0
        public ActionResult Download(string group, DocumentType type)
        {
            if (WebSettingsConfig.Instance.IsSectionForbidden(SectionId.Video))
            {
                return(RedirectToAction("Index", RouteConfig.MAIN_CONTROLLER_NAME));
            }

            VideoForUser video = GetVideoForUser(group);

            if (video == null)
            {
                return(RedirectToAction("Index", new { type = VideoType.Clip }));
            }

            string fileName   = string.Format("Текст из видео {0}", video.Title.ToLowerInvariant());
            var    downloader = new VideoTextDownloader(WebSettingsConfig.Instance.DomainWithProtocol,
                                                        CommonConstants.GetFontPath(Server));
            var documentGenerator = downloader.Download(type, fileName, video);

            return(File(documentGenerator.Generate(), documentGenerator.ContentType, documentGenerator.FileName));
        }
예제 #9
0
        public VideoForUser GetOrCreate(VideoType type, VideoForUser videoForUser, byte[] image, int?rating)
        {
            if (EnumValidator.IsInvalid(type) || videoForUser == null || string.IsNullOrWhiteSpace(videoForUser.Title) ||
                string.IsNullOrWhiteSpace(videoForUser.HtmlCode)
                /*|| EnumerableValidator.IsNullOrEmpty(videoForUser.Sentences)*/)
            {
                return(null);
            }

            byte         parsedVideoType = (byte)type;
            VideoForUser result          = null;

            Adapter.ActionByContext(c => {
                Data.Video.Video video = GetOrCreateVideo(videoForUser, image, rating, parsedVideoType, c);
                long videoId           = video.Id;
                if (IdValidator.IsInvalid(videoId))
                {
                    return;
                }

                DeleteVideoSentences(c, videoId);

                result    = new VideoForUser(video);
                int order = 1;
                foreach (var sentence in videoForUser.Sentences)
                {
                    var videoSentence = new VideoSentence {
                        VideoId     = videoId,
                        Source      = sentence.Item1,
                        Translation = sentence.Item2,
                        Order       = order++
                    };
                    c.VideoSentence.Add(videoSentence);
                }
                c.SaveChanges();
            });

            return(result);
        }
예제 #10
0
        public void Fill(VideoForUser videoForUser)
        {
            long languageId =
                new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown).GetByShortName(
                    LanguageShortName.En).Id;

            string title    = videoForUser.Title;
            string htmlCode = videoForUser.HtmlCode;

            byte[] image = GetImage(title, htmlCode);

            var          videosQuery = new VideosQuery(languageId);
            VideoForUser result      = videosQuery.GetOrCreate(VideoType.Clip, videoForUser, image, null);

            if (result != null)
            {
                Console.WriteLine("Видео \"{0}\" успешно добавлено", title);
            }
            else
            {
                Console.WriteLine("Не удалось добавить видео \"{0}\"!!!", title);
            }
        }
예제 #11
0
        public void Process()
        {
            long languageId =
                new LanguagesQuery(LanguageShortName.Unknown, LanguageShortName.Unknown).GetByShortName(
                    _shortName).Id;

            const int MAX_DOMAIN_TO_PROCESS = 10;

            List <Tuple <string, List <string> > > linksByDomains = _linksHelper.Analyze();

            foreach (var tuple in linksByDomains.Take(MAX_DOMAIN_TO_PROCESS))
            {
                IVideoDataGetter videoDataGetter = GetVideoDataGetterByDomain(tuple.Item1);
                if (videoDataGetter == null)
                {
                    continue;
                }

                int           failCount    = 0;
                int           successCount = 0;
                List <string> links        = tuple.Item2;
                foreach (string link in links)
                {
                    string encodedLink = FileHelper.EncodeFileName(link);

                    IVideoData videoData = GetVideoDataFromCache(videoDataGetter, encodedLink);
                    if (videoData == null)
                    {
                        //для этой ссылки данных нет в кэше - получить их от Getter'а
                        videoData = videoDataGetter.GetVideoData(link);
                    }

                    if (videoData == null)
                    {
                        Console.WriteLine("Для ссылки {0} НЕ удалось получить данные!", link);
                        failCount++;
                        continue;
                    }

                    SaveVideoDataToCache(videoDataGetter, encodedLink, videoData);

                    if (videoDataGetter.IsInvalid(videoData, _shortName))
                    {
                        Console.WriteLine("Для ссылки {0} данные некорректны!", link);
                        failCount++;
                        continue;
                    }

                    var videosQuery  = new VideosQuery(languageId);
                    var videoForUser = new VideoForUser(videoData.Title, videoData.HtmlCode);
                    //TODO: поиск дубликатов видео

                    //TODO: сохранять дополнительную информацию в БД
                    VideoForUser result = videosQuery.GetOrCreate(VideoType.Movie, videoForUser, videoData.ThumnailImage, videoData.Rating);
                    if (result != null)
                    {
                        successCount++;
                    }
                    else
                    {
                        Console.WriteLine("Не удалось добавить видео \"{0}\"!!!", videoData.Title);
                    }
                }
                Console.WriteLine("Обработан домен {0} из {1} ссылок успешно сохранены {2}, не удалось сохранить {3}",
                                  tuple.Item1, links.Count, successCount, failCount);
            }
        }
예제 #12
0
        private DocumentationGenerator GetGenerator(AllMaterialsQuery allMaterialsQuery,
                                                    SectionId sectionId,
                                                    long id,
                                                    string title)
        {
            DocumentationGenerator result;

            switch (sectionId)
            {
            case SectionId.GroupByWords:
                var groupWordsDownloader = new GroupDataDownloader(_domain, _fontPath)
                {
                    Header      = string.Format("Слова на тему «{0}»", title),
                    TableHeader = "Слово"
                };

                List <SourceWithTranslation> words = allMaterialsQuery.GetWordsByGroup(id);
                result = groupWordsDownloader.Download(DOCUMENT_TYPE, title, words);
                break;

            case SectionId.GroupByPhrases:
                var groupDataDownloader = new GroupDataDownloader(_domain, _fontPath)
                {
                    Header      = string.Format("Фразы на тему «{0}»", title),
                    TableHeader = "Фраза"
                };

                List <SourceWithTranslation> sentences = allMaterialsQuery.GetSentencesByGroup(id);
                result = groupDataDownloader.Download(DOCUMENT_TYPE, title, sentences);
                break;

            case SectionId.VisualDictionary:
                var visualDictionaryDownloader = new VisualDictionaryDownloader(_domain, _fontPath);

                RepresentationForUser representationForUser = allMaterialsQuery.GetVisualDictionary(title);
                result = visualDictionaryDownloader.Download(DOCUMENT_TYPE, title, representationForUser);
                break;

            case SectionId.FillDifference:
                var comparisonDownloader = new ComparisonDownloader(_domain, _fontPath);

                ComparisonForUser comparisonForUser = allMaterialsQuery.GetComparison(title);
                result = comparisonDownloader.Download(DOCUMENT_TYPE, title, comparisonForUser);
                break;

            case SectionId.Video:
                var videoTextDownloader = new VideoTextDownloader(_domain, _fontPath);

                VideoForUser videoForUser = allMaterialsQuery.GetVideo(title);
                result = videoTextDownloader.Download(DOCUMENT_TYPE, title, videoForUser);
                break;

            case SectionId.PopularWord:
                var popularWordsDownloader = new PopularWordsDownloader(_domain, _fontPath)
                {
                    Header = "Минилекс слов Гуннемарка"
                };

                List <SourceWithTranslation> popularWords = allMaterialsQuery.GetPopularWords();
                result = popularWordsDownloader.Download(DOCUMENT_TYPE, title, popularWords);
                break;

            default:
                result = null;
                break;
            }
            return(result);
        }