script/utils/export_chat_zoom.py
author ymh <ymh.work@gmail.com>
Tue, 03 Sep 2024 11:09:40 +0200
changeset 1558 761ba7426984
parent 1548 39186950a53e
permissions -rw-r--r--
upgrade metadataplayer and add a sitemap
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
1548
39186950a53e Correct zoom export
ymh <ymh.work@gmail.com>
parents: 1542
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)
39186950a53e Correct zoom export
ymh <ymh.work@gmail.com>
parents: 1542
diff changeset
   191
CHAT_REGEXP = re.compile(r"^(?P<created_at>\d{2}:\d{2}:\d{2})\t(?:(?:\sFrom\s{2}(?P<user_from>.+?)\s)|(?P<user>[^:]+)):\s(?P<text>.*)$", re.DOTALL)
39186950a53e Correct zoom export
ymh <ymh.work@gmail.com>
parents: 1542
diff changeset
   192
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
   193
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
   194
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
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
   196
    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
   197
        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
   198
        res['id'] = chat_id
1548
39186950a53e Correct zoom export
ymh <ymh.work@gmail.com>
parents: 1542
diff changeset
   199
        if user_str := res.get('user_from'):
39186950a53e Correct zoom export
ymh <ymh.work@gmail.com>
parents: 1542
diff changeset
   200
            res['user'] = user_str
1538
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
        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
   202
        return res
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
    else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
        return {}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
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
   207
    current_line = ""
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
    chat_content = []
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
    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
   210
        for chat_line in chat_file:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
            if CHAT_LINE_REGEXP.match(chat_line) is not None:
1541
61423ca4e0af remove direct messages
ymh <ymh.work@gmail.com>
parents: 1538
diff changeset
   212
                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
   213
                    chat_content.append(current_line)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
                current_line = chat_line
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
            else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
                current_line = current_line + "\n" + chat_line
1541
61423ca4e0af remove direct messages
ymh <ymh.work@gmail.com>
parents: 1538
diff changeset
   217
    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
   218
        chat_content.append(current_line)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
    return chat_content
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
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
if __name__ == "__main__" :
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
    (options, parser) = get_options()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
    set_logging(options)
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
    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
   229
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
    deltas = [(0,0)]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
    total_delta = 0
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
    if options.cuts:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
        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
   235
        for c, d in cuts_raw:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
            deltas.append((c+total_delta, -1))
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
            total_delta += d
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
            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
   239
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
    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
   241
        parser.print_help()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
        sys.exit(1)
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
    user_whitelist_file = options.user_whitelist
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
    user_whitelist = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
    if options.listconf:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
        parameters = []
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
        confdoc = etree.parse(options.listconf)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
        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
   252
            params = {}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
            for snode in node:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
                if snode.tag == "path":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
                    params['content_file'] = snode.text
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
                    params['content_file_write'] = snode.text
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
                elif snode.tag == "project_id":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
                    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
   259
                    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
   260
                    params['project_id'] = snode.text
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
                elif snode.tag == "start_date":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
                    params['start_date'] = snode.text
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
                elif snode.tag == "end_date":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
                    params['end_date'] = snode.text
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
                elif snode.tag == "duration":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
                    params['duration'] = int(snode.text)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
                elif snode.tag == "hashtags":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
                    params['hashtags'] = [snode.text]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
            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
   270
                params['hashtags'] = options.hashtag
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
            parameters.append(params)
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
        if options.project_id:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
            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
   275
        else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
            content_file = options.content_file
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
        parameters = [{
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
            'start_date': options.start_date,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
            'end_date' : options.end_date,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
            'duration' : options.duration,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
            'content_file' : content_file,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
            'content_file_write' : content_file,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
            'hashtags' : options.hashtag,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
            'project_id' : options.project_id
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
        }]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
    post_param = {}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
    if options.post_param:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
        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
   289
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
    item_tz = dateutil.tz.UTC
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
    if options.timezone:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
        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
   293
        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
   294
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
    if item_tz is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
        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
   297
        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
   298
        parser.print_help()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
        sys.exit(1)
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
    display_content_node = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
    for params in parameters:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
        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
   305
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
        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
   307
        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
   308
        duration = params.get("duration", None)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
        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
   310
        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
   311
        hashtags = list(set(params.get('hashtags', [])))
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
        if user_whitelist_file:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
            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
   315
                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
   316
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
        start_date = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
        if start_date_str:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
            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
   320
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
        root = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
        ensemble_parent = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
        project = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
        #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
   326
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
        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
   328
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
            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
   330
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
            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
   332
            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
   333
            project = r.json()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
            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
   335
            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
   336
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
        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
   338
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
            doc = etree.parse(content_file)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
            root = doc.getroot()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
            for child in root:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
                if child.tag == "project":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
                    project = child
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
                    break
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
            if project is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
                root = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
        content_id = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
        if root is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
            root = etree.Element("iri")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
            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
   355
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
            medias = etree.SubElement(root, "medias")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
            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
   358
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
            annotations = etree.SubElement(root, "annotations")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
            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
   361
            ensemble_parent = content
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
            content_id = options.content_id
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
        if ensemble_parent is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
            file_type = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
            for node in root:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
                if node.tag == "project":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
                    file_type = "ldt"
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
                elif node.tag == "head":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
                    file_type = "iri"
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
                    break
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
            if file_type == "ldt":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
                media_nodes = root.xpath("//media")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
                media = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
                if len(media_nodes) > 0:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
                    media = media_nodes[0]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
                annotations_node = root.find("annotations")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
                if annotations_node is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
                    annotations_node = etree.SubElement(root, "annotations")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
                content_node = annotations_node.find("content")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
                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
   386
                    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
   387
                ensemble_parent = content_node
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
                content_id = content_node.get("id")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
                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
   390
                if len(display_nodes) == 0:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
                    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
   392
                    display_content_node = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
                else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
                    display_content_node = display_nodes[0]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
            elif file_type == "iri":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
                body_node = root.find("body")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
                if body_node is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
                    body_node = etree.SubElement(root, "body")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
                ensembles_node = body_node.find("ensembles")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
                if ensembles_node is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
                    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
   403
                ensemble_parent = ensembles_node
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
                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
   405
                display_content_node = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
        if ensemble_parent is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
            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
   410
            sys.exit()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
        if options.replace:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
            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
   414
                ens_id = ens.get("id","")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
                if ens_id.startswith("chat_"):
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
                    ensemble_parent.remove(ens)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
                    # remove in display nodes
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
                    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
   419
                        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
   420
                            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
   421
                                display_content_node.remove(cut_display)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
        ensemble = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
        elements = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
        decoupage = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
        if options.merge:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
            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
   429
                if ens.get('id',"").startswith("chat_"):
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
                    ensemble = ens
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
                    break
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
            if ensemble is not None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
                elements = ensemble.find(".//elements")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
                decoupage = ensemble.find("decoupage")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
        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
   437
            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
   438
            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
   439
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
            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
   441
            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
   442
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
            elements = etree.SubElement(decoupage, "elements")
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
        ensemble_id = ensemble.get('id', '')
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
        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
   447
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
        end_date = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
        if end_date_str:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
            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
   451
        elif start_date and duration:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
            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
   453
        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
   454
            # get duration from api
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
            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
   456
            r = requests.get(content_url)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
            duration = int(r.json()['duration'])
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
            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
   459
            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
   460
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
            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
   462
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
        if end_date and deltas:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
            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
   465
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
        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
   467
        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
   468
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
            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
   470
            #TODO parse chat line
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
            cht_ts_dt = cht['created_at']
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
            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
   473
            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
   474
            if start_date is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
                start_date = cht_ts
1541
61423ca4e0af remove direct messages
ymh <ymh.work@gmail.com>
parents: 1538
diff changeset
   476
            if cht_ts < start_date or cht_ts > end_date:
61423ca4e0af remove direct messages
ymh <ymh.work@gmail.com>
parents: 1538
diff changeset
   477
                continue
1538
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
            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
   479
            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
   480
            if deltas:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
                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
   482
                if d[1] < 0:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
                    continue
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
                else :
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
                    cht_ts_rel_milli -= d[1]
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
            username = cht['user'] or "anon."
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
            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
   490
            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
   491
            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
   492
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
            tags_node = etree.SubElement(element, "tags")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
            for tag in cht['tags']:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
                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
   497
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
            meta_element = etree.SubElement(element, 'meta')
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
            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
   501
            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
   502
            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
   503
            if polemics_list:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
                polemics_element = etree.Element('polemics')
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
                for pol in polemics_list:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
                    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
   507
                meta_element.append(polemics_element)
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
            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
   510
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
        # sort by tc in
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
        if options.merge :
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
            # 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
   514
            # sort them with tc
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
            #put them back
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
            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
   517
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
        #add to display node
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
        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
   520
            display_dec = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
            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
   522
                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
   523
                    display_dec = dec
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
                    break
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
            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
   526
                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
   527
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
        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
   529
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
        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
   531
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
            project["ldt"] = output_data
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
            project['owner'] = project['owner'].replace('%7E','~')
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
            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
   535
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
            post_param = {}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
            if options.post_param:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
                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
   539
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
            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
   541
            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
   542
            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
   543
            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
   544
            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
   545
            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
   546
                r.raise_for_status()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
        else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
            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
   549
                dest_file_name = content_file_write
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
            else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
                dest_file_name = options.filename
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
            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
   554
            output = open(dest_file_name, "w")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
            output.write(output_data)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
            output.flush()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
            output.close()