1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import os |
3
|
|
|
from tkinter import messagebox |
4
|
|
|
|
5
|
|
|
name_dict={} |
6
|
|
|
unknown_index=0 |
7
|
|
|
def zero_insert(input_string): |
8
|
|
|
|
9
|
|
|
''' |
10
|
|
|
This function get a string as input if input is one digit add a zero |
11
|
|
|
:param input_string: input digit az string |
12
|
|
|
:type input_string:str |
13
|
|
|
:return: modified output as str |
14
|
|
|
''' |
15
|
|
|
if len(input_string)==1: |
16
|
|
|
return "0"+input_string |
17
|
|
|
return input_string |
18
|
|
|
|
19
|
|
|
def time_convert(input_data): |
20
|
|
|
''' |
21
|
|
|
This function convert input_sec from sec to DD,HH,MM,SS Format |
22
|
|
|
:param input_string: input time string in sec |
23
|
|
|
:type input_string:str |
24
|
|
|
:return: converted time as string |
25
|
|
|
''' |
26
|
|
|
input_sec=input_data |
27
|
|
|
input_minute=input_sec//60 |
28
|
|
|
input_sec=int(input_sec-input_minute*60) |
29
|
|
|
input_hour=input_minute//60 |
30
|
|
|
input_minute=int(input_minute-input_hour*60) |
31
|
|
|
input_day=int(input_hour//24) |
32
|
|
|
input_hour=int(input_hour-input_day*24) |
33
|
|
|
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" |
34
|
|
|
|
35
|
|
|
def VCF_init(file): |
36
|
|
|
''' |
37
|
|
|
This function add static part of VCF |
38
|
|
|
:param file: file object |
39
|
|
|
:return: None |
40
|
|
|
''' |
41
|
|
|
file.write("BEGIN:VCARD\n") |
42
|
|
|
file.write("VERSION:3.0\n") |
43
|
|
|
|
44
|
|
|
def VCF_name(file,first_name,last_name): |
45
|
|
|
''' |
46
|
|
|
This function add names to VCF file |
47
|
|
|
:param file: file object |
48
|
|
|
:param first_name: Name |
49
|
|
|
:type first_name:str |
50
|
|
|
:param last_name: Name |
51
|
|
|
:type last_name:str |
52
|
|
|
:return: None |
53
|
|
|
''' |
54
|
|
|
file.write("N:"+last_name+";"+first_name+";;;"+"\n") |
55
|
|
|
file.write("FN:" + first_name+" "+last_name + "\n") |
56
|
|
|
|
57
|
|
|
def VCF_phone(file,tel_mobile,tel_home,tel_work): |
58
|
|
|
''' |
59
|
|
|
This function add numbers |
60
|
|
|
:param file: file object |
61
|
|
|
:param tel_mobile: mobile number |
62
|
|
|
:type tel_mobile:str |
63
|
|
|
:param tel_home: home number |
64
|
|
|
:type tel_home:str |
65
|
|
|
:param tel_work: work number |
66
|
|
|
:type tel_work:str |
67
|
|
|
:return: None |
68
|
|
|
''' |
69
|
|
|
file.write("TEL;type=CELL:" + tel_mobile + "\n") |
70
|
|
|
file.write("TEL;type=HOME:" + tel_home + "\n") |
71
|
|
|
file.write("TEL;type=WORK:" + tel_work + "\n") |
72
|
|
|
|
73
|
|
|
def VCF_email(file,email_home,email_mobile,email_work): |
74
|
|
|
''' |
75
|
|
|
This function add emails |
76
|
|
|
:param file: file object |
77
|
|
|
:param email_home: Email |
78
|
|
|
:type email_home:str |
79
|
|
|
:param email_mobile: Email |
80
|
|
|
:type email_mobile:str |
81
|
|
|
:param email_work: Email |
82
|
|
|
:type email_work:str |
83
|
|
|
:return: None |
84
|
|
|
''' |
85
|
|
|
file.write("EMAIL;type=INTERNET;type=WORK;type=pref:" + email_work + "\n") |
86
|
|
|
file.write("EMAIL;type=INTERNET;type=HOME;type=pref:" + email_home + "\n") |
87
|
|
|
file.write("EMAIL;type=INTERNET;type=CELL;type=pref:" + email_mobile + "\n") |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
def VCF_adr(file,adr_work,adr_home): |
91
|
|
|
''' |
92
|
|
|
This function add Address |
93
|
|
|
:param file: file object |
94
|
|
|
:param adr_work: Address |
95
|
|
|
:type adr_work:str |
96
|
|
|
:param adr_home: Address |
97
|
|
|
:type adr_home;str |
98
|
|
|
:return: None |
99
|
|
|
''' |
100
|
|
|
file.write('item1.ADR;type=WORK:;; ' + adr_work + "\n") |
101
|
|
|
file.write('item2.ADR;type=HOME;type=pref:;; ' + adr_home + "\n") |
102
|
|
|
|
103
|
|
|
def VCF_website(file,website_url): |
104
|
|
|
''' |
105
|
|
|
This function add website url |
106
|
|
|
:param file: file object |
107
|
|
|
:param website_url: URL |
108
|
|
|
:type website_url:str |
109
|
|
|
:return: None |
110
|
|
|
''' |
111
|
|
|
file.write('item3.URL;type=pref:' + website_url + "\n") |
112
|
|
|
file.write("END:VCARD") |
113
|
|
|
file.close() |
114
|
|
|
|
115
|
|
|
def VCF_Folder(filename): |
116
|
|
|
''' |
117
|
|
|
This function create VCF folder from file name |
118
|
|
|
:param filename: input file name |
119
|
|
|
:type filename:str |
120
|
|
|
:return: VCF_folder_adr as str |
121
|
|
|
''' |
122
|
|
|
folder_adr=os.path.dirname(filename) |
123
|
|
|
filename_split=filename.split("/")[-1].split(".")[0] |
124
|
|
|
VCF_folder_adr = os.path.join(folder_adr, "VCF_CONVERT_" + filename_split) |
125
|
|
|
if "VCF_CONVERT_"+filename_split not in os.listdir(folder_adr): |
126
|
|
|
os.mkdir(VCF_folder_adr) |
127
|
|
|
return VCF_folder_adr |
128
|
|
|
|
129
|
|
|
def VCF_creator(folder_name,first_name,last_name,tel_mobile,tel_home,tel_work,email_home,email_work,email_mobile,adr_work,adr_home,website_url): |
130
|
|
|
''' |
131
|
|
|
This function create VCF files |
132
|
|
|
:param folder_name: Folder name |
133
|
|
|
:param first_name: Name |
134
|
|
|
:param last_name: Name |
135
|
|
|
:param tel_mobile: tel |
136
|
|
|
:param tel_home: Tel |
137
|
|
|
:param tel_work: Tel |
138
|
|
|
:param email_home: Email |
139
|
|
|
:param email_work:Email |
140
|
|
|
:param email_mobile: Email |
141
|
|
|
:param adr_work: Address |
142
|
|
|
:param adr_home: Address |
143
|
|
|
:param website_url: URL |
144
|
|
|
:return: None |
145
|
|
|
''' |
146
|
|
|
file=open(os.path.join(folder_name,last_name+"_"+first_name+".vcf"),"w") |
147
|
|
|
VCF_init(file) |
148
|
|
|
VCF_name(file,first_name,last_name) |
149
|
|
|
VCF_phone(file,tel_mobile,tel_home,tel_work) |
150
|
|
|
VCF_email(file,email_home,email_mobile,email_work) |
151
|
|
|
VCF_adr(file,adr_work,adr_home) |
152
|
|
|
VCF_website(file,website_url) |
153
|
|
|
|
154
|
|
|
def name_dict_update(name): |
155
|
|
|
''' |
156
|
|
|
This function save number of each name |
157
|
|
|
:param name: input name |
158
|
|
|
:type name:str |
159
|
|
|
:return:None |
160
|
|
|
''' |
161
|
|
|
global name_dict |
162
|
|
|
if name not in name_dict.keys(): |
163
|
|
|
name_dict[name] = 0 |
164
|
|
|
else: |
165
|
|
|
name_dict[name] = name_dict[name] + 1 |
166
|
|
|
|
167
|
|
|
def VCF_write(temp,name_dict,foldername): |
168
|
|
|
''' |
169
|
|
|
This function write VCF files in loop (call VCF_creator) |
170
|
|
|
:param temp: list of each row information |
171
|
|
|
:type temp:list |
172
|
|
|
:param name_dict: dictionary of each name number |
173
|
|
|
:type name_dict:dict |
174
|
|
|
:param foldername: folder name |
175
|
|
|
:type foldername:str |
176
|
|
|
:return: None |
177
|
|
|
''' |
178
|
|
|
name = temp[0] + "," + temp[1] |
179
|
|
|
name_dict_update(name) |
180
|
|
|
global unknown_index |
181
|
|
|
if len(temp[0]) == 0 and len(temp[1]) == 0: |
182
|
|
|
unknown_index += 1 |
183
|
|
|
VCF_creator(foldername, str(unknown_index),"Unknown ", temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], |
184
|
|
|
temp[8], temp[9], temp[10]) |
185
|
|
|
else: |
186
|
|
|
if name_dict[name] != 0: |
187
|
|
|
VCF_creator(foldername, temp[0] + "_" + str(name_dict[name]), temp[1], temp[2], temp[3], temp[4], temp[5], |
188
|
|
|
temp[6], temp[7], temp[8], temp[9], temp[10]) |
189
|
|
|
else: |
190
|
|
|
VCF_creator(foldername, temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8], |
191
|
|
|
temp[9], temp[10]) |
192
|
|
|
def csv_reader(file_name): |
193
|
|
|
''' |
194
|
|
|
This function read input csv file and parse it |
195
|
|
|
:param file_name: file name or address of file |
196
|
|
|
:type file_name:tr |
197
|
|
|
:return: vcf_counter as integer |
198
|
|
|
''' |
199
|
|
|
try: |
200
|
|
|
file=open(file_name,"r") |
201
|
|
|
|
202
|
|
|
vcf_counter=0 |
203
|
|
|
|
204
|
|
|
foldername=VCF_Folder(file_name) |
205
|
|
|
for index,line in enumerate(file): |
206
|
|
|
if index>0: |
207
|
|
|
stripped_line=line.strip() |
208
|
|
|
temp=stripped_line.split(",") |
209
|
|
|
if len(temp)>11: |
210
|
|
|
print("[Warning] CSV File Line "+str(index)+" Bad Format") |
211
|
|
|
continue |
212
|
|
|
else: |
213
|
|
|
VCF_write(temp,name_dict,foldername) |
214
|
|
|
vcf_counter+=1 |
215
|
|
|
return vcf_counter |
216
|
|
|
|
217
|
|
|
except FileNotFoundError: |
218
|
|
|
print("[Warning] Please Open CSV File") |
219
|
|
|
except Exception: |
220
|
|
|
messagebox.showinfo("CSV2VCF", "Error In Reading Input File") |
221
|
|
|
print("[Error] In Reading Input File") |
222
|
|
|
|
223
|
|
|
|