script/utils/export_chat_zoom.py
author ymh <ymh.work@gmail.com>
Tue, 12 Jan 2021 13:47:11 +0100
changeset 1538 4e5ee8e79e7f
child 1541 61423ca4e0af
permissions -rw-r--r--
add command to export zoom chat files as annotation
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
#class TweetExclude(object):
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
#    def __init__(self, id):
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
#        self.id = id
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 __repr__(self):
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
#        return "<TweetExclude(id=%d)>" % (self.id)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
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
    29
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
    30
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
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
    32
    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
    33
    if res.tzinfo is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        res = res.replace(tzinfo=default_tz)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    return res
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
def re_fn(expr, item):
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    reg = re.compile(expr, re.I)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    res = reg.search(item)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    if res:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        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
    43
    return res is not None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
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
    46
    """
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    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
    48
    """
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    polemics = {}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
    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
    51
        pol_link = {
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
            '++' : 'OK',
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            '--' : 'KO',
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
            '??' : 'Q',
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            '==' : 'REF'}[m.group(1)]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        polemics[pol_link] = pol_link
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    if extended_mode:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        if "?" in tw_text:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
            polemics["Q"] = "Q"
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    if len(polemics) > 0:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        return polemics.keys()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        return None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
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
    68
    """
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    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
    70
    """
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    polemics = {}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
    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
    73
        pol_link = {
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
            '++' : 'OK',
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
            '!!' : 'KO',
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
            '??' : 'Q',
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
            '==' : 'REF'}[m.group(1)]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        polemics[pol_link] = pol_link
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
    if extended_mode:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
        if "?" in tw_text:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            polemics["Q"] = "Q"
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
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
    if len(polemics) > 0:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        return polemics.keys()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
        return None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
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
    91
    """
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
    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
    93
    """
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
    polemics = {}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
    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
    96
        pol_link = {
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
            '++' : 'OK',
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
            '??' : 'KO',
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
            '**' : 'REF',
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
            '==' : 'Q'}[m.group(1)]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
        polemics[pol_link] = pol_link
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
    if len(polemics) > 0:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        return polemics.keys()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
    else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        return None
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
protocol_version_map = {
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
    "1" : parse_polemics_1,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    "2" : parse_polemics_2,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    "3" : parse_polemics_3
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
def get_options():
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
    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
   118
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
    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
   120
                      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
   121
    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
   122
                      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
   123
    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
   124
                      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
   125
                      default="2")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
    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
   127
                      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
   128
    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
   129
                      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
   130
    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
   131
                      help="Content file", metavar="CONTENT_FILE")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
    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
   133
                      help="Content url", metavar="CONTENT")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
    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
   135
                      help="video url", metavar="VIDEO")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
    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
   137
                      help="Content id", metavar="CONTENT_ID")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
    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
   139
                      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
   140
    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
   141
                      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
   142
    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
   143
                      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
   144
    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
   145
                      help="Duration", metavar="DURATION", default=None)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
    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
   147
                      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
   148
    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
   149
                      help="Replace tweet ensemble", default=False)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
    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
   151
                      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
   152
    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
   153
                      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
   154
    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
   155
                      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
   156
    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
   157
                      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
   158
    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
   159
                      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
   160
    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
   161
                      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
   162
    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
   163
                      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
   164
    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
   165
                      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
   166
    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
   167
                      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
   168
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
    set_logging_options(parser)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
    return (parser.parse_args(), parser)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
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
def find_delta(deltas, ts):
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
    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
   177
    if i:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
        return deltas[i-1]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
    return (0,0)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
def parse_duration(s):
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
    try:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
        return int(s)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
    except ValueError:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
        parts = s.split(":")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
        if len(parts) < 2:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
            raise ValueError("Bad duration format")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
        time_params = {
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
            'hours': int(parts[0]),
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
            'minutes': int(parts[1]),
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
            '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
   193
        }
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
        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
   195
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
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
   197
CHAT_LINE_REGEXP = re.compile(r"^\d{2}:\d{2}:\d{2}\t\sFrom\s{2}.+?\s:")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
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
   200
    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
   201
        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
   202
        res['id'] = chat_id
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
        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
   204
        return res
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
    else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
        return {}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
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
   209
    current_line = ""
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
    chat_content = []
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
    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
   212
        for chat_line in chat_file:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
            if CHAT_LINE_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
   214
                if current_line:
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
                current_line = chat_line
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
            else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
                current_line = current_line + "\n" + chat_line
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
    if current_line:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
        chat_content.append(current_line)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
    return chat_content
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
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
if __name__ == "__main__" :
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
    (options, parser) = get_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
    set_logging(options)
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
    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
   231
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
    deltas = [(0,0)]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
    total_delta = 0
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
    if options.cuts:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
        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
   237
        for c, d in cuts_raw:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
            deltas.append((c+total_delta, -1))
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
            total_delta += d
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
            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
   241
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
    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
   243
        parser.print_help()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
        sys.exit(1)
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
    user_whitelist_file = options.user_whitelist
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
    user_whitelist = None
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
    if options.listconf:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
        parameters = []
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
        confdoc = etree.parse(options.listconf)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
        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
   254
            params = {}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
            for snode in node:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
                if snode.tag == "path":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
                    params['content_file'] = snode.text
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
                    params['content_file_write'] = snode.text
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
                elif snode.tag == "project_id":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
                    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
   261
                    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
   262
                    params['project_id'] = snode.text
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
                elif snode.tag == "start_date":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
                    params['start_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 == "end_date":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
                    params['end_date'] = snode.text
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
                elif snode.tag == "duration":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
                    params['duration'] = int(snode.text)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
                elif snode.tag == "hashtags":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
                    params['hashtags'] = [snode.text]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
            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
   272
                params['hashtags'] = options.hashtag
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
            parameters.append(params)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
    else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
        if options.project_id:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
            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
   277
        else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
            content_file = options.content_file
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
        parameters = [{
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
            'start_date': options.start_date,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
            'end_date' : options.end_date,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
            'duration' : options.duration,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
            'content_file' : content_file,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
            'content_file_write' : content_file,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
            'hashtags' : options.hashtag,
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
            'project_id' : options.project_id
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
        }]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
    post_param = {}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
    if options.post_param:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
        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
   291
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
    item_tz = dateutil.tz.UTC
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
    if options.timezone:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
        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
   295
        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
   296
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
    if item_tz is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
        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
   299
        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
   300
        parser.print_help()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
        sys.exit(1)
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
    display_content_node = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
    for params in parameters:
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
        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
   307
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
        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
   309
        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
   310
        duration = params.get("duration", None)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
        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
   312
        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
   313
        hashtags = list(set(params.get('hashtags', [])))
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
        if user_whitelist_file:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
            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
   317
                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
   318
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
        start_date = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
        if start_date_str:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
            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
   322
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
        root = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
        ensemble_parent = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
        project = None
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
        #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
   328
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
        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
   330
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
            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
   332
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
            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
   334
            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
   335
            project = r.json()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
            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
   337
            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
   338
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
        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
   340
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
            doc = etree.parse(content_file)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
            root = doc.getroot()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
            for child in root:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
                if child.tag == "project":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
                    project = child
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
                    break
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
            if project is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
                root = 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
        content_id = 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
        if root is None:
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
            root = etree.Element("iri")
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
            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
   357
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
            medias = etree.SubElement(root, "medias")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
            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
   360
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
            annotations = etree.SubElement(root, "annotations")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
            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
   363
            ensemble_parent = content
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
            content_id = options.content_id
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
        if ensemble_parent is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
            file_type = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
            for node in root:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
                if node.tag == "project":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
                    file_type = "ldt"
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
                    break
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
                elif node.tag == "head":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
                    file_type = "iri"
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
                    break
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
            if file_type == "ldt":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
                media_nodes = root.xpath("//media")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
                media = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
                if len(media_nodes) > 0:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
                    media = media_nodes[0]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
                annotations_node = root.find("annotations")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
                if annotations_node is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
                    annotations_node = etree.SubElement(root, "annotations")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
                content_node = annotations_node.find("content")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
                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
   388
                    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
   389
                ensemble_parent = content_node
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
                content_id = content_node.get("id")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
                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
   392
                if len(display_nodes) == 0:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
                    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
   394
                    display_content_node = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
                else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
                    display_content_node = display_nodes[0]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
            elif file_type == "iri":
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
                body_node = root.find("body")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
                if body_node is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
                    body_node = etree.SubElement(root, "body")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
                ensembles_node = body_node.find("ensembles")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
                if ensembles_node is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
                    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
   405
                ensemble_parent = ensembles_node
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
                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
   407
                display_content_node = None
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
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
        if ensemble_parent is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
            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
   412
            sys.exit()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
        if options.replace:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
            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
   416
                ens_id = ens.get("id","")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
                if ens_id.startswith("chat_"):
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
                    ensemble_parent.remove(ens)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
                    # remove in display nodes
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
                    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
   421
                        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
   422
                            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
   423
                                display_content_node.remove(cut_display)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
        ensemble = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
        elements = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
        decoupage = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
        if options.merge:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
            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
   431
                if ens.get('id',"").startswith("chat_"):
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
                    ensemble = ens
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
                    break
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
            if ensemble is not None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
                elements = ensemble.find(".//elements")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
                decoupage = ensemble.find("decoupage")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
        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
   439
            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
   440
            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
   441
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
            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
   443
            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
   444
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
            elements = etree.SubElement(decoupage, "elements")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
        ensemble_id = ensemble.get('id', '')
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
        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
   449
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
        end_date = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
        if end_date_str:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
            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
   453
        elif start_date and duration:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
            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
   455
        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
   456
            # get duration from api
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
            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
   458
            r = requests.get(content_url)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
            duration = int(r.json()['duration'])
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
            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
   461
            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
   462
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
            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
   464
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
        if end_date and deltas:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
            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
   467
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
        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
   469
        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
   470
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
            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
   472
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
            #TODO parse chat line
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
            cht_ts_dt = cht['created_at']
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
            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
   476
            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
   477
            if start_date is None:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
                start_date = cht_ts
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
            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
   480
            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
   481
            if deltas:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
                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
   483
                if d[1] < 0:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
                    continue
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
                else :
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
                    cht_ts_rel_milli -= d[1]
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
            username = cht['user'] or "anon."
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
            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
   491
            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
   492
            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
   493
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
            tags_node = etree.SubElement(element, "tags")
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
            for tag in cht['tags']:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
                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
   498
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
            meta_element = etree.SubElement(element, 'meta')
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
            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
   502
            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
   503
            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
   504
            if polemics_list:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
                polemics_element = etree.Element('polemics')
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
                for pol in polemics_list:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
                    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
   508
                meta_element.append(polemics_element)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
            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
   511
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
        # sort by tc in
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
        if options.merge :
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
            # 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
   515
            # sort them with tc
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
            #put them back
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
            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
   518
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
        #add to display node
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
        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
   521
            display_dec = None
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
            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
   523
                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
   524
                    display_dec = dec
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
                    break
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
            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
   527
                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
   528
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
        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
   530
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
        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
   532
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
            project["ldt"] = output_data
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
            project['owner'] = project['owner'].replace('%7E','~')
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
            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
   536
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
            post_param = {}
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
            if options.post_param:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
                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
   540
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
            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
   542
            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
   543
            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
   544
            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
   545
            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
   546
            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
   547
                r.raise_for_status()
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
            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
   550
                dest_file_name = content_file_write
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
            else:
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
                dest_file_name = options.filename
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
            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
   555
            output = open(dest_file_name, "w")
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
            output.write(output_data)
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
            output.flush()
4e5ee8e79e7f add command to export zoom chat files as annotation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
            output.close()