internal Dictionary <int, PostArtifact> getDBPostArtifacts(string post_ids) { Dictionary <int, PostArtifact> postArtifacts = new Dictionary <int, PostArtifact>(); using (SQLiteConnection dbConn = new SQLiteConnection(dbPath)) { string sql = @"select p.Id, p.PubDate, p.ModDate, max(c.CommentDate) from Post as P left outer join Comments as c on p.Id = c.PostId where p.Id IN (?) group by p.Id;"; using (var statement = dbConn.Prepare(sql)) { statement.Bind(1, post_ids); while (statement.Step() == SQLiteResult.ROW) { PostArtifact pf = new PostArtifact(); pf.PostID = (int)((long)statement[0]); pf.PubDate = DateTime.Parse(statement[1].ToString()); pf.ModDate = DateTime.Parse(statement[2].ToString()); try { pf.CommentDate = DateTime.Parse(statement[3].ToString()); } catch { pf.CommentDate = new DateTime(2011, 11, 4); } postArtifacts.Add(pf.PostID, pf); } } } return(postArtifacts); }
internal async Task <int> fetchPostArtifacts() { // Hanu Epoch time. DateTime lastSyncTime = new DateTime(2011, 11, 4); DateTime now = DateTime.Now; string post_id_list = ""; var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; if (localSettings.Values["LastSyncTime"] != null) { // This is not the first use. lastSyncTime = DateTime.Parse(localSettings.Values["LastSyncTime"].ToString()); } var iid = localSettings.Values["InstanceID"]; TimeSpan interval = now.Subtract(lastSyncTime); if (interval.Minutes < 5) { return(0); } // It has been more than 5 min since we checked, so we can check again. var syncData = new JObject(); if (localSettings.Values["SyncCategory"] != null) { syncData.Add("category", localSettings.Values["SyncCategory"].ToString()); } if (localSettings.Values["SyncTags"] != null) { syncData.Add("tag", localSettings.Values["SyncTags"].ToString()); } string sync_params = JsonConvert.SerializeObject(syncData); using (HttpClient hc = new HttpClient()) { String url = HanuDowsApplication.getInstance().BlogURL + "/wp-content/plugins/hanu-droid/PostArtifacts.php"; Uri address = new Uri(url); var values = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("modified_time", lastSyncTime.ToString("yyyy-MM-dd HH:mm:ss")), new KeyValuePair <string, string>("iid", iid.ToString()), new KeyValuePair <string, string>("sync_params", sync_params) }; HttpFormUrlEncodedContent postContent = new HttpFormUrlEncodedContent(values); HttpResponseMessage response = await hc.PostAsync(address, postContent).AsTask(); string response_text = await response.Content.ReadAsStringAsync(); XDocument xdoc = XDocument.Parse(response_text); foreach (XElement post_artifact in xdoc.Root.Elements("PostArtifcatData")) { PostArtifact pf = new PostArtifact(); pf.PostID = (int)post_artifact.Attribute("Id"); pf.PubDate = DateTime.Parse(post_artifact.Attribute("PublishDate").Value); pf.ModDate = DateTime.Parse(post_artifact.Attribute("ModifiedDate").Value); // Sometimes comment date may not be available. try { pf.CommentDate = DateTime.Parse(post_artifact.Attribute("CommentDate").Value); } catch { pf.CommentDate = new DateTime(2011, 11, 4); } _postArtifacts.Add(pf); post_id_list += pf.PostID + ","; } } // Get DB Artifacts now post_id_list = post_id_list.TrimEnd(','); Dictionary <int, PostArtifact> dbPostArtifacts = DBHelper.getInstance().getDBPostArtifacts(post_id_list);; // Filter Artifacts now PostArtifact dbArtifact; foreach (PostArtifact pf in _postArtifacts.ToList()) { dbPostArtifacts.TryGetValue(pf.PostID, out dbArtifact); if (dbArtifact != null) { // See if DB artifact is older or not if (pf.PubDate.CompareTo(dbArtifact.PubDate) >= 0 || pf.ModDate.CompareTo(dbArtifact.ModDate) >= 0 || pf.CommentDate.CompareTo(dbArtifact.CommentDate) >= 0) { // Keep this } else { // Remove this _postArtifacts.Remove(pf); } } } return(_postArtifacts.Count); }