DEV Community

Ashinze Ekene
Ashinze Ekene

Posted on

How I got my cousemates' birthday dates

My love for automation and looking for shortcuts at it again. I've always wanted to have all my classmates names (Surname and Firstname) in my address book and their birthday dates so I can send them birthday wishes even afer leaving school. A google form was created for us to fill in our profile(final year stuff), I had access to it, so I downloaded the form in csv. Of course I could have manually created entries for each of the 86 of them, but this would have been time consuming. I did some research on vCard and vcf files using these resources https://en.wikipedia.org/wiki/VCard and https://tools.ietf.org/html/rfc6350. I found out that the basic format for a vcf is like:

BEGIN:VCARD
VERSION:2.1
N:Gump;Forrest;;Mr.
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TEL;HOME;VOICE:(404) 555-1212
ADR;WORK;PREF:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;HOME;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:42 Plantation St.=0D=0A=
 Baytown, LA 30314=0D=0AUnited States of America
EMAIL:forrestgump@example.com
END:VCARD
Enter fullscreen mode Exit fullscreen mode

so I wrote a code in python so create a vcard entry for each classmate from the csv file which had the following rows

"Name","Nickname","Date of Birth","State of Origin","Phone Number","Twitter Handle","Instagram Handle","Snapchat handle","Philosophy of Life","Best Department Course","Worst Department Course"...
Enter fullscreen mode Exit fullscreen mode

Here's the code:

import csv

VCF_TEXT = ''

with open('./Final Year Bioscope Form.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    lines_read = 0
    for row in csv_reader:
        if lines_read > 0:
            VCF_TEXT += ("BEGIN:VCARD\n"
            "VERSION:3.0\n" +
            "N:{}\n".format(row['Name'].replace(',', '')) +
            "FN:{}\n".format(row['Name'].replace(',', '')) +
            "NICKNAME:{}\n".format(row['Nickname']) +
            "NOTE:{}\n".format(row['Philosophy of Life']) +
            "X-TWITTER:{}\n".format(row['Twitter Handle']) +
            "TEL;WORK;VOICE:{}\n".format(row['Phone Number']) +
            "BDAY:{}\n".format(row['Date of Birth']) +
            "CATEGORIES:University of Lagos,CellBiology and Genetics,Helicase 18\n" +
            "END:VCARD\n")
        lines_read += 1

with open('Final Year Bioscope Form.vcf', 'w+') as vcf_file:
    vcf_file.write(VCF_TEXT)
Enter fullscreen mode Exit fullscreen mode

One challenge I had was the N field from the CSV required that the names be seperated by semicolon(;) but some people had their names seperated by a comma (,) while other used a whitespace, so I used this regular expression to solve my issue

import re

NAME_SUB = re.compile('( +|,) ?')
NAME_SUB.sub(';', row['Name'])
Enter fullscreen mode Exit fullscreen mode

The code replaces commas and spaces and its different combinations with the appropriate delimiter.

Next issue was some with the birthday date. Google saved birthdays in this format

BDAY: Jul 23
Enter fullscreen mode Exit fullscreen mode

but I got this format from the csv 06-23 so I had to convert it to the appropriate format. This helped

from datetime import datetime

strDate = row['Date of Birth']
objDate = datetime.strptime(strDate, '%m-%d')
bday = datetime.strftime(objDate,'%b %d')
Enter fullscreen mode Exit fullscreen mode

It reads the birthday date from the csv and converts it to the required format. At the end the reformed code looked like this

import csv
import re
from datetime import datetime

VCF_TEXT = ''

NAME_SUB = re.compile('( +|,) ?')
with open('./Final Year Bioscope Form.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    lines_read = 0
    for row in csv_reader:
        strDate = row['Date of Birth']
        objDate = datetime.strptime(strDate, '%m-%d')
        bday = datetime.strftime(objDate,'%b %d')
        if lines_read > 0:
            VCF_TEXT += ("BEGIN:VCARD\n"
            "VERSION:3.0\n" +
            "N:{}\n".format(NAME_SUB.sub(';', row['Name'])) +
            "FN:{}\n".format(row['Name'].replace(',', '')) +
            "NICKNAME:{}\n".format(row['Nickname']) +
            "NOTE:{}\n".format(row['Philosophy of Life']) +
            "X-TWITTER:{}\n".format(row['Twitter Handle']) +
            "TEL;WORK;VOICE:{}\n".format(row['Phone Number']) +
            "BDAY:{}\n".format(bday) +
            "CATEGORIES:University of Lagos,CellBiology and Genetics,Helicase 18\n" +
            "END:VCARD\n")
        lines_read += 1

with open('Final Year Bioscope Form.vcf', 'w+') as vcf_file:
    vcf_file.write(VCF_TEXT)
Enter fullscreen mode Exit fullscreen mode

After running the code. It generated the vcf for me. All I then did was import the vcf file into google contacts. This can be done on mobile and on the web. And hey presto I had all my classmates full names, phone numbers, birthday dates and twitter handles.

Top comments (0)