Exemplo n.º 1
0
        public static Question GetQuestion(MessageEventArgs message, out CQ dom)
        {
            var obj = JsonObject.Parse(message.Data);
            var data = obj.Get("data");
            var innerObj = JsonSerializer.DeserializeFromString<Dictionary<string, object>>(data);

            var url = TrimUrl((string)innerObj["url"]);
            var id = 0u;
            var host = "";
            ParsePostLink(url, out host, out id);
            var title = WebUtility.HtmlDecode((string)innerObj["titleEncodedFancy"]);
            var authorName = WebUtility.HtmlDecode((string)innerObj["ownerDisplayName"]);
            var tags = JsonSerializer.DeserializeFromString<string[]>((string)innerObj["tags"]);
            var networkID = -1;
            var authorLink = "";

            if (innerObj.ContainsKey("ownerUrl") && innerObj["ownerUrl"] != null)
            {
                authorLink = TrimUrl((string)innerObj["ownerUrl"]);
            }

            if (!string.IsNullOrEmpty(authorLink))
            {
                networkID = GetUserNetworkID(authorLink);
            }

            var html = new StringDownloader().DownloadString(url);
            dom = CQ.Create(html, Encoding.UTF8);

            var body = WebUtility.HtmlDecode(questionStatusDiv.Replace(dom[".post-text"].Html(), "").Trim());
            var score = int.Parse(dom[".vote-count-post"].Html());
            var isClosed = IsQuestionClosed(dom, url);
            var authorRep = ParseRep(dom[".post-signature.owner .reputation-score"].Html());
            var creationDate = DateTime.MaxValue;
            foreach (var timestamp in dom[".post-signature .user-info .user-action-time .relativetime"])
            {
                var dt = DateTime.Parse(timestamp.Attributes["title"]);

                if (dt < creationDate)
                {
                    creationDate = dt;
                }
            }

            return new Question(id, url, host, title, body, score, isClosed, creationDate, authorName, authorLink, networkID, authorRep, tags);
        }
Exemplo n.º 2
0
        public static Answer GetAnswer(string postUrl)
        {
            string host;
            uint id;

            ParsePostLink(postUrl, out host, out id);

            var getUrl = "http://" + host + "/posts/ajax-load-realtime/" + id;
            var html = new StringDownloader().DownloadString(getUrl);
            var dom = CQ.Create(html, Encoding.UTF8);

            return GetAnswer(dom, host, id.ToString(CultureInfo.InvariantCulture));
        }