script/utils/export_chat_zoom_cloud.py
author ymh <ymh.work@gmail.com>
Wed, 18 Dec 2024 15:24:41 +0100
changeset 1584 257c14dae52a
parent 1542 82b5f22448f6
permissions -rw-r--r--
Added tag V09.006 for changeset 459a88818bec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1542
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
# coding=utf-8
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import argparse
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import bisect
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
import datetime
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
import json
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
import os.path
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import re
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
import sys
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
import uuid  # @UnresolvedImport
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
import requests
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
from dateutil.parser import parse as parse_date
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
from iri_tweet.utils import get_logger, set_logging, set_logging_options
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
from lxml import etree
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
LDT_CONTENT_REST_API_PATH = "api/ldt/1.0/contents/"
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
LDT_PROJECT_REST_API_PATH = "api/ldt/1.0/projects/"
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
def re_fn(expr, item):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    reg = re.compile(expr, re.I)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    res = reg.search(item)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    if res:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        get_logger().debug("re_fn : " + repr(expr) + "~" + repr(item)) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    return res is not None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
def parse_polemics_1(tw_text, extended_mode):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    """
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    parse polemics in text and return a list of polemic code. None if not polemic found
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    """
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    polemics = {}
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    for m in re.finditer(r"(\+\+|\-\-|\?\?|\=\=)",tw_text):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        pol_link = {
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
            '++' : 'OK',
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
            '--' : 'KO',
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
            '??' : 'Q',
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
            '==' : 'REF'}[m.group(1)]
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        polemics[pol_link] = pol_link
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    if extended_mode:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        if "?" in tw_text:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
            polemics["Q"] = "Q"
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    if len(polemics) > 0:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        return polemics.keys()
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
    else:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        return None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
def parse_polemics_2(tw_text, extended_mode):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
    """
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    parse polemics in text and return a list of polemic code. None if not polemic found
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
    """
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
    polemics = {}
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    for m in re.finditer(r"(\+\+|\!\!|\?\?|\=\=)",tw_text):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        pol_link = {
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
            '++' : 'OK',
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
            '!!' : 'KO',
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            '??' : 'Q',
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            '==' : 'REF'}[m.group(1)]
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
        polemics[pol_link] = pol_link
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    if extended_mode:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        if "?" in tw_text:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
            polemics["Q"] = "Q"
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    if len(polemics) > 0:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        return polemics.keys()
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
    else:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        return None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
def parse_polemics_3(tw_text, extended_mode):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    """
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    parse polemics in text and return a list of polemic code. None if not polemic found
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
    """
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
    polemics = {}
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    for m in re.finditer(r"(\+\+|\?\?|\*\*|\=\=)",tw_text):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        pol_link = {
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
            '++' : 'OK',
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
            '??' : 'KO',
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
            '**' : 'REF',
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
            '==' : 'Q'}[m.group(1)]
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        polemics[pol_link] = pol_link
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
    if len(polemics) > 0:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
        return polemics.keys()
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
    else:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        return None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
protocol_version_map = {
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
    "1" : parse_polemics_1,
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
    "2" : parse_polemics_2,
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
    "3" : parse_polemics_3
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
}
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
def get_options():
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
    parser = argparse.ArgumentParser(description="All date should be given using iso8601 format. If no timezone is used, the date is considered as UTC")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
    parser.add_argument("-f", "--file", dest="filename",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
                      help="write export to file", metavar="FILE", default="project.ldt")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
    parser.add_argument("-d", "--chat-database", dest="database",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
                      help="Input chat file", metavar="CHAT_DATABASE")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
    parser.add_argument("-s", "--start-date", dest="start_date",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
                      help="start date", metavar="START_DATE", default=None)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    parser.add_argument("-a", "--annotation-protocol", dest="protocol_version",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
                      help="annotation protocol version", metavar="PROTOCOL_VERSION",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
                      default="2")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
    parser.add_argument("-I", "--content-file", dest="content_file",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
                      help="Content file", metavar="CONTENT_FILE")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
    parser.add_argument("-c", "--content", dest="content",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
                      help="Content url", metavar="CONTENT")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
    parser.add_argument("-V", "--video-url", dest="video",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
                      help="video url", metavar="VIDEO")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
    parser.add_argument("-i", "--content-id", dest="content_id",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
                      help="Content id", metavar="CONTENT_ID")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
    parser.add_argument("-C", "--color", dest="color",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
                      help="Color code", metavar="COLOR", default="16763904")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
    parser.add_argument("-D", "--duration", dest="duration", type=int,
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
                      help="Duration", metavar="DURATION", default=None)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
    parser.add_argument("-n", "--name", dest="name",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
                      help="Cutting name", metavar="NAME", default="Chats")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
    parser.add_argument("-R", "--replace", dest="replace", action="store_true",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
                      help="Replace tweet ensemble", default=False)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
    parser.add_argument("-m", "--merge", dest="merge", action="store_true",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
                      help="merge tweet ensemble, choose the first ensemble", default=False)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
    parser.add_argument("-E", "--extended", dest="extended_mode", action="store_true",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
                      help="Trigger polemic extended mode", default=False)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
    parser.add_argument("-b", "--base-url", dest="base_url",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
                      help="base URL of the platform", metavar="BASE_URL", default="http://ldt.iri.centrepompidou.fr/ldtplatform/")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
    parser.add_argument("-p", "--project", dest="project_id",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
                      help="Project id", metavar="PROJECT_ID", default=None)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
    parser.add_argument("-P", "--post-param", dest="post_param",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
                      help="Post param", metavar="POST_PARAM", default=None)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
    parser.add_argument("--user-whitelist", dest="user_whitelist", action="store",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
                      help="A list of user screen name", metavar="USER_WHITELIST",default=None)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
    parser.add_argument("--cut", dest="cuts", action="append",
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
                      help="A cut with the forma <ts in ms>::<duration>", metavar="CUT", default=[])
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
    set_logging_options(parser)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
    return (parser.parse_args(), parser)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
def find_delta(deltas, ts):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
    i = bisect.bisect_right(deltas, (ts+1,0))
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
    if i:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
        return deltas[i-1]
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
    return (0,0)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
def parse_duration(s):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
    try:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
        return int(s)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
    except ValueError:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        parts = s.split(":")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
        if len(parts) < 2:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
            raise ValueError("Bad duration format")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
        time_params = {
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
            'hours': int(parts[0]),
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
            'minutes': int(parts[1]),
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
            'seconds': int(parts[2]) if len(parts)>2 else 0
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
        }
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
        return int(round(datetime.timedelta(**time_params).total_seconds()*1000))
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
CHAT_REGEXP = re.compile(r"^(?P<created_at>\d{2}:\d{2}:\d{2})\t(?P<user>.+?)\s?:\s(?P<text>.*)$", re.DOTALL)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
CHAT_LINE_REGEXP = re.compile(r"^\d{2}:\d{2}:\d{2}\t.+?:\s")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
def parse_chat_line(chat_id, chat_line):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
    if (m := CHAT_REGEXP.match(chat_line)) is not None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
        res = {k: v.replace('\r','\n') if k == 'text' else v for k,v in m.groupdict().items()}
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
        res['id'] = chat_id
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
        res['tags'] = re.findall('#(\w+)',res['text'])
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
        return res
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
    else:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
        return {}
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
def read_chat_file(chat_file_path):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
    current_line = ""
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
    chat_content = []
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
    with open(chat_file_path, "r") as chat_file:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
        for chat_line in chat_file:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
            if CHAT_LINE_REGEXP.match(chat_line) is not None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
                if current_line:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
                    chat_content.append(current_line)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
                current_line = chat_line
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
            else:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
                current_line = current_line + "\n" + chat_line
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
    if current_line:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
        chat_content.append(current_line)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
    return chat_content
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
if __name__ == "__main__" :
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
    (options, parser) = get_options()
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
    set_logging(options)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
    get_logger().debug("OPTIONS : " + repr(options)) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
    deltas = [(0,0)]
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
    total_delta = 0
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
    if options.cuts:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
        cuts_raw = sorted([tuple([parse_duration(s) for s in c.split("::")]) for c in options.cuts])
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
        for c, d in cuts_raw:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
            deltas.append((c+total_delta, -1))
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
            total_delta += d
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
            deltas.append((c+total_delta, total_delta))
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
    if len(sys.argv) == 1 or options.database is None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
        parser.print_help()
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
        sys.exit(1)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
    user_whitelist_file = options.user_whitelist
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
    user_whitelist = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
    if options.project_id:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
        content_file = options.base_url + LDT_PROJECT_REST_API_PATH + options.project_id + "/?format=json"
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
    else:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
        content_file = options.content_file
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
    params = {
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
        'start_date': options.start_date,
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
        'duration' : options.duration,
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
        'content_file' : content_file,
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
        'content_file_write' : content_file,
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
        'project_id' : options.project_id
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
    }
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
    post_param = {}
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
    if options.post_param:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
        post_param = json.loads(options.post_param)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
    display_content_node = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
    get_logger().debug("PARAMETERS " + repr(params)) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
    start_date_str = params.get("start_date",None)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
    duration = params.get("duration", None)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
    content_file = params.get("content_file", None)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
    content_file_write = params.get("content_file_write", None)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
    if user_whitelist_file:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
        with open(user_whitelist_file, 'r+') as f:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
            user_whitelist = list(set([s.strip() for s in f]))
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
    start_date = datetime.datetime.now()
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
    if start_date_str:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
        start_date = parse_date(start_date_str)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
    root = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
    ensemble_parent = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
    project = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
    #to do : analyse situation ldt or iri ? filename set or not ?
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
    if content_file and content_file.find("http") == 0:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
        get_logger().debug("url : " + content_file) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
        r = requests.get(content_file, params=post_param)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
        get_logger().debug("url response " + repr(r) + " content " + repr(r.text)) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
        project = r.json()
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
        text_match = re.match(r"\<\?\s*xml.*?\?\>(.*)", project['ldt'], re.I|re.S)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
        root = etree.fromstring(text_match.group(1) if text_match else project['ldt'])
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
    elif content_file and os.path.exists(content_file):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
        doc = etree.parse(content_file)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
        root = doc.getroot()
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
        for child in root:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
            if child.tag == "project":
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
                project = child
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
                break
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
        if project is None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
            root = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
    content_id = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
    if root is None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
        root = etree.Element("iri")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
        project = etree.SubElement(root, "project", {"abstract":"Polemics Chat","title":"Polemic Chat", "user":"IRI Web", "id":str(uuid.uuid4())})
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
        medias = etree.SubElement(root, "medias")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
        media = etree.SubElement(medias, "media", {"pict":"", "src":options.content, "video":options.video, "id":options.content_id, "extra":""})
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
        annotations = etree.SubElement(root, "annotations")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
        content = etree.SubElement(annotations, "content", {"id":options.content_id})
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
        ensemble_parent = content
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
        content_id = options.content_id
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
    if ensemble_parent is None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
        file_type = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
        for node in root:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
            if node.tag == "project":
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
                file_type = "ldt"
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
                break
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
            elif node.tag == "head":
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
                file_type = "iri"
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
                break
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
        if file_type == "ldt":
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
            media_nodes = root.xpath("//media")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
            media = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
            if len(media_nodes) > 0:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
                media = media_nodes[0]
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
            annotations_node = root.find("annotations")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
            if annotations_node is None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
                annotations_node = etree.SubElement(root, "annotations")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
            content_node = annotations_node.find("content")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
            if content_node is None and media is not None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
                content_node = etree.SubElement(annotations_node,"content", id=media.get("id"))
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
            ensemble_parent = content_node
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
            content_id = content_node.get("id")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
            display_nodes = root.xpath("//displays/display/content[@id='%s']" % content_id)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
            if len(display_nodes) == 0:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
                get_logger().info("No display node found. Will not update display")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
                display_content_node = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
            else:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
                display_content_node = display_nodes[0]
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
        elif file_type == "iri":
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
            body_node = root.find("body")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
            if body_node is None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
                body_node = etree.SubElement(root, "body")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
            ensembles_node = body_node.find("ensembles")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
            if ensembles_node is None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
                ensembles_node = etree.SubElement(body_node, "ensembles")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
            ensemble_parent = ensembles_node
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
            content_id = root.xpath("head/meta[@name='id']/@content")[0]
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
            display_content_node = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
    if ensemble_parent is None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
        get_logger().error("Can not process file") #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
        sys.exit()
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
    if options.replace:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
        for ens in ensemble_parent.iterchildren(tag="ensemble"):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
            ens_id = ens.get("id","")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
            if ens_id.startswith("chat_"):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
                ensemble_parent.remove(ens)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
                # remove in display nodes
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
                if display_content_node is not None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
                    for cut_display in display_content_node.iterchildren():
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
                        if cut_display.get('idens','') == ens_id:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
                            display_content_node.remove(cut_display)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
    ensemble = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
    elements = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
    decoupage = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
    if options.merge:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
        for ens in ensemble_parent.findall("ensemble"):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
            if ens.get('id',"").startswith("chat_"):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
                ensemble = ens
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
                break
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
        if ensemble is not None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
            elements = ensemble.find(".//elements")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
            decoupage = ensemble.find("decoupage")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
    if ensemble is None or elements is None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
        ensemble = etree.SubElement(ensemble_parent, "ensemble", {"id":"chat_" + str(uuid.uuid4()), "title":"Ensemble Chat", "author":"IRI Web", "abstract":"Ensemble Chat"})
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
        decoupage = etree.SubElement(ensemble, "decoupage", {"id": str(uuid.uuid4()), "author": "IRI Web"})
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
        etree.SubElement(decoupage, "title").text = options.name
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
        etree.SubElement(decoupage, "abstract").text = options.name
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
        elements = etree.SubElement(decoupage, "elements")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
    ensemble_id = ensemble.get('id', '')
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
    decoupage_id = decoupage.get('id', '') if decoupage is not None else None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
    if not duration and options.base_url:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
        content_url = options.base_url + LDT_CONTENT_REST_API_PATH + content_id + "/?format=json"
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
        r = requests.get(content_url)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
        duration = int(r.json()['duration'])
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
        get_logger().debug("get duration " + content_url) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
        get_logger().debug("get duration " + repr(duration)) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
    chat_content_lines = read_chat_file(options.database.strip())
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
    for i,chat_line in enumerate(chat_content_lines):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
        cht = parse_chat_line("%04d" % (i+1) ,chat_line.strip())
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
        #TODO parse chat line
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
        cht_ts_dt = cht['created_at']
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
        cht_ts_rel_milli = parse_duration(cht_ts_dt)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
        element_date = start_date + datetime.timedelta(milliseconds=cht_ts_rel_milli)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
        if deltas:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
            d = find_delta(deltas, cht_ts_rel_milli)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
            if d[1] < 0:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
                continue
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
            else :
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
                cht_ts_rel_milli -= d[1]
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
        username = cht['user'] or "anon."
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
        element = etree.SubElement(elements, "element" , {"id": "%s-%s" % (uuid.uuid4(),cht['id']), "color":options.color, "author":username, "date":element_date.strftime("%Y/%m/%d"), "begin": str(cht_ts_rel_milli), "dur":"0", "src":"zoom"})
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
        etree.SubElement(element, "title").text = username + ": " + cht['text'][:255]
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
        etree.SubElement(element, "abstract").text = cht['text']
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
        tags_node = etree.SubElement(element, "tags")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
        for tag in cht['tags']:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
            etree.SubElement(tags_node,"tag").text = tag
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
        meta_element = etree.SubElement(element, 'meta')
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
        etree.SubElement(meta_element, "polemic_version").text = options.protocol_version
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
        parse_polemics = protocol_version_map.get(options.protocol_version, parse_polemics_2)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
        polemics_list = parse_polemics(cht['text'], options.extended_mode)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
        if polemics_list:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
            polemics_element = etree.Element('polemics')
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
            for pol in polemics_list:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
                etree.SubElement(polemics_element, 'polemic').text = pol
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
            meta_element.append(polemics_element)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
        etree.SubElement(meta_element, "source", attrib={"url":"http://zoom.io", "mimetype":"text/plain"}).text = etree.CDATA(json.dumps({'chat': chat_line}))
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
    # sort by tc in
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
    if options.merge :
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
        # remove all elements and put them in a array
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
        # sort them with tc
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
        #put them back
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
        elements[:] = sorted(elements,key=lambda n: int(n.get('begin')))
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
    #add to display node
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
    if display_content_node is not None:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
        display_dec = None
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
        for dec in display_content_node.iterchildren(tag="decoupage"):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
            if dec.get('idens','') == ensemble_id and dec.get('id', '') == decoupage_id:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
                display_dec = dec
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
                break
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
        if display_dec is None and ensemble_id and decoupage_id:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
            etree.SubElement(display_content_node, "decoupage", attrib={'idens': ensemble_id, 'id': decoupage_id, 'tagsSelect':''})
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
    output_data = etree.tostring(root, encoding="utf-8", method="xml", pretty_print=False, xml_declaration=True).decode('utf-8')
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
    if content_file_write and content_file_write.find("http") == 0:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
        project["ldt"] = output_data
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
        project['owner'] = project['owner'].replace('%7E','~')
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
        project['contents'] = [c_url.replace('%7E','~') for c_url in project['contents']]
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
        post_param = {}
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
        if options.post_param:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
            post_param = json.loads(options.post_param)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
        get_logger().debug("write http " + content_file_write) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
        get_logger().debug("write http " + repr(post_param)) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
        get_logger().debug("write http " + repr(project)) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
        r = requests.put(content_file_write, data=json.dumps(project), headers={'content-type':'application/json'}, params=post_param)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
        get_logger().debug("write http " + repr(r) + " content " + r.text) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
        if r.status_code != requests.codes.ok:  # pylint: disable=E1101
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
            r.raise_for_status()
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
    else:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
        if content_file_write and os.path.exists(content_file_write):
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
            dest_file_name = content_file_write
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
        else:
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
            dest_file_name = options.filename
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
        get_logger().debug("WRITE : " + dest_file_name) #@UndefinedVariable
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
        output = open(dest_file_name, "w")
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
        output.write(output_data)
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
        output.flush()
82b5f22448f6 add utilities to manipulate ldt and zoom chats
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
        output.close()