Esempio n. 1
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);
            }
        }
Esempio n. 2
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);
            }
        }