コード例 #1
0
            public IAsyncResult StartUserStream(
                FollowingCallback followingCallback = null,
                FollowersCallback followersCallback = null,
                PostsCallback postsCallback         = null,
                MentionsCallback mentionsCallback   = null,
                StreamCallback streamCallback       = null,
                UnifiedCallback unifiedCallback     = null,
                ChannelsCallback channelsCallback   = null,
                ChannelSubscribersCallback channelSubscribersCallback = null,
                ChannelMessagesCallback channelMessagesCallback       = null,
                FilesCallback filesCallback                 = null,
                RawJsonCallback rawJsonCallback             = null,
                StreamStoppedCallback streamStoppedCallback = null)
            {
                if (this.request != null)
                {
                    throw new InvalidOperationException("Stream is already open");
                }

                this.followingCallback          = followingCallback;
                this.followersCallback          = followersCallback;
                this.postsCallback              = postsCallback;
                this.mentionsCallback           = mentionsCallback;
                this.streamCallback             = streamCallback;
                this.unifiedCallback            = unifiedCallback;
                this.channelsCallback           = channelsCallback;
                this.channelSubscribersCallback = channelSubscribersCallback;
                this.channelMessagesCallback    = channelMessagesCallback;
                this.filesCallback              = filesCallback;
                this.rawJsonCallback            = rawJsonCallback;
                this.streamStoppedCallback      = streamStoppedCallback;

                subscribed_endpoints       = new Dictionary <string, StreamingEndpoint>();
                to_be_subscribed_endpoints = new List <StreamingEndpoint>();

                Dictionary <string, string> headers = new Dictionary <string, string>();

                headers.Add("Authorization", "Bearer " + access_token);


                string url = "https://pnut.io/stream/user?ct=AppNet.Net";

                if (streaming_options != null)
                {
                    if (!string.IsNullOrEmpty(streaming_options.query_string()))
                    {
                        url += "&" + streaming_options.query_string();
                    }
                }

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

                request.Method            = "GET";
                request.AllowAutoRedirect = true;
                request.Accept            = "*/*";
                request.Timeout           = 10000;
                request.KeepAlive         = true;

                request.UserAgent = "AppNet.Net (http://www.nymphicusapp.com/windows/chapper)";

                foreach (KeyValuePair <string, string> additonalHeader in headers)
                {
                    request.Headers.Add(additonalHeader.Key, additonalHeader.Value);
                }

                try
                {
                    return(request.BeginGetResponse(StreamCallback, request));
                }
                catch (WebException e)
                {
                    Console.WriteLine(e.Message);
                    return(null);
                }
            }
コード例 #2
0
        public void GetPosts(int threadId, int page, PostsCallback scb, ErrorCallback ecb)
        {
            string url = "post/list?threadid=" + threadId + (page > 1 ? "&page=" + page : "");

            if (LoggedIn)
            {
                url += "&_t=" + _sessionToken + "&_u=" + User.UserID;
            }

            RequestData(url, RequestType.GET, w => { },
                        data =>
            {
                JObject root  = JObject.Parse(data);
                string status = (string)root["status"];     //todo

                List <Post> posts = new List <Post>();

                foreach (JObject obj in root["data"]["posts"])
                {
                    int postId = (int)obj["postid"];
                    //threadid
                    string username = (string)obj["username"];
                    int userid      = (int)obj["userid"];
                    int timestamp   = (int)obj["dateline"];
                    string pageText = System.Net.HttpUtility.HtmlDecode((string)obj["pagetext"]);
                    bool visible    = ((int)obj["visible"]) == 1;

                    User author = new User()
                    {
                        Name   = username,
                        UserID = userid
                    };
                    Post post = new Post()
                    {
                        Author    = author,
                        PageText  = pageText,
                        Timestamp = timestamp,
                        Visible   = visible,
                        PostID    = postId
                    };
                    posts.Add(post);
                }
                //"thread":
                //{"threadid":1363571,
                //"title":"Valve: &quot;Bad communication is worse than none&quot;",
                //"forumid":51,"open":1,
                //"numposts":68,
                //"lastpost_userid":245787,
                //"lastpost_username":"******",
                //"lastpost_id":43903795,
                //"thread_userid":546131,"thread_username":"******","thread_postid":43903795
                int numPostsPerPage   = (int)root["data"]["forum"]["postsperpage"];
                string threadTitle    = (string)root["data"]["thread"]["title"];
                int numPosts          = (int)root["data"]["thread"]["numposts"];
                int forumId           = (int)root["data"]["thread"]["forumid"];
                int authorId          = (int)root["data"]["thread"]["thread_userid"];
                string authorName     = (string)root["data"]["thread"]["thread_username"];
                int lastPosterId      = (int)root["data"]["thread"]["lastpost_userid"];
                string lastPosterName = (string)root["data"]["thread"]["lastpost_username"];
                bool isOpen           = (bool)root["data"]["thread"]["open"];
                int pageCount         = (int)Math.Ceiling((double)numPosts / numPostsPerPage);
                User threadAuthor     = new User()
                {
                    Name = authorName, UserID = authorId
                };
                User lastPoster = new User()
                {
                    Name = lastPosterName, UserID = lastPosterId
                };

                //ugly casting...
                Thread thread = new Thread()
                {
                    Title        = HttpUtility.HtmlDecode(threadTitle), //fix this up
                    ThreadID     = threadId,
                    PageCount    = pageCount,
                    Author       = threadAuthor,
                    LatestPoster = lastPoster,
                    IsOpen       = isOpen,
                    ForumID      = forumId
                };
                //root["data"]["thread"]
                //root["data"]["forum"]

                scb(posts, thread);
            }, ecb);
        }