Exemple #1
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);
        }
Exemple #2
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();
 }
        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;
        }