public bool VerifyToken()
        {
            var db = new MiscDataContext(DbConnectionString.Value);

            return ((from t in db.FansiteApiTokens
                    where t.TokenId == this.TokenId
                    && !t.Disabled
                    select t).Any());
        }
Beispiel #2
0
        public TopicDo ApiGetTopic(int id, string token)
        {
            ApiTokenVerifier tokenVerifier = new ApiTokenVerifier(token);
            bool hasValidToken = tokenVerifier.VerifyToken();

            if (hasValidToken)
            {
                MiscDataContext db = new MiscDataContext(DbConnectionString.Value);

                FansiteApiToken fsToken = (from t in db.FansiteApiTokens
                                           where t.TokenId == tokenVerifier.TokenId
                                           && !t.Disabled
                                           select t).Single();

                if (fsToken.LastResetDate != null)
                {
                    TimeSpan diff = DateTime.UtcNow - fsToken.LastResetDate.Value;
                    if (diff.TotalHours >= 24)
                    {
                        fsToken.RequestCount = 0;
                        fsToken.LastResetDate = DateTime.UtcNow;
                    }
                }
                else
                {
                    fsToken.RequestCount = 0;
                    fsToken.LastResetDate = DateTime.UtcNow;
                }
                if (fsToken.RequestCount < fsToken.RequestLimit)
                {
                    fsToken.RequestCount++;
                    db.SubmitChanges();

                    TopicDo topic = GetTopic(id);
                    return topic;
                }
                else
                {
                    throw new BpApiException(ApiStatusCode.Forbidden);
                }

            }
            else
            {
                throw new BpApiException(ApiStatusCode.Forbidden);
            }
        }
Beispiel #3
0
        public ArticleDo GetArticle(string linkName)
        {
            ArticleDo articleObj = new ArticleDo();
            var db = new MiscDataContext(DbConnectionString.Value);

            Article art = null;
            try
            {
                art = (from a in db.Articles
                       where a.LinkName == linkName.ToLower()
                       select a).Single();
            }
            catch (InvalidOperationException)
            {
                return null;
            }

            articleObj.ArticleContent = art.ArticleContent;
            articleObj.ArticleHeading = art.ArticleTitle;

            return articleObj;
        }
Beispiel #4
0
        public List<ApiTopicDo> ApiGetTopics(string game, string region, string lang, string token)
        {
            foreach (var ch in game)
            {
                if (char.IsPunctuation(ch))
                {
                    throw new BpApiException(ApiStatusCode.BadRequest);
                }
            }
            foreach (var ch in region)
            {
                if (char.IsPunctuation(ch))
                {
                    throw new BpApiException(ApiStatusCode.BadRequest);
                }
            }
            foreach (var ch in lang)
            {
                if (char.IsPunctuation(ch))
                {
                    throw new BpApiException(ApiStatusCode.BadRequest);
                }
            }

            game = game.ToLower().Trim();
            region = region.ToLower().Trim();
            lang = lang.ToLower().Trim();

            ApiTokenVerifier tokenVerifier = new ApiTokenVerifier(token);
            bool hasValidToken = tokenVerifier.VerifyToken();

            if (hasValidToken)
            {
                using (MiscDataContext db = new MiscDataContext(DbConnectionString.Value))
                {
                    FansiteApiToken fsToken = (from t in db.FansiteApiTokens
                                               where t.TokenId == tokenVerifier.TokenId
                                               && !t.Disabled
                                               select t).Single();

                    if (fsToken.LastResetDate != null)
                    {
                        TimeSpan diff = DateTime.UtcNow - fsToken.LastResetDate.Value;
                        if (diff.TotalHours >= 24)
                        {
                            fsToken.RequestCount = 0;
                            fsToken.LastResetDate = DateTime.UtcNow;
                        }
                    }
                    else
                    {
                        fsToken.RequestCount = 0;
                        fsToken.LastResetDate = DateTime.UtcNow;
                    }
                    if (fsToken.RequestCount < fsToken.RequestLimit)
                    {
                        List<ApiTopicDo> topics = new List<ApiTopicDo>();

                        IQueryable<ApiGetForumTopic> queriableTopics = null;

                        if (!String.IsNullOrEmpty(region) && !String.IsNullOrWhiteSpace(region) && region != "any")
                        {
                            queriableTopics = from t in db.ApiGetForumTopics
                                              where t.RegionAbbreviation == region.ToUpper()
                                              && t.LanguageAbbreviation == lang.ToUpper()
                                              orderby t.LastPostDate descending
                                              select t;
                        }
                        else
                        {
                            queriableTopics = from t in db.ApiGetForumTopics
                                              where t.LanguageAbbreviation == lang.ToUpper()
                                              orderby t.LastPostDate descending
                                              select t;
                        }

                        if (!String.IsNullOrEmpty(game) && !String.IsNullOrWhiteSpace(game) && game != "any")
                        {
                            queriableTopics = from t in queriableTopics
                                              where t.GameAbbreviation == game.ToUpper()
                                              orderby t.LastPostDate descending
                                              select t;
                        }

                        queriableTopics = queriableTopics.Take(10);

                        List<ApiGetForumTopic> results = queriableTopics.ToList();

                        foreach (var result in results)
                        {
                            ApiTopicDo topic = new ApiTopicDo
                            {
                                TopicId = result.Id,
                                SubForumId = result.ForumBoardId,
                                SubForumBlizzardId = result.ForumBoardNo,
                                Game = result.GameAbbreviation.ToLower(),
                                Region = result.RegionAbbreviation.ToLower(),
                                Language = result.LanguageAbbreviation.ToLower(),
                                SubForumName = result.BoardName,
                                BlizzardThreadNumber = result.ThreadNumber,
                                TopicTitle = result.ThreadTitle,
                                AuthorOfThread = result.AuthorOfThread,
                                FullDateTimeUtc = result.LastPostDate,
                                UpdateStamp = result.LastModifiedStamp
                            };

                            topics.Add(topic);
                        }

                        return topics;

                    }
                    else
                    {
                        throw new BpApiException(ApiStatusCode.Forbidden);
                    }
                }
            }
            else
            {
                throw new BpApiException(ApiStatusCode.Forbidden);
            }
        }
Beispiel #5
0
        public TopicDo GetTopic(int id)
        {
            TopicDo topic = new TopicDo();
            var topicsDbCont = new ForumBlogsDataContext(DbConnectionString.Value);
            var miscDbCont = new MiscDataContext(DbConnectionString.Value);

            Topic dbTopic = null;
            try
            {
                dbTopic = (from t in topicsDbCont.Topics
                           where t.Id == id
                           select t).Single();
            }
            catch (InvalidOperationException)
            {
                return null;
            }

            topic.TopicId = dbTopic.Id;
            topic.GameAbbrev = dbTopic.ForumBoard.BlizzArea.Game.GameAbbreviation;
            topic.LanguageAbbrev = dbTopic.ForumBoard.BlizzArea.Language.LanguageAbbreviation;
            topic.RegionAbbrev = dbTopic.ForumBoard.BlizzArea.Region.RegionAbbreviation;
            topic.TopicTitle = dbTopic.ThreadTitle;
            topic.RegionFullName = dbTopic.ForumBoard.BlizzArea.Region.RegionName;
            topic.BlizzTopicId = dbTopic.ThreadNumber;

            IQueryable<Post> posts = null;
            IQueryable<MvpPosterBlacklist> blackList = null;

            posts = from p in topicsDbCont.Posts
                    where p.Topic.Id == id
                    orderby p.Id
                    select p;
            blackList = from b in miscDbCont.MvpPosterBlacklists
                        select b;

            List<Post> lstPosts = posts.ToList();
            List<MvpPosterBlacklist> blacklistedMvps = blackList.ToList();
            List<Post> newLstPosts = new List<Post>();

            foreach (var post in lstPosts)
            {
                try
                {
                    string directPostLink = post.DirectPostLink;
                    if (directPostLink[directPostLink.Count() - 1] == '1' && directPostLink[directPostLink.Count() - 2] == '#') //Always include first post, regardless of the blaclist.
                    {
                        //we need to always include the first post, regardless of whether the MVP is blacklisted or not
                        newLstPosts.Add(post);
                    }
                    else if (post.PosterType == 1)
                    {
                        bool isFound = false;
                        if (blacklistedMvps.Count > 0)
                        {
                            foreach (var mvp in blacklistedMvps)
                            {
                                if (post.PosterName.Contains(mvp.PosterName))
                                    isFound = true;
                            }
                        }
                        if (!isFound)
                        {
                            newLstPosts.Add(post);
                        }
                    }
                    else
                    {
                        newLstPosts.Add(post);
                    }
                }
                catch (ArgumentNullException)
                {
                    newLstPosts.Add(post);
                }
            }

            foreach (var post in newLstPosts)
            {
                PostDo postDomainObj = new PostDo();

                postDomainObj.PostId = post.Id;
                postDomainObj.PostContent = post.PostContent;
                postDomainObj.PosterName = post.PosterName;
                postDomainObj.PosterTitle = post.PosterSpecialTitle;
                postDomainObj.RawDateStr = post.PostDate;
                postDomainObj.SourceUri = post.DirectPostLink;
                postDomainObj.Type = post.PosterType.Value;
                postDomainObj.Avatar = post.AvatarLinkOfPost;

                if (post.PostDateTimeUtc.HasValue)
                    postDomainObj.DtUtc = post.PostDateTimeUtc.Value;

                topic.Posts.Add(postDomainObj);

            }
            //For ShowMvpPosts button.
            topic.HasMvpPosts = (topic.Posts.Where(x => x.Type == 1).Count() > 0 && !(topic.Posts.Where(x => x.Type == 1).Count() == 1 && topic.Posts[0].Type == 1));

            return topic;
        }
Beispiel #6
0
        public List<BlogEntryDo> ApiGetBlogEntries(string game, string region, string lang, string token)
        {
            //Example URL: http://www.blizzposts.com/api/bptopics/?game=wow&region=us&lang=en&token=7af9341c77b3497fb04fd2422ab2b34d

            foreach (var ch in game)
            {
                if (char.IsPunctuation(ch))
                {
                    throw new BpApiException(ApiStatusCode.BadRequest);
                }
            }
            foreach (var ch in region)
            {
                if (char.IsPunctuation(ch))
                {
                    throw new BpApiException(ApiStatusCode.BadRequest);
                }
            }
            foreach (var ch in lang)
            {
                if (char.IsPunctuation(ch))
                {
                    throw new BpApiException(ApiStatusCode.BadRequest);
                }
            }

            game = game.ToLower().Trim();
            region = region.ToLower().Trim();
            lang = lang.ToLower().Trim();

            MiscDataContext db = new MiscDataContext(DbConnectionString.Value);
            bool hasValidToken = false;
            Guid tokenGuid = new Guid();
            try
            {
                tokenGuid = new Guid(token);

                hasValidToken = ((from t in db.FansiteApiTokens
                                  where t.TokenId == tokenGuid
                                  && !t.Disabled
                                  select t).Count() > 0);
            }
            catch (FormatException)
            {
                throw new BpApiException(ApiStatusCode.BadRequest);
            }
            if (hasValidToken)
            {
                FansiteApiToken fsToken = (from t in db.FansiteApiTokens
                                           where t.TokenId == tokenGuid
                                           && !t.Disabled
                                           select t).Single();

                if (fsToken.LastResetDate != null)
                {
                    TimeSpan diff = DateTime.UtcNow - fsToken.LastResetDate.Value;
                    if (diff.TotalHours >= 24)
                    {
                        fsToken.RequestCount = 0;
                        fsToken.LastResetDate = DateTime.UtcNow;
                    }
                }
                else
                {
                    fsToken.RequestCount = 0;
                    fsToken.LastResetDate = DateTime.UtcNow;
                }

                if (fsToken.RequestCount < fsToken.RequestLimit)
                {
                    fsToken.RequestCount++;
                    db.SubmitChanges();

                    IQueryable<ApiGetBlogEntry> queriableBlogEntries = null;

                    if (!String.IsNullOrEmpty(region) && !String.IsNullOrWhiteSpace(region) && region != "any")
                    {
                        queriableBlogEntries = (from b in db.ApiGetBlogEntries
                                                where b.RegionAbbreviation == region.ToUpper()
                                                && b.LanguageAbbreviation == lang.ToUpper()
                                                orderby b.BlogDate descending
                                                select b);
                    }
                    else
                    {
                        queriableBlogEntries = from b in db.ApiGetBlogEntries
                                               where b.LanguageAbbreviation == lang.ToUpper()
                                               orderby b.BlogDate descending
                                               select b;
                    }
                    if (!String.IsNullOrEmpty(game) && !String.IsNullOrWhiteSpace(game) && game != "any")
                    {
                        queriableBlogEntries = from b in queriableBlogEntries
                                               where b.GameAbbreviation == game.ToUpper()
                                               && b.LanguageAbbreviation == lang.ToUpper()
                                               orderby b.BlogDate descending
                                               select b;
                    }

                    queriableBlogEntries = queriableBlogEntries.Take(10);

                    List<ApiGetBlogEntry> blogEntries = queriableBlogEntries.ToList();
                    List<BlogEntryDo> bpBlogEntries = new List<BlogEntryDo>();

                    foreach (var entry in blogEntries)
                    {
                        BlogEntryDo bpBlogEntry = new BlogEntryDo
                        {
                            GameAbbrev = entry.GameAbbreviation.ToLower(),
                            RegionAbbrev = entry.RegionAbbreviation.ToLower(),
                            LanguageAbbrev = entry.LanguageAbbreviation.ToLower(),
                            Id = entry.Id,
                            Title = entry.BlogTitle,
                            AnchoredLink = entry.BlogDirectLink,
                            BlogContent = entry.BlogContent.Replace("\n", " ").Replace("\r", " "),
                            EntryNumber = entry.EntryNumber,
                            BlogPostDate = entry.BlogDate
                        };

                        //Clean direct link:
                        bpBlogEntry.AnchoredLink = bpBlogEntry.AnchoredLink.Remove(0, bpBlogEntry.AnchoredLink.IndexOf("http"));
                        bpBlogEntry.AnchoredLink = bpBlogEntry.AnchoredLink.Remove(bpBlogEntry.AnchoredLink.IndexOf("\">"));

                        bpBlogEntries.Add(bpBlogEntry);
                    }

                    return bpBlogEntries;
                }
                else
                {
                    throw new BpApiException(ApiStatusCode.Forbidden);
                }
            }
            else
            {
                throw new BpApiException(ApiStatusCode.Forbidden);
            }
        }