Example #1
0
 public override IObservable<bool> Scrap(string url, string title = null, long? sourceTweetId = null)
 {
     var client = new OAuthClient(consumerKey, consumerSecret,
         new AccessToken(userToken, userSecret));
     client.Url = "https://www.readability.com/api/rest/v1/bookmarks";
     var param = new ParameterCollection();
     param.Add("url", HttpUtility.UrlEncode(url));
     client.Parameters = param;
     client.MethodType = MethodType.Post;
     return client.GetResponseText()
         .Select(_ => true);
 }
        public ViewModel()
        {
            _getRequestToken = new Lazy<Command>(() =>
                new Command(_ =>
                {
                    var authorizer = new OAuthAuthorizer(ConsumerKey, ConsumerSecret);
                    authorizer.GetRequestToken("http://twitter.com/oauth/request_token")
                    .Select(res => res.Token)
                    .ObserveOnDispatcher()
                    .Subscribe(token =>
                    {
                        requestToken = token;
                        AuthorizeUrl = authorizer.BuildAuthorizeUrl("http://twitter.com/oauth/authorize", token);
                    }, e => MessageBox.Show(e.ToString()));
                    _getAccessToken.Value.IsCanExecute = true;
                })
            );
            _getAccessToken = new Lazy<Command>(() =>
                new Command(_ =>
                {
                    new OAuthAuthorizer(ConsumerKey, ConsumerSecret).GetAccessToken("http://twitter.com/oauth/access_token", requestToken, PinCode)
                        .ObserveOnDispatcher()
                        .Subscribe(res =>
                        {
                            UserId = res.ExtraData["user_id"].First();
                            ScreenName = res.ExtraData["screen_name"].First();
                            accessToken = res.Token;
                            _startGetTimeline.Value.IsCanExecute = true;
                        }, e => MessageBox.Show(e.ToString()));
                    CanGetTimeline(this, new EventArgs());
                    _getAccessToken.Value.IsCanExecute = false;
                    AuthorizeUrl = "";
                }, false)
             );

            _startGetTimeline = new Lazy<Command>(() =>
            {
                return new Command(_ =>
                {
                    StreamingApi = new OAuthClient(ConsumerKey, ConsumerSecret, accessToken) { Url = "https://userstream.twitter.com/2/user.json" }
                        .GetResponseLines()
                        .Where(s => !string.IsNullOrWhiteSpace(s)) // filter invalid data
                        .Select(s => DynamicJson.Parse(s)).Publish();
                    StreamingApi.Take(1).Subscribe(x => friendList = new HashSet<int>(((double[])x.friends).Select(id => (int)id)), e => MessageBox.Show(e.ToString(), "FriendList"));
                    StreamingApi.Subscribe(x => this.PropertyChanged(x, new PropertyChangedEventArgs("StreamingApi")), e => MessageBox.Show(e.ToString(), "プロパティ変更"));
                    StreamingApi
                        .Where(x => x.text())
                        .ObserveOnDispatcher()
                        .Subscribe(x =>
                        {
                            _tweet.Add(new TimelineItemViewModel(x));
                            this.PropertyChanged(this, new PropertyChangedEventArgs("Tweet"));
                        });
                    ((IConnectableObservable<dynamic>)StreamingApi).Connect();
                    _startGetTimeline.Value.IsCanExecute = false;
                }, false);
            });

            //PropertyChanged += new PropertyChangedEventHandler((sender, target) => { if (target.PropertyName == "StreamingApi") MessageBox.Show(); });
            CanGetTimeline += new EventHandler((sender, e) => _startGetTimeline.Value.IsCanExecute = true);
        }