Exemplo n.º 1
0
        private void button4_Click(object sender, EventArgs e)
        {
            int postcount = 0;

            fbEntities  = new FBNewsDbEntities();
            label6.Text = "Loading";

            //  Url = Url + token;
            pages    = fbEntities.pages.ToList();
            keywords = fbEntities.keywords.Select(c => c.keywords).ToArray();


            foreach (page page in pages)
            {
                path = "https://graph.facebook.com/" + page.pagefbid + "/posts?limit=100&access_token=" + token;
                var clientRepsonse = "";
                try
                {
                    using (var client = new WebClient())
                    {
                        clientRepsonse = client.DownloadString(path);
                    }
                    int count = 0;

                    dynamic deserialize_post = JsonConvert.DeserializeObject(clientRepsonse);
                    if (deserialize_post.data != null)
                    {
                        // check if posts exist
                        var      listresponse  = deserialize_post.data;
                        DateTime lastCheckDate = listresponse[0].created_time;

                        for (int i = 0; i < listresponse.Count; i++)
                        {
                            count = listresponse.Count;
                            string   message    = listresponse[i].message;
                            DateTime createdate = listresponse[i].created_time;
                            string   id         = listresponse[i].id;



                            if (!string.IsNullOrEmpty(message))
                            {
                                foreach (string keyword in keywords)
                                {
                                    if (message.ToLower().Contains(keyword.ToLower()) && !(fbEntities.FBNews.Any(c => c.post_id == id)))
                                    {
                                        FBNew fb = new FBNew();
                                        fb.created_at = createdate;
                                        fb.message    = message;
                                        fb.pagename   = page.pagename;
                                        fb.post_id    = id;
                                        fbEntities.FBNews.Add(fb);
                                        fbEntities.SaveChanges();
                                        postcount++;
                                    }
                                }
                            }
                        }
                        page.lastcheckdate = lastCheckDate;
                        fbEntities.SaveChanges();
                    }
                    // MessageBox.Show(count + "");

                    // Do further processing.
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + "");
                }
            }

            MessageBox.Show("Done ! We Found  " + postcount + "  new posts");
            label6.Text = "";
        }
Exemplo n.º 2
0
        //Search
        private void button4_Click(object sender, EventArgs e)
        {
            int postcount = 0;

            fbEntities  = new FBNewsDbEntities();
            label6.Text = "Loading";
            var criterias = fbEntities.criterias.ToList();

            //Task f = Task.Factory.StartNew(() =>
            //{



            foreach (criteria criteria in criterias)
            {
                pages    = fbEntities.pages.Where(c => c.criteriaId == criteria.id).ToList();
                keywords = fbEntities.keywords.Where(c => c.cirteriaId == criteria.id).Select(c => c.keywords).ToArray();

                if (pages.Count == 0 || keywords.Length == 0)
                {
                    continue;
                }

                foreach (page page in pages)
                {
                    path = "https://graph.facebook.com/" + page.pagefbid + "/posts?limit=100&access_token=" + token;
                    var clientRepsonse = "";
                    try
                    {
                        using (var client = new WebClient())
                        {
                            clientRepsonse = client.DownloadString(path);
                        }
                        int count = 0;

                        dynamic deserialize_post = JsonConvert.DeserializeObject(clientRepsonse);
                        if (deserialize_post.data != null)
                        {
                            // check if posts exist
                            var      listresponse  = deserialize_post.data;
                            DateTime lastCheckDate = listresponse[0].created_time;

                            for (int i = 0; i < listresponse.Count; i++)
                            {
                                count = listresponse.Count;
                                string   message    = listresponse[i].message;
                                DateTime createdate = listresponse[i].created_time;
                                string   id         = listresponse[i].id;



                                if (!string.IsNullOrEmpty(message))
                                {
                                    foreach (string keyword in keywords)
                                    {
                                        string[] splitkeyword = keyword.Split(',');

                                        if (splitkeyword.Length == 1)
                                        {
                                            if (message.ToLower().Contains(keyword.ToLower()) && !(fbEntities.FBNews.Any(c => c.post_id == id)))
                                            {
                                                string[] url = id.Split('_');
                                                FBNew    fb  = new FBNew();
                                                fb.created_at = createdate;
                                                fb.message    = message;
                                                fb.pagename   = page.pagename;
                                                fb.post_id    = id;

                                                fb.location = page.location;
                                                fb.category = page.category;
                                                fb.keyword  = keyword;
                                                fb.url      = page.pageurl + url[1];
                                                fbEntities.FBNews.Add(fb);
                                                fbEntities.SaveChanges();
                                                postcount++;
                                            }
                                        }
                                        else if (splitkeyword.Length > 1)
                                        {
                                            for (int k = 0; k < splitkeyword.Length; k++)
                                            {
                                                if (splitkeyword[k].ToLower().Trim().Contains("بدون"))
                                                {
                                                    if (message.ToLower().Contains(splitkeyword[k].ToLower()) &&
                                                        message.ToLower().Contains(splitkeyword[k - 1].ToLower()) &&
                                                        !(fbEntities.FBNews.Any(c => c.post_id == id)))
                                                    {
                                                        string[] url = id.Split('_');
                                                        FBNew    fb  = new FBNew();
                                                        fb.created_at = createdate;
                                                        fb.message    = message;
                                                        fb.pagename   = page.pagename;
                                                        fb.post_id    = id;

                                                        fb.location = page.location;
                                                        fb.category = page.category;
                                                        fb.keyword  = keyword;
                                                        fb.url      = page.pageurl + url[1];
                                                        fbEntities.FBNews.Add(fb);
                                                        fbEntities.SaveChanges();
                                                        postcount++;
                                                    }
                                                }
                                                else if (message.ToLower().Contains(splitkeyword[k].ToLower()) && !(fbEntities.FBNews.Any(c => c.post_id == id)))
                                                {
                                                    string[] url = id.Split('_');
                                                    FBNew    fb  = new FBNew();
                                                    fb.created_at = createdate;
                                                    fb.message    = message;
                                                    fb.pagename   = page.pagename;
                                                    fb.post_id    = id;

                                                    fb.location = page.location;
                                                    fb.category = page.category;
                                                    fb.keyword  = splitkeyword[k];
                                                    fb.url      = page.pageurl + url[1];
                                                    fbEntities.FBNews.Add(fb);
                                                    fbEntities.SaveChanges();
                                                    postcount++;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            page.lastcheckdate = lastCheckDate;
                            fbEntities.SaveChanges();
                        }
                        // MessageBox.Show(count + "");

                        // Do further processing.
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + "");
                    }
                }
            }
            //});
            MessageBox.Show("Done ! We Found  " + postcount + "  new posts");
            label6.Text = "";
        }