예제 #1
0
파일: ListStatuses.cs 프로젝트: kazuki/ktwt
 public ListStatuses(TwitterAccount account, ListInfo li)
     : base()
 {
     Account = account;
     List = li;
     RestInfo = account.RestList.CopyConfig ();
 }
예제 #2
0
 public SearchStatuses(TwitterAccount account, string keyword)
     : base()
 {
     Account = account;
     Keyword = keyword;
     KeywordForRestAPI = keyword.Replace (",", " OR ").Replace ("  ", " ");
     _rest = account.RestSearch.CopyConfig ();
 }
예제 #3
0
        public StreamingClient(TwitterAccount account, IStreamingHandler target, bool dummy)
            : this(new TwitterAccount[]{account}, target, dummy)
        {
            if (dummy) return;

            ThreadPool.QueueUserWorkItem (delegate (object o) {
                _states[0].StreamingUri = UserStreamingURL;
                _states[0].HttpMethod = "GET";
                _states[0].StreamingPostData = null;
                StreamingStart ();
            });
        }
예제 #4
0
        public StreamingClient(TwitterAccount[] accounts, ulong[] friendIDs, IStreamingHandler target, bool dummy)
            : this(accounts, target, dummy)
        {
            StreamingUri = StreamingFilterUri;
            if (dummy) return;

            ThreadPool.QueueUserWorkItem (delegate (object o) {
                string[] postDatas = new string[accounts.Length];
                for (int j = 0, p = 0; j < accounts.Length; j++, p = Math.Min (friendIDs.Length, p + MaxFollowCount) % friendIDs.Length) {
                    StringBuilder sb = new StringBuilder ();
                    for (int i = 0; i < Math.Min (MaxFollowCount, friendIDs.Length - p); i++) {
                        sb.Append (friendIDs[i + p]);
                        sb.Append (',');
                    }
                    if (sb.Length > 0)
                        sb.Remove (sb.Length - 1, 1);
                    _states[j].StreamingPostData = "follow=" + OAuthBase.UrlEncode (sb.ToString ());
                }
                StreamingStart ();
            });
        }
예제 #5
0
        TwitterAccount LoadAccount(JsonObject obj)
        {
            string       uname      = (obj.Value["username"] as JsonString).Value;
            string       password   = (obj.Value["password"] as JsonString).Value;
            ICredentials credential = null;

            if (obj.Value.ContainsKey("token"))
            {
                string token  = (obj.Value["token"] as JsonString).Value;
                string secret = (obj.Value["secret"] as JsonString).Value;
                credential = new OAuthPasswordCache(uname, password, token, secret);
            }
            else
            {
                credential = new NetworkCredential(uname, password);
            }

            TwitterAccount account = new TwitterAccount(this);

            account.Credential = credential;
            if (obj.Value.ContainsKey("id"))
            {
                account.SelfUserID = (ulong)(obj.Value["id"] as JsonNumber).Value;
            }
            if (account.SelfUserID == 0)
            {
                // Backward compatibility (~0.0.5)
                ThreadPool.QueueUserWorkItem(delegate(object o) {
                    if (credential as OAuthCredentialCache == null)
                    {
                        return;
                    }
                    for (int i = 0; i < 5; i++)
                    {
                        try {
                            account.SelfUserID = account.TwitterClient.VerifyCredentials().ID;
                            break;
                        } catch {}
                        Thread.Sleep(5 * 1000);
                    }
                });
            }
            if (obj.Value.ContainsKey("rest"))
            {
                JsonObject restRoot              = (obj.Value["rest"] as JsonObject);
                string[]   rest_keys             = new string[] { "home", "mentions", "dm" };
                TwitterAccount.RestUsage[] rests = new TwitterAccount.RestUsage[] { account.RestHome, account.RestMentions, account.RestDirectMessages };
                for (int i = 0; i < rest_keys.Length; i++)
                {
                    if (!restRoot.Value.ContainsKey(rest_keys[i]))
                    {
                        continue;
                    }
                    JsonObject restInfoRoot = (restRoot.Value[rest_keys[i]] as JsonObject);
                    rests[i].IsEnabled = (restInfoRoot.Value["enable"] as JsonBoolean).Value;
                    rests[i].Count     = (int)(restInfoRoot.Value["count"] as JsonNumber).Value;
                    rests[i].Interval  = TimeSpan.FromSeconds((restInfoRoot.Value["interval"] as JsonNumber).Value);
                }
            }
            return(account);
        }
예제 #6
0
 void WriteRestUsage(JsonTextWriter writer, TwitterAccount.RestUsage usage)
 {
     writer.WriteKey ("interval");
     writer.WriteNumber (usage.Interval.TotalSeconds);
     writer.WriteKey ("count");
     writer.WriteNumber (usage.Count);
 }
예제 #7
0
 void WriteAccount(JsonTextWriter writer, TwitterAccount account)
 {
     writer.WriteStartObject ();
     if (account.Credential is NetworkCredential) {
         NetworkCredential nc = account.Credential as NetworkCredential;
         writer.WriteKey ("username");
         writer.WriteString (nc.UserName);
         writer.WriteKey ("password");
         writer.WriteString (nc.Password);
     } else if (account.Credential is OAuthPasswordCache) {
         OAuthPasswordCache pc = account.Credential as OAuthPasswordCache;
         writer.WriteKey ("username");
         writer.WriteString (pc.UserName);
         writer.WriteKey ("password");
         writer.WriteString (pc.Password);
         writer.WriteKey ("token");
         writer.WriteString (pc.AccessToken);
         writer.WriteKey ("secret");
         writer.WriteString (pc.AccessSecret);
     }
     if (account.SelfUserID > 0) {
         writer.WriteKey ("id");
         writer.WriteNumber (account.SelfUserID);
     }
     writer.WriteKey ("rest");
     writer.WriteStartObject ();
     string[] rest_keys = new string[] {"home", "mentions", "dm"};
     TwitterAccount.RestUsage[] rests = new TwitterAccount.RestUsage[] {account.RestHome, account.RestMentions, account.RestDirectMessages};
     for (int i = 0; i < rest_keys.Length; i ++) {
         writer.WriteKey (rest_keys[i]);
         writer.WriteStartObject ();
         writer.WriteKey ("enable");
         writer.WriteBoolean (rests[i].IsEnabled);
         writer.WriteKey ("count");
         writer.WriteNumber (rests[i].Count);
         writer.WriteKey ("interval");
         writer.WriteNumber ((int)rests[i].Interval.TotalSeconds);
         writer.WriteEndObject ();
     }
     writer.WriteEndObject ();
     writer.WriteKey ("streaming");
     if (account.StreamingClient == null) {
         writer.WriteNull ();
     } else {
         writer.WriteStartObject ();
         writer.WriteKey ("mode");
         if (account.StreamingClient.Target is TwitterAccount) {
             writer.WriteString ("follow");
             writer.WriteKey ("username");
             writer.WriteString ((account.StreamingClient.Target as TwitterAccount).ScreenName);
         } else if (account.StreamingClient.Target is SearchStatuses) {
             writer.WriteString ("track");
             writer.WriteKey ("keywords");
             writer.WriteString (account.StreamingClient.SearchKeywords);
         } else if (account.StreamingClient.Target is ListStatuses) {
             writer.WriteString ("list");
             writer.WriteKey ("id");
             writer.WriteNumber ((account.StreamingClient.Target as ListStatuses).List.ID);
         }
         writer.WriteEndObject ();
     }
     writer.WriteEndObject ();
 }
예제 #8
0
 IStreamingHandler LoadStreamingTarget(JsonObject obj, TwitterAccount[] accounts, SearchStatuses[] searches, ListStatuses[] lists)
 {
     JsonObject root = obj.Value["streaming"] as JsonObject;
     if (root == null) return null;
     string mode = (root.Value["mode"] as JsonString).Value;
     switch (mode) {
         case "follow":
             string username = (root.Value["username"] as JsonString).Value;
             for (int i = 0; i < accounts.Length; i ++)
                 if (username.Equals (accounts[i].ScreenName))
                     return accounts[i];
             break;
         case "track":
             string keywords = (root.Value["keywords"] as JsonString).Value;
             for (int i = 0; i < searches.Length; i ++)
                 if (keywords.Equals (searches[i].Keyword))
                     return searches[i];
             break;
         case "list":
             ulong id = (ulong)(root.Value["id"] as JsonNumber).Value;
             for (int i = 0; i < lists.Length; i++)
                 if (id == lists[i].List.ID)
                     return lists[i];
             break;
     }
     return null;
 }
예제 #9
0
 public TimelineInfo(TwitterAccountManager mgr, TimelineBase owner, TwitterAccount account, TwitterTimeLine timeline)
     : this(mgr, owner, timeline, CreateTitle (account, timeline))
 {
     RestAccount = account;
     RestUsage = (timeline == account.HomeTimeline ? account.RestHome :
         (timeline == account.Mentions ? account.RestMentions : account.RestDirectMessages));
 }
예제 #10
0
 StreamingClient(TwitterAccount[] accounts, IStreamingHandler target, bool dummy)
 {
     Accounts = new TwitterAccount[accounts.Length];
     _states = new InternalState[accounts.Length];
     for (int i = 0; i < accounts.Length; i ++) {
         Accounts[i] = accounts[i];
         Accounts[i].StreamingClient = this;
         _states[i] = new InternalState (accounts[i]);
         if (dummy)
             _states[i].ConnectionState = StreamingState.UpdatingFriendList;
     }
     SearchKeywords = string.Empty;
     Target = target;
 }
예제 #11
0
        TwitterAccount LoadAccount(JsonObject obj)
        {
            string uname = (obj.Value["username"] as JsonString).Value;
            string password = (obj.Value["password"] as JsonString).Value;
            ICredentials credential = null;
            if (obj.Value.ContainsKey ("token")) {
                string token = (obj.Value["token"] as JsonString).Value;
                string secret = (obj.Value["secret"] as JsonString).Value;
                credential = new OAuthPasswordCache (uname, password, token, secret);
            } else {
                credential = new NetworkCredential (uname, password);
            }

            TwitterAccount account = new TwitterAccount (this);
            account.Credential = credential;
            if (obj.Value.ContainsKey ("id"))
                account.SelfUserID = (ulong)(obj.Value["id"] as JsonNumber).Value;
            if (account.SelfUserID == 0) {
                // Backward compatibility (~0.0.5)
                ThreadPool.QueueUserWorkItem (delegate (object o) {
                    if (credential as OAuthCredentialCache == null)
                        return;
                    for (int i = 0; i < 5; i ++) {
                        try {
                            account.SelfUserID = account.TwitterClient.VerifyCredentials ().ID;
                            break;
                        } catch {}
                        Thread.Sleep (5 * 1000);
                    }
                });
            }
            if (obj.Value.ContainsKey ("rest")) {
                JsonObject restRoot = (obj.Value["rest"] as JsonObject);
                string[] rest_keys = new string[] {"home", "mentions", "dm"};
                TwitterAccount.RestUsage[] rests = new TwitterAccount.RestUsage[] {account.RestHome, account.RestMentions, account.RestDirectMessages};
                for (int i = 0; i < rest_keys.Length; i ++) {
                    if (!restRoot.Value.ContainsKey (rest_keys[i])) continue;
                    JsonObject restInfoRoot = (restRoot.Value[rest_keys[i]] as JsonObject);
                    rests[i].IsEnabled = (restInfoRoot.Value["enable"] as JsonBoolean).Value;
                    rests[i].Count = (int)(restInfoRoot.Value["count"] as JsonNumber).Value;
                    rests[i].Interval = TimeSpan.FromSeconds ((restInfoRoot.Value["interval"] as JsonNumber).Value);
                }
            }
            return account;
        }
예제 #12
0
 public void UpdateAccounts(TwitterAccount[] accounts)
 {
     HashSet<TwitterAccount> oldSet = new HashSet<TwitterAccount> (_accounts);
     _accounts = accounts;
     oldSet.ExceptWith (accounts);
     foreach (TwitterAccount account in oldSet) {
         if (account.StreamingClient != null)
             account.StreamingClient.Dispose ();
     }
     if (AccountsPropertyChanged != null)
         AccountsPropertyChanged (this, EventArgs.Empty);
 }
예제 #13
0
        public bool Load(ConfigLoadDelegate load, out IStreamingHandler[] targets)
        {
            targets = null;
            if (!File.Exists (ConfigFilePath))
                return false;
            try {
                JsonObject root = (JsonObject)new JsonValueReader (new StreamReader (ConfigFilePath, System.Text.Encoding.UTF8)).Read ();
                if (root.Value.ContainsKey ("accounts")) {
                    JsonArray array = (JsonArray)root.Value["accounts"];
                    JsonArray accountsArray = array;
                    TwitterAccount[] accounts = new TwitterAccount[array.Length];
                    for (int i = 0; i < array.Length; i++)
                        accounts[i] = LoadAccount ((JsonObject)array[i]);
                    UpdateAccounts (accounts);

                    array = (JsonArray)root.Value["searches"];
                    SearchStatuses[] searches = new SearchStatuses[array.Length];
                    for (int i = 0; i < array.Length; i ++)
                        searches[i] = LoadSearch ((JsonObject)array[i], accounts);
                    _searches = searches;

                    if (root.Value.ContainsKey ("lists")) {
                        array = (JsonArray)root.Value["lists"];
                        List<ListStatuses> lists = new List<ListStatuses> ();
                        for (int i = 0; i < array.Length; i++) {
                            ListStatuses ls = LoadList ((JsonObject)array[i], accounts);
                            if (ls != null)
                                lists.Add (ls);
                        }
                        _lists = lists.ToArray ();
                    }

                    targets = new IStreamingHandler[accountsArray.Length];
                    for (int i = 0; i < accountsArray.Length; i ++)
                        targets[i] = LoadStreamingTarget ((JsonObject)accountsArray[i], accounts, _searches, _lists);
                    ReconstructAllStreaming (targets, true);
                }
                load (root);
                return true;
            } catch {
                return false;
            }
        }
예제 #14
0
 private void Button_Click(object sender, RoutedEventArgs e)
 {
     LoginWindow win = new LoginWindow ();
     win.Owner = this;
     bool? ret = win.ShowDialog ();
     if (!ret.HasValue || !ret.Value)
         return;
     TwitterAccount account = new TwitterAccount (_mgr);
     for (int i = 0; i < _observableAccountList.Count; i ++) {
         ICredentials c = _observableAccountList[i].Credential;
         string userName = (c is NetworkCredential ? (c as NetworkCredential).UserName : (c as OAuthPasswordCache).UserName);
         if (userName.Equals (win.UserName)) {
             MessageBox.Show ("入力されたユーザ名はすでにアカウントとして登録されています");
             return;
         }
     }
     account.Credential = new NetworkCredential (win.UserName, win.Password);
     try {
         account.UpdateOAuthAccessToken ();
         account.SelfUserID = account.TwitterClient.VerifyCredentials ().ID;
         _observableAccountList.Add (account);
         Array.Resize<IStreamingHandler> (ref _targets, _observableAccountList.Count);
     } catch {
         MessageBox.Show ("認証に失敗しました");
     }
 }
예제 #15
0
 public InternalState(TwitterAccount account)
 {
     Account    = account;
     HttpMethod = "POST";
 }
예제 #16
0
 void Favorite(TwitterAccount account, Status status, bool isFavorite)
 {
     ThreadPool.QueueUserWorkItem (delegate (object o) {
         bool new_fav = isFavorite;
         try {
             if (isFavorite) {
                 account.TwitterClient.FavoritesCreate (status.ID);
             } else {
                 account.TwitterClient.FavoritesDestroy (status.ID);
             }
         } catch {
             new_fav = !isFavorite;
         }
         try {
             new_fav = account.TwitterClient.Show (status.ID).IsFavorited;
         } catch {}
         Dispatcher.Invoke (new EmptyDelegate (delegate () {
             status.IsFavorited = new_fav;
         }));
     });
 }
예제 #17
0
 static string CreateTitle(TwitterAccount account, TwitterTimeLine timeline)
 {
     return account.ScreenName + "'s " +
         (timeline == account.HomeTimeline ? "home" :
         (timeline == account.Mentions ? "mentions" : "dm"));
 }
예제 #18
0
 void WriteAccount(JsonTextWriter writer, TwitterAccount account)
 {
     writer.WriteStartObject();
     if (account.Credential is NetworkCredential)
     {
         NetworkCredential nc = account.Credential as NetworkCredential;
         writer.WriteKey("username");
         writer.WriteString(nc.UserName);
         writer.WriteKey("password");
         writer.WriteString(nc.Password);
     }
     else if (account.Credential is OAuthPasswordCache)
     {
         OAuthPasswordCache pc = account.Credential as OAuthPasswordCache;
         writer.WriteKey("username");
         writer.WriteString(pc.UserName);
         writer.WriteKey("password");
         writer.WriteString(pc.Password);
         writer.WriteKey("token");
         writer.WriteString(pc.AccessToken);
         writer.WriteKey("secret");
         writer.WriteString(pc.AccessSecret);
     }
     if (account.SelfUserID > 0)
     {
         writer.WriteKey("id");
         writer.WriteNumber(account.SelfUserID);
     }
     writer.WriteKey("rest");
     writer.WriteStartObject();
     string[] rest_keys = new string[] { "home", "mentions", "dm" };
     TwitterAccount.RestUsage[] rests = new TwitterAccount.RestUsage[] { account.RestHome, account.RestMentions, account.RestDirectMessages };
     for (int i = 0; i < rest_keys.Length; i++)
     {
         writer.WriteKey(rest_keys[i]);
         writer.WriteStartObject();
         writer.WriteKey("enable");
         writer.WriteBoolean(rests[i].IsEnabled);
         writer.WriteKey("count");
         writer.WriteNumber(rests[i].Count);
         writer.WriteKey("interval");
         writer.WriteNumber((int)rests[i].Interval.TotalSeconds);
         writer.WriteEndObject();
     }
     writer.WriteEndObject();
     writer.WriteKey("streaming");
     if (account.StreamingClient == null)
     {
         writer.WriteNull();
     }
     else
     {
         writer.WriteStartObject();
         writer.WriteKey("mode");
         if (account.StreamingClient.Target is TwitterAccount)
         {
             writer.WriteString("follow");
             writer.WriteKey("username");
             writer.WriteString((account.StreamingClient.Target as TwitterAccount).ScreenName);
         }
         else if (account.StreamingClient.Target is SearchStatuses)
         {
             writer.WriteString("track");
             writer.WriteKey("keywords");
             writer.WriteString(account.StreamingClient.SearchKeywords);
         }
         else if (account.StreamingClient.Target is ListStatuses)
         {
             writer.WriteString("list");
             writer.WriteKey("id");
             writer.WriteNumber((account.StreamingClient.Target as ListStatuses).List.ID);
         }
         writer.WriteEndObject();
     }
     writer.WriteEndObject();
 }
예제 #19
0
 public StreamingClient(TwitterAccount[] accounts, string searchKeywords, IStreamingHandler target, bool dummy)
     : this(accounts, target, dummy)
 {
     StreamingUri = StreamingFilterUri;
     string postData = "track=" + OAuthBase.UrlEncode (searchKeywords);
     for (int i = 0; i < accounts.Length; i ++)
         _states[i].StreamingPostData = postData;
     SearchKeywords = searchKeywords;
     if (!dummy)
         StreamingStart ();
 }
예제 #20
0
 ListStatuses LoadList(JsonObject obj, TwitterAccount[] accounts)
 {
     ulong id = (ulong)(obj.Value["id"] as JsonNumber).Value;
     string username = (obj.Value["username"] as JsonString).Value;
     for (int i = 0; i < accounts.Length; i++) {
         if (accounts[i].ScreenName == username) {
             ListInfo[] lists = accounts[i].TwitterClient.SelfAndFollowingList;
             for (int j = 0; j < lists.Length; j ++) {
                 if (lists[j].ID == id) {
                     ListStatuses statuses = new ListStatuses (accounts[i], lists[j]);
                     LoadRestUsage (obj, statuses.RestInfo);
                     return statuses;
                 }
             }
         }
     }
     return null;
 }
예제 #21
0
 public InternalState(TwitterAccount account)
 {
     Account = account;
 }
예제 #22
0
 public InternalState(TwitterAccount account)
 {
     Account = account;
 }
예제 #23
0
 void LoadRestUsage(JsonObject obj, TwitterAccount.RestUsage usage)
 {
     if (obj.Value.ContainsKey ("interval"))
         usage.Interval = TimeSpan.FromSeconds ((obj.Value["interval"] as JsonNumber).Value);
     if (obj.Value.ContainsKey ("count"))
         usage.Count = (int)(obj.Value["count"] as JsonNumber).Value;
 }
예제 #24
0
 public InternalState(TwitterAccount account)
 {
     Account = account;
     HttpMethod = "POST";
 }
예제 #25
0
 SearchStatuses LoadSearch(JsonObject obj, TwitterAccount[] accounts)
 {
     string keywords = (obj.Value["keywords"] as JsonString).Value;
     string username = (obj.Value["username"] as JsonString).Value;
     for (int i = 0; i < accounts.Length; i ++) {
         if (accounts[i].ScreenName == username) {
             SearchStatuses ss = new SearchStatuses (accounts[i], keywords);
             LoadRestUsage (obj, ss.RestInfo);
             return ss;
         }
     }
     throw new Exception ();
 }
예제 #26
0
파일: ListStatuses.cs 프로젝트: kazuki/ktwt
 public ListStatuses(TwitterAccount account, ListInfo li) : base()
 {
     Account  = account;
     List     = li;
     RestInfo = account.RestList.CopyConfig();
 }