Example #1
0
        public async Task <object> CheckForUpdates(dynamic input)
        {
            List <int> top30Ids    = GetTop100(30);
            List <int> currentList = new List <int>();

            object[] idList = (object[])input.idList;
            foreach (object id in idList)
            {
                currentList.Add(Convert.ToInt32((string)id));
            }
            List <int>      newIds     = top30Ids.Except(currentList).ToList();
            List <StoryObj> newStories = new List <StoryObj>();

            foreach (int id in newIds)
            {
                try
                {
                    FullStory fs           = FullStoryFactory.GetFullStory(id);
                    int       commentCount = fs.TotalComments;
                    string    commentUrl   = "https://news.ycombinator.com/item?id=" + id;
                    StoryObj  so           = new StoryObj()
                    {
                        StoryId = id, StoryTitle = fs.title, Author = fs.author, StoryText = fs.text, Url = fs.url ?? commentUrl, CommentUrl = commentUrl, StoryComments = commentCount
                    };
                    newStories.Add(so);
                }
                catch (Exception ex)
                {
                    //Sometimes algolia api throws an error, just move
                    //on to the next item
                    Console.WriteLine(ex.ToString());
                }
            }
            return(newStories);
        }
Example #2
0
 public async Task <object> GetArchivedStories(int id)
 {
     try
     {
         var             directory    = new DirectoryInfo("archive");
         var             files        = directory.GetFiles().OrderByDescending(x => x.LastWriteTime).Where(y => y.Extension == ".json").ToList();
         List <StoryObj> storyObjList = new List <StoryObj>();
         foreach (var file in files)
         {
             string    fileText   = File.ReadAllText(file.FullName);
             FullStory fs         = JsonConvert.DeserializeObject <FullStory>(fileText);
             string    commentUrl = "https://news.ycombinator.com/item?id=" + fs.id;
             StoryObj  so         = new StoryObj()
             {
                 StoryId = fs.id, StoryTitle = fs.title, Author = fs.author, StoryText = fs.text, Url = fs.url ?? commentUrl, CommentUrl = commentUrl, StoryComments = fs.TotalComments, ArchivedOn = fs.ArchivedOn
             };
             storyObjList.Add(so);
         }
         return(storyObjList.OrderByDescending(x => x.ArchivedOn).ToList());
     }
     catch (Exception ex)
     {
         return(ex.ToString());
     }
 }
Example #3
0
        public string GetCommentTreeString()
        {
            FullStory fs = this;
            Dictionary <string, int> topNWordsRoot = fs.GetTopNWordsDictionary(10);
            TagCloudNode             tgnRoot       = new TagCloudNode();

            tgnRoot.id    = fs.id;
            tgnRoot.key   = fs.id;
            tgnRoot.text  = fs.title;
            tgnRoot.title = fs.title;
            GetTagCloudFromDictionary(topNWordsRoot);
            tgnRoot.children = new List <TagCloudNode>();
            //sort like HN
            //fs.children =
            //    fs.children.OrderByDescending(x => (x.score - 1)/Math.Pow((DateTime.Now.Subtract(x.created_at).TotalHours+2),1.5))
            //        .ToList();
            fs.children = fs.children.OrderByDescending(x => x.created_at).ToList();
            foreach (children child in fs.children)
            {
                if (!string.IsNullOrWhiteSpace(child.SubtreeText))
                {
                    TagCloudNode tgnChild = GetCommentTreeString(child);
                    tgnRoot.children.Add(tgnChild);
                }
            }
            return(JsonConvert.SerializeObject(tgnRoot));
        }
Example #4
0
        public async Task <object> GetFullStory(int storyid)
        {
            FullStory fs           = FullStoryFactory.GetFullStory(storyid);
            string    json         = fs.GetCommentTreeString();
            string    tagCloudJson = fs.GetTagCloudTreeString();
            Dictionary <int, string> commentDictionary = GetCommentDictionary(fs);
            List <CommentObj>        comments          = new List <CommentObj>();
            List <SentenceObj>       topSentenceObjs   = fs.GetTopSentences(5);

            foreach (var item in commentDictionary)
            {
                comments.Add(new CommentObj()
                {
                    Id = item.Key, Text = item.Value
                });
            }
            List <UserCommentObj> userComments = new List <UserCommentObj>();
            var userCommentsForStory           = fs.GetUserComments();

            foreach (var kvp in userCommentsForStory)
            {
                userComments.Add(new UserCommentObj()
                {
                    User = kvp.Key, Comments = kvp.Value
                });
            }
            List <KeywordCommentObj> keywordComments = new List <KeywordCommentObj>();
            var namedObjectsForStory = fs.GetNamedObjects(20);

            foreach (var kvp in namedObjectsForStory)
            {
                keywordComments.Add(new KeywordCommentObj()
                {
                    Keyword = kvp.Key, Comments = kvp.Value
                });
            }
            Directory.CreateDirectory("data");
            string fileName = Path.Combine("data", "following.txt");

            if (!File.Exists(fileName))
            {
                File.Create(fileName);
            }
            string[] following        = File.ReadAllLines(fileName);
            string   csv              = string.Join(",", following);
            string   fileNameWatching = Path.Combine("data", "watching.txt");

            if (!File.Exists(fileNameWatching))
            {
                File.Create(fileNameWatching);
            }
            string[]     watching     = File.ReadAllLines(fileNameWatching);
            string       csvWatching  = string.Join(",", watching);
            FullStoryObj fullStoryObj = new FullStoryObj()
            {
                Json = json, TagCloudJson = tagCloudJson, TotalComments = commentDictionary.Count, Comments = comments, Sentences = topSentenceObjs, UserComments = userComments, KeywordComments = keywordComments, AllFollowing = csv, AllWatching = csvWatching, TotalWords = fs.TotalWords
            };

            return(fullStoryObj);
        }
Example #5
0
        Dictionary <int, string> GetCommentDictionary(FullStory fs)
        {
            Dictionary <int, string> commentDictionary = new Dictionary <int, string>();

            foreach (children child in fs.children)
            {
                commentDictionary[child.id] = child.text;
                GetCommentDictionary(child, ref commentDictionary);
            }
            return(commentDictionary);
        }
Example #6
0
        public async Task <object> GetStoryTopNWords(int storyid)
        {
            FullStory fs = FullStoryFactory.GetFullStory(storyid);
            Dictionary <string, int> topNWords = fs.GetTopNWordsDictionary(10);
            List <WordObj>           wordObjs  = topNWords.ToList().Select(x => new WordObj()
            {
                Word = x.Key, Count = x.Value
            }).ToList();

            return(wordObjs);
        }
Example #7
0
        public async Task <object> GetCommentTreeForId(string idTuple)
        {
            string       sStoryId = idTuple.Split(':')[1];
            string       sNodeId  = idTuple.Split(':')[0];
            int          storyid  = Convert.ToInt32(sStoryId);
            int          nodeid   = Convert.ToInt32(sNodeId);
            FullStory    fs       = FullStoryFactory.GetFullStory(storyid);
            children     child    = fs.GetNodeById(nodeid);
            TagCloudNode tgn      = fs.GetCommentTreeString(child);

            return(JsonConvert.SerializeObject(tgn));
        }
Example #8
0
 public async Task <object> ArchiveStory(int id)
 {
     try
     {
         FullStory fs = FullStoryFactory.GetFullStory(id);
         fs.ArchivedOn = DateTime.Now;
         Directory.CreateDirectory("archive");
         string fileName = Path.Combine("archive", id + ".json");
         File.WriteAllText(fileName, JsonConvert.SerializeObject(fs));
     }
     catch (Exception ex)
     {
         return(ex.ToString());
     }
     return(string.Empty);
 }
Example #9
0
        public async Task <object> GetTop100Stories(object input)
        {
            List <int>      top100Ids    = GetTop100();
            List <StoryObj> storyObjList = new List <StoryObj>();

            foreach (int id in top100Ids)
            {
                FullStory fs         = FullStoryFactory.GetFullStory(id);
                string    commentUrl = "https://news.ycombinator.com/item?id=" + id;
                StoryObj  so         = new StoryObj()
                {
                    StoryId = id, StoryTitle = fs.title, Author = fs.author, StoryText = fs.text, Url = fs.url ?? commentUrl, CommentUrl = commentUrl
                };
                storyObjList.Add(so);
            }
            return(storyObjList);
        }
 public static FullStory GetFullStory(int storyID)
 {
     FullStory fs = new FullStory();
     try
     {
         string storyURL = Globals.AlgoliaUrl + storyID;
         string response = Util.FetchJson(storyURL);
         string strToReplace = @"""points"":null";
         string strReplaceWith = @"""points"":0";
         response = response.Replace(strToReplace, strReplaceWith);
         fs = JsonConvert.DeserializeObject<FullStory>(response);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
     return fs;
 }
Example #11
0
        public static FullStory GetFullStory(int storyID)
        {
            FullStory fs = new FullStory();

            try
            {
                string storyURL       = Globals.AlgoliaUrl + storyID;
                string response       = Util.FetchJson(storyURL);
                string strToReplace   = @"""points"":null";
                string strReplaceWith = @"""points"":0";
                response = response.Replace(strToReplace, strReplaceWith);
                fs       = JsonConvert.DeserializeObject <FullStory>(response);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(fs);
        }
Example #12
0
        private StoryObj GetFullStoryObj(int id)
        {
            StoryObj so = new StoryObj();

            try
            {
                FullStory fs                 = FullStoryFactory.GetFullStory(id);
                int       commentCount       = fs.TotalComments;
                string    commentUrl         = "https://news.ycombinator.com/item?id=" + id;
                var       usercommentObjList = new List <UserCommentObj>();
                var       dictComments       = fs.GetUserComments();
                foreach (var kvp in dictComments)
                {
                    UserCommentObj userCommentObj = new UserCommentObj()
                    {
                        User = kvp.Key, Comments = kvp.Value, StoryId = id
                    };
                    usercommentObjList.Add(userCommentObj);
                }
                var keywordComments = fs.GetNamedObjects(20);
                List <KeywordCommentObj> keywordCommentObjList = new List <KeywordCommentObj>();
                foreach (var kvp in keywordComments)
                {
                    KeywordCommentObj keywordCommentObj = new KeywordCommentObj()
                    {
                        Keyword = kvp.Key, Comments = kvp.Value, StoryId = id
                    };
                    keywordCommentObjList.Add(keywordCommentObj);
                }
                so = new StoryObj()
                {
                    StoryId = id, StoryTitle = fs.title, Author = fs.author, StoryText = fs.text, Url = fs.url ?? commentUrl, CommentUrl = commentUrl, StoryComments = commentCount, AllUserComments = usercommentObjList, AllKeywordComments = keywordCommentObjList
                };
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(so);
        }
Example #13
0
        public async Task <Object> GetStory(int id)
        {
            List <StoryObj> storyObjList = new List <StoryObj>();

            try
            {
                FullStory fs           = FullStoryFactory.GetFullStory(id);
                int       commentCount = fs.TotalComments;
                string    commentUrl   = "https://news.ycombinator.com/item?id=" + id;
                StoryObj  so           = new StoryObj()
                {
                    StoryId = id, StoryTitle = fs.title, Author = fs.author, StoryText = fs.text, Url = fs.url ?? commentUrl, CommentUrl = commentUrl, StoryComments = commentCount
                };
                storyObjList.Add(so);
            }
            catch (Exception ex)
            {
                //Sometimes algolia api throws an error, just move
                //on to the next item
                Console.WriteLine(ex.ToString());
            }
            return(storyObjList);
        }
Example #14
0
        public int GetStoryCommentCount(int storyID)
        {
            FullStory fs = FullStoryFactory.GetFullStory(storyID);

            return(fs.TotalComments);
        }
Example #15
0
 Dictionary<int, string> GetCommentDictionary(FullStory fs)
 {
     Dictionary<int,string> commentDictionary = new Dictionary<int, string>();
     foreach (children child in fs.children)
     {
         commentDictionary[child.id] = child.text;
         GetCommentDictionary(child, ref commentDictionary);
     }
     return commentDictionary;
 }