public TimelineViewController(string title, TweetKind kind, bool pushing) : base(pushing) { timelineTitle = title; this.kind = kind; NavigationItem.LeftBarButtonItem = new UIBarButtonItem(settingsImage, UIBarButtonItemStyle.Plain, delegate { PresentModalViewController(new UINavigationController(new Settings(this)), true); }); }
/// <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; }
static string MakeTimelineRequest(TweetKind kind, long?since, long?max_id) { string uri = null; int count = 200; switch (kind) { case TweetKind.Home: uri = timelineUri; break; case TweetKind.Replies: uri = mentionsUri; count = 40; break; case TweetKind.Direct: uri = directUri; break; } return(uri + "?count=" + count + (since.HasValue ? "&since_id=" + since.Value : "") + (max_id.HasValue ? "&max_id=" + max_id.Value : "")); }
public void ReloadTimeline(TweetKind kind, long?since, long?max_id, Action <int> done) { var req = MakeTimelineRequest(kind, since, max_id); Download(req, false, result => { int count = -1; if (result != null) { try { count = Tweet.LoadJson(result, LocalAccountId, kind); lasterror = null; } catch (Exception e) { Console.WriteLine(e); lasterror = e.ToString(); } } invoker.BeginInvokeOnMainThread(delegate { done(count); }); }); }
/// <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); }
public void ReloadTimeline(TweetKind kind, long? since, long? max_id, Action<int> done) { string uri = null; switch (kind){ case TweetKind.Home: uri = timelineUri; break; case TweetKind.Replies: uri = mentionsUri; break; case TweetKind.Direct: uri = directUri; break; } var req = uri + "?count=200" + (since.HasValue ? "&since_id=" + since.Value : "") + (max_id.HasValue ? "&max_id=" + max_id.Value : ""); Download (req, false, result => { int count = -1; if (result != null){ try { count = Tweet.LoadJson (result, LocalAccountId, kind); } catch (Exception e) { Console.WriteLine (e); } } invoker.BeginInvokeOnMainThread (delegate { done (count); }); }); }
public TimelineViewController(string title, TweetKind kind, bool pushing) : base(pushing) { timelineTitle = title; this.kind = kind; NavigationItem.LeftBarButtonItem = new UIBarButtonItem (settingsImage, UIBarButtonItemStyle.Plain, delegate { PresentModalViewController (new UINavigationController (new Settings (this)), true); }); }
// // Alternative version that just parses the users and tweets and returns them as lists // I thought it would be useful, but it is not. The JSon parsing is too fast, we // only get bogged down with the actual sqlite insert // public static void ParseJson(Stream stream, int localAccount, TweetKind kind, out List<User> users, out List<Tweet> tweets) { JsonValue root; string userKey; try { root = JsonValue.Load (stream); if (kind == TweetKind.Direct) userKey = "sender"; else userKey = "user"; } catch (Exception e) { Console.WriteLine (e); tweets = null; users = null; return; } users = new List<User> (root.Count/4); tweets = new List<Tweet> (root.Count); var start = DateTime.UtcNow; var usersSeen = new HashSet<long> (); var user = new User (); foreach (JsonObject jentry in root){ var juser = jentry [userKey]; bool result; try { user.UpdateFromJson ((JsonObject) juser); if (!usersSeen.Contains (user.Id)){ usersSeen.Add (user.Id); users.Add (user); user = new User (); } } catch { continue; } var tweet = new Tweet (); if (kind == TweetKind.Direct) result = tweet.TryPopulateDirect (jentry); else result = tweet.TryPopulate (jentry); if (result){ PopulateUser (tweet, user); tweets.Add (tweet); } // Repeat user loading for the retweet info if (tweet.Retweeter != null){ user.UpdateFromJson ((JsonObject)(jentry ["retweeted_status"]["user"])); if (!usersSeen.Contains (user.Id)){ usersSeen.Add (user.Id); users.Add (user); user = new User (); } } } var end = DateTime.UtcNow; Console.WriteLine ("Parsing time for tweet stream: {0} for {1} tweets", end-start, tweets.Count); }
static string MakeTimelineRequest(TweetKind kind, long? since, long? max_id) { string uri = null; int count = 200; switch (kind){ case TweetKind.Home: uri = timelineUri; break; case TweetKind.Replies: uri = mentionsUri; count = 40; break; case TweetKind.Direct: uri = directUri; break; } return uri + "?count=" + count + (since.HasValue ? "&since_id=" + since.Value : "") + (max_id.HasValue ? "&max_id=" + max_id.Value : ""); }
// // Alternative version that just parses the users and tweets and returns them as lists // I thought it would be useful, but it is not. The JSon parsing is too fast, we // only get bogged down with the actual sqlite insert // public static void ParseJson(Stream stream, int localAccount, TweetKind kind, out List <User> users, out List <Tweet> tweets) { JsonValue root; string userKey; try { root = JsonValue.Load(stream); if (kind == TweetKind.Direct) { userKey = "sender"; } else { userKey = "user"; } } catch (Exception e) { Console.WriteLine(e); tweets = null; users = null; return; } users = new List <User> (root.Count / 4); tweets = new List <Tweet> (root.Count); var start = DateTime.UtcNow; var usersSeen = new HashSet <long> (); var user = new User(); foreach (JsonObject jentry in root) { var juser = jentry [userKey]; bool result; try { user.UpdateFromJson((JsonObject)juser); if (!usersSeen.Contains(user.Id)) { usersSeen.Add(user.Id); users.Add(user); user = new User(); } } catch { continue; } var tweet = new Tweet(); if (kind == TweetKind.Direct) { result = tweet.TryPopulateDirect(jentry); } else { result = tweet.TryPopulate(jentry); } if (result) { PopulateUser(tweet, user); tweets.Add(tweet); } // Repeat user loading for the retweet info if (tweet.Retweeter != null) { user.UpdateFromJson((JsonObject)(jentry ["retweeted_status"]["user"])); if (!usersSeen.Contains(user.Id)) { usersSeen.Add(user.Id); users.Add(user); user = new User(); } } } var end = DateTime.UtcNow; Util.Log("Parsing time for tweet stream: {0} for {1} tweets", end - start, tweets.Count); }
public void ReloadTimeline(TweetKind kind, long? since, long? max_id, Action<int> done) { var req = MakeTimelineRequest (kind, since, max_id); Download (req, false, result => { int count = -1; if (result != null){ try { count = Tweet.LoadJson (result, LocalAccountId, kind); } catch (Exception e) { Console.WriteLine (e); } } invoker.BeginInvokeOnMainThread (delegate { done (count); }); }); }
public void ReloadTimeline (TweetKind kind, long? since, long? max_id, Action<int> done) { string uri = null; switch (kind){ case TweetKind.Home: uri = timelineUri; break; case TweetKind.Replies: uri = mentionsUri; break; case TweetKind.Direct: uri = directUri; break; } var req = uri + "?count=200" + (since.HasValue ? "&since_id=" + since.Value : "") + (max_id.HasValue ? "&max_id=" + max_id.Value : ""); Download (req, true, result => { if (result == null){ done (-1); } else { int count = -1; try { count = Tweet.LoadJson (new MemoryStream (result), LocalAccountId, kind); } catch (Exception e) { Console.WriteLine (e); } done (count); } }); }