Example #1
0
        /// <summary>
        ///   Loads the tweets encoded in the JSon response from the server
        ///   into the database.   Users that are detected in the stream
        ///   are entered in the user database as well.
        /// </summary>
        static public int LoadJson(Stream stream, int localAccount, TweetKind kind)
        {
            //stream = File.OpenRead ("/Users/miguel/Projects/TweetStation/foo");
            Database  db    = Database.Main;
            int       count = 0;
            JsonValue root;
            string    userKey;

            try {
                root = JsonValue.Load(stream);
                if (kind == TweetKind.Direct)
                {
                    userKey = "sender";
                }
                else
                {
                    userKey = "user";
                }
            } catch (Exception e) {
                Console.WriteLine(e);
                return(-1);
            }

            // These are reusable instances that we used during population
            var tweet = new Tweet()
            {
                Kind = kind, LocalAccountId = localAccount
            };
            var user = new User();

            var start = DateTime.UtcNow;

            var usersSeen = new HashSet <long> ();

            lock (db){
                db.Execute("BEGIN");

                foreach (object djentry in root)
                {
                    // Sometimes twitter just inserts junk in the middle of the JsonStream (a null literal,
                    // instead of an actual tweet.   Go Twitter!
                    var jentry = djentry as JsonObject;
                    if (jentry == null)
                    {
                        continue;
                    }

                    var  juser = jentry [userKey];
                    bool result;

                    if (!ParseUser((JsonObject)juser, user, usersSeen))
                    {
                        continue;
                    }

                    try {
                        if (kind == TweetKind.Direct)
                        {
                            result = tweet.TryPopulateDirect(jentry);
                        }
                        else
                        {
                            result = tweet.TryPopulate(jentry);
                        }

                        if (result)
                        {
                            PopulateUser(tweet, user);
                            tweet.Insert(db);
                            count++;
                        }
                    } catch (Exception e) {
                        Console.WriteLine(e);
                    }

                    // Repeat user loading for the retweet info
                    if (tweet.Retweeter != null)
                    {
                        ParseUser((JsonObject)(jentry ["retweeted_status"]["user"]), user, usersSeen);
                    }
                }
                db.Execute("COMMIT");
            }
            var end = DateTime.UtcNow;

            Util.Log("With transactions: Spent {0} ticks in inserting {1} elements", (end - start).Ticks, count);
            return(count);
        }
Example #2
0
        /// <summary>
        ///   Loads the tweets encoded in the JSon response from the server
        ///   into the database.   Users that are detected in the stream
        ///   are entered in the user database as well.
        /// </summary>
        public static int LoadJson(Stream stream, int localAccount, TweetKind kind)
        {
            Database db = Database.Main;
            int count = 0;
            JsonValue root;
            string userKey;

            try {
                root = JsonValue.Load (stream);
                if (kind == TweetKind.Direct)
                    userKey = "sender";
                else
                    userKey = "user";
            } catch (Exception e) {
                Console.WriteLine (e);
                return -1;
            }

            // These are reusable instances that we used during population
            var tweet = new Tweet () { Kind = kind, LocalAccountId = localAccount };
            var user = new User ();

            var start = DateTime.UtcNow;

            var usersSeen = new HashSet<long> ();

            lock (db){
                db.Execute ("BEGIN");
                foreach (JsonObject jentry in root){
                    var juser = jentry [userKey];
                    bool result;

                    if (!ParseUser ((JsonObject) juser, user, usersSeen))
                        continue;

                    if (kind == TweetKind.Direct)
                        result = tweet.TryPopulateDirect (jentry);
                    else
                        result = tweet.TryPopulate (jentry);

                    if (result){
                        PopulateUser (tweet, user);
                        tweet.Insert (db);
                        count++;
                    }

                    // Repeat user loading for the retweet info
                    if (tweet.Retweeter != null)
                        ParseUser ((JsonObject)(jentry ["retweeted_status"]["user"]), user, usersSeen);
                }
                db.Execute ("COMMIT");
            }
            var end = DateTime.UtcNow;
            Util.Log ("With transactions: Spent {0} ticks in inserting {1} elements", (end-start).Ticks, count);
            return count;
        }