utils/export_annotations.py
author ymh <ymh.work@gmail.com>
Mon, 20 Oct 2014 01:31:33 +0200
changeset 43 e27c3c1c57f1
parent 30 c2294ac6e875
child 46 7cff1f0a6882
permissions -rw-r--r--
end of admin. change the index page and add a redirect to it on the landing page
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
# coding=utf-8
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
from lxml import etree
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import argparse
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
import json
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
import datetime
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
import requests
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import os.path
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
import re
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
import sys
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
import time
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
import uuid #@UnresolvedImport
30
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    14
from dateutil.parser import parse as parse_date_raw
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    15
from dateutil.tz import tzutc
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
import bisect
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    17
import logging
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
#class TweetExclude(object):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
#    def __init__(self, id):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
#        self.id = id
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
#
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
#    def __repr__(self):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
#        return "<TweetExclude(id=%d)>" % (self.id)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
LDT_CONTENT_REST_API_PATH = "api/ldt/1.0/contents/"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
LDT_PROJECT_REST_API_PATH = "api/ldt/1.0/projects/"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
DEFAULT_ANNOTATION_CHANNEL = 'ANNOT'
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
30
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    30
def parse_date(datestr):
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    31
    res = parse_date_raw(datestr)
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    32
    if res.tzinfo is None:
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    33
        res = res.replace(tzinfo=tzutc())
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    34
    return res
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    35
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    36
def get_logger():
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    37
    return logging.getLogger(__name__)
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    38
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
def get_filter(start_date, end_date, events, channels, user_whitelist):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    res = []
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    #TODO: check timezone...
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    if start_date:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        res.append({'name': 'ts', 'op': ">=", 'val':start_date.isoformat() })
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    if end_date:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        res.append({'name': 'ts', 'op': "<=", 'val':end_date.isoformat() })
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    if events:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        res.append({'name': 'event', 'op': "in", 'val':events })
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    if channels:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        res.append({'name': 'channel', 'op': "in", 'val':channels })
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    if user_whitelist:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        res.append({'name': 'user', 'op': "in", 'val':user_whitelist })
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    53
    return res
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    56
def set_logging(options, plogger=None, queue=None):
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    57
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    58
    logging_config = {
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    59
        "format" : '%(asctime)s %(levelname)s:%(name)s:%(message)s',
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    60
        "level" : max(logging.NOTSET, min(logging.CRITICAL, logging.WARNING - 10 * options.verbose + 10 * options.quiet)), #@UndefinedVariable
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    61
    }
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    62
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    63
    if options.logfile == "stdout":
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    64
        logging_config["stream"] = sys.stdout
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    65
    elif options.logfile == "stderr":
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    66
        logging_config["stream"] = sys.stderr
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    67
    else:
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    68
        logging_config["filename"] = options.logfile
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    69
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    70
    logger = plogger
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    71
    if logger is None:
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    72
        logger = get_logger() #@UndefinedVariable
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    73
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    74
    if len(logger.handlers) == 0:
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    75
        filename = logging_config.get("filename")
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    76
        if queue is not None:
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    77
            hdlr = QueueHandler(queue, True)
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    78
        elif filename:
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    79
            mode = logging_config.get("filemode", 'a')
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    80
            hdlr = logging.FileHandler(filename, mode) #@UndefinedVariable
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    81
        else:
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    82
            stream = logging_config.get("stream")
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    83
            hdlr = logging.StreamHandler(stream) #@UndefinedVariable
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    84
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    85
        fs = logging_config.get("format", logging.BASIC_FORMAT) #@UndefinedVariable
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    86
        dfs = logging_config.get("datefmt", None)
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    87
        fmt = logging.Formatter(fs, dfs) #@UndefinedVariable
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    88
        hdlr.setFormatter(fmt)
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    89
        logger.addHandler(hdlr)
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    90
        level = logging_config.get("level")
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    91
        if level is not None:
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    92
            logger.setLevel(level)
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    93
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    94
    options.debug = (options.verbose-options.quiet > 0)
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    95
    return logger
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    96
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    97
def set_logging_options(parser):
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    98
    parser.add_argument("-l", "--log", dest="logfile",
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
    99
                      help="log to file", metavar="LOG", default="stderr")
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   100
    parser.add_argument("-v", dest="verbose", action="count",
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   101
                      help="verbose", default=0)
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   102
    parser.add_argument("-q", dest="quiet", action="count",
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   103
                      help="quiet", default=0)
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   104
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   105
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
def get_options():
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
30
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   109
    parser = argparse.ArgumentParser(description="All date should be given using iso8601 format. If no timezone is used, the date is considered as UTC")
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    parser.add_argument("-f", "--file", dest="filename",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
                      help="write export to file", metavar="FILE", default="project.ldt")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
    parser.add_argument("-a", "--annot-url", dest="annot_url",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
                      help="annotation server url", metavar="ANNOT-URL", required=True)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
    parser.add_argument("-s", "--start-date", dest="start_date",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
                      help="start date", metavar="START_DATE", default=None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
    parser.add_argument("-e", "--end-date", dest="end_date",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
                      help="end date", metavar="END_DATE", default=None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
    parser.add_argument("-I", "--content-file", dest="content_file",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
                      help="Content file", metavar="CONTENT_FILE")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
    parser.add_argument("-c", "--content", dest="content",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
                      help="Content url", metavar="CONTENT")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
    parser.add_argument("-V", "--video-url", dest="video",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
                      help="video url", metavar="VIDEO")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
    parser.add_argument("-i", "--content-id", dest="content_id",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
                      help="Content id", metavar="CONTENT_ID")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
    parser.add_argument("-x", "--exclude", dest="exclude",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
                      help="file containing the id to exclude", metavar="EXCLUDE")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
    parser.add_argument("-C", "--color", dest="color",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
                      help="Color code", metavar="COLOR", default="16763904")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
    parser.add_argument("-H", "--channel", dest="channels",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
                      help="Channel", metavar="CHANNEL", default=[DEFAULT_ANNOTATION_CHANNEL], action="append")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
    parser.add_argument("-E", "--event", dest="events",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
                      help="Event", metavar="EVENT", default=[], action="append")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
    parser.add_argument("-D", "--duration", dest="duration", type=int,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
                      help="Duration", metavar="DURATION", default=None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
    parser.add_argument("-n", "--name", dest="name",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
                      help="Cutting name", metavar="NAME", default=u"annotations")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
    parser.add_argument("-R", "--replace", dest="replace", action="store_true",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
                      help="Replace annotation ensemble", default=False)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
    parser.add_argument("-m", "--merge", dest="merge", action="store_true",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
                      help="merge annotation ensemble, choose the first ensemble", default=False)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
    parser.add_argument("-L", "--list-conf", dest="listconf",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
                      help="list of file to process", metavar="LIST_CONF", default=None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
    parser.add_argument("-b", "--base-url", dest="base_url",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
                      help="base URL of the platform", metavar="BASE_URL", default="http://ldt.iri.centrepompidou.fr/ldtplatform/")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
    parser.add_argument("-p", "--project", dest="project_id",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
                      help="Project id", metavar="PROJECT_ID", default=None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
    parser.add_argument("-P", "--post-param", dest="post_param",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
                      help="Post param", metavar="POST_PARAM", default=None)
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   151
    parser.add_argument("-B", "--batch-size", dest="batch_size", type=int,
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   152
                      help="Batch size for annotation request", metavar="BATCH_SIZE", default=500)
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
    parser.add_argument("--user-whitelist", dest="user_whitelist", action="store",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
                      help="A list of user screen name", metavar="USER_WHITELIST",default=None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
    parser.add_argument("--cut", dest="cuts", action="append",
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
                      help="A cut with the forma <ts in ms>::<duration>", metavar="CUT", default=[])
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
    set_logging_options(parser)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
    return (parser.parse_args(), parser)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
def find_delta(deltas, ts):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
    i = bisect.bisect_right(deltas, (ts+1,0))
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
    if i:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
        return deltas[i-1]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
    return (0,0)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
def parse_duration(s):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
    try:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
        return int(s)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
    except ValueError:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
        parts = s.split(":")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
        if len(parts) < 2:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
            raise ValueError("Bad duration format")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
        time_params = {
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
            'hours': int(parts[0]),
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
            'minutes': int(parts[1]),
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
            'seconds': int(parts[2]) if len(parts)>2 else 0
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
        }
30
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   182
        return int(round(datetime.timedelta(**time_params).total_seconds()*1000))
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
def build_annotation_iterator(url, params, headers):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
    page = 0
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
    page_nb = 1
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
    while page < page_nb:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
        page += 1
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
        params['page'] = page
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
        resp = requests.get(url, params=params, headers=headers)
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   191
        if resp.status_code != requests.codes.ok:
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
            return
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
        resp_json = resp.json()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
        page_nb = resp_json.get('total_pages', 1)
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   195
        for item in resp_json.get('objects', []):
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
            #TODO: add progress log
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
            yield item
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
if __name__ == "__main__" :
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
    (options, parser) = get_options()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
    set_logging(options)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
    get_logger().debug("OPTIONS : " + repr(options)) #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
    deltas = [(0,0)]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
    total_delta = 0
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
    if options.cuts:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
        cuts_raw = sorted([tuple([parse_duration(s) for s in c.split("::")]) for c in options.cuts])
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
        for c, d in cuts_raw:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
            deltas.append((c+total_delta, -1))
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
            total_delta += d
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
            deltas.append((c+total_delta, total_delta))
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   218
    if len(sys.argv) == 1 or options.annot_url is None:
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
        parser.print_help()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
        sys.exit(1)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
    user_whitelist_file = options.user_whitelist
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
    user_whitelist = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
    annotation_url = options.annot_url
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
    if options.listconf:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
        parameters = []
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
        confdoc = etree.parse(options.listconf)
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   231
        for node in confdoc.xpath("/annotation_export/file"):
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
            params = {}
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
            for snode in node:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
                if snode.tag == "path":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
                    params['content_file'] = snode.text
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
                    params['content_file_write'] = snode.text
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
                elif snode.tag == "project_id":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
                    params['content_file'] = options.base_url + LDT_PROJECT_REST_API_PATH + snode.text + "/?format=json"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
                    params['content_file_write'] = options.base_url + LDT_PROJECT_REST_API_PATH + snode.text + "/?format=json"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
                    params['project_id'] = snode.text
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
                elif snode.tag == "start_date":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
                    params['start_date'] = snode.text
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
                elif snode.tag == "end_date":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
                    params['end_date'] = snode.text
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
                elif snode.tag == "duration":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
                    params['duration'] = int(snode.text)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
                elif snode.tag == "events":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
                        params['events'] = [snode.text]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
                elif snode.tag == "channels":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
                    params['channels'] = [snode.text]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
            if options.events or 'events' not in params :
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
                params['events'] = options.events
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
            if options.channels or 'channels' not in params :
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
                params['channels'] = options.channels
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
            parameters.append(params)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
    else:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
        if options.project_id:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
            content_file = options.base_url + LDT_PROJECT_REST_API_PATH + options.project_id + "/?format=json"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
        else:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
            content_file = options.content_file
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
        parameters = [{
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
            'start_date'            : options.start_date,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
            'end_date'              : options.end_date,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
            'duration'              : options.duration,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
            'events'                : options.events,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
            'channels'              : options.channels,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
            'content_file'          : content_file,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
            'content_file_write'    : content_file,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
            'project_id'            : options.project_id
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
        }]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
    post_param = {}
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
    if options.post_param:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
        post_param = json.loads(options.post_param)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
    for params in parameters:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
        get_logger().debug("PARAMETERS " + repr(params)) #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
        start_date_str = params.get("start_date",None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
        end_date_str = params.get("end_date", None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
        duration = params.get("duration", None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
        content_file = params.get("content_file", None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
        content_file_write = params.get("content_file_write", None)
30
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   285
        channels = list(set(params.get('channels', [DEFAULT_ANNOTATION_CHANNEL])))
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   286
        events = list(set(params.get('events', [])))
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
        if user_whitelist_file:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
            with open(user_whitelist_file, 'r+') as f:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
                user_whitelist = list(set([s.strip() for s in f]))
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
        start_date = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
        if start_date_str:
30
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   294
            start_date= parse_date(start_date_str)
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
        root = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
        ensemble_parent = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
        #to do : analyse situation ldt or iri ? filename set or not ?
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
        if content_file and content_file.find("http") == 0:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
            get_logger().debug("url : " + content_file) #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
            r = requests.get(content_file, params=post_param)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
            get_logger().debug("url response " + repr(r) + " content " + repr(r.text)) #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
            project = r.json()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
            text_match = re.match(r"\<\?\s*xml.*?\?\>(.*)", project['ldt'], re.I|re.S)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
            root = etree.fromstring(text_match.group(1) if text_match else project['ldt'])
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
        elif content_file and os.path.exists(content_file):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
            doc = etree.parse(content_file)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
            root = doc.getroot()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
        content_id = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
        if root is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
            root = etree.Element(u"iri")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
30
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   323
            project = etree.SubElement(root, u"project", {u"abstract":u"Annotations",u"title":u"Annotations", u"user":u"IRI Web", u"id":unicode(uuid.uuid4())})
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
            medias = etree.SubElement(root, u"medias")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
            media = etree.SubElement(medias, u"media", {u"pict":u"", u"src":unicode(options.content), u"video":unicode(options.video), u"id":unicode(options.content_id), u"extra":u""})
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
            annotations = etree.SubElement(root, u"annotations")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
            content = etree.SubElement(annotations, u"content", {u"id":unicode(options.content_id)})
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
            ensemble_parent = content
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
            content_id = options.content_id
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
        if ensemble_parent is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
            file_type = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
            for node in root:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
                if node.tag == "project":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
                    file_type = "ldt"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
                    break
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
                elif node.tag == "head":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
                    file_type = "iri"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
                    break
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
            if file_type == "ldt":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
                media_nodes = root.xpath("//media")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
                if len(media_nodes) > 0:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
                    media = media_nodes[0]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
                annotations_node = root.find(u"annotations")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
                if annotations_node is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
                    annotations_node = etree.SubElement(root, u"annotations")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
                content_node = annotations_node.find(u"content")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
                if content_node is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
                    content_node = etree.SubElement(annotations_node,u"content", id=media.get(u"id"))
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
                ensemble_parent = content_node
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
                content_id = content_node.get(u"id")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
                display_nodes = root.xpath("//displays/display/content[@id='%s']" % content_id)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
                if len(display_nodes) == 0:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
                    get_logger().info("No display node found. Will not update display")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
                    display_content_node = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
                else:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
                    display_content_node = display_nodes[0]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
            elif file_type == "iri":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
                body_node = root.find(u"body")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
                if body_node is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
                    body_node = etree.SubElement(root, u"body")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
                ensembles_node = body_node.find(u"ensembles")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
                if ensembles_node is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
                    ensembles_node = etree.SubElement(body_node, u"ensembles")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
                ensemble_parent = ensembles_node
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
                content_id = root.xpath("head/meta[@name='id']/@content")[0]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
                display_content_node = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
        if ensemble_parent is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
            get_logger().error("Can not process file") #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
            sys.exit()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
        if options.replace:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
            for ens in ensemble_parent.iterchildren(tag=u"ensemble"):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
                ens_id = ens.get("id","")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
                if ens_id.startswith("annot_"):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
                    ensemble_parent.remove(ens)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
                    # remove in display nodes
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
                    if display_content_node is not None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
                        for cut_display in display_content_node.iterchildren():
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
                            if cut_display.get('idens','') == ens_id:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
                                display_content_node.remove(cut_display)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
        ensemble = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
        elements = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
        if options.merge:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
            for ens in ensemble_parent.findall(u"ensemble"):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
                if ens.get('id',"").startswith("annot_"):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
                    ensemble = ens
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
                    break
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
            if ensemble is not None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
                elements = ensemble.find(u".//elements")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
                decoupage = ensemble.find(u"decoupage")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
        if ensemble is None or elements is None:
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   404
            ensemble = etree.SubElement(ensemble_parent, u"ensemble", {u"id":u"annot_" + unicode(uuid.uuid4()), u"title":u"Ensemble Annotation", u"author":u"IRI Web", u"abstract":u"Ensemble Annotation"})
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
            decoupage = etree.SubElement(ensemble, u"decoupage", {u"id": unicode(uuid.uuid4()), u"author": u"IRI Web"})
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
            etree.SubElement(decoupage, u"title").text = unicode(options.name)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
            etree.SubElement(decoupage, u"abstract").text = unicode(options.name)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
            elements = etree.SubElement(decoupage, u"elements")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
        ensemble_id = ensemble.get('id', '')
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
        decoupage_id = decoupage.get('id', '') if decoupage is not None else None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
        end_date = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
        if end_date_str:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
            end_date = parse_date(end_date_str)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
        elif start_date and duration:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
            end_date = start_date + datetime.timedelta(seconds=duration)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
        elif start_date and options.base_url:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
            # get duration from api
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
            content_url = options.base_url + LDT_CONTENT_REST_API_PATH + content_id + "/?format=json"
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   423
            get_logger().debug("get duration " + content_url) #@UndefinedVariable
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   424
            r = requests.get(content_url, params=post_param)
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   425
            get_logger().debug("get duration resp " + repr(r)) #@UndefinedVariable
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
            duration = int(r.json()['duration'])
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
            get_logger().debug("get duration " + repr(duration)) #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
            end_date = start_date + datetime.timedelta(seconds=int(duration/1000))
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
        if end_date and deltas:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
            end_date = end_date + datetime.timedelta(milliseconds=deltas[-1][1])
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   435
        filters = get_filter(start_date, end_date, events, channels, user_whitelist)
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
        headers = {'Content-Type': 'application/json'}
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
        params = { 'q':json.dumps({'filters':filters}), 'results_per_page': options.batch_size}
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
26
ebfd0d3cffab Correction on export_annotation. First working version
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   442
        for annot in build_annotation_iterator(annotation_url, params, headers):
30
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   443
            annot_ts = parse_date(annot['ts'])
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   444
            if start_date is None:
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   445
                star_date = annot_ts
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   446
            annot_ts_rel = annot_ts-start_date
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   447
            annot_ts_rel_milli = int(round(annot_ts_rel.total_seconds()*1000))
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
            if deltas:
30
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   449
                d = find_delta(deltas, annot_ts_rel_milli)
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
                if d[1] < 0:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
                    continue
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
                else :
30
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   453
                    annot_ts_rel_milli -= d[1]
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
            annot_content = annot.get('content',{'category':'', 'user':None})
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
            username = annot_content.get('user', 'anon.') or 'anon.'
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
            category = annot_content.get('category', None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
            if category is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
                continue
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
30
c2294ac6e875 correct timezone support for sync script
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   462
            element = etree.SubElement(elements, u"element" , {u"id":annot.get('uuid', uuid.uuid4()), u"color":unicode(options.color), u"author":unicode(username), u"date":unicode(annot_ts.strftime("%Y/%m/%d")), u"begin": unicode(annot_ts_rel_milli), u"dur":u"0"})
24
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
            etree.SubElement(element, u"title").text = unicode(username) + u": " + unicode(category.get('label', category.get('code', '')))
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
            etree.SubElement(element, u"abstract").text = unicode(category.get('label', category.get('code', '')))
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
            tags_node = etree.SubElement(element, u"tags")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
            etree.SubElement(tags_node,u"tag").text = category.get('code', '')
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
            meta_element = etree.SubElement(element, u'meta')
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
            polemics_element = etree.Element(u'polemics')
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
            etree.SubElement(polemics_element, u'polemic').text = category.get('code', '')
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
            meta_element.append(polemics_element)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
            etree.SubElement(meta_element, u"source", attrib={"url":annotation_url + "/" + annot['uuid'], "mimetype":u"application/json"}).text = etree.CDATA(json.dumps(annot))
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
        # sort by tc in
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
        if options.merge :
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
            # remove all elements and put them in a array
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
            # sort them with tc
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
            #put them back
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
            elements[:] = sorted(elements,key=lambda n: int(n.get('begin')))
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
        #add to display node
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
        if display_content_node is not None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
            display_dec = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
            for dec in display_content_node.iterchildren(tag=u"decoupage"):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
                if dec.get('idens','') == ensemble_id and dec.get('id', '') == decoupage_id:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
                    display_dec = dec
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
                    break
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
            if display_dec is None and ensemble_id and decoupage_id:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
                etree.SubElement(display_content_node, u"decoupage", attrib={'idens': ensemble_id, 'id': decoupage_id, 'tagsSelect':''})
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
        output_data = etree.tostring(root, encoding="utf-8", method="xml", pretty_print=False, xml_declaration=True)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
        if content_file_write and content_file_write.find("http") == 0:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
            project["ldt"] = output_data
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
            project['owner'] = project['owner'].replace('%7E','~')
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
            project['contents'] = [c_url.replace('%7E','~') for c_url in project['contents']]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
            post_param = {}
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
            if options.post_param:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
                post_param = json.loads(options.post_param)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
            get_logger().debug("write http " + content_file_write) #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
            get_logger().debug("write http " + repr(post_param)) #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
            get_logger().debug("write http " + repr(project)) #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
            r = requests.put(content_file_write, data=json.dumps(project), headers={'content-type':'application/json'}, params=post_param);
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
            get_logger().debug("write http " + repr(r) + " content " + r.text) #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
            if r.status_code != requests.codes.ok:  # @UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
                r.raise_for_status()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
        else:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
            if content_file_write and os.path.exists(content_file_write):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
                dest_file_name = content_file_write
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
            else:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
                dest_file_name = options.filename
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
            get_logger().debug("WRITE : " + dest_file_name) #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
            output = open(dest_file_name, "w")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
            output.write(output_data)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
            output.flush()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
            output.close()