Migration de dotProject à OwnCloud : Différence entre versions

De April MediaWiki
(Migration des contact de dotProjet vers vCard pour import dans OwnCloud)
(Migration des événements)
Ligne 131 : Ligne 131 :
  
 
== Migration des événements ==
 
== Migration des événements ==
J'ai mis en ligne les travail de migration des événements : [http://blog.thetys-retz.net/post/2014/07/08/De-la-conversion-une-liste-d-%C3%A9v%C3%A9nement-issue-de-dotproject]
+
J'ai mis en ligne les travail de migration des événements : [http://blog.thetys-retz.net/post/2014/07/08/De-la-conversion-une-liste-d-%C3%A9v%C3%A9nement-issue-de-dotproject De la conversion d'une liste d'événement de dotprojet]
 +
 
 +
Je mets le code ici :
 +
<syntaxhighlight lang="mysql">
 +
select event_title, event_start_date, event_end_date, event_description from events
 +
  into outfile event.csv
 +
  fields terminated by ','
 +
  enclosed by '"'
 +
  escaped by '\\'
 +
  lines terminated by '\n' ;
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="python">
 +
import csv
 +
from icalendar import Calendar, Event, LocalTimezone
 +
from datetime import datetime, timedelta
 +
from random import randint
 +
import sys
 +
#from __future__ import print_function
 +
 +
reader_builder = list(csv.DictReader(open('events_utf8.csv', 'Ub'), skipinitialspace = True))
 +
 +
headers = reader_builder[0].keys()
 +
 +
# Start calendar file
 +
cal = Calendar()
 +
cal.add('prodid', 'dotproject.april.org')
 +
cal.add('version', '2.0')
 +
 +
reader = reader_builder
 +
 +
rownum = 0
 +
 +
try:
 +
    for row in reader:
 +
        event = Event()
 +
        event.add('summary', row['event_title'])
 +
        event.add('dtstart', datetime.strptime(row['event_start_date'],'%Y-%m-%d %H:%M:%S'))
 +
                #.strftime('%Y%m%dT%H%M%SZ'))
 +
        event.add('dtend', datetime.strptime(row['event_end_date'],'%Y-%m-%d %H:%M:%S'))
 +
        event.add('description',row['event_description'])
 +
        event['uid'] = str(randint(1,10**30)) + datetime.now().strftime('%Y%m%dT%H%M%S') + '___dotproject.april.org'
 +
 +
        cal.add_component(event)
 +
        rownum += 1
 +
 +
except Exception, e:
 +
    if rownum > 0:
 +
        print('I had a problem with an event. I think I might have gotten through about {0} events and had trouble with an event with subject: {1} . Sorry!'.format(rownum, row['event_title']))
 +
    else:
 +
        print("N'arrivera pas")
 +
    #sys.exit(2)
 +
 +
f = open('./dotp_events.ics','wb')
 +
f.write(cal.to_ical())
 +
f.close()
 +
</syntaxhighlight>
 +
 
 +
Il faut arrondir les angles à différents moments :
 +
<syntaxhighlight lang="bash">
 +
sed -i.bak 's/\\"/«/g' events.csv
 +
iconv -f latin1 -t utf-8 events.csv > events_utf8.csv
 +
python script.py
 +
sed -i.bak s/;VALUE=DATE-TIME// dotp_events.ics
 +
</syntaxhighlight>
  
 
[[Catégorie:Admin sys]]
 
[[Catégorie:Admin sys]]

Version du 9 juillet 2014 à 21:42

Migration des contact de dotProjet vers vCard pour import dans OwnCloud

Nous avons utilisé un script python :

import csv
import vobject
import re

def are_null (row, args):
  for arg in args:
    if arg not in row:
      return True
    if row[arg] != "":
      return False
  return True

def add_name (v, row):
  v.add ('n')
  v.n.value = vobject.vcard.Name(
      family      = row["First Name"],
      given       = row["Last Name"],
      additional  = row["Middle Name"],
      prefix      = row["Title"],
      suffix      = row["Suffix"],
      )

def add_notes (v, row):
  if are_null (row, ["Notes"]):
    return
  v.add ('note')
  v.note.value = row["Notes"]

def add_anniversary (v, row):
  if are_null (row, ["Birthday"]):
    return
  v.add ('bday')
  v.bday.value = row["Birthday"]

def add_title (v, row):
  v.add ('fn')
  v.fn.value = "%s %s" % (row["First Name"], row["Last Name"])

def add_organization (v, row):
  if are_null (row, ["Company", "Department", "Job Title"]):
    return
  v.add ('org')
  v.org.value = [row["Company"], row["Department"], row ["Job Title"]]

def add_address (v, row, pre, typ):
  if are_null (row, [pre+" Street", pre+" City"]):
    return
  v.add ('adr')
  v.adr.type_param = typ
  v.adr.value = vobject.vcard.Address(
      street =  row[pre+" Street"]   + "\n " +
                row[pre+" Street 2"] + "\n " +
                row[pre+" Street 3"],
      city =    row[pre+" City"],
      region =  row[pre+" State"],
      code =    row[pre+" Postal Code"],
      country = row[pre+" Country"],
      )

def add_telephone (v, row, pre, typ):
  if are_null (row, [pre+" Phone"]):
    return
  v.add('tel')
  v.tel.type_param = typ
  v.tel.value = row[pre+" Phone"]

def add_fax (v, row, pre, typ):
  if are_null (row, [pre+" Fax"]):
    return
  v.add('fax')
  v.fax.type_param = typ
  v.fax.value = row[pre+" Fax"]

def unfuck_emails (s):
  t= re.split ("[<>]", s)
  for field in t:
    if field.find ('@') > -1:
      return field.strip()
  # should raise ValueError("Badly splitted: " + s)
  return s

def add_email (v, row, suf):
    dn=  "E-mail" + suf + " Display Name"
    ema= "E-mail" + suf + " Address"
    emt= "E-mail" + suf + " Type"
    if row[ema] != "":
      v.add('email')
      v.email.value = "%s <%s>" % (row[dn], unfuck_emails (row[ema]))
      if row[emt] != "SMTP":
        v.email.type_param = row[emt]

with open('APRILContacts-unix-utf8.csv', 'rb') as csvfile:
  reader = csv.DictReader(csvfile, delimiter=',', quotechar='"')
  for row in reader:
    for key in row:
      if type (row[key]) is str:
        row[key]= row[key].decode('utf8')

    v= vobject.vCard()
    
    add_title (v, row)
    add_name  (v, row)
    add_notes (v, row)
    add_anniversary  (v, row)
    add_organization (v, row)

    for (i,o) in [("Business", "WORK"), ("Home", "HOME"), ("Other", "OTHER")]:
      add_address (v, row, i, o)
      add_telephone (v, row, i, o)
      add_telephone (v, row, i+" 2", o+" 2")
      add_fax (v, row, i, o)

    for (i,o) in [("Mobile", "CELL")]:
      add_telephone (v, row, i, o)

    for suf in ["", " 2", " 3"]:
      add_email (v, row, suf)

    print v.serialize()

Pour l'utiliser:

  • exporter les contacts depuis dotProjet en CSV dans un fichier APRILContacts.csv ;
  • l'ouvrir avec Gedit (ou $editor) et le sauvegarder dans APRILContacts-unix-utf8.csv, en prenant soin de le coder en unicode, au format unix.
  • lancer le script : python dotp2vcard.py > contacts.vcard

Migration des événements

J'ai mis en ligne les travail de migration des événements : De la conversion d'une liste d'événement de dotprojet

Je mets le code ici :

select event_title, event_start_date, event_end_date, event_description from events
  into outfile event.csv
  fields terminated by ','
  enclosed by '"'
  escaped by '\\'
  lines terminated by '\n' ;
import csv
from icalendar import Calendar, Event, LocalTimezone
from datetime import datetime, timedelta
from random import randint
import sys
#from __future__ import print_function
 
reader_builder = list(csv.DictReader(open('events_utf8.csv', 'Ub'), skipinitialspace = True))
 
headers = reader_builder[0].keys()
 
# Start calendar file
cal = Calendar()
cal.add('prodid', 'dotproject.april.org')
cal.add('version', '2.0')
 
reader = reader_builder
 
rownum = 0
 
try:
    for row in reader:
        event = Event()
        event.add('summary', row['event_title'])
        event.add('dtstart', datetime.strptime(row['event_start_date'],'%Y-%m-%d %H:%M:%S'))
                #.strftime('%Y%m%dT%H%M%SZ'))
        event.add('dtend', datetime.strptime(row['event_end_date'],'%Y-%m-%d %H:%M:%S'))
        event.add('description',row['event_description'])
        event['uid'] = str(randint(1,10**30)) + datetime.now().strftime('%Y%m%dT%H%M%S') + '___dotproject.april.org'
 
        cal.add_component(event)
        rownum += 1
 
except Exception, e:
    if rownum > 0:
        print('I had a problem with an event. I think I might have gotten through about {0} events and had trouble with an event with subject: {1} . Sorry!'.format(rownum, row['event_title']))
    else:
        print("N'arrivera pas")
    #sys.exit(2)
 
f = open('./dotp_events.ics','wb')
f.write(cal.to_ical())
f.close()

Il faut arrondir les angles à différents moments :

sed -i.bak 's/\\"/«/g' events.csv
iconv -f latin1 -t utf-8 events.csv > events_utf8.csv
python script.py
sed -i.bak s/;VALUE=DATE-TIME// dotp_events.ics