I haven't got time to update this blog in the past months. Whatever. This post's idea is not this. This post's idea is automation.
I'm a lazy person, and all the people who know me know this. They also know that I'm not a stupid person, I'm just lazy and I try to resolve things in the easiest way possible.
A few weeks ago, someone told me that he wants to post an announce on a site every time he writes a specific post on his blog. He wanted something like a RSS, but only with the title and posted on other site. No RSS clients involved, no fancy and complicated apps, just pure HTML/CSS and a little work moving and copying the links and titles from one site to another. This was OK for a few times, but when I saw that he writes a few times per day, almost 3-4 times per week, I told to myself: Well, this is not going to work. It's not going to work this way, but it's gonna work my way. And you bet your ass that I did it my way. Just like Frank Sinatra.
I'm not going to post here the script, but I'm gonna write how I automated that boring task.
First of all, I was lucky since only one type of posts written by him were the ones that interested me. Let's say that the title was someting like "Weather in: \<city>". Since the keyword here is weather, then I could search for that keyword and voila, half of job is already done. But then, I'd have created me another problem. Because you know, when you use regex to solve a problem, you create yourself two problems.
Mine was that I must save the last link/title posted by me, and the last link/title posted by him. Then, if there are any other new links or titles between that frame, iterate through them and post them on the site. A little too much for a simple shell script. Doable, but too much. I gave up on writing that script, but I had other idea in mind.
So I did the standard approach: "Hey, can you send me a mail with any new links that you post? It would really help me if you could organize the new links in an email. I don't want to miss any new post and if you could write the post and then mail it back to me, it would be great."
The idea in mind was that I could fetch the mail somehow, parse it, regex the keyword and update the site via a cron job. All using CLI. Nice, but I still tought that I was overthinking. I needed something easier, more like a oneliner. Something that didn't need another program installed, something that I could use with a shell and standard Unix/BSD/Linux installation. And then it hit me: a shell script with arguments.
I have made this before. I have made some time ago a shell script that downloaded the apk from Aptoide site. (You cand find the script here, but I don't think it's working anymore)
So how can you do that? I told you that I had more than one link. I must somehow run the script with "script link1 link2 link3" if I want to parse all the links in one run. You can do this with a for to iterate through parameters and $@. (See #4 from this site)
Then it's all in wget-ing, sed-ing, some grep-ing and then echo the whole thing between a \<\/h3> and the next \<h3>. (End of a heading, start of another one.)
And we got this:
./lazy_job link1 link2 link3 ...
Until the next post, stay classy Internet!