This is a new section on my blog wherein I would be posting small scripts I have written to fix some very tiny troubles of life. Mostly they’re here to automate some manual work which would be cumbersome. These are not going to be rocket science. There might be little that you gain out of this, but you might gain some knowledge. While creating such scripts I often take aid of StackOverflow and get to learn something new every time. That is why I’m starting this section. I’m going to start the post with things you might learn from the post so you do not have to go through the whole post if it does not seem worth or you already are aware of it.
Remember that the codes written in these will not be very general. They were written to solve a very specific issue.
Things you might learn out of this:
- Time and Date formatting in python (strftime, strptime)
- A way to record calls on your android phone
- How to move these recordings from phone to some storage device but still keep sensible names
Sometimes a call may become too important or special to let it just go away as soon as it is over. In cases like those, ACR comes to aid. ACR is short for Automatic Call Recorder, and while there are tons of call recording applications on Play Store, this one has worked for me since almost a year now, without any problems even when I upgraded my android version. The interface is smooth and easy too. You should check it out once.
ACR asks you at the end of every call whether you want to save that call or not. So, you do not miss out on any call you would want to save. This is called the prompt feature which I forgot enabling for a small period of time, and as a conseuqence all the calls of that time were saved. I realized this when I moved all the call files (.3gp format) to my PC because I wanted to make some empty space on my phone. The problem, however, was the files looked like this :
I was like, “Oh man. This sucks.” when I saw the names of the files. Because if you look at the files in the app, it shows you by contact name and the date and the time.
I wanted to sort them in the same manner, and the first thing that struck me is if the name is really that long, it must be having a good amount of information in the name itself.
The easiest to spot things are : a phone number and date. We can clearly see numbers starting with +91 and the 2015xxyzabc.. things in the name. So I figured I would write a small python script to rename them properly once I have deciphered what these names really meant.
Let’s pick the first file name.
We clearly have a date there. In the YYYYMMDD format. 2015-12-18. That’s 18th December 2015. Following the letter ‘p’, we have an indian phone number. Questions that remained were:
- What does the starting ‘0’ mean?
- What are the 6 digits after the date?
To identify that, I put back one of the files into my phone and observed the time it displayed for the call. Those six digits basically represented the time of starting of recording in HHMMSS format. For example the above file is a recording made at 08:55am. I couldn’t decipher what the 0 meant at the starting, and some files had 1 at the starting, some had 2. It didn’t really matter as I had already extracted all I needed from the file.
Deciphering was done. The next step was to write a script that converts these file names into the format I wanted : ContactName_Date_Time.3gp
I divided this work into three steps:
- I wrote a script that looked at all the files in the folder and extracted the numbers out of them, and made a set (unique collection of elements) of those numbers. This set of numbers were then written into a text file.
- Manually I wrote the names in the text file beside the numbers. This step can be automated by using Google Contacts API if you have all the contacts on Google and you have a large set of numbers.
- I wrote a script that looked into each file of this folder, fetched the number and accordingly found the contact name, and then renamed the file to a more human readable format.
The first script looks like this:
We’ll be using Set for this. This is present in sets library and has been imported. More info here and here.
Line 5 – we create a new Set
Line 7 –
os.listdir lists contents of a directory.
os.getcwd() passes to it the current directory. This statement would loop the following code for every file in the current directory.
Line 9 separates the extension and the filename. Line 10 further separates the date part and the phone number part.
Line 14-17 basically take care of removing possible extension from the phone number, be it a 0 or +91. In my case there were two only.
Line 22 adds the phone number to the dictionary.
Line 24-27 takes care of printing these numbers to a file.
This generated a file of this sort.
Which I changed to a file like this:
Then I wrote the second script:
Till line 20, we’re doing repetitive work as we did in the first script.
Line 23, we come across python date time parser function –
strptime. In python, date (and time) is an object, not just a string. This function converts a string to a datetime object. Inside, we provide as the first argument the string that has the date and time, and the second argument tells the parser in what format is the date & time presented in the string. Once we have a datetime object, we can convert it to whatever format we want. I particularly wanted a very human readable format, something of the form – 13_Jun_2015 rather than 2015_06_13.
Line 24, we exactly fulfill that. A datetime object can be converted into whatever format we want by using the datetime formatter function in python –
The placeholders in the argument each represent something :
%dis the day number
%mis the month number
%bis the month abbreviation
%yis the year last two digits
%Yis the all year
More info here.
Line 28-33, we open the
phonenumbers.txt file we created and find the line that contains the
phonenum and use the corresponding name after that as the string value of the variable
In Line 34, we remove any whitespaces that might have come at the end of the name.
In Line 36, we append all the useful information to create the new name of the file
In Line 38, we use rename command of os library to rename the file from previous name to new name.
Hope you were able to understand everything if you went through all this. Let me know in the comments section if there’s something I missed, or something you would like me to cover. See ya guys!
December 20, 2015 at 4:41 pm
Bhavul, a well written article, with each process involved lucidly expressed. On the whole, a brilliantly useful piece!!