All posts by swapnilsarwe

PHP Whatsapp Email Conversation Parser

This PHP script parses the Whatsapp email conversation from the text file, which is sent to you in email when you say "Email Conversation" from Whatspp

Whatsapp is more than a need now. From being a casual chat application used between friends and family members to a more reliable application for even formal and serious communication, Whatsapp has grown wild. There is a lot of information and data in between the conversation stored on your mobile all the time. And the fact that Whatsapp never store this information on there servers your mobile has the only last backup copy of what you have communicated in the past. I usually use to email the conversation to my own email address just as a backup, but then that backup was just the text file where searching something was a pain, and what if I want to know about the conversation on a particular date. Hence I wrote this parser to see if it benefits me in someway or the other.

So I followed the regular steps to write a parser using PHP.

  1. Read the file
  2. Parse the single complete message sent in the conversation
  3. Extract information from that single message (eg: name, date & time, message)
  4. Once you have the extracted information you can store it wherever you want (In this script I am using SQLite 3)

Reading the file & parsing the information

I used fgets to read the content of the file line by line. But then there were messages which were spread over more than a line. So I first found out the pattern in which the message starts and I keep on reading the file line by line unless I match the pattern where I am sure that my previous message is complete and I can store it and then move on to the newly matched message.

The pattern to match the start of the message:

(([A-Za-z]{1,3}\s[0-9]{1,2},\s[0-9]{1,2}[:]{1}[0-9]{1,2}\s[APM]{2})|([A-Za-z]{1,3}\s[0-9]{1,2},\s[0-9]{1,4},\s[0-9]{1,2}[:]{1}[0-9]{1,2}\s[APM]{2}))\s-\s(.*):\s([\s\S]*)

I am not an expert in Regex but this pattern is what I was able to build which works fine AFAIK.

And the script which continuously reads line by line before concluding that the message is complete is:

$handle = @fopen(path-of the-file-to-parse, "r");
if ($handle) {
$tmpBuffer = "";
$line = 0;
while (($buffer = fgets($handle)) !== false) {
$matches = array();
$regEx = '/' . $Regex . '/';
preg_match($regEx, $buffer, $matches);
if (count($matches) == 6 && $line != 0) {
// here we know that the new line has been matched and hence we can store the message we were reading till now before this line
$tmpBuffer = $buffer;
} else {
//here we know that its not the start of the message that means it is the part of the old message and we append it to the message
$tmpBuffer .= $buffer;
}
$line++;
}
// store the very last message of the conversation
}

Extracting the information from the message

The same regex does the job again, we extract the datetime, person name & the message using the same regex. But there are 2 kinds of time stamp, for current year message the year is not mentioned and for the past year it is. But the regex takes care of that

function parseAndStore($msg)
{
$arrRecord = array();
$matches = array();
$regEx = '/' . $Regex . '/i';
preg_match($regEx, $msg, $matches);
if (count($matches) == 6) {
$arrRecord["datetime"] = isset($matches[2])?$matches[2]:$matches[3];;
$arrRecord["author"] = $matches[4];;
$arrRecord["message"] = $matches[5];
}

if (count($arrRecord) > 0) {
// store the message
}
return false;
}

Thats it, you can get the complete script on Github – Whatsapp Email Conversation Parser

Let me know if you have any suggestions or doubts.

PHP URL Builder Form

The tool allows you to play with the URL parameters in best possible manner.

Being a Web Developer, URL plays a very vital role in my life. On a daily basis I have to play with the URL for various reasons for several different purposes. Most of the times I find myself searching for the particular parameter in the URL in the address bar of the browser to change and test something or the other. For short URLs editing right into the address bar seems to work very fine. But when dealing with long URLs it becomes frustrating to find the particular parameter and edit it and when you want to edit several parameters the job seems to be like hell.

So I thought how would I simplify the process in a neat clean manner. And hence the idea of PHP URL Builder Form came up.

PHP URL Builder basically is the tool, where I can put a sample URL and submit. Which will give me all the parameters in the HTML Form with the values in the text boxes which can be edited easily. On submission of this form you are given the updated URL with the clickable link.

I hope you find this tool useful. WIll be working more to add new features like

  • Adding of new query parameters to the URL dynamically with JS
  • Validation to consider all kinds of URLs
  • Code it in a much cleaner manner for optimum development

You can find the code on github page for PHP URL Builder Form
You can test it out here at this link

My first nearly 50 km bicycle ride from Andheri to Churchgate and back

My first long distance travelling on bicycle with friends. From Andheri to Churchgate and back.

After I purchased a Rockrider 5.0 bicycle in Oct, 2012, I was always wondering when would I go for long tours on bicycle. And whenever I will go, how far I will be making it before giving up. But when I was doubting my abilities, my friend Surya Sen said when you are on bicycle dont think about the distance you are going to cover, just keep riding it till you can.

And say what on 11th May 2013, me and my friend Vinay Shriyan started our bicycle ride from Andheri Subway at 5.00 AM. We were heading for Mahim where our another friend Purushottam Bhatt (Puru) was about to join us. Puru was the one who knew the road to the Churchgate.

Since it was the early morning we never realised when we covered almost 50 % of the distance in an hour. And I realised riding bicycle in the morning is the best thing to do. You get fresh air with lot of oxygen to breath in and compraratively less traffic to maneuver. And frankly speaking riding bicycle is so close to being able to fly. LOL! Thats how I think of riding bicycle.

On the way we saw many fellow bicycle rider who take the benefit of early cold morning in Summer Time, where driving after 10.00 AM feels like setting yourself on fire. And before we realised we were at Marine Drive, which is approximately 24-25 kms from Andheri subway, at 6.50 AM. That was wonderful feeling to able to make it up to the destination and that too without any problems. I thought it would be extremely tough and exhausting but no, it was much better, exciting and wonderful.

We took a break at Marine Drive and sat there for around 15 minutes before heading back to our initial point. On the way back it was more difficult considering roads were crowded and roads feeled with vehicles and people all around. After seeing off Puru back in Mahim we were on the way back to Andheri. It took us around the same time i.e. almost 2 hrs to reach Andheri but contradictory to the previous ride, the ride back was tough and considering the sun was out now, it was much more exhausting. And when we reached back to the initial starting point we had already convered 47-49 kms in total in 4 hrs.

After this ride I am waiting for next venture out. We are planning to goto Matheran & Lonavla in near future on bicycle. Just waiting for the correct timing when all of us will be available and all set to go for the new ride. Till them keep riding.

my-first-nearly-50-km-bicycle-ride-from-andheri-to-churchgate-and-back

my-first-nearly-50-km-bicycle-ride-from-andheri-to-churchgate-and-back