Пример #1
0
        protected override async ValueTask <Nothing> OnInitialize(IModuleProvider moduleProvider)
        {
            await base.OnInitialize(moduleProvider);

            _postLinkRegex  = RegexCache.CreateRegex(PostLinkRegexText);
            _postLinkRegex2 = RegexCache.CreateRegex(PostLinkRegex2Text);
            return(Nothing.Value);
        }
Пример #2
0
 /// <summary>
 /// Получить идентификатор ютубы.
 /// </summary>
 /// <param name="uri">URI.</param>
 /// <returns>Идентификатор.</returns>
 public string GetYoutubeIdFromUri(string uri)
 {
     try
     {
         if (uri == null)
         {
             return(null);
         }
         var youtubeRegex = RegexCache.CreateRegex(YoutubeRegex);
         var match        = youtubeRegex.Match(uri);
         if (match.Success)
         {
             return(match.Groups[1].Value);
         }
         return(null);
     }
     catch
     {
         return(null);
     }
 }
Пример #3
0
        /// <summary>
        /// Распарсить.
        /// </summary>
        /// <param name="source">Источник.</param>
        /// <returns>Результат.</returns>
        public IBoardPost Parse(BoardPost2WithParentLink source)
        {
            var data      = source.Post;
            var link      = source.ParentLink;
            var isPreview = source.EntityType != PostStoreEntityType.Post;

            var ipIdRegex  = RegexCache.CreateRegex(IpIdRegexText);
            var ipIdRegex2 = RegexCache.CreateRegex(IpIdRegexText2);
            var colorRegex = RegexCache.CreateRegex(ColorRegexText);

            var flags = new HashSet <Guid>();

            if (data.Banned != "0" && !string.IsNullOrWhiteSpace(data.Banned))
            {
                flags.Add(BoardPostFlags.Banned);
            }
            if (data.Closed != "0" && !string.IsNullOrWhiteSpace(data.Closed))
            {
                flags.Add(BoardPostFlags.Closed);
            }
            if (data.Sticky != "0" && !string.IsNullOrWhiteSpace(data.Sticky))
            {
                flags.Add(BoardPostFlags.Sticky);
            }
            if (isPreview)
            {
                flags.Add(BoardPostFlags.ThreadPreview);
            }
            if (source.Counter == 1)
            {
                flags.Add(BoardPostFlags.ThreadOpPost);
            }
            if (data.Op != "0" && !string.IsNullOrWhiteSpace(data.Op))
            {
                flags.Add(BoardPostFlags.Op);
            }
            if ("mailto:sage".Equals((data.Email ?? "").Trim(), StringComparison.OrdinalIgnoreCase))
            {
                flags.Add(BoardPostFlags.Sage);
            }
            if (data.Edited != "0" && !string.IsNullOrWhiteSpace(data.Edited))
            {
                flags.Add(BoardPostFlags.IsEdited);
            }
            if ((data.Endless ?? 0) != 0)
            {
                flags.Add(BoardPostFlags.Endless);
            }
            string admName = null;

            if (data.Tripcode != null)
            {
                if (data.Tripcode.StartsWith("!!%") && data.Tripcode.EndsWith("%!!"))
                {
                    if ("!!%mod%!!".Equals(data.Tripcode))
                    {
                        admName = "## Mod ##";
                    }
                    else if ("!!%adm%!!".Equals(data.Tripcode))
                    {
                        admName = "## Abu ##";
                    }
                    else if ("!!%Inquisitor%!!".Equals(data.Tripcode))
                    {
                        admName = "## Applejack ##";
                    }
                    else if ("!!%coder%!!".Equals(data.Tripcode))
                    {
                        admName = "## Кодер ##";
                    }
                    else
                    {
                        admName = data.Tripcode.Replace("!!%", "## ").Replace("%!!", " ##");
                    }
                    flags.Add(BoardPostFlags.AdminTrip);
                }
            }
            var number   = data.Number.TryParseWithDefault();
            var thisLink = new PostLink()
            {
                Engine    = MakabaConstants.MakabaEngineId,
                Board     = link.Board,
                OpPostNum = link.OpPostNum,
                PostNum   = number
            };
            var    postDocument = _htmlParser.ParseHtml(data.Comment ?? "", thisLink);
            var    name         = admName != null && string.IsNullOrWhiteSpace(data.Name) ? admName : WebUtility.HtmlDecode(data.Name ?? string.Empty).Replace("&nbsp;", " ");
            string nameColor    = null;
            Color? color        = null;
            var    match        = ipIdRegex.Match(name);
            var    match2       = ipIdRegex2.Match(name);

            if (match.Success)
            {
                name = match.Groups["id"].Captures[0].Value;
            }
            else if (match2.Success)
            {
                name      = match2.Groups["id"].Captures[0].Value;
                nameColor = match2.Groups["style"].Captures[0].Value;
                var cmatch = colorRegex.Match(nameColor);
                if (cmatch.Success)
                {
                    try
                    {
                        var r = byte.Parse(cmatch.Groups["r"].Captures[0].Value, CultureInfo.InvariantCulture.NumberFormat);
                        var g = byte.Parse(cmatch.Groups["g"].Captures[0].Value, CultureInfo.InvariantCulture.NumberFormat);
                        var b = byte.Parse(cmatch.Groups["b"].Captures[0].Value, CultureInfo.InvariantCulture.NumberFormat);
                        color = Color.FromArgb(255, r, g, b);
                    }
                    catch (Exception)
                    {
                        color = null;
                    }
                }
            }
            else if (name.StartsWith("Аноним ID:", StringComparison.OrdinalIgnoreCase))
            {
                name = name.Remove(0, "Аноним ID:".Length).Trim();
            }
            PosterInfo posterInfo = null;

            if (!string.IsNullOrEmpty(name) || !string.IsNullOrWhiteSpace(data.Tripcode))
            {
                posterInfo = new PosterInfo()
                {
                    Name         = HtmlToText(name ?? ""),
                    NameColor    = color,
                    NameColorStr = nameColor,
                    Tripcode     = data.Tripcode
                };
            }
            var           iconAndFlag = ParseFlagAndIcon(data.Icon);
            BoardPostTags tags        = null;

            if (!string.IsNullOrWhiteSpace(data.Tags))
            {
                tags = new BoardPostTags()
                {
                    TagStr = data.Tags,
                    Tags   = new List <string>()
                    {
                        data.Tags
                    }
                };
            }
            BoardPostLikes likes = null;

            if (data.Likes != null || data.Dislikes != null)
            {
                likes = new BoardPostLikes()
                {
                    Likes    = data.Likes ?? 0,
                    Dislikes = data.Dislikes ?? 0
                };
            }
            Core.Models.Posts.BoardPost result;
            if (source.EntityType == PostStoreEntityType.CatalogPost)
            {
                result = new CatalogBoardPost()
                {
                    OnPageSequence = source.Counter
                };
            }
            else
            {
                result = new Core.Models.Posts.BoardPost();
            }
            result.Link              = thisLink;
            result.Comment           = postDocument;
            result.ParentLink        = link;
            result.Subject           = WebUtility.HtmlDecode(data.Subject ?? string.Empty);
            result.BoardSpecificDate = data.Date;
            result.Date              = DatesHelper.FromUnixTime(data.Timestamp.TryParseWithDefault());
            result.Flags             = flags.ToList();
            result.Quotes            = new List <ILink>();
            result.Hash              = data.Md5;
            result.Email             = data.Email;
            result.MediaFiles        = new List <IPostMedia>();
            result.Counter           = source.Counter;
            result.Poster            = posterInfo;
            result.Icon              = iconAndFlag.Icon;
            result.Country           = iconAndFlag.Country;
            result.Tags              = tags;
            result.UniqueId          = Guid.NewGuid().ToString();
            result.Likes             = likes;
            result.LoadedTime        = source.LoadedTime;
            result.OnServerCounter   = source.Post.CountNumber;
            if (data.Files != null)
            {
                foreach (var f in data.Files)
                {
                    BoardLinkBase mediaLink, tnLink;
                    if (IsBoardLink(f.Path, link.Board))
                    {
                        mediaLink = new BoardMediaLink()
                        {
                            Engine = MakabaConstants.MakabaEngineId,
                            Board  = link.Board,
                            Uri    = RemoveBoardFromLink(f.Path),
                        };
                        tnLink = new BoardMediaLink()
                        {
                            Engine = MakabaConstants.MakabaEngineId,
                            Board  = link.Board,
                            Uri    = RemoveBoardFromLink(f.Thumbnail),
                        };
                    }
                    else
                    {
                        mediaLink = new EngineMediaLink()
                        {
                            Engine = MakabaConstants.MakabaEngineId,
                            Uri    = f.Path,
                        };
                        tnLink = new EngineMediaLink()
                        {
                            Engine = MakabaConstants.MakabaEngineId,
                            Uri    = f.Thumbnail,
                        };
                    }
                    var media = new PostMediaWithThumbnail()
                    {
                        MediaLink   = mediaLink,
                        FileSize    = (ulong)(f.Size * 1024),
                        Height      = f.Heigth,
                        Width       = f.Width,
                        Name        = f.Name,
                        MediaType   = f.Type == MakabaMediaTypes.Webm ? PostMediaTypes.WebmVideo : PostMediaTypes.Image,
                        DisplayName = f.DisplayName,
                        FullName    = f.FullName,
                        Nsfw        = f.NotSafeForWork != 0,
                        Hash        = f.Md5,
                        Duration    = f.Duration,
                        Thumbnail   = new PostMediaWithSize()
                        {
                            MediaLink = tnLink,
                            Height    = f.TnHeight,
                            Width     = f.TnWidth,
                            FileSize  = null,
                            MediaType = PostMediaTypes.Image
                        },
                    };
                    result.MediaFiles.Add(media);
                }
            }
            if (source.Counter == 1 && string.IsNullOrWhiteSpace(result.Subject))
            {
                try
                {
                    var lines = result.Comment.ToPlainText();
                    if (lines.Count > 0)
                    {
                        var s = lines.FirstOrDefault(l => !string.IsNullOrWhiteSpace(l));
                        if (s != null)
                        {
                            if (s.Length >= 50)
                            {
                                s = s.Substring(0, 50 - 3) + "...";
                            }
                            result.Subject = s;
                        }
                    }
                }
                catch
                {
                }
            }
            return(result);
        }