1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import os |
3
|
|
|
|
4
|
|
|
def zero_insert(input_string): |
5
|
|
|
''' |
6
|
|
|
This function get a string as input if input is one digit add a zero |
7
|
|
|
:param input_string: input digit az string |
8
|
|
|
:type input_string:str |
9
|
|
|
:return: modified output as str |
10
|
|
|
''' |
11
|
|
|
if len(input_string)==1: |
12
|
|
|
return "0"+input_string |
13
|
|
|
return input_string |
14
|
|
|
|
15
|
|
|
def time_convert(input_data): |
16
|
|
|
''' |
17
|
|
|
This function convert input_sec from sec to DD,HH,MM,SS Format |
18
|
|
|
:param input_string: input time string in sec |
19
|
|
|
:type input_string:str |
20
|
|
|
:return: converted time as string |
21
|
|
|
''' |
22
|
|
|
input_sec=input_data |
23
|
|
|
input_minute=input_sec//60 |
24
|
|
|
input_sec=int(input_sec-input_minute*60) |
25
|
|
|
input_hour=input_minute//60 |
26
|
|
|
input_minute=int(input_minute-input_hour*60) |
27
|
|
|
input_day=int(input_hour//24) |
28
|
|
|
input_hour=int(input_hour-input_day*24) |
29
|
|
|
return zero_insert(str(input_day))+" days, "+zero_insert(str(input_hour))+" hour, "+zero_insert(str(input_minute))+" minutes, "+zero_insert(str(input_sec))+" seconds" |
30
|
|
|
|
31
|
|
|
def VCF_init(file): |
32
|
|
|
file.write("BEGIN:VCARD\n") |
33
|
|
|
file.write("VERSION:3.0\n") |
34
|
|
|
|
35
|
|
|
def VCF_name(file,first_name,last_name): |
36
|
|
|
file.write("N:"+last_name+";"+first_name+";;;"+"\n") |
37
|
|
|
file.write("FN:" + first_name+" "+last_name + "\n") |
38
|
|
|
|
39
|
|
|
def VCF_phone(file,tel_mobile,tel_home,tel_work): |
40
|
|
|
file.write("TEL;type=CELL:" + tel_mobile + "\n") |
41
|
|
|
file.write("TEL;type=HOME:" + tel_home + "\n") |
42
|
|
|
file.write("TEL;type=WORK:" + tel_work + "\n") |
43
|
|
|
|
44
|
|
|
def VCF_email(file,email_home,email_mobile,email_work): |
45
|
|
|
file.write("EMAIL;type=INTERNET;type=WORK;type=pref:" + email_work + "\n") |
46
|
|
|
file.write("EMAIL;type=INTERNET;type=HOME;type=pref:" + email_home + "\n") |
47
|
|
|
file.write("EMAIL;type=INTERNET;type=CELL;type=pref:" + email_mobile + "\n") |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
def VCF_adr(file,adr_work,adr_home): |
51
|
|
|
file.write('item1.ADR;type=WORK:;; ' + adr_work + "\n") |
52
|
|
|
file.write('item2.ADR;type=HOME;type=pref:;; ' + adr_home + "\n") |
53
|
|
|
|
54
|
|
|
def VCF_website(file,website_url): |
55
|
|
|
file.write('item3.URL;type=pref:' + website_url + "\n") |
56
|
|
|
file.write("END:VCARD") |
57
|
|
|
file.close() |
58
|
|
|
def VCF_creator(first_name,last_name,tel_mobile,tel_home,tel_work,email_home,email_work,email_mobile,adr_work,adr_home,website_url): |
59
|
|
|
if "VCF_CONVERT" not in os.listdir(): |
60
|
|
|
os.mkdir("VCF_CONVERT") |
61
|
|
|
file=open(os.path.join("VCF_CONVERT",last_name+"_"+first_name+".vcf"),"w") |
62
|
|
|
VCF_init(file) |
63
|
|
|
VCF_name(file,first_name,last_name) |
64
|
|
|
VCF_phone(file,tel_mobile,tel_home,tel_work) |
65
|
|
|
VCF_email(file,email_home,email_mobile,email_work) |
66
|
|
|
VCF_adr(file,adr_work,adr_home) |
67
|
|
|
VCF_website(file,website_url) |
68
|
|
|
|
69
|
|
|
def csv_reader(file_name): |
70
|
|
|
try: |
71
|
|
|
file=open(file_name,"r") |
72
|
|
|
unknown_index=0 |
73
|
|
|
vcf_counter=0 |
74
|
|
|
for index,line in enumerate(file): |
75
|
|
|
if index>0: |
76
|
|
|
stripped_line=line.strip() |
77
|
|
|
temp=stripped_line.split(",") |
78
|
|
|
if len(temp)>11: |
79
|
|
|
print("[Warning] CSV File Line "+str(index)+" Bad Format") |
80
|
|
|
continue |
81
|
|
|
else: |
82
|
|
|
if len(temp[0])==0 and len(temp[1])==0: |
83
|
|
|
unknown_index+=1 |
84
|
|
|
VCF_creator("Unknown ",str(unknown_index),temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8],temp[9],temp[10]) |
85
|
|
|
else: |
86
|
|
|
VCF_creator(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8],temp[9],temp[10]) |
87
|
|
|
vcf_counter+=1 |
88
|
|
|
return vcf_counter |
89
|
|
|
|
90
|
|
|
except Exception as e: |
91
|
|
|
print("[Error] In Reading CSV File") |
92
|
|
|
|
93
|
|
|
|