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