Programming: Blog Post Re-share Bot – part 2

For more information, look at my previous post.

Again, this post will be heavily technical. In my last post, I had started work on my bot but I had issues. I wasn’t sure if I wanted to use the bot to publish posts via Twitter or Reddit. I did not have a stable database or a reliable XML Reader. Since then, here is my progress so far.

1. XML Reader & Writer:

It was an accident I learned of XPATH. This was the discovery that made reading XML easy.

Understanding XPath & how LibXML2 uses XPath took me a day but once I did, all I had to do is build a wrapper around it for the RSS file that my blog link gives out. After mentoring C on Exercism, I knew this time I had to do things right. I began first by writing Unit Tests for each step.

I made a generic structure called XML_ITEM which references the structure that needs to be populated. The piece of code I am most proud of is how I was able to use one generic function to extract a XML file into a custom structure without any issues. It looks super clean & I briefly considered making a separate GitHub repo called XML-To-Struct or vice versa. Maybe I will.

typedef struct
    // Name of the element in the xml
    char *pszElementName;
    // Type of the element
    XML_TYPES eType;
    // Offset in the output structure
    uint32_t ulMemberOffset;
    // Sizeof the output variable
    uint32_t ulBufferSize;
    // Only applicable for XML_TABLE, XML_SUB_TABLE, pointer to the table containing XML_STRs
    const void *pavSubItem;
    // Only applicable for XML_TABLE number of XML_ITEMS in pvSubItem;
    uint32_t ulArrayElements;
    // Only applicable for XML_SUB_TABLE
    uint32_t ulArraySize;

#define XML_STR(element, structure, var)                                                                 \\
    {                                                                                                    \\
        element, XML_CHILD_STRING, offsetof(structure, var), sizeof(((structure *)0)->var), _null_, 0, 0 \\

After about a week and a half, I was able to successfully read the whole RSS file without any issues. An XML_ITEM array is enough to translate an XML into Structure. I discarded the idea of making a CSV database file because I didn’t wanna write another wrapper for it & I now had a perfect XML reader. So the database file became a XML file.

2. Social Media integration

This is where I couldn’t continue. I did a minor research into Twitter APIs to post something automatically but I couldn’t find any. I am sure that there is something for tweeting as there are heaps of bots out on the website.

The reason that I stopped looking after a little search is because at that point, I was actually pretty done with the project. I wanted to start using the bot rather than make it and adding a feature of integrating with Twitter seemed like too much work.

It was also around this point I realized that as I use any software, I gain a better understanding of what is actually required and what is just fluff.

I decided to change the bot output onto the command line, use Twitter on my browser and tweet manually. I have been using the bot somewhat regularly. I updated the documentation on the project to make it easier for anyone to pick up.

3. Future work:

I can see that the project is not done. Maybe in the future I will actually implement a Twitter integration so that the app on runtime actually tweets. Not sure if I will actually do it yet.

It is only during the last couple of days I realized my database may not update cleanly. So I need to fix that first.

Other than that, who knows, maybe some other blogger will wanna use my bot too. That means I will have to clean it & actually make an executable. Currently I build it every time I change it.

But as for now, I believe I have almost completed this.

Checkout the project on GitHub.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s