utils/export_annotations.py
author ymh <ymh.work@gmail.com>
Wed, 15 Oct 2014 13:09:47 +0200
changeset 24 eb1f7b06001f
child 26 ebfd0d3cffab
permissions -rw-r--r--
add api + first version (not tested) of export annotation script
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
from iri_tweet.models import setup_database, Tweet, User
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
from sqlalchemy.sql import select, func
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from iri_tweet.utils import (set_logging_options, set_logging, get_filter_query,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
    get_logger)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import argparse
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
import json
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
import datetime
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
import requests
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
import os.path
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
import re
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
import sys
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
import time
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
import uuid #@UnresolvedImport
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
from dateutil.parser import parse as parse_date
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
import bisect
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
#class TweetExclude(object):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
#    def __init__(self, id):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
#        self.id = id
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
#
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
#    def __repr__(self):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
#        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
    27
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
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
    29
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
    30
DEFAULT_ANNOTATION_CHANNEL = 'ANNOT'
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
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
    34
    res = []
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    #TODO: check timezone...
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    if start_date:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        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
    38
    if end_date:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        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
    40
    if events:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        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
    42
    if channels:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        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
    44
    if user_whitelist:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        res.append({'name': 'user', 'op': "in", 'val':user_whitelist })
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
# def parse_polemics(tw, extended_mode):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
#     """
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
#     parse polemics in text and return a list of polemic code. None if not polemic found
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
#     """
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
#     polemics = {}
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
#     for m in re.finditer("(\+\+|\-\-|\?\?|\=\=)",tw.text):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
#         pol_link = {
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
#             '++' : u'OK',
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
#             '--' : u'KO',
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
#             '??' : u'Q',
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
#             '==' : u'REF'}[m.group(1)]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
#         polemics[pol_link] = pol_link
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
#
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
#     if extended_mode:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
#         if "?" in tw.text:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
#             polemics["Q"] = "Q"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
#
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
#         for entity in tw.entity_list:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
#             if entity.type == "entity_url":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
#                 polemics["REF"] = "REF"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
#
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
#     if len(polemics) > 0:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
#         return polemics.keys()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
#     else:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
#         return None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
def get_options():
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    usage = "usage: %(prog)s [options]"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    parser = argparse.ArgumentParser(usage)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
    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
    80
                      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
    81
    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
    82
                      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
    83
    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
    84
                      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
    85
    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
    86
                      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
    87
    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
    88
                      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
    89
    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
    90
                      help="Content url", metavar="CONTENT")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
    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
    92
                      help="video url", metavar="VIDEO")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
    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
    94
                      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
    95
    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
    96
                      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
    97
    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
    98
                      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
    99
    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
   100
                      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
   101
    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
   102
                      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
   103
    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
   104
                      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
   105
    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
   106
                      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
   107
    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
   108
                      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
   109
    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
   110
                      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
   111
    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
   112
                      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
   113
    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
   114
                      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
   115
    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
   116
                      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
   117
    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
   118
                      help="Post param", metavar="POST_PARAM", 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("--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
   120
                      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
   121
    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
   122
                      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
   123
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
    set_logging_options(parser)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
    return (parser.parse_args(), parser)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
def find_delta(deltas, ts):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
    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
   131
    if i:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
        return deltas[i-1]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
    return (0,0)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
def parse_duration(s):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
    try:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        return int(s)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
    except ValueError:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        parts = s.split(":")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
        if len(parts) < 2:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
            raise ValueError("Bad duration format")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
        time_params = {
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
            'hours': int(parts[0]),
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
            'minutes': int(parts[1]),
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
            '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
   147
        }
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
        return int(datetime.timedelta(**time_params).total_seconds()*1000)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
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
   151
    page = 0
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
    page_nb = 1
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
    while page < page_nb:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
        page += 1
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
        params['page'] = page
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
        resp = requests.get(url, params=params, headers=headers)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
        if resp.code != 200:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
            return
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
        resp_json = resp.json()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
        page_nb = resp_json.get('total_pages', 1)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        for item in resp_json.get('results', []):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
            #TODO: add progress log
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
            yield item
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
if __name__ == "__main__" :
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
    (options, parser) = get_options()
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
    set_logging(options)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
    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
   173
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
    deltas = [(0,0)]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
    total_delta = 0
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
    if options.cuts:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
        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
   179
        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
   180
            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
   181
            total_delta += d
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
            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
   183
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
    if len(sys.argv) == 1 or options.database is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
        parser.print_help()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
        sys.exit(1)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
    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
   189
    user_whitelist = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
    annotation_url = options.annot_url
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
    if options.listconf:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
        parameters = []
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
        confdoc = etree.parse(options.listconf)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
        for node in confdoc.xpath("/twitter_export/file"):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
            params = {}
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
            for snode in node:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
                if snode.tag == "path":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
                    params['content_file'] = snode.text
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
                    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
   203
                elif snode.tag == "project_id":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
                    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
   205
                    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
   206
                    params['project_id'] = snode.text
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
                elif snode.tag == "start_date":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
                    params['start_date'] = snode.text
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
                elif snode.tag == "end_date":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
                    params['end_date'] = snode.text
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
                elif snode.tag == "duration":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
                    params['duration'] = int(snode.text)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
                elif snode.tag == "events":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
                        params['events'] = [snode.text]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
                elif snode.tag == "channels":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
                    params['channels'] = [snode.text]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
            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
   218
                params['events'] = options.events
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
            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
   220
                params['channels'] = options.channels
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
            parameters.append(params)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
    else:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
        if options.project_id:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
            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
   226
        else:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
            content_file = options.content_file
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
        parameters = [{
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
            'start_date'            : options.start_date,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
            'end_date'              : options.end_date,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
            'duration'              : options.duration,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
            'events'                : options.events,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
            'channels'              : options.channels,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
            'content_file'          : content_file,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
            'content_file_write'    : content_file,
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
            'project_id'            : options.project_id
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
        }]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
    post_param = {}
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
    if options.post_param:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
        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
   241
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
    for params in parameters:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
        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
   245
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
        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
   247
        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
   248
        duration = params.get("duration", None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
        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
   250
        content_file_write = params.get("content_file_write", None)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
        channels = params.get('channels', [DEFAULT_ANNOTATION_CHANNEL])
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
        events = params.get('events', [])
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
        if user_whitelist_file:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
            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
   256
                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
   257
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
        start_date = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
        ts = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
        if start_date_str:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
            start_date = parse_date(start_date_str)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
            ts = time.mktime(start_date.timetuple())
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
        root = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
        ensemble_parent = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
        #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
   269
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
        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
   271
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
            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
   273
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
            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
   275
            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
   276
            project = r.json()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
            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
   278
            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
   279
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
        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
   281
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
            doc = etree.parse(content_file)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
            root = doc.getroot()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
        content_id = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
        if root is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
            root = etree.Element(u"iri")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
            project = etree.SubElement(root, u"project", {u"abstract":u"Polemics Tweets",u"title":u"Polemic Tweets", u"user":u"IRI Web", u"id":unicode(uuid.uuid4())})
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
            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
   294
            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
   295
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
            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
   297
            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
   298
            ensemble_parent = content
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
            content_id = options.content_id
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
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
        if ensemble_parent is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
            file_type = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
            for node in root:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
                if node.tag == "project":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
                    file_type = "ldt"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
                    break
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
                elif node.tag == "head":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
                    file_type = "iri"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
                    break
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
            if file_type == "ldt":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
                media_nodes = root.xpath("//media")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
                if len(media_nodes) > 0:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
                    media = media_nodes[0]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
                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
   318
                if annotations_node is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
                    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
   320
                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
   321
                if content_node is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
                    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
   323
                ensemble_parent = content_node
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
                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
   325
                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
   326
                if len(display_nodes) == 0:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
                    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
   328
                    display_content_node = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
                else:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
                    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
   331
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
            elif file_type == "iri":
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
                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
   334
                if body_node is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
                    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
   336
                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
   337
                if ensembles_node is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
                    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
   339
                ensemble_parent = ensembles_node
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
                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
   341
                display_content_node = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
        if ensemble_parent is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
            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
   346
            sys.exit()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
        if options.replace:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
            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
   350
                ens_id = ens.get("id","")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
                if ens_id.startswith("annot_"):
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
                    ensemble_parent.remove(ens)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
                    # remove in display nodes
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
                    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
   355
                        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
   356
                            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
   357
                                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
   358
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
        ensemble = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
        elements = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
        if options.merge:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
            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
   364
                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
   365
                    ensemble = ens
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
                    break
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
            if ensemble is not None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
                elements = ensemble.find(u".//elements")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
                decoupage = ensemble.find(u"decoupage")
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
        if ensemble is None or elements is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
            ensemble = etree.SubElement(ensemble_parent, u"ensemble", {u"id":u"annot_" + unicode(uuid.uuid4()), u"title":u"Ensemble Twitter", u"author":u"IRI Web", u"abstract":u"Ensemble Twitter"})
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
            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
   374
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
            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
   376
            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
   377
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
            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
   379
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
        ensemble_id = ensemble.get('id', '')
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
        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
   382
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
        end_date = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
        if end_date_str:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
            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
   386
        elif start_date and duration:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
            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
   388
        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
   389
            # get duration from api
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
            content_url = options.base_url + LDT_CONTENT_REST_API_PATH + content_id + "/?format=json"
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
            r = requests.get(content_url)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
            duration = int(r.json()['duration'])
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
            get_logger().debug("get duration " + content_url) #@UndefinedVariable
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
            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
   395
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
            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
   397
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
        if end_date and deltas:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
            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
   400
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
        filters = get_filter(start_date, end_date, user_whitelist)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
        headers = {'Content-Type': 'application/json'}
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
        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
   407
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
        for annot in 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
   410
            #TODO : check timezone !!!
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
            annot_ts_dt = annot['ts']
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
            annot_ts = int(time.mktime(annot_ts_dt.timetuple()))
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
            if ts is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
                ts = annot_ts
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
            annot_ts_rel = (annot_ts-ts) * 1000
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
            if deltas:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
                d = find_delta(deltas, annot_ts_rel)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
                if d[1] < 0:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
                    continue
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
                else :
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
                    annot_ts_rel -= d[1]
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
            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
   423
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
            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
   425
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
            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
   427
            if category is None:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
                continue
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
            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_dt.strftime("%Y/%m/%d")), u"begin": unicode(annot_ts_rel), u"dur":u"0"})
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
            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
   432
            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
   433
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
            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
   435
            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
   436
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
            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
   438
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
            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
   440
            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
   441
            meta_element.append(polemics_element)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
            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
   444
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
        # sort by tc in
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
        if options.merge :
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
            # 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
   448
            # sort them with tc
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
            #put them back
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
            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
   451
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
        #add to display node
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
        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
   454
            display_dec = None
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
            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
   456
                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
   457
                    display_dec = dec
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
                    break
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
            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
   460
                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
   461
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
        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
   463
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
        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
   465
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
            project["ldt"] = output_data
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
            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
   468
            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
   469
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
            post_param = {}
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
            if options.post_param:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
                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
   473
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
            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
   475
            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
   476
            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
   477
            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
   478
            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
   479
            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
   480
                r.raise_for_status()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
        else:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
            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
   483
                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
   484
            else:
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
                dest_file_name = options.filename
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
            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
   488
            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
   489
            output.write(output_data)
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
            output.flush()
eb1f7b06001f add api + first version (not tested) of export annotation script
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
            output.close()