From The Compiler, 6 Years ago, written in Python.
Embed
  1. #!/usr/bin/python3
  2. # vim: set fileencoding=utf-8 ts=4 sts=4 sw=4 tw=80 expandtab :
  3.  
  4. # Copyright (C) 2012 Florian Bruhin <me@the-compiler.org>
  5.  
  6. # NAME is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # NAME is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with NAME.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. import sys
  20. import json
  21.  
  22. pclist = []
  23.  
  24. class Addressbook():
  25.     contacts = []
  26.  
  27.     def __init__(self, contacts):
  28.         self.contacts = contacts
  29.  
  30.  
  31. class Contact():
  32.     addresses = None
  33.     names = None
  34.     events = None
  35.     numbers = None
  36.     birthday = None
  37.  
  38.     def __init__(self, addresses, names, events, numbers, birthday):
  39.         self.adresses = addresses
  40.         self.names = names
  41.         self.events = events
  42.         self.numbers = numbers
  43.         self.birthday = birthday
  44.  
  45.     def __repr__(self):
  46.         fname = None
  47.         lname = None
  48.         for n in self.names:
  49.             if n.category == 'firstname':
  50.                 fname = n.name
  51.             elif n.category == 'lastname':
  52.                 lname = n.name
  53.         return "{} {}".format(fname or 'Unbekannt', lname or '')
  54.  
  55.  
  56. class Address():
  57.     street = None
  58.     plz = None
  59.     location = None
  60.  
  61.     def __init__(self, street, plz, location):
  62.         self.street = street
  63.         self.plz = plz
  64.         self.location = location
  65.  
  66.  
  67. class Name():
  68.     name = None
  69.     category = None
  70.    
  71.     def __init__(self, name, category):
  72.         self.name = name
  73.         self.category = category
  74.  
  75.  
  76. class Event():
  77.     name = None
  78.  
  79.     def __init__(self, name):
  80.         self.name = name
  81.  
  82.  
  83. class Number():
  84.     num = None
  85.     category = None
  86.  
  87.     def __init__(self, num, category):
  88.         self.num = num
  89.         self.category = category
  90.  
  91.  
  92. class Birthday():
  93.     day = None
  94.     month = None
  95.     year = None
  96.  
  97.     def __init__(self, day, month, year):
  98.         self.day = day
  99.         self.month = month
  100.         self.year = year
  101.  
  102.     def __repr__(self):
  103.         return '{}.{}.{}'.format(self.day, self.month, self.year)
  104.  
  105.  
  106. def main():
  107.     global pclist
  108.     with open("contacts.json") as f:
  109.         j = json.load(f)
  110.     for jc in j:
  111.         pnums = []
  112.         for jnum in jc['phoneNumber']:
  113.             num = Number(jnum['number'], jnum['type'])
  114.             pnums.append(num)
  115.         pfname = Name(jc['firstName'], 'firstname')
  116.         plname = Name(jc['lastName'], 'lastname')
  117.         paddr = Address(street=jc['address']['streetAddress'],
  118.                         plz=jc['address']['postalCode'],
  119.                         location=jc['address']['city'])
  120.         pcontact = Contact(addresses=[paddr],
  121.                            names=[pfname, plname],
  122.                            events=None,
  123.                            numbers=pnums,
  124.                            birthday=None)
  125.         pclist.append(pcontact)
  126.  
  127. if __name__ == '__main__':
  128.     sys.exit(main())
  129.