FIX: Umlauts in phonebook of newer firmwares
[enigma2-plugins.git] / fritzcall / src / FritzOutlookCSV.py
1 # -*- coding: utf-8 -*-
2 #===============================================================================
3 # $Author$
4 # $Revision$
5 # $Date$
6 # $Id$
7 #==============================
8 #
9 # needs python-textutils for csv
10 #
11 try:
12         from . import _, debug, normalizePhoneNumber #@UnresolvedImport
13 except ValueError:
14         def _(string):
15                 return string
16         
17         def debug(text):
18                 print text
19         
20         import re
21         def normalizePhoneNumber(intNo):
22                 found = re.match('^\+49(.*)', intNo)
23                 if found:
24                         intNo = '0' + found.group(1)
25                 found = re.match('^\+(.*)', intNo)
26                 if found:
27                         intNo = '00' + found.group(1)
28                 intNo = intNo.replace('(', '').replace(')', '').replace(' ', '').replace('/', '').replace('-', '')
29                 found = re.match('^49(.*)', intNo) # this is most probably an error
30                 if found:
31                         intNo = '0' + found.group(1)
32                 found = re.match('.*?([0-9]+)', intNo)
33                 if found:
34                         return found.group(1)
35                 else:
36                         return '0'
37
38 def out(number,name):
39         print number + '#' + name
40
41 import csv
42 #
43 # 31: Telefon geschäftlich
44 # 37: Telefon privat
45 # 40: Mobiltelefon
46 # 1: Vorname
47 # 3: Nachname
48 # 5: Firma
49 # 8: Straße geschäftlich
50 # 11: Ort geschäftlich
51 # 13: Postleitzahl geschäftlich
52 # 14: Land/Region geschäftlich
53 # 15: Straße privat
54 # 18: Ort privat
55 # 20: Postleitzahl privat
56 # 21: Land/Region privat
57 #
58
59 def findNumber(number, filename):
60         file = open(filename)
61         if not file: return
62         addrs = csv.reader(file, delimiter=',', quotechar='"')
63         addrs.next() # skip header
64         for row in addrs:
65                 row = map(lambda w: w.decode('cp1252').encode('utf-8'), row)
66                 name = u""
67                 nameB = u""
68                 address = u""
69                 addressB = u""
70                 try: # this is just to catch wrong lines
71                         if row[31] or (row[37] and number == normalizePhoneNumber(row[37])) or (row[40] and number == normalizePhoneNumber(row[40])): # Telefon geschäftlich
72                                 no = normalizePhoneNumber(row[31])
73                                 # debug("[FritzOutlookCSV] findNumber compare (business) %s with %s for %s" %(no,number,name))
74                                 if no == number or (row[37] and number == normalizePhoneNumber(row[37])) or (row[40] and number == normalizePhoneNumber(row[40])):
75                                         if row[3]: name = row[3] # Nachname
76                                         if row[1]:
77                                                 if name:
78                                                         name = row[1] + ' ' + name # Vorname
79                                                 else:
80                                                         name = row[1]
81                                         if row[5]: # Firma
82                                                 if name:
83                                                         nameB = name
84                                                         addressB = row[5]
85                                                 else:
86                                                         nameB = row[5]
87                                         else:
88                                                 nameB = name
89                                         if not nameB: continue
90                                         nameB = (nameB + ' (' + _('work') + ')')
91                                         if row[11]: # Ort geschäftlich
92                                                 addressB = row[11]
93                                                 if row[13]: addressB =  row[13] + ' ' + addressB# Postleitzahl geschäftlich
94                                                 if row[14]: addressB = addressB + ', ' + row[14] # Land/Region geschäftlich
95                                                 if row[8]: addressB = row[8] + ', ' + addressB# Stra￟e gesch¦ftlich
96                                                 nameB = (nameB + ', ' + addressB).replace('\n', ', ').replace('\r', '').replace('#', '')
97         
98                                         if no == number:
99                                                 debug("[FritzCallPhonebook] findNumber result: " + no + ' ' + nameB)
100                                                 file.close()
101                                                 return nameB
102                         for i in [37, 40]:
103                                 if row[i]:
104                                         no = normalizePhoneNumber(row[i])
105                                         # debug("[FritzOutlookCSV] findNumber compare (home,mobile) %s with %s for %s" %(no,number,name))
106                                         if no == number:
107                                                 if row[3]: name = row[3] # Nachname
108                                                 if row[1]:
109                                                         if name:
110                                                                 name = row[1] + ' ' + name # Vorname
111                                                         else:
112                                                                 name = row[1]
113                                                 if i == 40: # Mobiltelefon
114                                                         nameHM = name + ' (' + _('mobile') + ')'
115                                                 else:
116                                                         nameHM = name + ' (' + _('home') + ')'
117                                                 if row[18]: # Ort privat
118                                                         address = row[18]
119                                                         if row[20]: address = row[20] + ' ' + address # Postleitzahl privat
120                                                         if row[21]: address = address + ', ' + row[21] # Land/Region privat
121                                                         if row[15]: address = row[15] + ', ' + address # Straße privat
122                                                 if not address: address = addressB
123                                                 if address:     nameHM = nameHM + ', ' + address
124                                                 nameHM = nameHM.replace('\n', ', ').replace('\r', '').replace('#', '')
125                                                 file.close()
126                                                 debug("[FritzCallPhonebook] findNumber result: " + no + ' ' + nameHM)
127                                                 return nameHM
128                 except IndexError:
129                         continue
130         file.close()
131         return ""
132         
133 def readNumbers(filename, outFun):
134         file = open(filename, "rb")
135         if not file: return
136         addrs = csv.reader(file, delimiter=',', quotechar='"')
137         addrs.next() # skip header
138         for row in addrs:
139                 row = map(lambda w: w.decode('cp1252'), row)
140                 name = u""
141                 nameB = u""
142                 address = u""
143                 addressB = u""
144                 try:
145                         if row[31] or row[37] or row[40]:
146                                 if row[3]: name = row[3] # Nachname
147                                 if row[1]:
148                                         if name:
149                                                 name = row[1] + ' ' + name # Vorname
150                                         else:
151                                                 name = row[1]
152                                 if row[5]: # Firma
153                                         if name:
154                                                 nameB = name
155                                                 addressB = row[5]
156                                         else:
157                                                 nameB = row[5]
158                                 else:
159                                         nameB = name
160                                 if not nameB: continue
161                                 nameB = (nameB + ' (' + _('work') + ')')
162                                 if row[11]: # Ort gesch¦ftlich
163                                         addressB = row[11]
164                                         if row[13]: addressB =  row[13] + ' ' + addressB# Postleitzahl gesch¦ftlich
165                                         if row[14]: addressB = addressB + ', ' + row[14] # Land/Region gesch¦ftlich
166                                         if row[8]: addressB = row[8] + ', ' + addressB# Stra?e gesch?ftlich
167                                         nameB = (nameB + ', ' + addressB).replace('\n', ', ').replace('\r', '').replace('#', '')
168                                 if row[31]:
169                                         number = normalizePhoneNumber(row[31])
170                                         outFun(number, nameB)
171
172                         for i in [37, 40]:
173                                 if row[i]:
174                                         number = normalizePhoneNumber(row[i])
175                                         nameHM = nameB
176                                         if row[3]: nameHM = row[3] # Nachname
177                                         if row[1]:
178                                                 if nameHM:
179                                                         nameHM = row[1] + ' ' + nameHM # Vorname
180                                                 else:
181                                                         nameHM = row[1]
182                                         if i == 40: # Mobiltelefon
183                                                 nameHM = nameHM + ' (' + _('mobile') + ')'
184                                         else:
185                                                 nameHM = nameHM + ' (' + _('home') + ')'
186                                         if row[18]: # Ort privat
187                                                 address = row[18]
188                                                 if row[20]: address = row[20] + ' ' + address # Postleitzahl privat
189                                                 if row[21]: address = address + ', ' + row[21] # Land/Region privat
190                                                 if row[15]: address = row[15] + ', ' + address # Stra￟e privat
191                                         if not address: address = addressB
192                                         if address:     nameHM = nameHM + ', ' + address
193                                         nameHM = nameHM.replace('\n', ', ').replace('\r', '').replace('#', '')
194                                         outFun(number, nameHM)
195
196                 except IndexError:
197                         continue
198         file.close()
199
200 if __name__ == '__main__':
201         import os, sys
202         cwd = os.path.dirname(sys.argv[0])
203         if (len(sys.argv) == 1):
204                 readNumbers("Kontakte.csv", out)
205         elif (len(sys.argv) == 2):
206                 # nrzuname.py Nummer
207                 findNumber(sys.argv[1], "Kontakte.csv")