Switching to address based map generation. Divided to subroutines.
[treecutter.git] / xinclude / contact.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 from vobject import readComponents
5 import sys
6 from addr import mapimage, geocode
7 from geohash import encode
8
9 def coord2dms(coord):
10   ns = coord[0]
11   ew = coord[1]
12   mnt,sec = divmod(ns*3600,60)
13   deg,mnt = divmod(mnt,60)
14   out = u'''%d°%2d'%5.2f"%s''' % ( deg,mnt,sec,'N')
15   mnt,sec = divmod(ew*3600,60)
16   deg,mnt = divmod(mnt,60)
17   out +=  u''' %d°%2d'%05.2f"%s''' % ( deg,mnt,sec,'E')
18   return out
19
20
21 def maplink(lat,lon):
22   return '''
23 <uri type="location">
24   <link xlink:href=
25    "http://www.openstreetmap.org/?mlat=%s&amp;mlon=%s&amp;zoom=18&amp;layers=M">
26   <inlinemediaobject>
27     <imageobject condition="web">
28       <imagedata fileref="%s.png" format="PNG"/>
29     </imageobject>
30     <textobject>
31       <phrase>geo:%s,%s</phrase>
32     </textobject>
33   </inlinemediaobject>
34   <para>%s</para>
35   </link>
36 </uri>
37 ''' % (lat, lon, encode(float(lat), float(lon)), lat, lon, coord2dms((lat, lon)))
38
39 for arg in sys.argv[1:]:
40   al = arg.split("=")
41   if al[0] == "lang":
42     lang = al[1]
43   if al[0] == "xptr":
44     argument = al[1]
45
46
47 (cards,query) = argument.split('?')
48 (key, name) = query.split(':')
49 with open(cards, 'r') as f:
50   card_data = f.read()
51 f.closed
52
53 # Scan for the correct card
54 found = None
55 out = u''
56 for card in readComponents(card_data):
57 # card.prettyPrint()
58   if key in card.contents.keys():
59     if name.decode('utf-8') == card.contents[key][0].value[0]:
60       found = card
61   if key == 'firstname':
62     if name.decode('utf-8') == card.n.value.given:
63       found = card
64   if key == 'surname':
65     if name.decode('utf-8') == card.n.value.family:
66       found = card
67
68 if not found:
69   print query+' failed in '+cards
70   exit(2)
71 # when card is found parse it to docbook.
72 pn = ''
73
74 if 'n' in found.contents.keys():
75   n = found.n.value
76   empty = n.prefix == '' and n.given == '' and \
77       n.additional =='' and n.family ==''  and n.suffix == ''
78   if not empty:
79     pn += '<personname>'
80     if n.prefix != '':
81       pn += '<honorific>'+n.prefix+'</honorific>'
82     if n.given != '':
83       pn += '<firstname>'+n.given+'</firstname>'
84     if n.additional != '':
85       pn += '<othername>'+n.additional+'</othername>'
86     if n.family != '':
87       pn += '<surname>'+n.family+'</surname>'
88     if n.suffix != '':
89       pn += '<lineage>'+n.suffix+'</lineage>'
90     pn += '</personname>'
91
92 ad = ''
93 if 'adr' in found.contents.keys():
94   for a, t in zip(found.contents['adr'],found.contents['tel']):
95     ad += '\n<address type="'+a.type_param+'">'
96     if a.value.street != '':
97       ad += '<street>'+a.value.street+'</street>'
98     if a.value.code != '':
99       ad += '<postcode>'+a.value.code+'</postcode>'
100     if a.value.city != '':
101       ad += '<city>'+a.value.city+'</city>'
102     if a.value.country != '':
103       ad += '<country>'+a.value.country+'</country>'
104     if t.value != '':
105       ad += '<phone>'+t.value+'</phone>'
106     ad += '</address>'
107     geostr = u''+a.value.street+', '+a.value.city+', '+a.value.country
108     (lat,lon) = geocode(geostr.encode('utf-8'))
109     mapimage([(float(lat),float(lon))])
110     ad += maplink(lat,lon)
111
112 o = ''
113 if 'org' in found.contents.keys():
114   o = '''
115       <org>
116         <orgname>'''+found.org.value[0]+'''</orgname>
117         '''+ad+'''
118         </org>'''
119
120
121 url = ''
122 if 'url' in found.contents.keys():
123   url += '<uri type="website"><link xlink:href="'+found.url.value+'"/></uri>'
124 geo = ''
125 #if 'geo' in found.contents.keys():
126 #  (lat,lon) = found.geo.value.split(';')
127 #  mapimage([(float(lat),float(lon))])
128 #  # create picture
129 #  geo += maplink(lat,lon)
130
131
132 # Turn off email for now
133 email = ''
134 #if 'email' in found.contents.keys():
135 #  email += '<email>'+found.email.value+'</email>'
136
137 if empty:
138   content = pn+o+url+geo+email
139 else:
140   if o == '':
141     content = '<person>'+pn+ad+url+geo+email+'</person>'
142   else:
143     content = '<person>'+pn+'<affiliation>'+o+'</affiliation>'+url+geo+email+ \
144         '</person>'
145 out = '''
146 <para xmlns="http://docbook.org/ns/docbook"
147       xmlns:xlink="http://www.w3.org/1999/xlink">
148   '''+content+'''
149 </para>
150 '''
151
152 ex ='''
153     <address>
154       <street>Street</street>
155       <postcode>Postcode</postcode> <city>City</city>
156       <country>Country</country>
157       <phone>+1 123 456 789</phone>
158     </address>
159     <affiliation>
160       <jobtitle>Occupation</jobtitle>
161       <org>
162         <orgname>Organization</orgname>
163         <address>
164           <street>Street</street>
165           <postcode>Postcode</postcode> <city>City</city>
166           <country>Country</country>
167           <phone>+1 123 456 789</phone>
168         </address>
169       </org>
170     </affiliation>
171   </person>
172 </para>
173 '''
174
175 sys.stdout.write(out.encode('utf-8'))