Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@raphaelschaad
Last active January 27, 2016 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raphaelschaad/72addce2a3fdafdf359d to your computer and use it in GitHub Desktop.
Save raphaelschaad/72addce2a3fdafdf359d to your computer and use it in GitHub Desktop.
Renames CSV files of iMessages extracted with iExplorer from iTunes backup and prints start and end date of conversations.
#!/usr/bin/env python
#coding=utf-8
#
# imsg-cleaner.py
#
# Renames CSV files of iMessages extracted with iExplorer from iTunes backup and prints start and end date of conversations.
#
# Created by Raphael Schaad on 2013-12.
# This is free and unencumbered software released into the public domain.
#
# Usage: $ python imsg-cleaner.py <path/to/csv/directory>
#
import sys
from os import listdir
import os
import datetime
start_date = None
end_date = None
dir_path = sys.argv[1]
for filename in listdir(dir_path):
src_path = os.path.join(dir_path, filename)
# Remove double-spaces and space before file extension.
dst_filename = filename.replace(' ', ' ')
dst_filename = dst_filename.replace(' .csv', '.csv')
dst_path = os.path.join(dir_path, dst_filename)
if src_path != dst_path:
print('Renaming "' + filename + '" to "' + dst_filename + '"')
os.rename(src_path, dst_path)
# Find start and end date of conversations. Date example: Mar 1, 2012, 12:43:50 AM
file_handler = open(dst_path, 'r')
file_lines = file_handler.readlines()
for file_line in file_lines:
values = file_line.split('","')
for value in values:
try:
date_time = datetime.datetime.strptime(value, '%b %d, %Y, %I:%M:%S %p')
date = datetime.datetime.strftime(date_time, '%Y-%m-%d')
if start_date == None or date < start_date:
start_date = date
if end_date == None or date > end_date:
end_date = date
except:
continue
file_handler.close()
print ('start date: ' + start_date)
print ('end date: ' + end_date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment