0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Facebook;
using System.Net;
using System.IO;

namespace WebSite_Login_And_Browsing
{
    class Posts
    {
        public string PostId { get; set; }
        public string PostStory { get; set; }
        public string PostMessage { get; set; }
        public string PostPicture { get; set; }
        public string UserId { get; set; }
        public string UserName { get; set; }

    }

    class FacebookPosts
    {
        static string accesstoken;
        //static string token = "2f89d691b5f39";
        static string token = "1186840401345424|GoJRCpM";
        static string mytoken = "CAACEdEose0cBACPu39NSSalHCGFGDGRKZAvwiTuzG8PHlNRJwbyMVugovDxgL7CT3a1QbRuVDZALXxWU0ntwSrDyq75LIIuzFpBtx47cJYCY2OiA21lpTRKt2bB0t5HrsQYIXHXhmU7GnavWZCzqN8yeuv5NWXxTIOfVCZAZArjYNiPWhZBqZAZAO03s6FKNIulm4kjzXvp4QKiahAlcyaZBg";
        static string mytokenaslip = "CAACEdEose0cBABmWuBI9p9dpPxEsMJoFZAG3kScx61kZAImNBgt52kVrd8WWPRpwjWP8nCPX69zdLuFyVQHzxYfMk85ZBZC4BIajVWXNLo7OI7yaCbNIwqkcdwpabQVFZBRWt0rzTQrQr6ZBij45XnrQyEUqFKP4gADeO4Fl9yRaZAZCOFtV3b84sWUFEgwaKbZAPY4BCljVjWQZDZD";
        public static void RetrievePosts()
        {
            try
            {
                var client = new FacebookClient(mytokenaslip);
                dynamic result = client.Get("/me/posts");
                List<Posts> postsList = new List<Posts>();

                //all the posts and their information (like pictures and links) is strored in result.data not in result

                for (int i = 0; i < result.data.Count; i++)
                {
                    Posts posts = new Posts();

                    posts.PostId = result.data[i].id;
                    if (object.ReferenceEquals(result.data[i].story, null))
                        posts.PostStory = "this story is null";
                    else
                        posts.PostStory = result.data[i].story;
                    if (object.ReferenceEquals(result.data[i].message, null))
                        posts.PostMessage = "this message is null";
                    else
                        posts.PostMessage = result.data[i].message;

                    posts.PostPicture = result.data[i].picture;
                    posts.UserId = result.data[i].from.id;
                    posts.UserName = result.data[i].from.name;

                    postsList.Add(posts);
                }

            }
            catch (Exception err)
            {
                //throw;
                string myerr = err.ToString();
            }
        }
    }
}

I'm getting 25 results in the List postsList How do i loop now asgain to get the next page with the next 25 results and add it to postsList and loop over and over again untill there are no more results ?

What i want to do is to delete automatic every 50 minutes the last old 25 posts. In my other class in my project i'm posting automatic to my wall a post every minute. After 50 minutes i want to delete the last old 25 posts so on my wall will be all the time with 25 posts only.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using mshtml;
using HtmlAgilityPack;
using System.Net;
using System.IO;

namespace WebSite_Login_And_Browsing
{
    public partial class Facebook_Post : Form
    {
        WebBrowser wb = new WebBrowser();
        int postsCounter = 0;
        StreamWriter w = new StreamWriter(@"e:\posts.txt");
        WebBrowser webBrowser1;

        public Facebook_Post()
        {
            InitializeComponent();

            webBrowser1 = new WebBrowser();
            webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.Navigate("https://www.facebook.com/");
            label4.Text = DateTime.Now.ToString();
            w.WriteLine(label4.Text.ToString());
            w.WriteLine(Environment.NewLine);
            label5.Visible = false;
            label2.Visible = false;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                if (e.Url.AbsoluteUri != webBrowser1.Url.AbsoluteUri)
                {
                    return;
                }


                wb = webBrowser1;

                foreach (HtmlElement he in wb.Document.All.GetElementsByName("xhpc_message"))
                {
                    he.SetAttribute("value", RandomString(10));
                }
                var elems = wb.Document.GetElementsByTagName("button");

                foreach (HtmlElement elem in elems)
                {
                    if (elem.InnerText == "Post")
                    {
                        elem.InvokeMember("click");
                    }
                }
                sent = true;
                postsCounter += 1;
                label2.Text = postsCounter.ToString();
                label2.Visible = true;
                timer1.Enabled = true;
                webBrowser1.Dispose();
                if (postsCounter == 720)
                {
                    w.WriteLine(postsCounter.ToString());
                    w.WriteLine(Environment.NewLine);
                    label5.Text = DateTime.Now.ToString();
                    label5.Visible = true;
                    w.WriteLine(label5.Text.ToString());
                    w.Close();
                }
            }
            catch(Exception err)
            {
                string myerr = err.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<string> results = new List<string>();
            HtmlElementCollection elems = wb.Document.GetElementsByTagName("INPUT");
            foreach (HtmlElement elem in elems)
            {
                String nameStr = elem.GetAttribute("value");
                results.Add(nameStr);
            }  
        }

        bool sent = false;
        int count = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                count += 1;
                if (sent == true && count >= 60)
                {
                    count = 0;
                    timer1.Enabled = false;
                    webBrowser1 = new WebBrowser();
                    if (webBrowser1.IsBusy == false)
                    {
                        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
                        webBrowser1.Navigate("https://www.facebook.com/");
                    }
                    sent = false;
                }
            }
            catch(Exception err)
            {
                string myerr = err.ToString();
            }
        }

        private StringBuilder builder;
        private static Random random = new Random((int)DateTime.Now.Ticks);
        private string RandomString(int size)
        {
            try
            {
                builder = new StringBuilder();
                char ch;
                for (int i = 0; i < size; i++)
                {
                    ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                    builder.Append(ch);
                }
            }
            catch(Exception err)
            {
                string myerr = err.ToString();
            }
            return builder.ToString();
        }
    }
}
Daniel Hamutel
  • 643
  • 1
  • 13
  • 30

2 Answers2

0

I believe this is what you're looking for:

var client = new FacebookClient(mytokenaslip);

//1-25
dynamic result = client.Get("/me/posts", new { limit = "25", offset = "0"});

//26-50
dynamic result = client.Get("/me/posts", new { limit = "25", offset = "25"});

You can also chose to get more than 25 posts at once.

//51-100
dynamic result = client.Get("/me/posts", new { limit = "50", offset = "50"});
Oceans
  • 3,445
  • 2
  • 17
  • 38
  • Oceans right this is the logic what i mean. But how do i add it in my code to the FOR loop so it will automatic raise by offset 25 each time until result will be 0 so i know no more posts ? – Daniel Hamutel Dec 28 '15 at 12:49
  • You wish to get all previous post? Which could basically be almost infinite, are you sure this is what you want? A solution would be to add a button to load the next page. Then simply use an attribute that represents the page (**offset**) you're on. – Oceans Dec 28 '15 at 12:51
  • 2
    that´s a very un-dynamic solution...but you are right, getting all previous posts may even make you hit the api limit, because that could be a lot of calls. – andyrandy Dec 28 '15 at 12:52
  • also, you should not use an offset parameter like that, you should always use paging correctly, with the "next" parameter, as you can read in the docs. – andyrandy Dec 28 '15 at 12:53
  • You can define a larger amount of posts to get at once though. But I do agree that loading all posts is a bad idea. When deciding to implement paging, using the default api's paging system is of course recommended. I simply answered the question to get the next 25 posts. – Oceans Dec 28 '15 at 12:56
  • What i want to do in general is to delete old posts. So not all of them but let's say the rule will be if there are 50 posts on the wall then delete the last 25 old. And each time to check when there are 50 posts again delete the last 25. So it will be all the time with the newest 25 posted posts. Maybe using a timer and check each X minutes if there are 50 posts delete the last 25. The time should be check every 50 minutes. – Daniel Hamutel Dec 28 '15 at 13:07
  • 2
    in that case, you do need to loop through all the old posts and you do need a recursive function. you should add that information to your question though. – andyrandy Dec 28 '15 at 13:09
0

You can use a "recursive function" to get all entries, and the "next" parameter in the API result includes the API call for the next batch of results: https://developers.facebook.com/docs/graph-api/using-graph-api#paging

Be careful though, you may hit an API limit if you try to do this too fast and if there are too many results. Also, since you want to delete old entries and deleting one entry is one API call, you should try with a timeout after each call just to make sure not to hit a limit.

Make sure you learn and understand how recursive functions work, here´s one of countless threads about that: Help with Creating a Recursive Function C#

Community
  • 1
  • 1
andyrandy
  • 72,880
  • 8
  • 113
  • 130